├── LICENSE ├── README.md ├── build.xml ├── java └── com │ └── google │ └── gwt │ └── testing │ └── easygwtmock │ ├── Easygwtmock.gwt.xml │ ├── client │ ├── Answer.java │ ├── ArgumentMatcher.java │ ├── Capture.java │ ├── ExpectationSetters.java │ ├── MocksControl.java │ ├── Nice.java │ ├── UndeclaredThrowableException.java │ └── internal │ │ ├── AnswerFactory.java │ │ ├── AssertionErrorWrapper.java │ │ ├── Call.java │ │ ├── ExpectationSettersImpl.java │ │ ├── ExpectedCall.java │ │ ├── IllegalStateExceptionWrapper.java │ │ ├── Method.java │ │ ├── MocksBehavior.java │ │ ├── MocksControlBase.java │ │ ├── MocksControlState.java │ │ ├── Range.java │ │ ├── RecordState.java │ │ ├── ReplayState.java │ │ ├── Utils.java │ │ └── matchers │ │ ├── Any.java │ │ ├── ArgumentCapture.java │ │ ├── AsyncCallbackMatcher.java │ │ └── Equals.java │ └── rebind │ ├── MocksControlGenerator.java │ └── MocksGenerator.java ├── javatests └── com │ └── google │ └── gwt │ └── testing │ └── easygwtmock │ └── client │ ├── AnswerGwtTest.java │ ├── BaseGwtTestCase.java │ ├── CallbackGwtTest.java │ ├── CaptureGwtTest.java │ ├── CaptureJavaTest.java │ ├── ClassMockingGwtTest.java │ ├── FrameworkMissuseGwtTest.java │ ├── GenericInterfacesGwtTest.java │ ├── MatcherGwtTest.java │ ├── MethodsWithGenericsGwtTest.java │ ├── MockExtendedInterfacesGwtTest.java │ ├── MockMethodsWithArgumentsGwtTest.java │ ├── MockMethodsWithVarArgsGwtTest.java │ ├── NiceMockGwtTest.java │ ├── RangeGwtTest.java │ ├── ReturnValuesGwtTest.java │ ├── SimpleExpectationsGwtTest.java │ ├── StacktraceGwtTest.java │ ├── ThrowingExceptionsGwtTest.java │ ├── UnmockableMethodsGwtTest.java │ ├── dummyclasses │ ├── ClassToMock.java │ └── OneArgClassToMock.java │ └── internal │ ├── AnswerFactoryJavaTest.java │ ├── CallJavaTest.java │ ├── ExceptionJavaTest.java │ ├── ExpectedCallJavaTest.java │ ├── MethodJavaTest.java │ ├── RangeJavaTest.java │ ├── RecordStateJavaTest.java │ ├── ReplayStateJavaTest.java │ ├── UtilsJavaTest.java │ └── matchers │ ├── AnyJavaTest.java │ ├── ArgumentCaptureJavaTest.java │ └── AsyncCallbackMatcherJavaTest.java └── lib └── README /build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 174 | 175 | 176 | 177 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/Easygwtmock.gwt.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/Answer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2010 the original author or authors. 3 | * Portions Copyright 2011 Google Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 6 | * use this file except in compliance with the License. You may obtain a copy of 7 | * the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14 | * License for the specific language governing permissions and limitations under 15 | * the License. 16 | */ 17 | 18 | package com.google.gwt.testing.easygwtmock.client; 19 | 20 | /** 21 | * Used to answer expected calls. 22 | * 23 | * @param 24 | * the type to return. 25 | * 26 | * @author Michael Goderbauer 27 | * Originally written for EasyMock {@link "www.easymock.org"} by OFFIS, Tammo Freese 28 | */ 29 | public interface Answer { 30 | 31 | /** 32 | * Is called by EasyGwtMock to answer an expected call. The answer may be to 33 | * return a value, or to throw an exception. Be careful when using the methods 34 | * arguments - using them is not refactoring-safe. 35 | * 36 | * @return the value to be returned 37 | * @throws Throwable 38 | * the throwable to be thrown 39 | */ 40 | T answer(Object[] args) throws Throwable; 41 | } 42 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/ArgumentMatcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2010 the original author or authors. 3 | * Portions Copyright 2011 Google Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 6 | * use this file except in compliance with the License. You may obtain a copy of 7 | * the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14 | * License for the specific language governing permissions and limitations under 15 | * the License. 16 | */ 17 | 18 | package com.google.gwt.testing.easygwtmock.client; 19 | 20 | /** 21 | * Decides whether an actual argument is accepted. 22 | * Implement this interface to write your own matcher. 23 | * 24 | * @author Michael Goderbauer 25 | * Originally written for EasyMock {@link "www.easymock.org"} by OFFIS, Tammo Freese 26 | */ 27 | public interface ArgumentMatcher { 28 | /** 29 | * Returns true if this matcher accepts the given argument. 30 | *

31 | * Like Object.equals(), it should be aware that the argument passed might 32 | * be null and of any type. So you will usually start the method with an 33 | * instanceof and/or null check. 34 | *

35 | * The method should never assert if the argument doesn't match. It 36 | * should only return false. EasyGwtMock will take care of asserting if the 37 | * call is really unexpected. 38 | */ 39 | boolean matches(Object argument); 40 | 41 | /** 42 | * Appends a string representation of this matcher to the given buffer. In 43 | * case of failure, the printed message will show this string to allow to 44 | * know which matcher was used for the failing call. 45 | */ 46 | void appendTo(StringBuffer buffer); 47 | } 48 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/Capture.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2010 the original author or authors. 3 | * Portions Copyright 2011 Google Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 6 | * use this file except in compliance with the License. You may obtain a copy of 7 | * the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14 | * License for the specific language governing permissions and limitations under 15 | * the License. 16 | */ 17 | 18 | package com.google.gwt.testing.easygwtmock.client; 19 | 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | 23 | /** 24 | * Will contain what was captured by the {@code capture()} matcher. 25 | * 26 | * @param Type of the captured element 27 | * 28 | * @author Michael Goderbauer 29 | * Originally written for EasyMock {@link "www.easymock.org"} by Henri Tremblay 30 | */ 31 | public class Capture { 32 | 33 | private List values = new ArrayList(); 34 | 35 | /** 36 | * Builds a new {@link Capture} with inferred type. 37 | * 38 | * @param the generic type to be captured 39 | * @return the new {@code Capture} 40 | */ 41 | public static Capture create() { 42 | return new Capture(); 43 | } 44 | 45 | /** 46 | * Will reset capture to a "nothing captured yet" state 47 | */ 48 | public void reset() { 49 | this.values.clear(); 50 | } 51 | 52 | /** 53 | * @return true if something was captured 54 | */ 55 | public boolean hasCaptured() { 56 | return !this.values.isEmpty(); 57 | } 58 | 59 | /** 60 | * @return all captured values. 61 | */ 62 | public List getValues() { 63 | return this.values; 64 | } 65 | 66 | /** 67 | * @return the first captured value. 68 | */ 69 | public T getFirstValue() { 70 | return this.values.get(0); 71 | } 72 | 73 | /** 74 | * @return the last captured value. 75 | */ 76 | public T getLastValue() { 77 | return this.values.get(this.values.size() - 1); 78 | } 79 | 80 | // The following is for internal EasyGwtMock usage only 81 | 82 | /** 83 | * Used internally by EasyGwtMock to capture a value. 84 | */ 85 | @SuppressWarnings("unchecked") 86 | public void captureValue(Object value) { 87 | this.values.add((T) value); 88 | } 89 | 90 | @Override 91 | public String toString() { 92 | if (this.values.isEmpty()) { 93 | return ""; 94 | } 95 | String string = this.values.toString(); 96 | return string.substring(1, string.length() - 1); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/ExpectationSetters.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2010 the original author or authors. 3 | * Portions Copyright 2011 Google Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 6 | * use this file except in compliance with the License. You may obtain a copy of 7 | * the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14 | * License for the specific language governing permissions and limitations under 15 | * the License. 16 | */ 17 | 18 | package com.google.gwt.testing.easygwtmock.client; 19 | 20 | /** 21 | * Allows setting expectations for an associated expected invocation. 22 | * Implementations of this interface are returned by 23 | * {@link MocksControl#expect(Object)}, and by {@link MocksControl#expectLastCall()}. 24 | * 25 | * @param type of what should be returned by this expected call 26 | * 27 | * @author Michael Goderbauer 28 | * JavaDoc partly by OFFIS and Tammo Freese of EasyMock 29 | */ 30 | public interface ExpectationSetters { 31 | 32 | /** 33 | * Sets a return value that will be returned for the expected invocation. 34 | */ 35 | ExpectationSetters andReturn(T value); 36 | 37 | /** 38 | * Sets a throwable that will be thrown for the expected invocation. 39 | */ 40 | ExpectationSetters andThrow(Throwable throwable); 41 | 42 | /** 43 | * Calls the onSuccess() method of a {@link com.google.gwt.user.client.rpc.AsyncCallback} 44 | * object, which is provided as argument to the expected invocation. 45 | * 46 | * @param result 47 | * is passed to the onSuccess() method 48 | */ 49 | ExpectationSetters andCallOnSuccess(Object result); 50 | 51 | /** 52 | * Calls the onFailure() method of a {@link com.google.gwt.user.client.rpc.AsyncCallback} 53 | * object, which is provided as argument to the expected invocation. 54 | * 55 | * @param caught 56 | * is passed to the onFailure() method 57 | */ 58 | ExpectationSetters andCallOnFailure(Throwable caught); 59 | 60 | /** 61 | * Sets an object that will be used to calculate the answer for the expected 62 | * invocation (either return a value, or throw an exception). 63 | */ 64 | ExpectationSetters andAnswer(Answer answer); 65 | 66 | /** 67 | * Expect the last invocation count times. 68 | */ 69 | ExpectationSetters times(int count); 70 | 71 | /** 72 | * Expect the last invocation between min and max 73 | * times. 74 | */ 75 | ExpectationSetters times(int min, int max); 76 | 77 | /** 78 | * Expect the last invocation once. This is the default. 79 | */ 80 | ExpectationSetters once(); 81 | 82 | /** 83 | * Expect the last invocation at least once. 84 | */ 85 | ExpectationSetters atLeastOnce(); 86 | 87 | /** 88 | * Expect the last invocation any times. 89 | */ 90 | ExpectationSetters anyTimes(); 91 | } 92 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/MocksControl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2010 the original author or authors. 3 | * Portions Copyright 2011 Google Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 6 | * use this file except in compliance with the License. You may obtain a copy of 7 | * the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14 | * License for the specific language governing permissions and limitations under 15 | * the License. 16 | */ 17 | 18 | package com.google.gwt.testing.easygwtmock.client; 19 | 20 | import com.google.gwt.user.client.rpc.AsyncCallback; 21 | 22 | /** 23 | * To create mock objects, extend this interface and populate it with methods 24 | * that return the types you want to mock. 25 | * 26 | *

Example: If you want to create a mock of {@code ComplexType} and {@code AnotherType} create 27 | * the following interface: 28 | * 29 | *

 30 |  * public interface MyMocksControl extends MocksControl {
 31 |  *   ComplexType getComplexTypeMock();
 32 |  *   AnotherType getAnotherTypeMock();
 33 |  * } 
 34 |  * 
35 | * 36 | *

Pass this interface to {@code GWT.create()} to generate mocks and access them in the 37 | * following way: 38 | * 39 | *

 40 |  * MyMocksControl ctrl = GWT.create(MyMocksControl.class);
 41 |  * ComplexType mock = ctrl.getComplexTypeMock();
 42 |  * 
43 | * 44 | *

The interface also provides the methods listed below to manipulate the state of the mocks 45 | * and to access various argument matcher. 46 | * 47 | * @author Michael Goderbauer 48 | * JavaDoc partly by OFFIS and Tammo Freese of EasyMock 49 | */ 50 | public interface MocksControl { 51 | 52 | /** 53 | * Switches mocks to replay mode. 54 | */ 55 | public void replay(); 56 | 57 | /** 58 | * Verifies if all expectations on mocks were met. 59 | */ 60 | public void verify(); 61 | 62 | /** 63 | * Resets all mocks, deletes all expectations. 64 | */ 65 | public void reset(); 66 | 67 | /** 68 | * Returns the expectation setter for the last expected invocation. 69 | * 70 | * @param 71 | * type returned by the expected method 72 | * @param value 73 | * the parameter is used to transport the type to the 74 | * ExpectationSetter. It allows writing the expected call as 75 | * argument, i.e. 76 | * ctrl.expect(mock.getName()).andReturn("John Doe"). 77 | */ 78 | public ExpectationSetters expect(T value); 79 | 80 | /** 81 | * Returns the expectation setter for the last expected invocation. 82 | * This method is used for expected invocations on void methods. 83 | * 84 | * @param 85 | * type returned by the expected method 86 | */ 87 | public ExpectationSetters expectLastCall(); 88 | 89 | /** 90 | * Turns a mock into a nice mock which will return an appropriate default value 91 | * (0, null, false) in response to unexpected method calls instead of throwing 92 | * an exception. 93 | * 94 | * @return the same mock you passed into the method 95 | */ 96 | public T setToNice(T mock); 97 | 98 | /** 99 | * Turns a nice mock into a regular mock which will throw an exception as response to 100 | * an unexpected call. This is default behavior. 101 | * 102 | * @see #setToNice(Object) 103 | * 104 | * @return the same mock you passed into the method 105 | */ 106 | public T setToNotNice(T mock); 107 | 108 | /** 109 | * Expects any boolean argument. 110 | */ 111 | public boolean anyBoolean(); 112 | 113 | /** 114 | * Expects any byte argument. 115 | */ 116 | public byte anyByte(); 117 | 118 | /** 119 | * Expects any char argument. 120 | */ 121 | public char anyChar(); 122 | 123 | /** 124 | * Expects any int argument. 125 | */ 126 | public int anyInt(); 127 | 128 | /** 129 | * Expects any long argument. 130 | */ 131 | public long anyLong(); 132 | 133 | /** 134 | * Expects any float argument. 135 | */ 136 | public float anyFloat(); 137 | 138 | /** 139 | * Expects any double argument. 140 | */ 141 | public double anyDouble(); 142 | 143 | /** 144 | * Expects any short argument. 145 | */ 146 | public short anyShort(); 147 | 148 | /** 149 | * Expects any Object argument. 150 | * This matcher (and {@link #anyObject(Class)}) can be used in these three ways: 151 | *

    152 | *
  • (T)ctrl.anyObject() // explicit cast
  • 153 | *
  • 154 | * ctrl.<T> anyObject() // fixing the returned generic 155 | *
  • 156 | *
  • 157 | * ctrl.anyObject(T.class) // pass the returned type in parameter 158 | *
  • 159 | *
160 | * 161 | * @return null 162 | */ 163 | public T anyObject(); 164 | 165 | /** 166 | * Expects any Object argument. 167 | * To work well with generics, this matcher can be used in three different ways. 168 | * See {@link #anyObject()}. 169 | * 170 | * @param 171 | * type of the method argument to match 172 | * @param clazz 173 | * the class of the argument to match 174 | * @return null 175 | */ 176 | public T anyObject(final Class clazz); 177 | 178 | /** 179 | * Expects a boolean that is equal to the given value. 180 | */ 181 | public boolean eq(boolean value); 182 | 183 | /** 184 | * Expects a byte that is equal to the given value. 185 | */ 186 | public byte eq(byte value); 187 | 188 | /** 189 | * Expects a char that is equal to the given value. 190 | */ 191 | public char eq(char value); 192 | 193 | /** 194 | * Expects a double that is equal to the given value. 195 | */ 196 | public double eq(double value); 197 | 198 | /** 199 | * Expects a float that is equal to the given value. 200 | */ 201 | public float eq(float value); 202 | 203 | /** 204 | * Expects an int that is equal to the given value. 205 | */ 206 | public int eq(int value); 207 | 208 | /** 209 | * Expects a long that is equal to the given value. 210 | */ 211 | public long eq(long value); 212 | 213 | /** 214 | * Expects a short that is equal to the given value. 215 | */ 216 | public short eq(short value); 217 | 218 | /** 219 | * Expects an Object that is equal to the given value. 220 | */ 221 | public T eq(T value); 222 | 223 | /** 224 | * Expects an AsyncCallback<T> object. 225 | * 226 | * 227 | * myRemoteApi.getInteger(ctrl.asyncCallback()); 228 | * ctrl.expectLastCall().andCallOnSuccess(...); 229 | * 230 | * 231 | * Alternatively, you can also use {@link #asyncCallback(Class)} 232 | * 233 | * @return null 234 | */ 235 | public AsyncCallback asyncCallback(); 236 | 237 | /** 238 | * Expects an AsyncCallback<T> object. 239 | * 240 | * Example for expecting an AsyncCallback<Integer>: 241 | * 242 | * myRemoteApi.getInteger(ctrl.asyncCallback(Integer.class)); 243 | * ctrl.expectLastCall().andCallOnSuccess(...); 244 | * 245 | * 246 | * Alternatively, you can also use {@link #asyncCallback()} 247 | * 248 | * @return null 249 | */ 250 | public AsyncCallback asyncCallback(final Class clazz); 251 | 252 | /** 253 | * Expects any object and captures it for later use. 254 | */ 255 | public T captureObject(Capture captured); 256 | 257 | /** 258 | * Expects a byte and captures it for later use. 259 | */ 260 | public byte captureByte(Capture captured); 261 | 262 | /** 263 | * Expects a short and captures it for later use. 264 | */ 265 | public short captureShort(Capture captured); 266 | 267 | /** 268 | * Expects an int and captures it for later use. 269 | */ 270 | public int captureInt(Capture captured); 271 | 272 | /** 273 | * Expects an long and captures it for later use. 274 | */ 275 | public long captureLong(Capture captured); 276 | 277 | /** 278 | * Expects a float and captures it for later use. 279 | */ 280 | public float captureFloat(Capture captured); 281 | 282 | /** 283 | * Expects a double and captures it for later use. 284 | */ 285 | public double captureDouble(Capture captured); 286 | 287 | /** 288 | * Expects a boolean and captures it for later use. 289 | */ 290 | public boolean captureBoolean(Capture captured); 291 | 292 | /** 293 | * Expects a char and captures it for later use. 294 | */ 295 | public char captureChar(Capture captured); 296 | 297 | /** 298 | * Report an argument matcher for a double value. 299 | */ 300 | public byte matchesByte(ArgumentMatcher matcher); 301 | 302 | /** 303 | * Report an argument matcher for a double value. 304 | */ 305 | public short matchesShort(ArgumentMatcher matcher); 306 | 307 | /** 308 | * Report an argument matcher for a double value. 309 | */ 310 | public int matchesInt(ArgumentMatcher matcher); 311 | 312 | /** 313 | * Report an argument matcher for a double value. 314 | */ 315 | public long matchesLong(ArgumentMatcher matcher); 316 | 317 | /** 318 | * Report an argument matcher for a double value. 319 | */ 320 | public float matchesFloat(ArgumentMatcher matcher); 321 | 322 | /** 323 | * Report an argument matcher for a double value. 324 | */ 325 | public double matchesDouble(ArgumentMatcher matcher); 326 | 327 | /** 328 | * Report an argument matcher for a boolean value. 329 | */ 330 | public boolean matchesBoolean(ArgumentMatcher matcher); 331 | 332 | /** 333 | * Report an argument matcher for a char value. 334 | */ 335 | public char matchesChar(ArgumentMatcher matcher); 336 | 337 | /** 338 | * Report an argument matcher for an object. 339 | * This method (and {@link #matchesObject(ArgumentMatcher, Class)}) 340 | * can be used in these three ways: 341 | *
    342 | *
  • (T)ctrl.matchesObject(matcher) // explicit cast
  • 343 | *
  • 344 | * ctrl.<T> matchesObject(matcher) // fixing the returned generic 345 | *
  • 346 | *
  • 347 | * ctrl.matchesObject(matcher, T.class) // pass the returned type in parameter 348 | *
  • 349 | *
350 | * 351 | * @return null 352 | */ 353 | public T matchesObject(ArgumentMatcher matcher); 354 | 355 | /** 356 | * Report an argument matcher for an object. 357 | * To work well with generics, this matcher can be used in three different ways. 358 | * See {@link #matchesObject(ArgumentMatcher)}. 359 | * 360 | * @param 361 | * type of the method argument to match 362 | * @param matcher 363 | * the matcher to report 364 | * @param clazz 365 | * the class of the argument to match 366 | * @return null 367 | */ 368 | public T matchesObject(ArgumentMatcher matcher, Class clazz); 369 | } 370 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/Nice.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Target; 21 | 22 | 23 | /** 24 | * Turns a mock into a nice mock which will return an appropriate default value 25 | * (0, null, false) in response to unexpected method calls instead of throwing 26 | * an exception. 27 | * 28 | * Can be used to annotate a method within the extended MocksControl interface or 29 | * to annotate the entire interface. 30 | * 31 | * @author Michael Goderbauer 32 | */ 33 | @Target({ElementType.TYPE, ElementType.METHOD}) 34 | public @interface Nice { 35 | boolean value() default true; 36 | } 37 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/UndeclaredThrowableException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | /** 20 | * Exception is thrown when a mock method is supposed to throw a checked exception, 21 | * that is not declared in the method's signature. 22 | * 23 | * @author Michael Goderbauer 24 | */ 25 | public class UndeclaredThrowableException extends RuntimeException { 26 | 27 | private Throwable undeclaredThrowable; 28 | 29 | public UndeclaredThrowableException(Throwable exception) { 30 | super("Cannot throw undeclared exception: " + exception.toString(), exception); 31 | this.undeclaredThrowable = exception; 32 | } 33 | 34 | public Throwable getUndeclaredThrowable() { 35 | return undeclaredThrowable; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/internal/AnswerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | import com.google.gwt.testing.easygwtmock.client.Answer; 20 | import com.google.gwt.user.client.rpc.AsyncCallback; 21 | 22 | /** 23 | * Creates simple Answer objects 24 | * 25 | * @author Michael Goderbauer 26 | */ 27 | public class AnswerFactory { 28 | 29 | static Answer forValue(final Object value) { 30 | return new Answer() { 31 | @Override 32 | public Object answer(Object[] args) throws Throwable { 33 | return value; 34 | }}; 35 | } 36 | 37 | static Answer forThrowable(final Throwable throwable) { 38 | return new Answer() { 39 | @Override 40 | public Object answer(Object[] args) throws Throwable { 41 | throw throwable; 42 | }}; 43 | } 44 | 45 | static Answer forOnSuccess(final Object result) { 46 | return new Answer() { 47 | @SuppressWarnings("unchecked") 48 | @Override 49 | public Object answer(Object[] args) throws Throwable { 50 | if (args.length > 0 && args[args.length - 1] instanceof AsyncCallback) { 51 | ((AsyncCallback) args[args.length - 1]).onSuccess(result); 52 | return null; 53 | } 54 | throw new IllegalArgumentException( 55 | "No com.google.gwt.user.client.rpc.AsyncCallback object as last argument provided"); 56 | }}; 57 | } 58 | 59 | static Answer forOnFailure(final Throwable caught) { 60 | return new Answer() { 61 | @SuppressWarnings("unchecked") 62 | @Override 63 | public Object answer(Object[] args) throws Throwable { 64 | if (args.length > 0 && args[args.length - 1] instanceof AsyncCallback) { 65 | ((AsyncCallback) args[args.length - 1]).onFailure(caught); 66 | return null; 67 | } 68 | throw new IllegalArgumentException( 69 | "No com.google.gwt.user.client.rpc.AsyncCallback object as last argument provided"); 70 | }}; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/internal/AssertionErrorWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | /** 20 | * Wraps an AssertionError that is thrown somewhere in the internals of EasyGwtMock. 21 | * Before the exception leaves the framework, it is unwrapped and the stacktrace is cut 22 | * to hide the internals of EasyGwtMock from the user. 23 | * 24 | * @author Michael Goderbauer 25 | */ 26 | public class AssertionErrorWrapper extends Exception { 27 | 28 | private final AssertionError exception; 29 | 30 | AssertionErrorWrapper(AssertionError exception) { 31 | this.exception = exception; 32 | } 33 | 34 | public AssertionError getAssertionError() { 35 | return this.exception; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/internal/Call.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | import java.util.ArrayList; 20 | import java.util.Arrays; 21 | import java.util.List; 22 | 23 | /** 24 | * An object that represents a method call. 25 | * 26 | * @author Michael Goderbauer 27 | */ 28 | public class Call { 29 | 30 | private final Object mock; 31 | private final Method method; 32 | private final List arguments; 33 | 34 | public Call(Object mock, Method method, Object... arguments) { 35 | this.mock = mock; 36 | this.method = method; 37 | this.arguments = new ArrayList(Arrays.asList(arguments)); 38 | } 39 | 40 | public List getArguments() { 41 | return this.arguments; 42 | } 43 | 44 | /** 45 | * Add the varargs argument to the call. 46 | */ 47 | public void addVarArgument(Object[] arg) { 48 | for (Object o : arg) { 49 | this.arguments.add(o); 50 | } 51 | } 52 | 53 | /** 54 | * Add the varargs argument to the call. 55 | */ 56 | public void addVarArgument(byte[] arg) { 57 | for (byte o : arg) { 58 | this.arguments.add(o); 59 | } 60 | } 61 | 62 | /** 63 | * Add the varargs argument to the call. 64 | */ 65 | public void addVarArgument(short[] arg) { 66 | for (short o : arg) { 67 | this.arguments.add(o); 68 | } 69 | } 70 | 71 | /** 72 | * Add the varargs argument to the call. 73 | */ 74 | public void addVarArgument(int[] arg) { 75 | for (int o : arg) { 76 | this.arguments.add(o); 77 | } 78 | } 79 | 80 | /** 81 | * Add the varargs argument to the call. 82 | */ 83 | public void addVarArgument(long[] arg) { 84 | for (long o : arg) { 85 | this.arguments.add(o); 86 | } 87 | } 88 | 89 | /** 90 | * Add the varargs argument to the call. 91 | */ 92 | public void addVarArgument(float[] arg) { 93 | for (float o : arg) { 94 | this.arguments.add(o); 95 | } 96 | } 97 | 98 | /** 99 | * Add the varargs argument to the call. 100 | */ 101 | public void addVarArgument(double[] arg) { 102 | for (double o : arg) { 103 | this.arguments.add(o); 104 | } 105 | } 106 | 107 | /** 108 | * Add the varargs argument to the call. 109 | */ 110 | public void addVarArgument(boolean[] arg) { 111 | for (boolean o : arg) { 112 | this.arguments.add(o); 113 | } 114 | } 115 | 116 | /** 117 | * Add the varargs argument to the call. 118 | */ 119 | public void addVarArgument(char[] arg) { 120 | for (char o : arg) { 121 | this.arguments.add(o); 122 | } 123 | } 124 | 125 | @Override 126 | public String toString() { 127 | return this.method.getName() + "(" + argumentsToString() + ")"; 128 | } 129 | 130 | private String argumentsToString() { 131 | StringBuffer b = new StringBuffer(); 132 | for (int i = 0; i < this.arguments.size(); i++) { 133 | if (i != 0) { 134 | b.append(", "); 135 | } 136 | Utils.appendArgumentTo(this.arguments.get(i), b); 137 | } 138 | return b.toString(); 139 | } 140 | 141 | Method getMethod() { 142 | return this.method; 143 | } 144 | 145 | Object getMock() { 146 | return this.mock; 147 | } 148 | 149 | Object getDefaultReturnValue() { 150 | return this.method.getDefaultReturnValue(); 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/internal/ExpectationSettersImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | import com.google.gwt.testing.easygwtmock.client.Answer; 20 | import com.google.gwt.testing.easygwtmock.client.ArgumentMatcher; 21 | import com.google.gwt.testing.easygwtmock.client.ExpectationSetters; 22 | import com.google.gwt.testing.easygwtmock.client.internal.matchers.ArgumentCapture; 23 | import com.google.gwt.testing.easygwtmock.client.internal.matchers.Equals; 24 | import com.google.gwt.user.client.rpc.AsyncCallback; 25 | 26 | import java.util.ArrayList; 27 | import java.util.List; 28 | import java.util.Set; 29 | 30 | /** 31 | * Allows setting expectations for an associated expected call. 32 | * 33 | * @author Michael Goderbauer 34 | */ 35 | public class ExpectationSettersImpl implements ExpectationSetters { 36 | 37 | private final Call call; 38 | private final List matchers; 39 | private Set captures; 40 | 41 | private final MocksBehavior behavior; 42 | 43 | private boolean retired; 44 | private boolean callUsedAtLeastOnce; 45 | private Answer answer; 46 | private boolean unusedAnswer; 47 | 48 | ExpectationSettersImpl(Call call, List matchers, 49 | Set captures, MocksBehavior behavior) { 50 | this.call = call; 51 | this.matchers = createMissingMatchers(call, matchers); 52 | this.captures = captures; 53 | this.behavior = behavior; 54 | this.retired = false; 55 | } 56 | 57 | @Override 58 | public ExpectationSetters andAnswer(Answer answer) { 59 | checkIsNotRetired(); 60 | 61 | if (this.unusedAnswer) { 62 | saveExpectation(Range.DEFAULT); 63 | } 64 | 65 | this.answer = answer; 66 | this.unusedAnswer = true; 67 | 68 | return this; 69 | } 70 | 71 | @Override 72 | public ExpectationSetters andReturn(final Object value) { 73 | Method method = this.call.getMethod(); 74 | if (method.isReturnValueVoid()) { 75 | throw new IllegalStateException("Cannot add return value to void method"); 76 | } 77 | if (method.isReturnValuePrimitive() && value == null) { 78 | throw new IllegalStateException("Cannot add 'null' as return value to premitive method"); 79 | } 80 | 81 | return this.andAnswer(AnswerFactory.forValue(value)); 82 | } 83 | 84 | @Override 85 | public ExpectationSetters andThrow(final Throwable throwable) { 86 | Method method = this.call.getMethod(); 87 | if (!method.canThrow(throwable)) { 88 | throw new IllegalStateException(Utils.cutPackage(throwable.getClass().getName()) + 89 | " is not declared by " + call.getMethod()); 90 | } 91 | 92 | return this.andAnswer(AnswerFactory.forThrowable(throwable)); 93 | } 94 | 95 | @Override 96 | public ExpectationSetters andCallOnSuccess(final Object result) { 97 | if (!this.call.getMethod().isReturnValueVoid()) { 98 | throw new IllegalStateException("andCallOnSuccess() is only supported for void methods"); 99 | } 100 | 101 | Class[] argumentTypes = call.getMethod().getArgumentTypes(); 102 | if (argumentTypes.length < 1 || 103 | !Utils.isSubclass(argumentTypes[argumentTypes.length - 1], AsyncCallback.class)) { 104 | throw new IllegalStateException( 105 | "andCallOnSuccess() can only be used with methods " + 106 | "that take an AsyncCallback as last argument"); 107 | } 108 | return this.andAnswer(AnswerFactory.forOnSuccess(result)); 109 | } 110 | 111 | @Override 112 | public ExpectationSetters andCallOnFailure(final Throwable caught) { 113 | if (!this.call.getMethod().isReturnValueVoid()) { 114 | throw new IllegalStateException("andCallOnSuccess() is only supported for void methods"); 115 | } 116 | 117 | Class[] argumentTypes = call.getMethod().getArgumentTypes(); 118 | if (argumentTypes.length < 1 || 119 | !argumentTypes[argumentTypes.length - 1].equals(AsyncCallback.class)) { 120 | throw new IllegalStateException( 121 | "andCallOnFailure() can only be used with methods " + 122 | "that take an AsyncCallback as last argument"); 123 | } 124 | return this.andAnswer(AnswerFactory.forOnFailure(caught)); 125 | } 126 | 127 | @Override 128 | public ExpectationSetters times(int min, int max) { 129 | if (min < 0) { 130 | throw new IllegalArgumentException("min has to be non-negative"); 131 | } 132 | if (max < 1) { 133 | throw new IllegalArgumentException("max has to be positive"); 134 | } 135 | if (min > max) { 136 | throw new IllegalArgumentException("max has to be greater than min"); 137 | } 138 | 139 | saveExpectation(new Range(min, max)); 140 | return this; 141 | } 142 | 143 | @Override 144 | public ExpectationSetters times(int count) { 145 | if (count < 1) { 146 | throw new IllegalArgumentException("Argument to times has to be greater than 1"); 147 | } 148 | if (count == Range.UNLIMITED_MAX) { 149 | throw new IllegalArgumentException("Argument to times cannot be unlimited"); 150 | } 151 | saveExpectation(new Range(count, count)); 152 | return this; 153 | } 154 | 155 | @Override 156 | public ExpectationSetters once() { 157 | saveExpectation(Range.DEFAULT); 158 | return this; 159 | } 160 | 161 | @Override 162 | public ExpectationSetters atLeastOnce() { 163 | saveExpectation(new Range(1, Range.UNLIMITED_MAX)); 164 | return this; 165 | } 166 | 167 | @Override 168 | public ExpectationSetters anyTimes() { 169 | saveExpectation(new Range(0, Range.UNLIMITED_MAX)); 170 | return this; 171 | } 172 | 173 | private void saveExpectation(Range range) { 174 | checkIsNotRetired(); 175 | 176 | if (!this.call.getMethod().isReturnValueVoid() && !this.unusedAnswer) { 177 | throw new IllegalStateException( 178 | "Missing behavior definition for preceding method call " + this.call.toString()); 179 | } 180 | 181 | if (this.answer == null) { 182 | // void methods do not need an answer, create a dummy one. 183 | this.answer = AnswerFactory.forValue(null); 184 | } 185 | 186 | ExpectedCall expected = new ExpectedCall(this.call, this.matchers, 187 | this.captures, this.answer, range); 188 | behavior.addExpected(expected); 189 | 190 | this.unusedAnswer = false; 191 | this.callUsedAtLeastOnce = true; 192 | } 193 | 194 | /** 195 | * Disables the ExpectationSetter. After calling this method it 196 | * cannot be used to record expectations anymore. 197 | */ 198 | void retire() { 199 | if (this.unusedAnswer || !this.callUsedAtLeastOnce) { 200 | saveExpectation(Range.DEFAULT); 201 | } 202 | this.retired = true; 203 | } 204 | 205 | /** 206 | * In case no matchers were used for the call, this method creates 207 | * equal matchers for the provided arguments. 208 | */ 209 | private List createMissingMatchers(Call call, 210 | List matchers) { 211 | if (matchers == null) { 212 | // no matchers used 213 | List result = new ArrayList(); 214 | for (final Object argument : call.getArguments()) { 215 | result.add(new Equals(argument)); 216 | } 217 | return result; 218 | } 219 | 220 | if (matchers.size() != call.getArguments().size()) { 221 | throw new IllegalStateException("" 222 | + call.getArguments().size() 223 | + " matchers expected, " 224 | + matchers.size() 225 | + " recorded.\n" 226 | + "This exception usually occurs when matchers " 227 | + "are mixed with raw values when recording a method:\n" 228 | + "\tmock.foo(5, ctrl.eq(6));\t// wrong\n" 229 | + "You need to use no matcher at all or a matcher for every single param:\n" 230 | + "\tmock.foo(ctrl.eq(5), ctrl.eq(6));\t// right\n" 231 | + "\tmock.foo(5, 6);\t// also right"); 232 | } 233 | return matchers; 234 | } 235 | 236 | /** 237 | * Check, if this setter is still valid to set expectations. 238 | */ 239 | private void checkIsNotRetired() { 240 | if (this.retired) { 241 | throw new IllegalStateException( 242 | "Cannot use this expectation setter anymore." + 243 | "\nThis exception usually occurs when you hold on to an ExpectationSetter." + 244 | "\nYou should not do that."); 245 | } 246 | } 247 | } 248 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/internal/ExpectedCall.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | import com.google.gwt.testing.easygwtmock.client.Answer; 20 | import com.google.gwt.testing.easygwtmock.client.ArgumentMatcher; 21 | import com.google.gwt.testing.easygwtmock.client.internal.matchers.ArgumentCapture; 22 | 23 | import java.util.Iterator; 24 | import java.util.List; 25 | import java.util.Set; 26 | 27 | /** 28 | * An object that represents an expected method call. 29 | * 30 | * @author Michael Goderbauer 31 | */ 32 | public class ExpectedCall { 33 | 34 | private final Call call; // call made during recording to set up expectation 35 | private final List matchers; 36 | private final Set captures; 37 | private final Answer answer; 38 | private final Range range; 39 | 40 | private int answerUsageCount; 41 | 42 | ExpectedCall(Call call, List matchers, Set captures, 43 | Answer answer, Range range) { 44 | this.call = call; 45 | this.matchers = matchers; 46 | this.captures = captures; 47 | this.answer = answer; 48 | this.range = range; 49 | this.answerUsageCount = 0; 50 | } 51 | 52 | /** 53 | * Determines if the provided call matches this expected call. 54 | */ 55 | boolean matches(Call actual) { 56 | return this.call.getMock() == actual.getMock() 57 | && this.call.getMethod() == actual.getMethod() 58 | && matchesArguments(actual.getArguments()); 59 | } 60 | 61 | /** 62 | * Determines if the provided argument list fulfills the argument matchers of this call. 63 | */ 64 | private boolean matchesArguments(List arguments) { 65 | if (arguments.size() != matchers.size()) { 66 | return false; 67 | } 68 | for (int i = 0; i < arguments.size(); i++) { 69 | if (!matchers.get(i).matches(arguments.get(i))) { 70 | return false; 71 | } 72 | } 73 | return true; 74 | } 75 | 76 | @Override 77 | public String toString() { 78 | StringBuffer result = new StringBuffer(); 79 | result.append(call.getMethod().getName()); 80 | result.append("("); 81 | for (Iterator it = matchers.iterator(); it.hasNext();) { 82 | it.next().appendTo(result); 83 | if (it.hasNext()) { 84 | result.append(", "); 85 | } 86 | } 87 | result.append(")"); 88 | return result.toString(); 89 | } 90 | 91 | boolean expectationMet() { 92 | return this.range.includes(this.answerUsageCount); 93 | } 94 | 95 | boolean canBeInvoked() { 96 | return this.answerUsageCount < this.range.getMax(); 97 | } 98 | 99 | Answer invoke() { 100 | this.answerUsageCount++; 101 | captureArguemnts(); 102 | return this.answer; 103 | } 104 | 105 | private void captureArguemnts() { 106 | if (this.captures == null) { 107 | return; 108 | } 109 | for (ArgumentCapture capture : this.captures) { 110 | capture.captureArgument(); 111 | } 112 | } 113 | 114 | int getCallCount() { 115 | return this.answerUsageCount; 116 | } 117 | 118 | String getExpectedCallRange() { 119 | return this.range.toString(); 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/internal/IllegalStateExceptionWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | /** 20 | * Wraps an IllegalStateException that is thrown somewhere in the internals of EasyGwtMock. 21 | * Before the exception leaves the framework, it is unwrapped and the stacktrace is cut 22 | * to hide the internals of EasyGwtMock from the user. 23 | * 24 | * @author Michael Goderbauer 25 | */ 26 | public class IllegalStateExceptionWrapper extends Exception { 27 | 28 | private final IllegalStateException exception; 29 | 30 | IllegalStateExceptionWrapper(IllegalStateException exception) { 31 | this.exception = exception; 32 | } 33 | 34 | public IllegalStateException getIllegalStateException() { 35 | return this.exception; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/internal/Method.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | import java.util.HashMap; 20 | import java.util.Map; 21 | 22 | /** 23 | * An object that represents a method that can be called on a mock object. 24 | * 25 | * @author Michael Goderbauer 26 | */ 27 | public class Method { 28 | 29 | private final String name; 30 | private final Class returnType; 31 | private final Class[] declaredThrowables; 32 | private final Class[] argumentTypes; 33 | 34 | private static Map, Object> defaultReturnValues = new HashMap, Object>(); 35 | static { 36 | defaultReturnValues.put(byte.class, (byte) 0); 37 | defaultReturnValues.put(short.class, (short) 0); 38 | defaultReturnValues.put(int.class, 0); 39 | defaultReturnValues.put(long.class, (long) 0); 40 | defaultReturnValues.put(float.class, (float) 0); 41 | defaultReturnValues.put(double.class, (double) 0); 42 | defaultReturnValues.put(boolean.class, false); 43 | defaultReturnValues.put(char.class, (char) 0); 44 | } 45 | 46 | 47 | public Method(String name, Class returnType, Class[] argumentTypes, 48 | Class[] declaredThrowables) { 49 | this.name = name; 50 | this.returnType = returnType; 51 | this.declaredThrowables = declaredThrowables; 52 | this.argumentTypes = argumentTypes; 53 | } 54 | 55 | public String getName() { 56 | return this.name; 57 | } 58 | 59 | public Object getDefaultReturnValue() { 60 | return defaultReturnValues.get(this.returnType); 61 | } 62 | 63 | public boolean isReturnValueVoid() { 64 | return this.returnType.equals(void.class); 65 | } 66 | 67 | public boolean isReturnValuePrimitive() { 68 | return this.returnType.isPrimitive(); 69 | } 70 | 71 | public boolean canThrow(Throwable throwable) { 72 | if (throwable instanceof RuntimeException) { 73 | return true; 74 | } 75 | if (throwable instanceof Error) { 76 | return true; 77 | } 78 | Class throwableClass = throwable.getClass(); 79 | for (Class declaredThrowable : this.declaredThrowables) { 80 | if (Utils.isSubclass(throwableClass, declaredThrowable)) { 81 | return true; 82 | } 83 | } 84 | return false; 85 | } 86 | 87 | public Class[] getArgumentTypes() { 88 | return this.argumentTypes; 89 | } 90 | 91 | @Override 92 | public String toString() { 93 | StringBuilder result = new StringBuilder(this.name); 94 | result.append("("); 95 | for (Class argument : this.argumentTypes) { 96 | result.append(Utils.cutPackage(argument.getName())).append(", "); 97 | } 98 | if (this.argumentTypes.length > 0) { 99 | result.setLength(result.length() - 2); 100 | } 101 | 102 | result.append(")"); 103 | return result.toString(); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/internal/MocksBehavior.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | import com.google.gwt.testing.easygwtmock.client.Answer; 20 | 21 | import java.util.HashSet; 22 | import java.util.LinkedList; 23 | import java.util.Queue; 24 | import java.util.Set; 25 | 26 | /** 27 | * An object that keeps track of the expected method calls of mock objects (= behavior 28 | * of mock objects). 29 | * 30 | * @author Michael Goderbauer 31 | */ 32 | public class MocksBehavior { 33 | private final Queue expectedCalls; 34 | private Set niceMocks; 35 | 36 | MocksBehavior() { 37 | this.expectedCalls = new LinkedList(); 38 | this.niceMocks = new HashSet(); 39 | } 40 | 41 | /** 42 | * Checks if all expected calls were met. 43 | */ 44 | void verify() throws AssertionErrorWrapper { 45 | for (ExpectedCall call : this.expectedCalls) { 46 | if (!call.expectationMet()) { 47 | StringBuilder error = new StringBuilder("\n Expectation failure on verify. "); 48 | appendExpectationList(error, true, null); 49 | throw new AssertionErrorWrapper(new AssertionError(error.toString())); 50 | } 51 | } 52 | } 53 | 54 | /** 55 | * Adds an expected call to the behavior of the mocks. 56 | */ 57 | void addExpected(ExpectedCall expected) { 58 | this.expectedCalls.add(expected); 59 | } 60 | 61 | /** 62 | * Checks, if an actual call was expected. 63 | * 64 | * @return expected return value for invocation 65 | */ 66 | Answer addActual(Call actual) throws AssertionErrorWrapper { 67 | for (ExpectedCall expected : this.expectedCalls) { 68 | if (!expected.canBeInvoked()) { 69 | continue; 70 | } 71 | if (!expected.matches(actual)) { 72 | continue; 73 | } 74 | return expected.invoke(); 75 | } 76 | 77 | if (isNiceMock(actual.getMock())) { 78 | return AnswerFactory.forValue(actual.getDefaultReturnValue()); 79 | } 80 | 81 | StringBuilder error = new StringBuilder("\n Unexpected method call "); 82 | error.append(actual.toString()).append(". "); 83 | appendExpectationList(error, true, actual); 84 | throw new AssertionErrorWrapper(new AssertionError(error.toString())); 85 | } 86 | 87 | private boolean isNiceMock(Object mock) { 88 | return this.niceMocks.contains(mock); 89 | } 90 | 91 | void addNiceMock(Object mock) { 92 | this.niceMocks.add(mock); 93 | } 94 | 95 | void removeNiceMock(Object mock) { 96 | this.niceMocks.remove(mock); 97 | } 98 | 99 | private void appendExpectationList(StringBuilder builder, boolean markUnfullfiled, 100 | Call matchingCall) { 101 | builder.append("List of all expectations:"); 102 | 103 | if (this.expectedCalls.size() == 0) { 104 | builder.append("\n \n"); 105 | return; 106 | } 107 | 108 | if (matchingCall != null) { 109 | builder.append("\n Potential matches are marked with (+1)."); 110 | } 111 | 112 | for (ExpectedCall expected : this.expectedCalls) { 113 | builder.append("\n "); 114 | if (markUnfullfiled && !expected.expectationMet()) { 115 | builder.append("--> "); 116 | } else { 117 | builder.append(" "); 118 | } 119 | builder.append(expected.toString()) 120 | .append(": expected ").append(expected.getExpectedCallRange()) 121 | .append(", actual ").append(expected.getCallCount()); 122 | if (matchingCall != null && expected.matches(matchingCall)) { 123 | builder.append(" (+1)"); 124 | } 125 | } 126 | 127 | builder.append("\n"); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/internal/MocksControlState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | import com.google.gwt.testing.easygwtmock.client.Answer; 20 | import com.google.gwt.testing.easygwtmock.client.ArgumentMatcher; 21 | import com.google.gwt.testing.easygwtmock.client.ExpectationSetters; 22 | import com.google.gwt.testing.easygwtmock.client.internal.matchers.ArgumentCapture; 23 | 24 | /** 25 | * Interface for the states a MocksManager can be in 26 | * (state pattern). 27 | * 28 | * {@link "http://en.wikipedia.org/wiki/State_pattern"} 29 | * 30 | * @author Michael Goderbauer 31 | */ 32 | interface MocksControlState { 33 | 34 | void checkCanSwitchToReplay() throws IllegalStateExceptionWrapper; 35 | 36 | void verify() throws AssertionErrorWrapper, IllegalStateExceptionWrapper; 37 | 38 | Answer invoke(Call invocation) throws AssertionErrorWrapper; 39 | 40 | void reportMatcher(ArgumentMatcher matcher) throws IllegalStateExceptionWrapper; 41 | 42 | void reportCapture(ArgumentCapture argumentCapture) throws IllegalStateExceptionWrapper; 43 | 44 | ExpectationSetters getExpectationSetter() throws IllegalStateExceptionWrapper; 45 | 46 | void unmockableCallTo(String methodName); 47 | 48 | } 49 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/internal/Range.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | /** 20 | * Represents how often a call is expected. 21 | * 22 | * @author Michael Goderbauer 23 | */ 24 | public class Range { 25 | 26 | /** 27 | * Represents an open ended max value 28 | */ 29 | static final int UNLIMITED_MAX = Integer.MAX_VALUE; 30 | 31 | /** 32 | * Default range is Range(1, 1) 33 | */ 34 | public static final Range DEFAULT = new Range(1, 1); 35 | 36 | private final int max; 37 | private final int min; 38 | 39 | Range(int min, int max) { 40 | this.min = min; 41 | this.max = max; 42 | } 43 | 44 | @Override 45 | public String toString() { 46 | if (this.min == this.max) { 47 | return "" + this.min; 48 | } 49 | if (this.max == UNLIMITED_MAX) { 50 | return "at least " + this.min; 51 | } 52 | return "between " + this.min + " and " + this.max; 53 | } 54 | 55 | int getMax() { 56 | return this.max; 57 | } 58 | 59 | int getMin() { 60 | return this.min; 61 | } 62 | 63 | /** 64 | * Checks, if the given value is within the range. 65 | */ 66 | boolean includes(int value) { 67 | return value >= this.min && value <= this.max; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/internal/RecordState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | import com.google.gwt.testing.easygwtmock.client.Answer; 20 | import com.google.gwt.testing.easygwtmock.client.ArgumentMatcher; 21 | import com.google.gwt.testing.easygwtmock.client.ExpectationSetters; 22 | import com.google.gwt.testing.easygwtmock.client.internal.matchers.ArgumentCapture; 23 | 24 | import java.util.ArrayList; 25 | import java.util.HashSet; 26 | import java.util.List; 27 | import java.util.Set; 28 | 29 | /** 30 | * An object representing the record state. 31 | * 32 | * @author Michael Goderbauer 33 | */ 34 | public class RecordState implements MocksControlState { 35 | 36 | private final MocksBehavior behavior; 37 | List argumentMatchers; // Visible for testing 38 | Set argumentCaptures; // Visible for testing 39 | ExpectationSettersImpl currentSetter; // Visible for testing 40 | private String unmockableLastCall; 41 | 42 | RecordState(MocksBehavior behavior) { 43 | this.behavior = behavior; 44 | } 45 | 46 | @Override 47 | public void checkCanSwitchToReplay() { 48 | retireCurrentSetter(); 49 | } 50 | 51 | @Override 52 | public ExpectationSetters getExpectationSetter() throws IllegalStateExceptionWrapper { 53 | if (this.unmockableLastCall != null) { 54 | throw new IllegalStateExceptionWrapper( 55 | new IllegalStateException("Method " + this.unmockableLastCall + " cannot be mocked")); 56 | } 57 | if (this.currentSetter == null) { 58 | throw new IllegalStateExceptionWrapper( 59 | new IllegalStateException("Method call on mock needed before setting expectations")); 60 | } 61 | return this.currentSetter; 62 | } 63 | 64 | @Override 65 | public void verify() throws IllegalStateExceptionWrapper { 66 | throw new IllegalStateExceptionWrapper( 67 | new IllegalStateException("Calling verify is not allowed in record state")); 68 | } 69 | 70 | @Override 71 | public Answer invoke(final Call call) { 72 | retireCurrentSetter(); 73 | this.currentSetter = new ExpectationSettersImpl(call, this.argumentMatchers, 74 | this.argumentCaptures, this.behavior); 75 | this.unmockableLastCall = null; 76 | this.argumentMatchers = null; 77 | this.argumentCaptures = null; 78 | return AnswerFactory.forValue(call.getDefaultReturnValue()); 79 | } 80 | 81 | @Override 82 | public void unmockableCallTo(String methodName) { 83 | retireCurrentSetter(); 84 | this.argumentMatchers = null; 85 | this.argumentCaptures = null; 86 | this.unmockableLastCall = methodName; 87 | } 88 | 89 | @Override 90 | public void reportMatcher(ArgumentMatcher matcher) { 91 | if (this.argumentMatchers == null) { 92 | this.argumentMatchers = new ArrayList(); 93 | } 94 | this.argumentMatchers.add(matcher); 95 | } 96 | 97 | private void retireCurrentSetter() { 98 | if (this.currentSetter != null) { 99 | this.currentSetter.retire(); 100 | this.currentSetter = null; 101 | } 102 | } 103 | 104 | @Override 105 | public void reportCapture(ArgumentCapture argumentCapture) throws IllegalStateExceptionWrapper { 106 | this.reportMatcher(argumentCapture); 107 | if (this.argumentCaptures == null) { 108 | this.argumentCaptures = new HashSet(); 109 | } 110 | if (!this.argumentCaptures.add(argumentCapture)) { 111 | throw new IllegalStateExceptionWrapper( 112 | new IllegalStateException("Cannot use same capture twice for same method call")); 113 | } 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/internal/ReplayState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | import com.google.gwt.testing.easygwtmock.client.Answer; 20 | import com.google.gwt.testing.easygwtmock.client.ArgumentMatcher; 21 | import com.google.gwt.testing.easygwtmock.client.ExpectationSetters; 22 | import com.google.gwt.testing.easygwtmock.client.internal.matchers.ArgumentCapture; 23 | 24 | /** 25 | * An object representing the replay state. 26 | * 27 | * @author Michael Goderbauer 28 | */ 29 | public class ReplayState implements MocksControlState { 30 | 31 | private MocksBehavior behavior; 32 | 33 | ReplayState(MocksBehavior behavior) { 34 | this.behavior = behavior; 35 | } 36 | 37 | @Override 38 | public ExpectationSetters getExpectationSetter() throws IllegalStateExceptionWrapper { 39 | throw new IllegalStateExceptionWrapper( 40 | new IllegalStateException("Cannot set expectations while in replay state")); 41 | } 42 | 43 | @Override 44 | public void checkCanSwitchToReplay() throws IllegalStateExceptionWrapper { 45 | throw new IllegalStateExceptionWrapper( 46 | new IllegalStateException("Cannot switch to replay mode while in replay state")); 47 | } 48 | 49 | @Override 50 | public void verify() throws AssertionErrorWrapper { 51 | this.behavior.verify(); 52 | } 53 | 54 | @Override 55 | public Answer invoke(Call call) throws AssertionErrorWrapper { 56 | return this.behavior.addActual(call); 57 | } 58 | 59 | @Override 60 | public void reportMatcher(ArgumentMatcher matcher) throws IllegalStateExceptionWrapper { 61 | throw new IllegalStateExceptionWrapper( 62 | new IllegalStateException("Argument matchers must not be used in replay state")); 63 | } 64 | 65 | @Override 66 | public void reportCapture(ArgumentCapture argumentCapture) throws IllegalStateExceptionWrapper { 67 | throw new IllegalStateExceptionWrapper( 68 | new IllegalStateException("Captures must not be used in replay state")); 69 | } 70 | 71 | @Override 72 | public void unmockableCallTo(String methodName) { 73 | // we don't care about this during replay mode 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/internal/Utils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | import java.util.Arrays; 20 | 21 | /** 22 | * Provides different static helper methods. 23 | * 24 | * @author Michael Goderbauer 25 | */ 26 | public class Utils { 27 | 28 | /** 29 | * Appends a string representation of the given argument to the buffer. 30 | */ 31 | public static void appendArgumentTo(Object argument, StringBuffer b) { 32 | if (argument == null) { 33 | b.append("null"); 34 | } else if (argument.getClass().isArray()) { 35 | if (argument instanceof Object[]) { 36 | b.append(Arrays.deepToString((Object[]) argument)); 37 | } else if (argument instanceof boolean[]) { 38 | b.append(Arrays.toString((boolean[]) argument)); 39 | } else if (argument instanceof byte[]) { 40 | b.append(Arrays.toString((byte[]) argument)); 41 | } else if (argument instanceof char[]) { 42 | b.append(Arrays.toString((char[]) argument)); 43 | } else if (argument instanceof short[]) { 44 | b.append(Arrays.toString((short[]) argument)); 45 | } else if (argument instanceof int[]) { 46 | b.append(Arrays.toString((int[]) argument)); 47 | } else if (argument instanceof long[]) { 48 | b.append(Arrays.toString((long[]) argument)); 49 | } else if (argument instanceof float[]) { 50 | b.append(Arrays.toString((float[]) argument)); 51 | } else if (argument instanceof double[]) { 52 | b.append(Arrays.toString((double[]) argument)); 53 | } 54 | } else { 55 | b.append(argument.toString()); 56 | } 57 | } 58 | 59 | public static boolean isSubclass(Class subclass, Class baseclass) { 60 | while (subclass != null) { 61 | if (subclass.equals(baseclass)) { 62 | return true; 63 | } 64 | subclass = subclass.getSuperclass(); 65 | } 66 | return false; 67 | } 68 | 69 | public static String cutPackage(String str) { 70 | String[] components = str.split("\\."); 71 | return components[components.length - 1].replace("$", "."); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/internal/matchers/Any.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2010 the original author or authors. 3 | * Portions Copyright 2011 Google Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 6 | * use this file except in compliance with the License. You may obtain a copy of 7 | * the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14 | * License for the specific language governing permissions and limitations under 15 | * the License. 16 | */ 17 | 18 | package com.google.gwt.testing.easygwtmock.client.internal.matchers; 19 | 20 | import com.google.gwt.testing.easygwtmock.client.ArgumentMatcher; 21 | 22 | /** 23 | * Argument matcher that matches any object. 24 | * 25 | * @author Michael Goderbauer 26 | * Originally written for EasyMock {@link "www.easymock.org"} by OFFIS, Tammo Freese 27 | */ 28 | public class Any implements ArgumentMatcher{ 29 | 30 | public static final Any ANY = new Any(); 31 | 32 | private Any() { 33 | } 34 | 35 | @Override 36 | public boolean matches(Object argument) { 37 | return true; 38 | } 39 | 40 | @Override 41 | public void appendTo(StringBuffer buffer) { 42 | buffer.append(""); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/internal/matchers/ArgumentCapture.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2010 the original author or authors. 3 | * Portions Copyright 2011 Google Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 6 | * use this file except in compliance with the License. You may obtain a copy of 7 | * the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14 | * License for the specific language governing permissions and limitations under 15 | * the License. 16 | */ 17 | 18 | package com.google.gwt.testing.easygwtmock.client.internal.matchers; 19 | 20 | import com.google.gwt.testing.easygwtmock.client.ArgumentMatcher; 21 | import com.google.gwt.testing.easygwtmock.client.Capture; 22 | 23 | /** 24 | * Matches any argument and saves it in the provided capture for later usage. 25 | * 26 | * @author Michael Goderbauer 27 | * Originally written for EasyMock {@link "www.easymock.org"} by Henri Tremblay 28 | */ 29 | public class ArgumentCapture implements ArgumentMatcher { 30 | 31 | private Capture capture; 32 | private Object potentialValue; 33 | 34 | public ArgumentCapture(Capture capture) { 35 | this.capture = capture; 36 | } 37 | 38 | @Override 39 | public boolean matches(Object argument) { 40 | this.potentialValue = argument; 41 | return true; 42 | } 43 | 44 | @Override 45 | public void appendTo(StringBuffer buffer) { 46 | buffer.append("captured(").append(this.capture).append(")"); 47 | } 48 | 49 | public void captureArgument() { 50 | this.capture.captureValue(this.potentialValue); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/internal/matchers/AsyncCallbackMatcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal.matchers; 18 | 19 | import com.google.gwt.testing.easygwtmock.client.ArgumentMatcher; 20 | import com.google.gwt.user.client.rpc.AsyncCallback; 21 | 22 | /** 23 | * Matches any AsyncCallback object 24 | * 25 | * @author Michael Goderbauer 26 | */ 27 | public class AsyncCallbackMatcher implements ArgumentMatcher { 28 | 29 | public static final ArgumentMatcher MATCHER = new AsyncCallbackMatcher(); 30 | 31 | private AsyncCallbackMatcher() { 32 | } 33 | 34 | @Override 35 | public boolean matches(Object argument) { 36 | return argument instanceof AsyncCallback; 37 | } 38 | 39 | @Override 40 | public void appendTo(StringBuffer buffer) { 41 | buffer.append(""); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/client/internal/matchers/Equals.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2010 the original author or authors. 3 | * Portions Copyright 2011 Google Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 6 | * use this file except in compliance with the License. You may obtain a copy of 7 | * the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14 | * License for the specific language governing permissions and limitations under 15 | * the License. 16 | */ 17 | 18 | package com.google.gwt.testing.easygwtmock.client.internal.matchers; 19 | 20 | import com.google.gwt.testing.easygwtmock.client.ArgumentMatcher; 21 | import com.google.gwt.testing.easygwtmock.client.internal.Utils; 22 | 23 | /** 24 | * Matches any argument that is equal to a given value. 25 | * Equality is determined by .equals(). 26 | * 27 | * @author Michael Goderbauer 28 | * Originally written for EasyMock {@link "www.easymock.org"} by OFFIS, Tammo Freese 29 | */ 30 | public class Equals implements ArgumentMatcher { 31 | 32 | private Object expected; 33 | 34 | public Equals(Object expected) { 35 | this.expected = expected; 36 | } 37 | 38 | @Override 39 | public boolean matches(Object actual) { 40 | if (this.expected == null) { 41 | return actual == null; 42 | } 43 | return expected.equals(actual); 44 | } 45 | 46 | @Override 47 | public void appendTo(StringBuffer buffer) { 48 | Utils.appendArgumentTo(this.expected, buffer); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /java/com/google/gwt/testing/easygwtmock/rebind/MocksControlGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.rebind; 18 | 19 | import com.google.gwt.core.ext.Generator; 20 | import com.google.gwt.core.ext.GeneratorContext; 21 | import com.google.gwt.core.ext.TreeLogger; 22 | import com.google.gwt.core.ext.UnableToCompleteException; 23 | import com.google.gwt.core.ext.typeinfo.JClassType; 24 | import com.google.gwt.core.ext.typeinfo.JConstructor; 25 | import com.google.gwt.core.ext.typeinfo.JMethod; 26 | import com.google.gwt.core.ext.typeinfo.JPackage; 27 | import com.google.gwt.core.ext.typeinfo.JParameter; 28 | import com.google.gwt.core.ext.typeinfo.JType; 29 | import com.google.gwt.core.ext.typeinfo.TypeOracle; 30 | import com.google.gwt.testing.easygwtmock.client.MocksControl; 31 | import com.google.gwt.testing.easygwtmock.client.Nice; 32 | import com.google.gwt.testing.easygwtmock.client.internal.MocksControlBase; 33 | import com.google.gwt.user.rebind.ClassSourceFileComposerFactory; 34 | import com.google.gwt.user.rebind.SourceWriter; 35 | 36 | import java.io.PrintWriter; 37 | import java.util.HashSet; 38 | import java.util.Set; 39 | 40 | /** 41 | * MocksControlGenerator generates the concrete implementation of an interface that extends 42 | * {@link com.google.gwt.testing.easygwtmock.client.MocksControl} giving access to the 43 | * mocks specified in that interface. 44 | * 45 | * @author Michael Goderbauer 46 | */ 47 | public class MocksControlGenerator extends Generator { 48 | 49 | /** 50 | * Generates the concrete MocksControl implementation of the {@code typeName} interface and 51 | * delegates generation of mock classes to 52 | * {@link com.google.gwt.testing.easygwtmock.rebind.MocksGenerator} 53 | */ 54 | @Override 55 | public String generate(TreeLogger logger, GeneratorContext context, String typeName) 56 | throws UnableToCompleteException { 57 | 58 | TypeOracle typeOracle = context.getTypeOracle(); 59 | JClassType mockControlInterface = typeOracle.findType(typeName); 60 | 61 | if (mockControlInterface == null) { 62 | logger.log(TreeLogger.ERROR, "Unable to find metadata for type '" + typeName + "'", null); 63 | throw new UnableToCompleteException(); 64 | } 65 | 66 | if (mockControlInterface.isInterface() == null) { 67 | logger.log(TreeLogger.ERROR, 68 | mockControlInterface.getQualifiedSourceName() + " is not an interface", null); 69 | throw new UnableToCompleteException(); 70 | } 71 | 72 | JPackage interfacePackage = mockControlInterface.getPackage(); 73 | String packageName = interfacePackage == null ? "" : interfacePackage.getName(); 74 | String newClassName = mockControlInterface.getName().replace(".", "_") + "Impl"; 75 | String fullNewClassName = packageName + "." + newClassName; 76 | 77 | PrintWriter printWriter = context.tryCreate(logger, packageName, newClassName); 78 | if (printWriter == null) { 79 | // We generated this before. 80 | return fullNewClassName; 81 | } 82 | 83 | ClassSourceFileComposerFactory composer = 84 | new ClassSourceFileComposerFactory(packageName, newClassName); 85 | composer.setSuperclass(MocksControlBase.class.getCanonicalName()); 86 | composer.addImplementedInterface(mockControlInterface.getQualifiedSourceName()); 87 | 88 | SourceWriter writer = composer.createSourceWriter(context, printWriter); 89 | writer.println(); 90 | 91 | MocksGenerator mocksGenerator = new MocksGenerator(context, logger); 92 | JClassType markerInterface = typeOracle.findType(MocksControl.class.getCanonicalName()); 93 | 94 | Set reservedNames = getMethodNames(composer.getSuperclassName(), logger, typeOracle); 95 | 96 | // Report one error per method in the control interface. They are likely to be independent, 97 | // so it's a bit nicer for the user. 98 | boolean failed = false; 99 | for (JMethod method : mockControlInterface.getOverridableMethods()) { 100 | if (method.getEnclosingType().equals(markerInterface)) { 101 | // Method is implemented in MocksControlBase 102 | continue; 103 | } 104 | 105 | if (reservedNames.contains(method.getName())) { 106 | // Method name is already used in base class and method should not be overwritten! 107 | logger.log(TreeLogger.ERROR, method.getName() + 108 | " is a reserved name. Do not use it in the extended MocksControl interface", null); 109 | failed = true; 110 | continue; 111 | } 112 | 113 | JClassType typeToMock = method.getReturnType().isClassOrInterface(); 114 | 115 | if (typeToMock == null) { 116 | logger.log(TreeLogger.ERROR, method.getReturnType().getQualifiedSourceName() + 117 | " is not an interface or a class", null); 118 | failed = true; 119 | continue; 120 | } 121 | 122 | if (typeToMock.isInterface() != null) { 123 | 124 | if (method.getParameterTypes().length != 0) { 125 | String methodName = mockControlInterface.getSimpleSourceName() + "." + method.getName(); 126 | logger.log(TreeLogger.ERROR, 127 | "This method should not have parameters because it creates Ua mock of an interface: " + 128 | methodName, null); 129 | failed = true; 130 | continue; 131 | } 132 | 133 | } else { 134 | 135 | JConstructor constructorToCall = typeToMock.findConstructor(method.getParameterTypes()); 136 | if (constructorToCall == null) { 137 | String methodName = mockControlInterface.getSimpleSourceName() + "." + method.getName(); 138 | logger.log(TreeLogger.ERROR, 139 | "Cannot find matching constructor to call for " + methodName, null); 140 | failed = true; 141 | continue; 142 | } 143 | } 144 | 145 | String mockClassName = mocksGenerator.generateMock(typeToMock); 146 | 147 | printFactoryMethod(writer, method, mockControlInterface, mockClassName); 148 | } 149 | 150 | if (failed) { 151 | throw new UnableToCompleteException(); 152 | } 153 | 154 | writer.commit(logger); 155 | return fullNewClassName; 156 | } 157 | 158 | private void printFactoryMethod(SourceWriter out, JMethod methodToImplement, 159 | JClassType mockControlInterface, String classToCreate) { 160 | out.println("%s {", methodToImplement.getReadableDeclaration(false, false, false, false, true)); 161 | out.indent(); 162 | if (isNiceMock(methodToImplement, mockControlInterface)) { 163 | out.print("return this.setToNice(new %s(", classToCreate); 164 | printMatchingParameters(out, methodToImplement); 165 | out.println(").__mockInit(this));"); 166 | } else { 167 | out.print("return new %s(", classToCreate); 168 | printMatchingParameters(out, methodToImplement); 169 | out.println(").__mockInit(this);"); 170 | } 171 | out.outdent(); 172 | out.println("}"); 173 | } 174 | 175 | private void printMatchingParameters(SourceWriter out, JMethod methodToImplement) { 176 | JParameter[] params = methodToImplement.getParameters(); 177 | for (int i = 0; i < params.length; i++) { 178 | if (i > 0) { 179 | out.print(", "); 180 | } 181 | out.print(params[i].getName()); 182 | } 183 | } 184 | 185 | private boolean isNiceMock(JMethod method, JClassType mockControlInterface) { 186 | boolean isNice = false; //default 187 | 188 | Nice interfaceAnnotation = mockControlInterface.getAnnotation(Nice.class); 189 | if (interfaceAnnotation != null) { 190 | isNice = interfaceAnnotation.value(); 191 | } 192 | 193 | Nice methodAnotation = method.getAnnotation(Nice.class); 194 | if (methodAnotation != null) { 195 | isNice = methodAnotation.value(); 196 | } 197 | 198 | return isNice; 199 | } 200 | 201 | private Set getMethodNames(String className, TreeLogger logger, 202 | TypeOracle typeOracle) throws UnableToCompleteException { 203 | 204 | JClassType baseClass = typeOracle.findType(className); 205 | if (baseClass == null) { 206 | logger.log(TreeLogger.ERROR, "Unable to find metadata for type '" + baseClass + "'", null); 207 | throw new UnableToCompleteException(); 208 | } 209 | 210 | Set result = new HashSet(); 211 | for (JMethod method : baseClass.getMethods()) { 212 | if (!method.isPrivate()) { 213 | result.add(method.getName()); 214 | } 215 | } 216 | 217 | return result; 218 | } 219 | } 220 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/AnswerGwtTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import com.google.gwt.core.client.GWT; 20 | import com.google.gwt.testing.easygwtmock.client.BaseGwtTestCase; 21 | 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | /** 26 | * Tests Answer in combination with andAnswer, andThrow, andReturn. 27 | * 28 | * @author Michael Goderbauer 29 | */ 30 | public class AnswerGwtTest extends BaseGwtTestCase { 31 | 32 | interface MyControl extends MocksControl { 33 | ToMock getMock(); 34 | } 35 | 36 | interface ToMock { 37 | int throwsSomething(int i, String s) throws MyException; 38 | int callTheCallback(Callback callback); 39 | List genericMethod(T obj); 40 | } 41 | 42 | private MyControl ctrl; 43 | private ToMock mock; 44 | 45 | @Override 46 | public void gwtSetUp() { 47 | this.ctrl = GWT.create(MyControl.class); 48 | this.mock = ctrl.getMock(); 49 | } 50 | 51 | public void testThrowCheckedException() throws MyException { 52 | MyException exception = new MyException(); 53 | ctrl.expect(mock.throwsSomething(1, "Hello")).andThrow(exception); 54 | 55 | ctrl.replay(); 56 | 57 | try { 58 | mock.throwsSomething(1, "Hello"); 59 | fail("should have thrown exception"); 60 | } catch (MyException expected) { 61 | assertSame(exception, expected); 62 | } 63 | 64 | ctrl.verify(); 65 | } 66 | 67 | public void testThrowUncheckedException() throws MyException { 68 | RuntimeException exception = new RuntimeException(); 69 | ctrl.expect(mock.throwsSomething(1, "Hello")).andThrow(exception); 70 | 71 | ctrl.replay(); 72 | 73 | try { 74 | mock.throwsSomething(1, "Hello"); 75 | fail("should have thrown exception"); 76 | } catch (RuntimeException expected) { 77 | assertSame(exception, expected); 78 | } 79 | 80 | ctrl.verify(); 81 | } 82 | 83 | public void testThrowUndeclaredCheckedException() throws MyException { 84 | MyUndeclaredException exception = new MyUndeclaredException(); 85 | 86 | try { 87 | ctrl.expect(mock.throwsSomething(1, "Hello")).andThrow(exception); 88 | fail("should have thrown exception"); 89 | } catch (IllegalStateException expected) { 90 | assertEquals( 91 | "AnswerGwtTest.MyUndeclaredException is not declared by throwsSomething(int, String)", 92 | expected.getMessage()); 93 | } 94 | } 95 | 96 | public void testAccessMethodArgs() throws MyException { 97 | 98 | MyIAnswer answer = new MyIAnswer(); 99 | 100 | ctrl.expect(mock.throwsSomething(10, "Hello")).andAnswer(answer); 101 | 102 | ctrl.replay(); 103 | 104 | assertEquals(0, answer.counter); 105 | assertEquals(110, mock.throwsSomething(10, "Hello")); 106 | assertEquals(1, answer.counter); 107 | 108 | ctrl.verify(); 109 | } 110 | 111 | public void testCallback() { 112 | final Callback callback = new Callback(); 113 | ctrl.expect(mock.callTheCallback(callback)).andAnswer(new Answer() { 114 | @Override 115 | public Integer answer(Object[] args) throws Throwable { 116 | assertEquals(1, args.length); 117 | assertEquals(callback, args[0]); 118 | ((Callback) args[0]).doIt(); 119 | return 42; 120 | } 121 | }); 122 | 123 | ctrl.replay(); 124 | 125 | assertEquals(0, callback.callCounter); 126 | assertEquals(42, mock.callTheCallback(callback)); 127 | assertEquals(1, callback.callCounter); 128 | 129 | ctrl.verify(); 130 | } 131 | 132 | public void testGenericsWithAnswer() { 133 | final ArrayList result = new ArrayList(); 134 | result.add(4); 135 | result.add(6); 136 | 137 | ctrl.expect(mock.genericMethod(1)).andAnswer(new Answer>() { 138 | @Override 139 | public List answer(Object[] args) throws Throwable { 140 | return result; 141 | } 142 | }); 143 | 144 | ctrl.replay(); 145 | 146 | assertSame(result, mock.genericMethod(1)); 147 | 148 | ctrl.verify(); 149 | } 150 | 151 | public void testThrowUndeclaredExceptionWithAnswer() throws MyException { 152 | final Throwable exception = new MyUndeclaredException(); 153 | 154 | ctrl.expect(mock.throwsSomething(1, "Hallo")).andAnswer(new Answer() { 155 | @Override 156 | public Integer answer(Object[] args) throws Throwable { 157 | throw exception; 158 | } 159 | }); 160 | 161 | ctrl.replay(); 162 | try { 163 | mock.throwsSomething(1, "Hallo"); 164 | fail("should have thrown exception"); 165 | } catch (UndeclaredThrowableException expected) { 166 | assertSame(exception, expected.getUndeclaredThrowable()); 167 | } 168 | 169 | ctrl.verify(); 170 | } 171 | 172 | class MyIAnswer implements Answer { 173 | 174 | int counter = 0; 175 | 176 | @Override 177 | public Integer answer(Object[] args) throws Throwable { 178 | counter++; 179 | assertEquals(2, args.length); 180 | assertEquals(10, args[0]); 181 | assertEquals("Hello", args[1]); 182 | return (Integer) args[0] + 100; 183 | } 184 | } 185 | 186 | class MyException extends Exception { 187 | 188 | } 189 | 190 | class MyUndeclaredException extends Exception { 191 | 192 | } 193 | 194 | class Callback { 195 | int callCounter = 0; 196 | 197 | void doIt() { 198 | callCounter++; 199 | } 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/BaseGwtTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import com.google.gwt.junit.client.GWTTestCase; 20 | 21 | /** 22 | * Base class for GWT test cases in Easy GWT Mock 23 | */ 24 | public abstract class BaseGwtTestCase extends GWTTestCase { 25 | 26 | @Override 27 | public String getModuleName() { 28 | return "com.google.gwt.testing.easygwtmock.Easygwtmock"; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/CallbackGwtTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import com.google.gwt.core.client.GWT; 20 | import com.google.gwt.testing.easygwtmock.client.BaseGwtTestCase; 21 | import com.google.gwt.user.client.rpc.AsyncCallback; 22 | 23 | /** 24 | * Tests callOnSuccess() and callOnFailure() of ExpectationSetter. 25 | * 26 | * @author Michael Goderbauer 27 | */ 28 | public class CallbackGwtTest extends BaseGwtTestCase { 29 | interface MyControl extends MocksControl { 30 | ToMock getMock(); 31 | } 32 | 33 | interface ToMock { 34 | void oneArgument(AsyncCallback callback); 35 | void twoArguments(int i, AsyncCallback callback); 36 | int nonVoid(String s); 37 | void noCallback(String s); 38 | } 39 | 40 | private MyControl ctrl; 41 | private ToMock mock; 42 | 43 | @Override 44 | public void gwtSetUp() { 45 | this.ctrl = GWT.create(MyControl.class); 46 | this.mock = ctrl.getMock(); 47 | } 48 | 49 | public void testOnSuccess_oneArgument() { 50 | MyCallback callback = new MyCallback(); 51 | 52 | mock.oneArgument(ctrl.asyncCallback(String.class)); 53 | ctrl.expectLastCall().andCallOnSuccess("Hallo"); 54 | 55 | ctrl.replay(); 56 | 57 | assertEquals(callback.onSuccessCalledCount, 0); 58 | mock.oneArgument(callback); 59 | assertEquals(callback.onSuccessCalledCount, 1); 60 | assertEquals("Hallo", callback.result); 61 | assertEquals(callback.onFailureCalledCount, 0); 62 | } 63 | 64 | public void testOnSuccess_twoArguments() { 65 | MyCallback callback = new MyCallback(); 66 | 67 | mock.twoArguments(ctrl.eq(4), ctrl.asyncCallback(Integer.class)); 68 | ctrl.expectLastCall().andCallOnSuccess(42); 69 | 70 | ctrl.replay(); 71 | 72 | assertEquals(callback.onSuccessCalledCount, 0); 73 | mock.twoArguments(4, callback); 74 | assertEquals(callback.onSuccessCalledCount, 1); 75 | assertEquals(42, callback.result); 76 | assertEquals(callback.onFailureCalledCount, 0); 77 | } 78 | 79 | public void testOnFailure_oneArgument() { 80 | MyCallback callback = new MyCallback(); 81 | Throwable throwable = new RuntimeException(); 82 | 83 | mock.oneArgument(ctrl.asyncCallback(String.class)); 84 | ctrl.expectLastCall().andCallOnFailure(throwable); 85 | 86 | ctrl.replay(); 87 | 88 | assertEquals(callback.onFailureCalledCount, 0); 89 | mock.oneArgument(callback); 90 | assertEquals(callback.onFailureCalledCount, 1); 91 | assertEquals(throwable, callback.caught); 92 | assertEquals(callback.onSuccessCalledCount, 0); 93 | } 94 | 95 | public void testOnFailure_twoArgument() { 96 | MyCallback callback = new MyCallback(); 97 | Throwable throwable = new RuntimeException(); 98 | 99 | mock.twoArguments(ctrl.eq(4), ctrl.asyncCallback(Integer.class)); 100 | ctrl.expectLastCall().andCallOnFailure(throwable); 101 | 102 | ctrl.replay(); 103 | 104 | assertEquals(callback.onFailureCalledCount, 0); 105 | mock.twoArguments(4, callback); 106 | assertEquals(callback.onFailureCalledCount, 1); 107 | assertEquals(throwable, callback.caught); 108 | assertEquals(callback.onSuccessCalledCount, 0); 109 | } 110 | 111 | public void testOnSuccess_NonVoidMethod() { 112 | MyCallback callback = new MyCallback(); 113 | 114 | try { 115 | ctrl.expect(mock.nonVoid("hi")).andCallOnSuccess(11); 116 | fail("should have thrown exception"); 117 | } catch (IllegalStateException expected) { 118 | } 119 | } 120 | 121 | public void testOnFailure_NonVoidMethod() { 122 | MyCallback callback = new MyCallback(); 123 | 124 | try { 125 | ctrl.expect(mock.nonVoid("hi")).andCallOnFailure(new Error()); 126 | fail("should have thrown exception"); 127 | } catch (IllegalStateException expected) { 128 | } 129 | } 130 | 131 | public void testOnSuccess_MethodWithoutCallbackArg() { 132 | MyCallback callback = new MyCallback(); 133 | mock.noCallback("hi"); 134 | 135 | try { 136 | ctrl.expectLastCall().andCallOnSuccess(11); 137 | fail("should have thrown exception"); 138 | } catch (IllegalStateException expected) { 139 | assertEquals("andCallOnSuccess() can only be used with methods " + 140 | "that take an AsyncCallback as last argument", expected.getMessage()); 141 | } 142 | } 143 | 144 | public void testOnFailure_MethodWithoutCallbackArg() { 145 | MyCallback callback = new MyCallback(); 146 | 147 | mock.noCallback("hi"); 148 | 149 | try { 150 | ctrl.expectLastCall().andCallOnFailure(null); 151 | fail("should have thrown exception"); 152 | } catch (IllegalStateException expected) { 153 | assertEquals( 154 | "andCallOnFailure() can only be used with methods " + 155 | "that take an AsyncCallback as last argument", expected.getMessage()); 156 | } 157 | } 158 | 159 | public void testOnSuccess_NoCallback() { 160 | mock.oneArgument(ctrl.>anyObject()); 161 | ctrl.expectLastCall().andCallOnSuccess("Hallo"); 162 | 163 | ctrl.replay(); 164 | 165 | try { 166 | mock.oneArgument(null); 167 | fail("should have thrown exception"); 168 | } catch (IllegalArgumentException expected) { 169 | } 170 | } 171 | 172 | public void testOnFailure_NoCallback() { 173 | mock.oneArgument(ctrl.>anyObject()); 174 | ctrl.expectLastCall().andCallOnFailure(null); 175 | 176 | ctrl.replay(); 177 | 178 | try { 179 | mock.oneArgument(null); 180 | fail("should have thrown exception"); 181 | } catch (IllegalArgumentException expected) { 182 | } 183 | } 184 | 185 | class MyCallback implements AsyncCallback { 186 | 187 | int onFailureCalledCount = 0; 188 | int onSuccessCalledCount = 0; 189 | 190 | Object result; 191 | Throwable caught; 192 | 193 | @Override 194 | public void onFailure(Throwable caught) { 195 | this.onFailureCalledCount++; 196 | this.caught = caught; 197 | } 198 | 199 | @Override 200 | public void onSuccess(Object result) { 201 | this.onSuccessCalledCount++; 202 | this.result = result; 203 | } 204 | } 205 | } 206 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/CaptureGwtTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import com.google.gwt.core.client.GWT; 20 | import com.google.gwt.testing.easygwtmock.client.BaseGwtTestCase; 21 | 22 | import java.util.List; 23 | 24 | /** 25 | * Tests to capture arguments of expected method calls 26 | * 27 | * @author Michael Goderbauer 28 | */ 29 | public class CaptureGwtTest extends BaseGwtTestCase { 30 | 31 | interface MyControl extends MocksControl { 32 | ToMock getMock(); 33 | } 34 | 35 | interface ToMock { 36 | void doSomething(String s, int i, boolean b); 37 | int returnInt(String s, int i, boolean b); 38 | boolean returnBoolean(String s, int i, boolean b); 39 | String returnString(String s, int i, boolean b); 40 | } 41 | 42 | private MyControl ctrl; 43 | private ToMock mock; 44 | private Capture captureBoolean; 45 | private Capture captureInteger; 46 | private Capture captureString; 47 | 48 | @Override 49 | public void gwtSetUp() { 50 | this.ctrl = GWT.create(MyControl.class); 51 | this.mock = ctrl.getMock(); 52 | this.captureBoolean = new Capture(); 53 | this.captureInteger = new Capture(); 54 | this.captureString = new Capture(); 55 | } 56 | 57 | public void testCaptureValuesOfBooleanMethod() { 58 | ctrl.expect(mock.returnBoolean(ctrl.captureObject(captureString), 59 | ctrl.captureInt(captureInteger), 60 | ctrl.captureBoolean(captureBoolean))).andReturn(true); 61 | 62 | ctrl.replay(); 63 | 64 | mock.returnBoolean("Hallo", 356, true); 65 | 66 | ctrl.verify(); 67 | 68 | assertTrue(captureBoolean.hasCaptured()); 69 | assertTrue(captureInteger.hasCaptured()); 70 | assertTrue(captureString.hasCaptured()); 71 | 72 | assertEquals("Hallo", captureString.getFirstValue()); 73 | assertEquals(356, (int) captureInteger.getFirstValue()); 74 | assertTrue(captureBoolean.getFirstValue()); 75 | } 76 | 77 | public void testCaptureValuesOfIntMethod() { 78 | ctrl.expect(mock.returnInt(ctrl.captureObject(captureString), 79 | ctrl.captureInt(captureInteger), 80 | ctrl.captureBoolean(captureBoolean))).andReturn(22); 81 | 82 | ctrl.replay(); 83 | 84 | mock.returnInt("Hallo", 356, true); 85 | 86 | ctrl.verify(); 87 | 88 | assertTrue(captureBoolean.hasCaptured()); 89 | assertTrue(captureInteger.hasCaptured()); 90 | assertTrue(captureString.hasCaptured()); 91 | 92 | assertEquals("Hallo", captureString.getFirstValue()); 93 | assertEquals(356, (int) captureInteger.getFirstValue()); 94 | assertTrue(captureBoolean.getFirstValue()); 95 | } 96 | 97 | public void testCaptureValuesOfStringMethod() { 98 | ctrl.expect(mock.returnString(ctrl.captureObject(captureString), 99 | ctrl.captureInt(captureInteger), 100 | ctrl.captureBoolean(captureBoolean))).andReturn("Ho"); 101 | 102 | ctrl.replay(); 103 | 104 | mock.returnString("Hallo", 356, true); 105 | 106 | ctrl.verify(); 107 | 108 | assertTrue(captureBoolean.hasCaptured()); 109 | assertTrue(captureInteger.hasCaptured()); 110 | assertTrue(captureString.hasCaptured()); 111 | 112 | assertEquals("Hallo", captureString.getFirstValue()); 113 | assertEquals(356, (int) captureInteger.getFirstValue()); 114 | assertTrue(captureBoolean.getFirstValue()); 115 | } 116 | 117 | public void testCaptureValuesOfVoidMethod() { 118 | mock.doSomething(ctrl.captureObject(captureString), 119 | ctrl.captureInt(captureInteger), 120 | ctrl.captureBoolean(captureBoolean)); 121 | 122 | ctrl.replay(); 123 | 124 | mock.doSomething("Hallo", 356, true); 125 | 126 | ctrl.verify(); 127 | 128 | assertTrue(captureBoolean.hasCaptured()); 129 | assertTrue(captureInteger.hasCaptured()); 130 | assertTrue(captureString.hasCaptured()); 131 | 132 | assertEquals("Hallo", captureString.getFirstValue()); 133 | assertEquals(356, (int) captureInteger.getFirstValue()); 134 | assertTrue(captureBoolean.getFirstValue()); 135 | } 136 | 137 | public void testCaptureMoreValuesInOneCapture() { 138 | ctrl.expect(mock.returnInt(ctrl.captureObject(captureString), ctrl.eq(322), ctrl.eq(true))) 139 | .andReturn(21).times(3); 140 | 141 | ctrl.replay(); 142 | 143 | mock.returnInt("Hallo", 322, true); 144 | mock.returnInt("Hi", 322, true); 145 | mock.returnInt("Bye", 322, true); 146 | 147 | ctrl.verify(); 148 | 149 | assertTrue(captureString.hasCaptured()); 150 | 151 | assertEquals("Hallo", captureString.getFirstValue()); 152 | assertEquals("Bye", captureString.getLastValue()); 153 | 154 | List values = captureString.getValues(); 155 | assertEquals(3, values.size()); 156 | assertEquals("Hallo", values.get(0)); 157 | assertEquals("Hi", values.get(1)); 158 | assertEquals("Bye", values.get(2)); 159 | } 160 | } 161 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/CaptureJavaTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import junit.framework.TestCase; 20 | 21 | import java.util.List; 22 | 23 | /** 24 | * Tests the Capture class 25 | * 26 | * @author Michael Goderbauer 27 | */ 28 | public class CaptureJavaTest extends TestCase { 29 | 30 | private Capture capture; 31 | 32 | @Override 33 | public void setUp() { 34 | capture = new Capture(); 35 | } 36 | 37 | public void testCreate() { 38 | String test = "test"; 39 | Capture captureString = Capture.create(); 40 | captureString.captureValue(test); 41 | assertEquals(test, captureString.getFirstValue()); 42 | } 43 | 44 | public void testGetValues() { 45 | assertTrue("should be empty", capture.getValues().isEmpty()); 46 | 47 | capture.captureValue(10); 48 | capture.captureValue(20); 49 | 50 | List values = capture.getValues(); 51 | assertEquals(2, values.size()); 52 | assertEquals(10, (int) values.get(0)); 53 | assertEquals(20, (int) values.get(1)); 54 | } 55 | 56 | public void testHasCaptured() { 57 | assertFalse("should not have captured", capture.hasCaptured()); 58 | 59 | capture.captureValue(10); 60 | capture.captureValue(20); 61 | 62 | assertTrue("should have captured", capture.hasCaptured()); 63 | } 64 | 65 | public void testGetFirstAndLastValue_oneValue() { 66 | capture.captureValue(10); 67 | 68 | assertEquals(10, (int) capture.getFirstValue()); 69 | assertEquals(10, (int) capture.getLastValue()); 70 | } 71 | 72 | public void testGetFirstAndLastValue_threeValue() { 73 | capture.captureValue(10); 74 | capture.captureValue(20); 75 | capture.captureValue(30); 76 | 77 | assertEquals(10, (int) capture.getFirstValue()); 78 | assertEquals(30, (int) capture.getLastValue()); 79 | } 80 | 81 | public void testToString() { 82 | assertEquals("", capture.toString()); 83 | 84 | capture.captureValue(10); 85 | 86 | assertEquals("10", capture.toString()); 87 | 88 | capture.captureValue(20); 89 | 90 | assertEquals("10, 20", capture.toString()); 91 | } 92 | 93 | public void testReset() { 94 | assertFalse("should not have captured", capture.hasCaptured()); 95 | capture.captureValue(10); 96 | assertTrue("should have captured", capture.hasCaptured()); 97 | assertFalse("should not be empty", capture.getValues().isEmpty()); 98 | 99 | capture.reset(); 100 | 101 | assertFalse("should not have captured", capture.hasCaptured()); 102 | assertTrue("should be empty", capture.getValues().isEmpty()); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/ClassMockingGwtTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import com.google.gwt.core.client.GWT; 20 | import com.google.gwt.testing.easygwtmock.client.BaseGwtTestCase; 21 | import com.google.gwt.testing.easygwtmock.client.dummyclasses.ClassToMock; 22 | import com.google.gwt.testing.easygwtmock.client.dummyclasses.OneArgClassToMock; 23 | 24 | /** 25 | * Can we mock classes? 26 | * 27 | * @author Michael Goderbauer 28 | */ 29 | public class ClassMockingGwtTest extends BaseGwtTestCase { 30 | 31 | interface MyControl extends MocksControl { 32 | ClassToMock getMock(); 33 | OneArgClassToMock getOneArgMock(String arg); 34 | ZeroArgSubClass getSubClassMock(); 35 | } 36 | 37 | public static class ZeroArgSubClass extends OneArgClassToMock { 38 | public ZeroArgSubClass() { 39 | super("fixed"); 40 | } 41 | } 42 | 43 | private MyControl ctrl; 44 | private ClassToMock mock; 45 | 46 | @Override 47 | public void gwtSetUp() { 48 | this.ctrl = GWT.create(MyControl.class); 49 | this.mock = ctrl.getMock(); 50 | } 51 | 52 | public void testClassMocking() { 53 | ctrl.expect(mock.returnString()).andReturn("Hallo"); 54 | ctrl.expect(mock.returnInt()).andReturn(42); 55 | mock.noReturnValue(); 56 | 57 | ctrl.replay(); 58 | 59 | assertEquals("Hallo", mock.returnString()); 60 | assertEquals(42, mock.returnInt()); 61 | mock.noReturnValue(); 62 | 63 | ctrl.verify(); 64 | } 65 | 66 | public void testConstructMockWithArgument() throws Exception { 67 | OneArgClassToMock mock = ctrl.getOneArgMock("hello"); 68 | assertEquals("hello", mock.arg); 69 | } 70 | 71 | public void testConstructMockWithFixedArgument() throws Exception { 72 | ZeroArgSubClass mock = ctrl.getSubClassMock(); 73 | assertEquals("fixed", mock.arg); 74 | } 75 | 76 | public void testPartialMock() throws Exception { 77 | OneArgClassToMock mock = ctrl.getOneArgMock("earthling"); 78 | 79 | ctrl.expect(mock.getGreeting()).andReturn("Greetings,"); 80 | ctrl.replay(); 81 | 82 | assertEquals("Greetings, earthling!", mock.makeMessage()); 83 | 84 | ctrl.verify(); 85 | } 86 | 87 | public void testFinalMethod() { 88 | assertEquals("I am final", mock.finalMethod()); 89 | } 90 | 91 | public void testToString() { 92 | assertEquals("Mock for ClassToMock", mock.toString()); 93 | } 94 | 95 | public void testFinalEquals() { 96 | assertTrue("should be true", mock.equals(null)); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/FrameworkMissuseGwtTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import com.google.gwt.core.client.GWT; 20 | import com.google.gwt.testing.easygwtmock.client.BaseGwtTestCase; 21 | 22 | /** 23 | * Tests various framework misuse cases. 24 | * 25 | * @author Michael Goderbauer 26 | */ 27 | public class FrameworkMissuseGwtTest extends BaseGwtTestCase { 28 | 29 | interface InterfaceToMock { 30 | void doSomething(int e); 31 | } 32 | 33 | interface MyIMocksControl extends MocksControl { 34 | InterfaceToMock getMock(); 35 | } 36 | 37 | private MyIMocksControl ctrl; 38 | private InterfaceToMock mock; 39 | 40 | @Override 41 | public void gwtSetUp() { 42 | this.ctrl = GWT.create(MyIMocksControl.class); 43 | this.mock = ctrl.getMock(); 44 | } 45 | 46 | public void testVerifyBeforeReplay() { 47 | try { 48 | ctrl.verify(); 49 | fail(); 50 | } catch (IllegalStateException expected) { 51 | } 52 | } 53 | 54 | public void testReplayAfterReplay() { 55 | ctrl.replay(); 56 | try { 57 | ctrl.replay(); 58 | fail(); 59 | } catch (IllegalStateException expected) { 60 | } 61 | } 62 | 63 | public void testReplayVerifyAfterReset() { 64 | 65 | mock.doSomething(1); 66 | ctrl.replay(); 67 | mock.doSomething(1); 68 | ctrl.verify(); 69 | 70 | ctrl.reset(); 71 | 72 | mock.doSomething(2); 73 | ctrl.replay(); 74 | mock.doSomething(2); 75 | ctrl.verify(); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/GenericInterfacesGwtTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import com.google.gwt.core.client.GWT; 20 | import com.google.gwt.testing.easygwtmock.client.BaseGwtTestCase; 21 | 22 | /** 23 | * Tests generating mocks of generic interfaces 24 | * 25 | * @author Michael Goderbauer 26 | */ 27 | public class GenericInterfacesGwtTest extends BaseGwtTestCase { 28 | 29 | interface MyControl extends MocksControl { 30 | ToMock getStringMock(); 31 | ToMock getIntegerMock(); 32 | ToMock getAnotherStringMock(); 33 | } 34 | 35 | interface ToMock { 36 | T doSomething(T t); 37 | } 38 | 39 | private MyControl ctrl; 40 | 41 | @Override 42 | public void gwtSetUp() { 43 | this.ctrl = GWT.create(MyControl.class); 44 | } 45 | 46 | public void testStringMock() { 47 | ToMock stringMock = ctrl.getStringMock(); 48 | ctrl.expect(stringMock.doSomething("Hi")).andReturn("Hello"); 49 | ctrl.replay(); 50 | assertEquals("Hello", stringMock.doSomething("Hi")); 51 | ctrl.verify(); 52 | } 53 | 54 | public void testIntegerMock() { 55 | ToMock integerMock = ctrl.getIntegerMock(); 56 | ctrl.expect(integerMock.doSomething(1)).andReturn(42); 57 | ctrl.replay(); 58 | assertEquals(42, (int) integerMock.doSomething(1)); 59 | ctrl.verify(); 60 | } 61 | 62 | public void testBothMocks() { 63 | ToMock stringMock = ctrl.getStringMock(); 64 | ToMock integerMock = ctrl.getIntegerMock(); 65 | 66 | ctrl.expect(stringMock.doSomething("Hi")).andReturn("Hello"); 67 | ctrl.expect(integerMock.doSomething(1)).andReturn(42); 68 | 69 | ctrl.replay(); 70 | 71 | assertEquals("Hello", stringMock.doSomething("Hi")); 72 | assertEquals(42, (int) integerMock.doSomething(1)); 73 | ctrl.verify(); 74 | } 75 | 76 | public void testMocksAreSameClass() { 77 | ToMock stringMock = ctrl.getStringMock(); 78 | ToMock anotherStringMock = ctrl.getAnotherStringMock(); 79 | assertSame(stringMock.getClass(), anotherStringMock.getClass()); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/MatcherGwtTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import com.google.gwt.core.client.GWT; 20 | import com.google.gwt.testing.easygwtmock.client.BaseGwtTestCase; 21 | 22 | /** 23 | * Tests the interaction between argument matchers and framework. 24 | * 25 | * @author Michael Goderbauer 26 | */ 27 | public class MatcherGwtTest extends BaseGwtTestCase { 28 | 29 | private InterfaceToMock mock; 30 | private MyIMockControl ctrl; 31 | 32 | interface MyIMockControl extends MocksControl { 33 | InterfaceToMock getMock(); 34 | } 35 | 36 | interface InterfaceToMock { 37 | void subtract(int a, int b); 38 | void add(int...sumands); 39 | void doSomething(String a, float b); 40 | } 41 | 42 | @Override 43 | public void gwtSetUp() { 44 | this.ctrl = GWT.create(MyIMockControl.class); 45 | this.mock = ctrl.getMock(); 46 | } 47 | 48 | public void testAllExpectationMet() { 49 | mock.subtract(ctrl.anyInt(), ctrl.anyInt()); 50 | mock.add(ctrl.anyInt(), ctrl.anyInt()); 51 | mock.doSomething(ctrl.anyObject(), ctrl.eq(0f)); 52 | 53 | ctrl.replay(); 54 | 55 | mock.add(3, 5); 56 | mock.subtract(4, 5); 57 | mock.doSomething("Hi", 0); 58 | 59 | ctrl.verify(); 60 | } 61 | 62 | public void testMatcherNotSatisfied() { 63 | mock.add(ctrl.eq(4), ctrl.eq(5)); 64 | 65 | ctrl.replay(); 66 | 67 | boolean exceptionThrown = true; 68 | try { 69 | mock.add(3, 5); 70 | exceptionThrown = false; 71 | } catch (AssertionError expected) { 72 | assertEquals("\n Unexpected method call add(3, 5). List of all expectations:" + 73 | "\n Potential matches are marked with (+1)." + 74 | "\n --> add(4, 5): expected 1, actual 0\n", expected.getMessage()); 75 | } 76 | assertTrue("should have thrown exception", exceptionThrown); 77 | } 78 | 79 | public void testMatcherMismatch() { 80 | try { 81 | mock.subtract(ctrl.anyInt(), 0); // should be ..., ctrl.eq(0)); 82 | fail(); 83 | } catch (IllegalStateException expected) { 84 | expected.getMessage().startsWith("2 matchers expected, 1 recorded."); 85 | } 86 | } 87 | 88 | public void testCustomMatcherMatches() { 89 | mock.subtract(ctrl.matchesInt(new IsOdd()), ctrl.eq(3)); 90 | 91 | ctrl.replay(); 92 | 93 | mock.subtract(5, 3); 94 | } 95 | 96 | public void testCustomMatcherDoesNotMatch() { 97 | mock.subtract(ctrl.matchesInt(new IsOdd()), ctrl.eq(3)); 98 | 99 | ctrl.replay(); 100 | 101 | boolean exceptionThrown = true; 102 | try { 103 | mock.subtract(4, 3); 104 | exceptionThrown = false; 105 | } catch (AssertionError expected) { 106 | assertTrue(expected.getMessage().contains("subtract(isOdd(), 3)")); 107 | } 108 | assertTrue("should have thrown exception", exceptionThrown); 109 | } 110 | 111 | class IsOdd implements ArgumentMatcher { 112 | 113 | @Override 114 | public boolean matches(Object argument) { 115 | if (!(argument instanceof Integer)) { 116 | return false; 117 | } 118 | return ((Integer) argument) % 2 == 1; 119 | } 120 | 121 | @Override 122 | public void appendTo(StringBuffer buffer) { 123 | buffer.append("isOdd()"); 124 | } 125 | 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/MethodsWithGenericsGwtTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import com.google.gwt.core.client.GWT; 20 | import com.google.gwt.testing.easygwtmock.client.BaseGwtTestCase; 21 | 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | /** 26 | * Tests mocking generic methods. 27 | * 28 | * @author Michael Goderbauer 29 | */ 30 | public class MethodsWithGenericsGwtTest extends BaseGwtTestCase { 31 | 32 | interface MyControl extends MocksControl { 33 | ToMock getMock(); 34 | } 35 | 36 | interface ToMock { 37 | List doSomething(T a, List b); 38 | T genericMethod(T a); 39 | List wildcard(List a); 40 | } 41 | 42 | private MyControl ctrl; 43 | private ToMock mock; 44 | private ArrayList integerList; 45 | private ArrayList stringList; 46 | 47 | @Override 48 | public void gwtSetUp() { 49 | this.ctrl = GWT.create(MyControl.class); 50 | this.mock = ctrl.getMock(); 51 | this.integerList = new ArrayList(); 52 | this.stringList = new ArrayList(); 53 | } 54 | 55 | public void testAllExpectationMet() { 56 | ctrl.expect(mock.doSomething(1, this.integerList)).andReturn(this.integerList); 57 | ctrl.expect(mock.genericMethod("hallo")).andReturn("hi"); 58 | ctrl.>expect(mock.wildcard(this.integerList)).andReturn(this.stringList); 59 | 60 | ctrl.replay(); 61 | 62 | assertSame(mock.doSomething(1, this.integerList), this.integerList); 63 | assertEquals(mock.genericMethod("hallo"), "hi"); 64 | assertSame(mock.wildcard(this.integerList), this.stringList); 65 | 66 | ctrl.verify(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/MockExtendedInterfacesGwtTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import com.google.gwt.core.client.GWT; 20 | import com.google.gwt.testing.easygwtmock.client.BaseGwtTestCase; 21 | 22 | /** 23 | * We test if we can mock an interface that extends another interface. 24 | * 25 | * @author Michael Goderbauer 26 | */ 27 | public class MockExtendedInterfacesGwtTest extends BaseGwtTestCase { 28 | 29 | interface BaseInterface { 30 | void baseDoSomething(); 31 | } 32 | 33 | interface ChildInterface extends BaseInterface { 34 | void childDoSomething(); 35 | } 36 | 37 | interface MyIMocksControl extends MocksControl { 38 | ChildInterface getMock(); 39 | } 40 | 41 | public void testExtendedInterface() { 42 | MyIMocksControl ctrl = GWT.create(MyIMocksControl.class); 43 | ChildInterface mock = ctrl.getMock(); 44 | 45 | mock.baseDoSomething(); 46 | mock.childDoSomething(); 47 | 48 | ctrl.replay(); 49 | 50 | mock.baseDoSomething(); 51 | mock.childDoSomething(); 52 | 53 | ctrl.verify(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/MockMethodsWithArgumentsGwtTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import com.google.gwt.core.client.GWT; 20 | import com.google.gwt.testing.easygwtmock.client.BaseGwtTestCase; 21 | 22 | /** 23 | * We test if the expected arguments to a method can be specified. 24 | * 25 | * @author Michael Goderbauer 26 | */ 27 | public class MockMethodsWithArgumentsGwtTest extends BaseGwtTestCase { 28 | 29 | private InterfaceToMock mock; 30 | private MyIMockControl ctrl; 31 | 32 | interface InterfaceToMock { 33 | void subtract(int a, int b); 34 | void add(int...sumands); 35 | void doSomething(String a, int b); 36 | } 37 | 38 | interface MyIMockControl extends MocksControl { 39 | InterfaceToMock getMock(); 40 | } 41 | 42 | @Override 43 | public void gwtSetUp() { 44 | this.ctrl = GWT.create(MyIMockControl.class); 45 | this.mock = ctrl.getMock(); 46 | } 47 | 48 | public void testAllExpectationMet() { 49 | mock.subtract(3, 4); 50 | mock.add(1, 2, 3, 4); 51 | mock.doSomething("Hallo", 10); 52 | 53 | ctrl.replay(); 54 | 55 | mock.subtract(3, 4); 56 | mock.add(1, 2, 3, 4); 57 | mock.doSomething("Hallo", 10); 58 | 59 | ctrl.verify(); 60 | } 61 | 62 | public void testOneExpectedMethodCallMissing() { 63 | mock.subtract(3, 4); 64 | mock.add(1, 2, 3, 4); 65 | mock.doSomething("Hallo", 10); 66 | 67 | ctrl.replay(); 68 | 69 | mock.subtract(3, 4); 70 | mock.add(1, 2, 3, 4); 71 | // we do not call mock.doSomething("Hallo", 10); 72 | 73 | boolean exceptionThrown = true; 74 | try { 75 | ctrl.verify(); 76 | exceptionThrown = false; 77 | } catch (AssertionError expected) { 78 | assertEquals("\n Expectation failure on verify. List of all expectations:" + 79 | "\n subtract(3, 4): expected 1, actual 1" + 80 | "\n add(1, 2, 3, 4): expected 1, actual 1" + 81 | "\n --> doSomething(Hallo, 10): expected 1, actual 0\n", expected.getMessage()); 82 | } 83 | assertTrue("should have thrown exception", exceptionThrown); 84 | } 85 | 86 | public void testUnexpectedMethodCall() { 87 | mock.subtract(3, 4); 88 | mock.add(1, 2, 3, 4); 89 | mock.doSomething("Hallo", 10); 90 | 91 | ctrl.replay(); 92 | 93 | mock.subtract(3, 4); 94 | mock.add(1, 2, 3, 4); 95 | mock.doSomething("Hallo", 10); 96 | 97 | boolean exceptionThrown = true; 98 | try { 99 | mock.add(1, 2, 3, 4); // unexpected! 100 | exceptionThrown = false; 101 | } catch (AssertionError expected) { 102 | assertEquals("\n Unexpected method call add(1, 2, 3, 4). List of all expectations:" + 103 | "\n Potential matches are marked with (+1)." + 104 | "\n subtract(3, 4): expected 1, actual 1" + 105 | "\n add(1, 2, 3, 4): expected 1, actual 1 (+1)" + 106 | "\n doSomething(Hallo, 10): expected 1, actual 1\n", expected.getMessage()); 107 | } 108 | assertTrue("should have thrown exception", exceptionThrown); 109 | 110 | ctrl.verify(); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/MockMethodsWithVarArgsGwtTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import com.google.gwt.core.client.GWT; 20 | import com.google.gwt.testing.easygwtmock.client.BaseGwtTestCase; 21 | 22 | /** 23 | * We test the generation of mock classes for interfaces with vararg methods. 24 | * 25 | * @author Michael Goderbauer 26 | */ 27 | public class MockMethodsWithVarArgsGwtTest extends BaseGwtTestCase { 28 | 29 | interface InterfaceToMock { 30 | void foo1(int i, Object...a); 31 | void foo2(float i, byte...a); 32 | void foo3(String i, short...a); 33 | void foo4(Object i, int...a); 34 | void foo5(boolean i, long...a); 35 | void foo6(char i, float...a); 36 | void foo7(long i, double...a); 37 | void foo8(boolean...a); 38 | void foo9(char...a); 39 | } 40 | 41 | interface MyIMockControl extends MocksControl { 42 | InterfaceToMock getMock(); 43 | } 44 | 45 | public void testVarArgs() { 46 | MyIMockControl ctrl = GWT.create(MyIMockControl.class); 47 | InterfaceToMock mock = ctrl.getMock(); 48 | 49 | mock.foo1(2, 3, 4); 50 | mock.foo2(2f, (byte) 4, (byte) 3); 51 | mock.foo3("Hi", (short) 3, (short) 4); 52 | mock.foo4(null, 3, 4); 53 | mock.foo5(true, 3L, 4L); 54 | mock.foo6('A', 3f, 4f); 55 | mock.foo7(12L, 3d, 4d); 56 | mock.foo8(true, false, true, true); 57 | mock.foo9('A', 'B', 'C'); 58 | 59 | ctrl.replay(); 60 | 61 | mock.foo1(2, 3, 4); 62 | mock.foo2(2f, (byte) 4, (byte) 3); 63 | mock.foo3("Hi", (short) 3, (short) 4); 64 | mock.foo4(null, 3, 4); 65 | mock.foo5(true, 3L, 4L); 66 | mock.foo6('A', 3f, 4f); 67 | mock.foo7(12L, 3d, 4d); 68 | mock.foo8(true, false, true, true); 69 | mock.foo9('A', 'B', 'C'); 70 | 71 | ctrl.verify(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/NiceMockGwtTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import com.google.gwt.core.client.GWT; 20 | import com.google.gwt.testing.easygwtmock.client.BaseGwtTestCase; 21 | 22 | /** 23 | * Tests the behavior of nice mocks 24 | * 25 | * @author Michael Goderbauer 26 | */ 27 | public class NiceMockGwtTest extends BaseGwtTestCase { 28 | 29 | interface ToMock { 30 | String returnString(); 31 | int returnInt(); 32 | boolean returnBoolean(); 33 | float returnFloat(); 34 | } 35 | 36 | interface MyControl extends MocksControl { 37 | ToMock getMock(); 38 | } 39 | 40 | public void testNiceMocks() { 41 | MyControl ctrl = GWT.create(MyControl.class); 42 | ToMock mock = ctrl.getMock(); 43 | ctrl.replay(); 44 | 45 | ctrl.setToNice(mock); 46 | 47 | assertNull(mock.returnString()); 48 | assertEquals(0, mock.returnInt()); 49 | assertEquals(false, mock.returnBoolean()); 50 | assertEquals(0f, mock.returnFloat()); 51 | 52 | ctrl.verify(); 53 | } 54 | 55 | public void testNiceMocks_withExpectationSet() { 56 | MyControl ctrl = GWT.create(MyControl.class); 57 | ToMock mock = ctrl.getMock(); 58 | 59 | ctrl.expect(mock.returnString()).andReturn("Hallo"); 60 | ctrl.replay(); 61 | 62 | ctrl.setToNice(mock); 63 | 64 | assertEquals("Hallo", mock.returnString()); 65 | assertNull(mock.returnString()); 66 | 67 | ctrl.verify(); 68 | } 69 | 70 | public void testNiceMocks_switching() { 71 | MyControl ctrl = GWT.create(MyControl.class); 72 | ToMock mock = ctrl.getMock(); 73 | 74 | ctrl.expect(mock.returnString()).andReturn("Hallo"); 75 | ctrl.replay(); 76 | 77 | ctrl.setToNice(mock); 78 | 79 | assertEquals("Hallo", mock.returnString()); 80 | assertNull(mock.returnString()); 81 | assertNull(mock.returnString()); 82 | 83 | ctrl.setToNotNice(mock); 84 | 85 | boolean exceptionThrown = true; 86 | try { 87 | mock.returnString(); 88 | exceptionThrown = false; 89 | } catch (AssertionError expected) { 90 | } 91 | assertTrue("should have thrown exception", exceptionThrown); 92 | 93 | ctrl.verify(); 94 | } 95 | 96 | interface MyControlWithNiceMock extends MocksControl { 97 | @Nice ToMock getNiceMock(); 98 | ToMock getNotNiceMock(); 99 | } 100 | 101 | public void testAnotationOnMethod() { 102 | MyControlWithNiceMock ctrl = GWT.create(MyControlWithNiceMock.class); 103 | ToMock niceMock = ctrl.getNiceMock(); 104 | ToMock notNiceMock = ctrl.getNotNiceMock(); 105 | ctrl.replay(); 106 | 107 | assertNull(niceMock.returnString()); 108 | assertEquals(0, niceMock.returnInt()); 109 | assertEquals(false, niceMock.returnBoolean()); 110 | assertEquals(0f, niceMock.returnFloat()); 111 | 112 | boolean exceptionThrown = true; 113 | try { 114 | notNiceMock.returnInt(); 115 | exceptionThrown = false; 116 | } catch (AssertionError expected) { 117 | } 118 | assertTrue("should have thrown exception", exceptionThrown); 119 | 120 | ctrl.verify(); 121 | } 122 | 123 | @Nice interface MyNiceControl extends MocksControl { 124 | ToMock getOneMock(); 125 | ToMock getAnotherMock(); 126 | } 127 | 128 | public void testAnotationOnInterface() { 129 | MyNiceControl ctrl = GWT.create(MyNiceControl.class); 130 | ToMock aMock = ctrl.getOneMock(); 131 | ToMock anotherMock = ctrl.getOneMock(); 132 | ctrl.replay(); 133 | 134 | assertNull(aMock.returnString()); 135 | assertEquals(0, aMock.returnInt()); 136 | assertEquals(false, aMock.returnBoolean()); 137 | assertEquals(0f, aMock.returnFloat()); 138 | 139 | assertNull(anotherMock.returnString()); 140 | assertEquals(0, anotherMock.returnInt()); 141 | assertEquals(false, anotherMock.returnBoolean()); 142 | assertEquals(0f, anotherMock.returnFloat()); 143 | 144 | ctrl.verify(); 145 | } 146 | 147 | @Nice interface MyMixedControl extends MocksControl { 148 | ToMock getNiceMockOne(); 149 | ToMock getNiceMockTwo(); 150 | @Nice(false) ToMock getNotNiceMock(); 151 | } 152 | 153 | public void testMixedAnotationOnInterfaceAndMethod() { 154 | MyMixedControl ctrl = GWT.create(MyMixedControl.class); 155 | ToMock niceMockOne = ctrl.getNiceMockOne(); 156 | ToMock niceMockTwo = ctrl.getNiceMockTwo(); 157 | ToMock notNiceMock = ctrl.getNotNiceMock(); 158 | 159 | ctrl.replay(); 160 | 161 | assertNull(niceMockOne.returnString()); 162 | assertEquals(0, niceMockOne.returnInt()); 163 | assertEquals(false, niceMockOne.returnBoolean()); 164 | assertEquals(0f, niceMockOne.returnFloat()); 165 | 166 | assertNull(niceMockTwo.returnString()); 167 | assertEquals(0, niceMockTwo.returnInt()); 168 | assertEquals(false, niceMockTwo.returnBoolean()); 169 | assertEquals(0f, niceMockTwo.returnFloat()); 170 | 171 | boolean exceptionThrown = true; 172 | try { 173 | notNiceMock.returnInt(); 174 | exceptionThrown = false; 175 | } catch (AssertionError expected) { 176 | } 177 | assertTrue("should have thrown exception", exceptionThrown); 178 | 179 | ctrl.verify(); 180 | } 181 | } 182 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/RangeGwtTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import com.google.gwt.core.client.GWT; 20 | import com.google.gwt.testing.easygwtmock.client.BaseGwtTestCase; 21 | 22 | /** 23 | * We test setting ranges on expected calls. 24 | * 25 | * @author Michael Goderbauer 26 | */ 27 | public class RangeGwtTest extends BaseGwtTestCase { 28 | 29 | interface MyControl extends MocksControl { 30 | ToMock getMock(); 31 | } 32 | 33 | interface ToMock { 34 | String returnString(); 35 | int returnInt(); 36 | void noReturnValue(); 37 | } 38 | 39 | private MyControl ctrl; 40 | private ToMock mock; 41 | 42 | @Override 43 | public void gwtSetUp() { 44 | this.ctrl = GWT.create(MyControl.class); 45 | this.mock = ctrl.getMock(); 46 | } 47 | 48 | public void testTimes_allMet() { 49 | ctrl.expect(mock.returnString()).andReturn("Hallo").times(2).andReturn("Hi").times(2, 3); 50 | 51 | ctrl.replay(); 52 | 53 | assertEquals("Hallo", mock.returnString()); 54 | assertEquals("Hallo", mock.returnString()); 55 | assertEquals("Hi", mock.returnString()); 56 | assertEquals("Hi", mock.returnString()); 57 | assertEquals("Hi", mock.returnString()); 58 | 59 | ctrl.verify(); 60 | } 61 | 62 | public void testTimes_unexpectedCall() { 63 | ctrl.expect(mock.returnString()).andReturn("Hallo").times(2).andReturn("Hi").times(2, 3); 64 | 65 | ctrl.replay(); 66 | 67 | assertEquals("Hallo", mock.returnString()); 68 | assertEquals("Hallo", mock.returnString()); 69 | assertEquals("Hi", mock.returnString()); 70 | assertEquals("Hi", mock.returnString()); 71 | assertEquals("Hi", mock.returnString()); 72 | 73 | boolean exceptionThrown = true; 74 | try { 75 | assertEquals("Hi", mock.returnString()); //unexpected 76 | exceptionThrown = false; 77 | } catch (AssertionError expected) { 78 | assertEquals("\n Unexpected method call returnString(). List of all expectations:" + 79 | "\n Potential matches are marked with (+1)." + 80 | "\n returnString(): expected 2, actual 2 (+1)" + 81 | "\n returnString(): expected between 2 and 3, actual 3 (+1)\n", 82 | expected.getMessage()); 83 | } 84 | assertTrue("should have thrown exception", exceptionThrown); 85 | 86 | ctrl.verify(); 87 | } 88 | 89 | public void testTimes_missingCall() { 90 | ctrl.expect(mock.returnString()).andReturn("Hallo").times(2).andReturn("Hi").times(2, 3); 91 | 92 | ctrl.replay(); 93 | 94 | assertEquals("Hallo", mock.returnString()); 95 | assertEquals("Hallo", mock.returnString()); 96 | assertEquals("Hi", mock.returnString()); 97 | 98 | boolean exceptionThrown = true; 99 | try { 100 | ctrl.verify(); 101 | exceptionThrown = false; 102 | } catch (AssertionError expected) { 103 | assertEquals("\n Expectation failure on verify. List of all expectations:" + 104 | "\n returnString(): expected 2, actual 2" + 105 | "\n --> returnString(): expected between 2 and 3, actual 1\n", expected.getMessage()); 106 | } 107 | assertTrue("should have thrown exception", exceptionThrown); 108 | } 109 | 110 | public void testAnyTimes() { 111 | ctrl.expect(mock.returnString()).andReturn("Hallo").andReturn("Hi").anyTimes(); 112 | 113 | ctrl.replay(); 114 | 115 | assertEquals("Hallo", mock.returnString()); 116 | assertEquals("Hi", mock.returnString()); 117 | assertEquals("Hi", mock.returnString()); 118 | assertEquals("Hi", mock.returnString()); 119 | 120 | ctrl.verify(); 121 | } 122 | 123 | public void testTimes_voidMethods_allMet1() { 124 | mock.noReturnValue(); 125 | ctrl.expectLastCall().times(2, 8); 126 | 127 | ctrl.replay(); 128 | 129 | mock.noReturnValue(); 130 | mock.noReturnValue(); 131 | mock.noReturnValue(); 132 | mock.noReturnValue(); 133 | 134 | ctrl.verify(); 135 | } 136 | 137 | public void testTimes_voidMethods_allMet2() { 138 | mock.noReturnValue(); 139 | ctrl.expectLastCall().times(3).times(2); 140 | 141 | ctrl.replay(); 142 | 143 | mock.noReturnValue(); 144 | mock.noReturnValue(); 145 | mock.noReturnValue(); 146 | mock.noReturnValue(); 147 | mock.noReturnValue(); 148 | 149 | ctrl.verify(); 150 | } 151 | 152 | public void testTimes_voidMethods_unexpectedCall() { 153 | mock.noReturnValue(); 154 | ctrl.expectLastCall().times(2, 4); 155 | 156 | ctrl.replay(); 157 | 158 | mock.noReturnValue(); 159 | mock.noReturnValue(); 160 | mock.noReturnValue(); 161 | mock.noReturnValue(); 162 | 163 | 164 | boolean exceptionThrown = true; 165 | try { 166 | mock.noReturnValue(); 167 | exceptionThrown = false; 168 | } catch (AssertionError expected) { 169 | assertEquals("\n Unexpected method call noReturnValue(). List of all expectations:" + 170 | "\n Potential matches are marked with (+1)." + 171 | "\n noReturnValue(): expected between 2 and 4, actual 4 (+1)\n", 172 | expected.getMessage()); 173 | } 174 | assertTrue("should have thrown exception", exceptionThrown); 175 | 176 | ctrl.verify(); 177 | } 178 | 179 | public void testTimes_voidMethods_callMissing() { 180 | mock.noReturnValue(); 181 | ctrl.expectLastCall().times(2); 182 | ctrl.expectLastCall().times(3); 183 | 184 | ctrl.replay(); 185 | 186 | mock.noReturnValue(); 187 | mock.noReturnValue(); 188 | mock.noReturnValue(); 189 | mock.noReturnValue(); 190 | 191 | 192 | boolean exceptionThrown = true; 193 | try { 194 | ctrl.verify(); 195 | exceptionThrown = false; 196 | } catch (AssertionError expected) { 197 | assertEquals("\n Expectation failure on verify. List of all expectations:" + 198 | "\n noReturnValue(): expected 2, actual 2" + 199 | "\n --> noReturnValue(): expected 3, actual 2\n", expected.getMessage()); 200 | } 201 | assertTrue("should have thrown exception", exceptionThrown); 202 | } 203 | 204 | public void testTimes_negativeMin() { 205 | try { 206 | ctrl.expect(mock.returnInt()).andReturn(43).times(-2); 207 | fail("should have thrown exception"); 208 | } catch (IllegalArgumentException expected) { 209 | } 210 | } 211 | 212 | public void testTimes_negativeMax() { 213 | try { 214 | ctrl.expect(mock.returnInt()).andReturn(43).times(3, 1); 215 | fail("should have thrown exception"); 216 | } catch (IllegalArgumentException expected) { 217 | } 218 | } 219 | 220 | public void testTimes_illegalRange() { 221 | try { 222 | ctrl.expect(mock.returnInt()).andReturn(43).times(-3, -1); 223 | fail("should have thrown exception"); 224 | } catch (IllegalArgumentException expected) { 225 | } 226 | } 227 | } 228 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/ReturnValuesGwtTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import com.google.gwt.core.client.GWT; 20 | import com.google.gwt.testing.easygwtmock.client.BaseGwtTestCase; 21 | 22 | /** 23 | * Tests if return values can be specified. 24 | * 25 | * @author Michael Goderbauer 26 | */ 27 | public class ReturnValuesGwtTest extends BaseGwtTestCase { 28 | 29 | interface MyControl extends MocksControl { 30 | ToMock getMock(); 31 | } 32 | 33 | interface ToMock { 34 | String returnString(); 35 | int returnInt(); 36 | void noReturnValue(); 37 | } 38 | 39 | private MyControl ctrl; 40 | private ToMock mock; 41 | 42 | @Override 43 | public void gwtSetUp() { 44 | this.ctrl = GWT.create(MyControl.class); 45 | this.mock = ctrl.getMock(); 46 | } 47 | 48 | public void testAllExpectationMet() { 49 | ctrl.expect(mock.returnString()).andReturn("Hallo"); 50 | ctrl.expect(mock.returnString()).andReturn("Hi"); 51 | ctrl.expect(mock.returnInt()).andReturn(42); 52 | mock.returnInt(); 53 | ctrl.expectLastCall().andReturn(44); 54 | mock.noReturnValue(); 55 | 56 | ctrl.replay(); 57 | 58 | assertEquals(mock.returnString(), "Hallo"); 59 | assertEquals(mock.returnString(), "Hi"); 60 | assertEquals(mock.returnInt(), 42); 61 | assertEquals(mock.returnInt(), 44); 62 | mock.noReturnValue(); 63 | 64 | ctrl.verify(); 65 | } 66 | 67 | public void testReturnValueNotSpecified() { 68 | mock.returnInt(); 69 | 70 | try { 71 | ctrl.replay(); 72 | fail(); 73 | } catch (IllegalStateException expected) { 74 | assertEquals(expected.getMessage(), 75 | "Missing behavior definition for preceding method call returnInt()"); 76 | } 77 | } 78 | 79 | public void testNoMethodCall() { 80 | try { 81 | ctrl.expectLastCall().andReturn(44); 82 | fail(); 83 | } catch (IllegalStateException expected) { 84 | assertEquals(expected.getMessage(), "Method call on mock needed before setting expectations"); 85 | } 86 | } 87 | 88 | public void testSpecifyReturnForVoidMethods() { 89 | mock.noReturnValue(); 90 | try { 91 | ctrl.expectLastCall().andReturn(44); 92 | fail(); 93 | } catch (IllegalStateException expected) { 94 | assertEquals(expected.getMessage(), "Cannot add return value to void method"); 95 | } 96 | } 97 | 98 | public void testSetNullAsReturnValueForPrimitiveMethod() { 99 | try { 100 | ctrl.expect(mock.returnInt()).andReturn(null); 101 | fail(); 102 | } catch (IllegalStateException expected) { 103 | assertEquals(expected.getMessage(), "Cannot add 'null' as return value to premitive method"); 104 | } 105 | } 106 | 107 | public void testChaining() { 108 | ctrl.expect(mock.returnInt()).andReturn(11).andReturn(12).andReturn(13); 109 | 110 | ctrl.replay(); 111 | 112 | assertEquals(11, mock.returnInt()); 113 | assertEquals(12, mock.returnInt()); 114 | assertEquals(13, mock.returnInt()); 115 | 116 | try { 117 | mock.returnInt(); 118 | fail(); 119 | } catch (AssertionError expected) { 120 | } 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/SimpleExpectationsGwtTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import com.google.gwt.core.client.GWT; 20 | import com.google.gwt.testing.easygwtmock.client.BaseGwtTestCase; 21 | 22 | /** 23 | * We only test if we can record simple expectations (e. g. method x gets invoked") 24 | * and if those mocked methods return their specified default value. 25 | * 26 | * @author Michael Goderbauer 27 | */ 28 | public class SimpleExpectationsGwtTest extends BaseGwtTestCase { 29 | 30 | private InterfaceToMock mock; 31 | private MyIMockControl ctrl; 32 | 33 | interface MyIMockControl extends MocksControl { 34 | InterfaceToMock getMock(); 35 | } 36 | 37 | interface InterfaceToMock { 38 | String returnString(); 39 | int returnInt(); 40 | Integer returnInteger(); 41 | boolean returnBoolean(); 42 | void noReturnValue(); 43 | } 44 | 45 | @Override 46 | public void gwtSetUp() { 47 | this.ctrl = GWT.create(MyIMockControl.class); 48 | this.mock = ctrl.getMock(); 49 | } 50 | 51 | public void testAllExpectationMet() { 52 | ctrl.expect(mock.returnString()).andReturn("Hi"); 53 | ctrl.expect(mock.returnInt()).andReturn(42); 54 | ctrl.expect(mock.returnInteger()).andReturn(3); 55 | ctrl.expect(mock.returnBoolean()).andReturn(true); 56 | mock.noReturnValue(); 57 | 58 | ctrl.replay(); 59 | 60 | assertEquals(mock.returnString(), "Hi"); 61 | assertEquals(mock.returnInt(), 42); 62 | assertEquals(mock.returnInteger(), new Integer(3)); 63 | assertTrue(mock.returnBoolean()); 64 | mock.noReturnValue(); 65 | 66 | ctrl.verify(); 67 | } 68 | 69 | public void testOneExpectedMethodCallMissing() { 70 | mock.noReturnValue(); 71 | ctrl.expect(mock.returnInt()).andReturn(10); 72 | 73 | ctrl.replay(); 74 | 75 | mock.noReturnValue(); 76 | // We do not call mock.returnInt(); 77 | 78 | boolean exceptionThrown = true; 79 | try { 80 | ctrl.verify(); 81 | exceptionThrown = false; 82 | } catch (AssertionError expected) { 83 | assertEquals("\n Expectation failure on verify. List of all expectations:" + 84 | "\n noReturnValue(): expected 1, actual 1" + 85 | "\n --> returnInt(): expected 1, actual 0\n", expected.getMessage()); 86 | } 87 | assertTrue("should have thrown exception", exceptionThrown); 88 | } 89 | 90 | public void testUnexpectedMethodCall() { 91 | mock.noReturnValue(); 92 | 93 | ctrl.replay(); 94 | 95 | mock.noReturnValue(); 96 | 97 | boolean exceptionThrown = true; 98 | try { 99 | mock.noReturnValue(); // We only expected it once! 100 | exceptionThrown = false; 101 | } catch (AssertionError expected) { 102 | assertEquals("\n Unexpected method call noReturnValue(). List of all expectations:" + 103 | "\n Potential matches are marked with (+1)." + 104 | "\n noReturnValue(): expected 1, actual 1 (+1)\n", expected.getMessage()); 105 | } 106 | assertTrue("should have thrown exception", exceptionThrown); 107 | 108 | ctrl.verify(); 109 | } 110 | } 111 | 112 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/StacktraceGwtTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import com.google.gwt.core.client.GWT; 20 | import com.google.gwt.testing.easygwtmock.client.BaseGwtTestCase; 21 | 22 | /** 23 | * Tests, if the stacktrace is cut properly to hide EasyGwtMock internals. 24 | * In other words: The first displayed stackframe should include a method 25 | * of the EasyGwtMock API. 26 | * 27 | * @author Michael Goderbauer 28 | * 29 | */ 30 | public class StacktraceGwtTest extends BaseGwtTestCase { 31 | 32 | interface MyControl extends MocksControl { 33 | ComplexType getMock(); 34 | } 35 | 36 | interface ComplexType { 37 | int foo(); 38 | } 39 | 40 | private MyControl ctrl; 41 | private ComplexType mock; 42 | 43 | @Override 44 | public void gwtSetUp() { 45 | this.ctrl = GWT.create(MyControl.class); 46 | this.mock = ctrl.getMock(); 47 | } 48 | 49 | public void testUnexpectedMethod() { 50 | if (GWT.isScript()) { 51 | return; // no real stacktrace in javaScript! 52 | } 53 | ctrl.replay(); 54 | 55 | try { 56 | mock.foo(); 57 | } catch (AssertionError expected) { 58 | StackTraceElement stackFrame = expected.getStackTrace()[0]; 59 | assertEquals( 60 | "com.google.gwt.testing.easygwtmock.client.StacktraceGwtTest_ComplexTypeMock.foo", 61 | stackFrame.getClassName() + "." + stackFrame.getMethodName()); 62 | } 63 | } 64 | 65 | public void testVerify() { 66 | if (GWT.isScript()) { 67 | return; // no real stacktrace in javaScript! 68 | } 69 | ctrl.expect(mock.foo()).andReturn(32); 70 | 71 | ctrl.replay(); 72 | 73 | try { 74 | ctrl.verify(); 75 | } catch (AssertionError expected) { 76 | StackTraceElement stackFrame = expected.getStackTrace()[0]; 77 | assertEquals( 78 | "com.google.gwt.testing.easygwtmock.client.internal.MocksControlBase.verify", 79 | stackFrame.getClassName() + "." + stackFrame.getMethodName()); 80 | } 81 | } 82 | 83 | public void testReplay() { 84 | if (GWT.isScript()) { 85 | return; // no real stacktrace in javaScript! 86 | } 87 | ctrl.expect(mock.foo()).andReturn(32); 88 | 89 | ctrl.replay(); 90 | 91 | try { 92 | ctrl.replay(); 93 | } catch (IllegalStateException expected) { 94 | StackTraceElement stackFrame = expected.getStackTrace()[0]; 95 | assertEquals( 96 | "com.google.gwt.testing.easygwtmock.client.internal.MocksControlBase.replay", 97 | stackFrame.getClassName() + "." + stackFrame.getMethodName()); 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/ThrowingExceptionsGwtTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import com.google.gwt.core.client.GWT; 20 | import com.google.gwt.testing.easygwtmock.client.BaseGwtTestCase; 21 | 22 | /** 23 | * Tests .andThrows() function of ExpectationSetters 24 | * 25 | * @author Michael Goderbauer 26 | */ 27 | public class ThrowingExceptionsGwtTest extends BaseGwtTestCase { 28 | 29 | interface MyControl extends MocksControl { 30 | ToMock getMock(); 31 | } 32 | 33 | interface ToMock { 34 | int throw1() throws MyException, MyExtendedException; 35 | int throw2() throws MyExtendedException, MyException; 36 | int throw3() throws Throwable, RuntimeException, MyExtendedException; 37 | int throw4() throws MyException, MyException; 38 | int throw5() throws MyException; 39 | } 40 | 41 | private MyControl ctrl; 42 | private ToMock mock; 43 | 44 | @Override 45 | public void gwtSetUp() { 46 | this.ctrl = GWT.create(MyControl.class); 47 | this.mock = ctrl.getMock(); 48 | } 49 | 50 | public void testHiddenExceptions() throws MyExtendedException, MyException { 51 | ctrl.expect(mock.throw1()).andThrow(new MyException()); 52 | ctrl.expect(mock.throw1()).andThrow(new MyExtendedException()); 53 | ctrl.expect(mock.throw2()).andThrow(new MyException()); 54 | ctrl.expect(mock.throw2()).andThrow(new MyExtendedException()); 55 | 56 | ctrl.replay(); 57 | 58 | try { 59 | mock.throw1(); 60 | fail("should have thrown exception"); 61 | } catch (MyException expected) { 62 | } 63 | try { 64 | mock.throw1(); 65 | fail("should have thrown exception"); 66 | } catch (MyExtendedException expected) { 67 | } 68 | try { 69 | mock.throw2(); 70 | fail("should have thrown exception"); 71 | } catch (MyException expected) { 72 | } 73 | try { 74 | mock.throw2(); 75 | fail("should have thrown exception"); 76 | } catch (MyExtendedException expected) { 77 | } 78 | 79 | ctrl.verify(); 80 | } 81 | 82 | public void testDeclaredUncheckedExceptions() throws RuntimeException, Throwable { 83 | ctrl.expect(mock.throw3()).andThrow(new MyException()); 84 | ctrl.expect(mock.throw3()).andThrow(new MyExtendedException()); 85 | ctrl.expect(mock.throw3()).andThrow(new ArithmeticException()); 86 | ctrl.expect(mock.throw3()).andThrow(new RuntimeException()); 87 | 88 | ctrl.replay(); 89 | 90 | try { 91 | mock.throw3(); 92 | fail("should have thrown exception"); 93 | } catch (MyException expected) { 94 | } 95 | try { 96 | mock.throw3(); 97 | fail("should have thrown exception"); 98 | } catch (MyExtendedException expected) { 99 | } 100 | try { 101 | mock.throw3(); 102 | fail("should have thrown exception"); 103 | } catch (ArithmeticException expected) { 104 | } 105 | try { 106 | mock.throw3(); 107 | fail("should have thrown exception"); 108 | } catch (RuntimeException expected) { 109 | } 110 | 111 | ctrl.verify(); 112 | } 113 | 114 | public void testSameExceptionDeclaredTwice() throws MyException { 115 | ctrl.expect(mock.throw4()).andThrow(new MyException()); 116 | ctrl.expect(mock.throw4()).andThrow(new MyExtendedException()); 117 | 118 | ctrl.replay(); 119 | 120 | try { 121 | mock.throw4(); 122 | fail("should have thrown exception"); 123 | } catch (MyException expected) { 124 | } 125 | try { 126 | mock.throw4(); 127 | fail("should have thrown exception"); 128 | } catch (MyExtendedException expected) { 129 | } 130 | 131 | ctrl.verify(); 132 | } 133 | 134 | public void testExceptionHierarchy() throws MyException { 135 | ctrl.expect(mock.throw5()).andThrow(new MyExtendedException()); 136 | 137 | ctrl.replay(); 138 | 139 | try { 140 | mock.throw5(); 141 | fail("should have thrown exception"); 142 | } catch (MyException expected) { 143 | } 144 | 145 | ctrl.verify(); 146 | } 147 | 148 | class MyException extends Exception { 149 | } 150 | 151 | class MyExtendedException extends MyException { 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/UnmockableMethodsGwtTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client; 18 | 19 | import com.google.gwt.core.client.GWT; 20 | import com.google.gwt.testing.easygwtmock.client.BaseGwtTestCase; 21 | 22 | /** 23 | * Test behavior of unmockable toString(), equals(), hashCode() 24 | * 25 | * @author Michael Goderbauer 26 | */ 27 | public class UnmockableMethodsGwtTest extends BaseGwtTestCase { 28 | 29 | interface MyControl extends MocksControl { 30 | ToMock getMock(); 31 | } 32 | 33 | interface ToMock { 34 | int aMethod(); 35 | } 36 | 37 | private MyControl ctrl; 38 | private ToMock mock; 39 | 40 | @Override 41 | public void gwtSetUp() { 42 | this.ctrl = GWT.create(MyControl.class); 43 | this.mock = ctrl.getMock(); 44 | } 45 | 46 | public void testMockingUnmockableMethod_toString() { 47 | try { 48 | ctrl.expect(mock.toString()).andReturn("Hallo"); 49 | fail("should have thrown exception"); 50 | } catch (IllegalStateException expected) { 51 | assertEquals("Method toString() cannot be mocked", expected.getMessage()); 52 | } 53 | } 54 | 55 | public void testMockingUnmockableMethod_equals() { 56 | try { 57 | ctrl.expect(mock.equals(ctrl.anyInt())).andReturn(true); 58 | fail("should have thrown exception"); 59 | } catch (IllegalStateException expected) { 60 | assertEquals("Method equals() cannot be mocked", expected.getMessage()); 61 | } 62 | } 63 | 64 | public void testMockingUnmockableMethod_hashCode() { 65 | try { 66 | ctrl.expect(mock.hashCode()).andReturn(42); 67 | fail("should have thrown exception"); 68 | } catch (IllegalStateException expected) { 69 | assertEquals("Method hashCode() cannot be mocked", expected.getMessage()); 70 | } 71 | } 72 | 73 | public void testImplementationOfUnmockableMethods_equals() { 74 | ToMock secondMock = ctrl.getMock(); 75 | 76 | assertTrue("should be equal", mock.equals(mock)); 77 | assertFalse("should not be equal", mock.equals(42)); 78 | assertFalse("should be equal", mock.equals(secondMock)); 79 | 80 | ctrl.reset(); 81 | ctrl.replay(); 82 | 83 | assertTrue("should be equal", mock.equals(mock)); 84 | assertFalse("should not be equal", mock.equals(42)); 85 | assertFalse("should be equal", mock.equals(secondMock)); 86 | 87 | ctrl.verify(); 88 | } 89 | 90 | public void testImplementationOfUnmockableMethods_hashCode() { 91 | int hashCode = mock.hashCode(); 92 | 93 | ctrl.reset(); 94 | ctrl.replay(); 95 | 96 | assertEquals(hashCode, mock.hashCode()); 97 | 98 | ctrl.verify(); 99 | } 100 | 101 | public void testImplementationOfUnmockableMethods_toString() { 102 | assertEquals("Mock for UnmockableMethodsGwtTest.ToMock", mock.toString()); 103 | 104 | ctrl.reset(); 105 | ctrl.replay(); 106 | 107 | assertEquals("Mock for UnmockableMethodsGwtTest.ToMock", mock.toString()); 108 | 109 | ctrl.verify(); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/dummyclasses/ClassToMock.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.dummyclasses; 18 | 19 | /** 20 | * Dummy class for testing purposes 21 | * 22 | * @author Michael Goderbauer 23 | */ 24 | public class ClassToMock { 25 | 26 | public String returnString() { 27 | return "Hi"; 28 | } 29 | 30 | public int returnInt() { 31 | return 0; 32 | } 33 | 34 | public void noReturnValue() { 35 | } 36 | 37 | public final String finalMethod() { 38 | return "I am final"; 39 | } 40 | 41 | @Override 42 | public final boolean equals(Object obj) { 43 | return true; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/dummyclasses/OneArgClassToMock.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | 3 | package com.google.gwt.testing.easygwtmock.client.dummyclasses; 4 | 5 | /** 6 | * Dummy class for testing. 7 | * 8 | * @author skybrian@google.com (Brian Slesinsky) 9 | */ 10 | public class OneArgClassToMock { 11 | public final String arg; 12 | 13 | public OneArgClassToMock(String arg) { 14 | this.arg = arg; 15 | } 16 | 17 | // cannot be mocked 18 | public final String makeMessage() { 19 | return getGreeting() + " " + arg + "!"; 20 | } 21 | 22 | // can be mocked 23 | public String getGreeting() { 24 | return "Hello,"; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/internal/AnswerFactoryJavaTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | import com.google.gwt.testing.easygwtmock.client.Answer; 20 | import com.google.gwt.user.client.rpc.AsyncCallback; 21 | 22 | import junit.framework.TestCase; 23 | import org.easymock.EasyMock; 24 | 25 | /** 26 | * Tests {@link AnswerFactory} 27 | * 28 | * @author Michael Goderbauer 29 | */ 30 | public class AnswerFactoryJavaTest extends TestCase { 31 | 32 | public void testForValue_null() throws Throwable { 33 | Answer answer = AnswerFactory.forValue(null); 34 | assertNull(answer.answer(null)); 35 | } 36 | 37 | public void testForValue_primitive() throws Throwable { 38 | Answer answer = AnswerFactory.forValue(42); 39 | assertEquals(42, answer.answer(null)); 40 | } 41 | 42 | public void testForValue_object() throws Throwable { 43 | Object obj = new Object(); 44 | Answer answer = AnswerFactory.forValue(obj); 45 | assertSame(obj, answer.answer(null)); 46 | } 47 | 48 | public void testForThrowable() throws Throwable { 49 | NullPointerException exception = new NullPointerException(); 50 | Answer answer = AnswerFactory.forThrowable(exception); 51 | 52 | try { 53 | answer.answer(null); 54 | fail("should have thrown exception"); 55 | } catch (NullPointerException expected) { 56 | assertSame(exception, expected); 57 | } 58 | } 59 | 60 | public void testForOnSuccess() throws Throwable { 61 | @SuppressWarnings("unchecked") 62 | AsyncCallback callback = EasyMock.createMock(AsyncCallback.class); 63 | callback.onSuccess(42); 64 | EasyMock.replay(callback); 65 | 66 | Answer answer = AnswerFactory.forOnSuccess(42); 67 | Object[] args = { 14, "Hallo", callback }; 68 | 69 | answer.answer(args); 70 | 71 | EasyMock.verify(callback); 72 | } 73 | 74 | public void testForOnSuccess_noCallbackProvided() throws Throwable { 75 | Answer answer = AnswerFactory.forOnSuccess(42); 76 | Object[] args = { 14, "Hallo" }; 77 | try { 78 | answer.answer(args); 79 | fail("should have thrown exception"); 80 | } catch (IllegalArgumentException expected) { 81 | assertEquals( 82 | "No com.google.gwt.user.client.rpc.AsyncCallback object as last argument provided", 83 | expected.getMessage()); 84 | } 85 | } 86 | 87 | public void testForOnFailure() throws Throwable { 88 | Throwable throwable = new Exception(); 89 | @SuppressWarnings("unchecked") 90 | AsyncCallback callback = EasyMock.createMock(AsyncCallback.class); 91 | callback.onFailure(throwable); 92 | EasyMock.replay(callback); 93 | 94 | Answer answer = AnswerFactory.forOnFailure(throwable); 95 | Object[] args = { 14, "Hallo", callback }; 96 | 97 | answer.answer(args); 98 | 99 | EasyMock.verify(callback); 100 | } 101 | 102 | public void testForOnFailure_noCallbackProvided() throws Throwable { 103 | Answer answer = AnswerFactory.forOnFailure(new Exception()); 104 | Object[] args = { 14, "Hallo" }; 105 | try { 106 | answer.answer(args); 107 | fail("should have thrown exception"); 108 | } catch (IllegalArgumentException expected) { 109 | assertEquals( 110 | "No com.google.gwt.user.client.rpc.AsyncCallback object as last argument provided", 111 | expected.getMessage()); 112 | } 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/internal/CallJavaTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | import junit.framework.TestCase; 20 | 21 | import java.util.List; 22 | 23 | /** 24 | * Tests the Call class 25 | * 26 | * @author Michael Goderbauer 27 | */ 28 | public class CallJavaTest extends TestCase { 29 | 30 | private Object mock; 31 | private Method method; 32 | 33 | @Override 34 | public void setUp(){ 35 | this.mock = new Object(); 36 | Class[] empty = {}; 37 | this.method = new Method("foo", int.class, empty, empty); 38 | } 39 | 40 | public void testAddVarArgument() { 41 | Call call = new Call(mock, method, 1, 2, "hallo"); 42 | 43 | Object[] varArgs = { "arg1", "arg2", "arg3" }; 44 | call.addVarArgument(varArgs); 45 | 46 | List args = call.getArguments(); 47 | assertEquals(args.get(0), 1); 48 | assertEquals(args.get(1), 2); 49 | assertEquals(args.get(2), "hallo"); 50 | assertEquals(args.get(3), "arg1"); 51 | assertEquals(args.get(4), "arg2"); 52 | assertEquals(args.get(5), "arg3"); 53 | } 54 | 55 | public void testToString() { 56 | Call call = new Call(mock, method, 1, 2, "hallo"); 57 | assertEquals("foo(1, 2, hallo)", call.toString()); 58 | 59 | call = new Call(mock, method); 60 | assertEquals("foo()", call.toString()); 61 | 62 | call = new Call(mock, method, 1, "hallo"); 63 | 64 | int[] varArgs = { 42, 43, 44 }; 65 | call.addVarArgument(varArgs); 66 | 67 | assertEquals("foo(1, hallo, 42, 43, 44)", call.toString()); 68 | } 69 | 70 | public void testGetDefaultReturnValue() { 71 | Call call = new Call(mock, method, 1, 2, "hallo"); 72 | assertEquals(method.getDefaultReturnValue(), call.getDefaultReturnValue()); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/internal/ExceptionJavaTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | import com.google.gwt.testing.easygwtmock.client.UndeclaredThrowableException; 20 | 21 | import junit.framework.TestCase; 22 | 23 | /** 24 | * Tests AssertionErrorWrapper, IllegalStateExceptionWrapper, UndeclaredThrowableException 25 | * 26 | * @author Michael Goderbauer 27 | */ 28 | public class ExceptionJavaTest extends TestCase { 29 | 30 | public void testAssertionErrorWraper() { 31 | AssertionError wrapped = new AssertionError(); 32 | AssertionErrorWrapper exeption = new AssertionErrorWrapper(wrapped); 33 | 34 | assertSame(exeption.getAssertionError(), wrapped); 35 | } 36 | 37 | public void testIllegalStateExceptionWrapper() { 38 | IllegalStateException wrapped = new IllegalStateException(); 39 | IllegalStateExceptionWrapper exeption = new IllegalStateExceptionWrapper(wrapped); 40 | 41 | assertSame(exeption.getIllegalStateException(), wrapped); 42 | } 43 | 44 | public void testUndeclaredThrowableException() { 45 | Throwable wrapped = new IllegalStateException(); 46 | UndeclaredThrowableException exeption = new UndeclaredThrowableException(wrapped); 47 | 48 | assertSame(exeption.getUndeclaredThrowable(), wrapped); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/internal/ExpectedCallJavaTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | import com.google.gwt.testing.easygwtmock.client.Answer; 20 | import com.google.gwt.testing.easygwtmock.client.ArgumentMatcher; 21 | import com.google.gwt.testing.easygwtmock.client.Capture; 22 | import com.google.gwt.testing.easygwtmock.client.internal.matchers.ArgumentCapture; 23 | import com.google.gwt.testing.easygwtmock.client.internal.matchers.Equals; 24 | 25 | import junit.framework.TestCase; 26 | 27 | import java.util.ArrayList; 28 | import java.util.HashSet; 29 | import java.util.List; 30 | import java.util.Set; 31 | 32 | /** 33 | * Test the ExpectedCall class 34 | * 35 | * @author Michael Goderbauer 36 | */ 37 | public class ExpectedCallJavaTest extends TestCase { 38 | 39 | private Object mock; 40 | private Method method; 41 | 42 | @Override 43 | public void setUp(){ 44 | this.mock = new Object(); 45 | Class[] argumentTypes = { String.class, int.class }; 46 | Class[] declaredThrowables = {}; 47 | this.method = new Method("foo", int.class, argumentTypes, declaredThrowables); 48 | } 49 | 50 | public void testMatches_equal() { 51 | Call call1 = new Call(this.mock, this.method, 1, "Hallo", 3); 52 | ExpectedCall exp = new ExpectedCall(call1, createMatchersFor(call1), null, null, Range.DEFAULT); 53 | Call call2 = new Call(this.mock, this.method, 1, "Hallo", new Integer(3)); 54 | 55 | assertTrue("should match", exp.matches(call2)); 56 | assertTrue("should match", exp.matches(call1)); 57 | } 58 | 59 | public void testMatches_equalWithNull() { 60 | Call call1 = new Call(this.mock, this.method, 1, null, 3); 61 | ExpectedCall exp = new ExpectedCall(call1, createMatchersFor(call1), null, null, Range.DEFAULT); 62 | Call call2 = new Call(this.mock, this.method, 1, null, new Integer(3)); 63 | 64 | assertTrue("should match", exp.matches(call2)); 65 | assertTrue("should match", exp.matches(call1)); 66 | } 67 | 68 | public void testMatches_NotEqualSameArgumentCount() { 69 | Call call1 = new Call(this.mock, this.method, 1, "Hallo", 3); 70 | ExpectedCall exp = new ExpectedCall(call1, createMatchersFor(call1), null, null, Range.DEFAULT); 71 | Call call2 = new Call(this.mock, this.method, 1, "Hi", new Integer(3)); 72 | 73 | assertFalse("should not match", exp.matches(call2)); 74 | } 75 | 76 | public void testMatches_NotEqualNotSameArgumentCount() { 77 | Call call1 = new Call(this.mock, this.method, 1, "Hallo", 3); 78 | ExpectedCall exp = new ExpectedCall(call1, createMatchersFor(call1), null, null, Range.DEFAULT); 79 | Call call2 = new Call(this.mock, this.method, 1, "Hi"); 80 | 81 | assertFalse("should not match", exp.matches(call2)); 82 | } 83 | 84 | public void testMatches_NotEqualWithNull1() { 85 | Call call1 = new Call(this.mock, this.method, 1, null, 3); 86 | ExpectedCall exp = new ExpectedCall(call1, createMatchersFor(call1), null, null, Range.DEFAULT); 87 | Call call2 = new Call(this.mock, this.method, 1, "Hi", new Integer(3)); 88 | 89 | assertFalse("should not match", exp.matches(call2)); 90 | } 91 | 92 | public void testMatches_NotEqualWithNull2() { 93 | Call call1 = new Call(this.mock, this.method, 1, "Hallo", 3); 94 | ExpectedCall exp = new ExpectedCall(call1, createMatchersFor(call1), null, null, Range.DEFAULT); 95 | Call call2 = new Call(this.mock, this.method, 1, null, new Integer(3)); 96 | 97 | assertFalse("should not match", exp.matches(call2)); 98 | } 99 | 100 | public void testToString_threeArgs() { 101 | Call call1 = new Call(this.mock, this.method, 1, "Hallo", 3); 102 | ExpectedCall exp = new ExpectedCall(call1, createMatchersFor(call1), null, null, Range.DEFAULT); 103 | 104 | assertEquals("foo(1, Hallo, 3)", exp.toString()); 105 | } 106 | 107 | public void testToString_noArgs() { 108 | Call call1 = new Call(this.mock, this.method); 109 | ExpectedCall exp = new ExpectedCall(call1, createMatchersFor(call1), null, null, Range.DEFAULT); 110 | 111 | assertEquals("foo()", exp.toString()); 112 | } 113 | 114 | public void testInvoke() { 115 | Call call1 = new Call(this.mock, this.method); 116 | Answer answer = AnswerFactory.forValue(42); 117 | 118 | ExpectedCall exp = new ExpectedCall(call1, createMatchersFor(call1), 119 | null, answer, Range.DEFAULT); 120 | 121 | assertFalse("expectations should not be met", exp.expectationMet()); 122 | assertTrue("should be invokable", exp.canBeInvoked()); 123 | assertEquals(0, exp.getCallCount()); 124 | 125 | assertSame(answer, exp.invoke()); 126 | 127 | assertTrue("expectations should be met", exp.expectationMet()); 128 | assertFalse("should not be invokable", exp.canBeInvoked()); 129 | assertEquals(1, exp.getCallCount()); 130 | } 131 | 132 | public void testInvoke_withCapture() { 133 | Call call1 = new Call(this.mock, this.method); 134 | 135 | Capture capture = new Capture(); 136 | Set argumentCaptures = new HashSet(); 137 | argumentCaptures.add(new ArgumentCapture(capture)); 138 | 139 | ExpectedCall exp = new ExpectedCall(call1, createMatchersFor(call1), 140 | argumentCaptures, null, Range.DEFAULT); 141 | 142 | assertFalse("should not have captured", capture.hasCaptured()); 143 | 144 | exp.invoke(); 145 | 146 | assertTrue("should have captured", capture.hasCaptured()); 147 | } 148 | 149 | static List createMatchersFor(Call call) { 150 | List argMatcher = new ArrayList(); 151 | for (Object arg : call.getArguments()) { 152 | argMatcher.add(new Equals(arg)); 153 | } 154 | return argMatcher; 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/internal/MethodJavaTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | import junit.framework.TestCase; 20 | 21 | import java.util.HashMap; 22 | import java.util.Map; 23 | 24 | /** 25 | * Tests the method class 26 | * 27 | * @author Michael Goderbauer 28 | */ 29 | public class MethodJavaTest extends TestCase { 30 | 31 | private Map, Method> methods; 32 | 33 | @Override 34 | public void setUp() { 35 | methods = new HashMap, Method>(); 36 | methods.put(byte.class, new Method("foo", byte.class, null, null)); 37 | methods.put(short.class, new Method("foo", short.class, null, null)); 38 | methods.put(int.class, new Method("foo", int.class, null, null)); 39 | methods.put(long.class, new Method("foo", long.class, null, null)); 40 | methods.put(float.class, new Method("foo", float.class, null, null)); 41 | methods.put(double.class, new Method("foo", double.class, null, null)); 42 | methods.put(boolean.class, new Method("foo", boolean.class, null, null)); 43 | methods.put(char.class, new Method("foo", char.class, null, null)); 44 | methods.put(Object.class, new Method("foo", Object.class, null, null)); 45 | methods.put(String.class, new Method("foo", String.class, null, null)); 46 | methods.put(void.class, new Method("foo", void.class, null, null)); 47 | } 48 | 49 | public void testDefaultReturnValue() { 50 | assertEquals(methods.get(byte.class).getDefaultReturnValue(), (byte) 0); 51 | assertEquals(methods.get(short.class).getDefaultReturnValue(), (short) 0); 52 | assertEquals(methods.get(int.class).getDefaultReturnValue(), 0); 53 | assertEquals(methods.get(long.class).getDefaultReturnValue(), 0L); 54 | assertEquals(methods.get(float.class).getDefaultReturnValue(), 0f); 55 | assertEquals(methods.get(double.class).getDefaultReturnValue(), 0d); 56 | assertEquals(methods.get(boolean.class).getDefaultReturnValue(), false); 57 | assertEquals(methods.get(char.class).getDefaultReturnValue(), (char) 0); 58 | assertNull("should be null", methods.get(Object.class).getDefaultReturnValue()); 59 | assertNull("should be null", methods.get(String.class).getDefaultReturnValue()); 60 | } 61 | 62 | public void testIsPrimitive() { 63 | assertTrue("should be primitive", methods.get(byte.class).isReturnValuePrimitive()); 64 | assertTrue("should be primitive", methods.get(short.class).isReturnValuePrimitive()); 65 | assertTrue("should be primitive", methods.get(int.class).isReturnValuePrimitive()); 66 | assertTrue("should be primitive", methods.get(long.class).isReturnValuePrimitive()); 67 | assertTrue("should be primitive", methods.get(float.class).isReturnValuePrimitive()); 68 | assertTrue("should be primitive", methods.get(double.class).isReturnValuePrimitive()); 69 | assertTrue("should be primitive", methods.get(boolean.class).isReturnValuePrimitive()); 70 | assertTrue("should be primitive", methods.get(char.class).isReturnValuePrimitive()); 71 | assertFalse("should not be primitive", methods.get(Object.class).isReturnValuePrimitive()); 72 | assertFalse("should not be primitive", methods.get(String.class).isReturnValuePrimitive()); 73 | } 74 | 75 | public void testIsVoid() { 76 | assertFalse("should not be void", methods.get(byte.class).isReturnValueVoid()); 77 | assertFalse("should not be void", methods.get(short.class).isReturnValueVoid()); 78 | assertFalse("should not be void", methods.get(int.class).isReturnValueVoid()); 79 | assertFalse("should not be void", methods.get(long.class).isReturnValueVoid()); 80 | assertFalse("should not be void", methods.get(float.class).isReturnValueVoid()); 81 | assertFalse("should not be void", methods.get(double.class).isReturnValueVoid()); 82 | assertFalse("should not be void", methods.get(boolean.class).isReturnValueVoid()); 83 | assertFalse("should not be void", methods.get(char.class).isReturnValueVoid()); 84 | assertFalse("should not be void", methods.get(Object.class).isReturnValueVoid()); 85 | assertFalse("should not be void", methods.get(String.class).isReturnValueVoid()); 86 | assertTrue("should be void", methods.get(void.class).isReturnValueVoid()); 87 | } 88 | 89 | public void testCanThrow_true() { 90 | Class[] throwable = { MyException.class }; 91 | Method method = new Method("foo", int.class, null, throwable); 92 | 93 | assertTrue("should be throwable", method.canThrow(new MyException())); 94 | assertTrue("should be throwable", method.canThrow(new MyExtendedException())); 95 | assertTrue("should be throwable", method.canThrow(new RuntimeException())); 96 | assertTrue("should be throwable", method.canThrow(new Error())); 97 | } 98 | 99 | public void testCanThrow_false() { 100 | Class[] throwable = { MyExtendedException.class }; 101 | Method method = new Method("foo", int.class, null, throwable); 102 | 103 | assertFalse("should not be throwable", method.canThrow(new MyException())); 104 | assertFalse("should not be throwable", method.canThrow(new MyUndeclaredException())); 105 | 106 | assertTrue("should be throwable", method.canThrow(new MyExtendedException())); 107 | assertTrue("should be throwable", method.canThrow(new RuntimeException())); 108 | assertTrue("should be throwable", method.canThrow(new Error())); 109 | } 110 | 111 | public void testToString() { 112 | Class[] args = { MyException.class, int.class, String.class }; 113 | Method method = new Method("foo", int.class, args, null); 114 | 115 | assertEquals("foo(MethodJavaTest.MyException, int, String)", method.toString()); 116 | } 117 | 118 | class MyException extends Exception { 119 | } 120 | 121 | class MyExtendedException extends MyException { 122 | } 123 | 124 | class MyUndeclaredException extends Exception { 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/internal/RangeJavaTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | import junit.framework.TestCase; 20 | 21 | /** 22 | * Tests the Range class 23 | * 24 | * @author Michael Goderbauer 25 | */ 26 | public class RangeJavaTest extends TestCase { 27 | 28 | public void testToString() { 29 | Range range = new Range(1, 1); 30 | assertEquals("1", range.toString()); 31 | 32 | range = new Range(1, 4); 33 | assertEquals("between 1 and 4", range.toString()); 34 | 35 | range = new Range(1, Range.UNLIMITED_MAX); 36 | assertEquals("at least 1", range.toString()); 37 | } 38 | 39 | public void testIncludes() { 40 | Range range = new Range(4, 10); 41 | 42 | assertTrue("should include 6", range.includes(6)); 43 | assertTrue("should include 4", range.includes(4)); 44 | assertTrue("should include 10", range.includes(10)); 45 | 46 | assertFalse("should not include 11", range.includes(11)); 47 | assertFalse("should not include 3", range.includes(3)); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/internal/RecordStateJavaTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | import com.google.gwt.testing.easygwtmock.client.ArgumentMatcher; 20 | import com.google.gwt.testing.easygwtmock.client.internal.matchers.Any; 21 | import com.google.gwt.testing.easygwtmock.client.internal.matchers.ArgumentCapture; 22 | import com.google.gwt.testing.easygwtmock.client.internal.matchers.Equals; 23 | 24 | import junit.framework.TestCase; 25 | 26 | import org.easymock.EasyMock; 27 | 28 | import java.util.ArrayList; 29 | 30 | /** 31 | * Tests the RecordState class 32 | * 33 | * @author Michael Goderbauer 34 | */ 35 | public class RecordStateJavaTest extends TestCase { 36 | 37 | private MocksBehavior behavior; 38 | private RecordState recordState; 39 | 40 | @Override 41 | public void setUp(){ 42 | behavior = EasyMock.createMock(MocksBehavior.class); 43 | recordState = new RecordState(behavior); 44 | } 45 | 46 | public void testVerify() { 47 | EasyMock.replay(behavior); 48 | try { 49 | recordState.verify(); 50 | } catch (IllegalStateExceptionWrapper expected) { 51 | assertEquals("Calling verify is not allowed in record state", 52 | expected.getIllegalStateException().getMessage()); 53 | } 54 | EasyMock.verify(behavior); 55 | } 56 | 57 | public void testReportMatcher() { 58 | EasyMock.replay(behavior); 59 | 60 | recordState.reportMatcher(Any.ANY); 61 | assertEquals(1, recordState.argumentMatchers.size()); 62 | assertTrue(recordState.argumentMatchers.contains(Any.ANY)); 63 | 64 | 65 | ArgumentMatcher matcher = new Equals(3); 66 | recordState.reportMatcher(matcher); 67 | assertEquals(2, recordState.argumentMatchers.size()); 68 | assertTrue(recordState.argumentMatchers.contains(Any.ANY)); 69 | assertTrue(recordState.argumentMatchers.contains(matcher)); 70 | 71 | EasyMock.verify(behavior); 72 | } 73 | 74 | public void testReportCapture() throws IllegalStateExceptionWrapper { 75 | EasyMock.replay(behavior); 76 | 77 | ArgumentCapture capture1 = EasyMock.createMock(ArgumentCapture.class); 78 | ArgumentCapture capture2 = EasyMock.createMock(ArgumentCapture.class); 79 | EasyMock.replay(capture1); 80 | EasyMock.replay(capture2); 81 | 82 | recordState.reportCapture(capture1); 83 | assertEquals(1, recordState.argumentCaptures.size()); 84 | assertTrue(recordState.argumentCaptures.contains(capture1)); 85 | 86 | recordState.reportCapture(capture2); 87 | assertEquals(2, recordState.argumentCaptures.size()); 88 | assertTrue(recordState.argumentCaptures.contains(capture1)); 89 | assertTrue(recordState.argumentCaptures.contains(capture2)); 90 | 91 | EasyMock.verify(behavior); 92 | EasyMock.verify(capture1); 93 | EasyMock.verify(capture2); 94 | } 95 | 96 | public void testReportCapture_SameTwice() throws IllegalStateExceptionWrapper { 97 | EasyMock.replay(behavior); 98 | 99 | ArgumentCapture capture = EasyMock.createMock(ArgumentCapture.class); 100 | EasyMock.replay(capture); 101 | 102 | recordState.reportCapture(capture); 103 | 104 | try { 105 | recordState.reportCapture(capture); 106 | } catch (IllegalStateExceptionWrapper expected) { 107 | } 108 | 109 | EasyMock.verify(behavior); 110 | EasyMock.verify(capture); 111 | } 112 | 113 | public void testCheckCanSwitchToReplay_noCurrentSetter() { 114 | EasyMock.replay(behavior); 115 | 116 | assertNull(recordState.currentSetter); 117 | recordState.checkCanSwitchToReplay(); 118 | assertNull(recordState.currentSetter); 119 | 120 | EasyMock.verify(behavior); 121 | } 122 | 123 | public void testCheckCanSwitchToReplay_withCurrentSetter() { 124 | EasyMock.replay(behavior); 125 | 126 | ExpectationSettersImpl setter = EasyMock.createMock(ExpectationSettersImpl.class); 127 | setter.retire(); 128 | EasyMock.replay(setter); 129 | 130 | recordState.currentSetter = setter; 131 | recordState.checkCanSwitchToReplay(); 132 | assertNull(recordState.currentSetter); 133 | 134 | EasyMock.verify(setter); 135 | EasyMock.verify(behavior); 136 | } 137 | 138 | public void testGetExpectationSetter_noSetter() { 139 | EasyMock.replay(behavior); 140 | assertNull(recordState.currentSetter); 141 | 142 | try { 143 | recordState.getExpectationSetter(); 144 | } catch (IllegalStateExceptionWrapper expected) { 145 | } 146 | 147 | EasyMock.verify(behavior); 148 | } 149 | 150 | public void testGetExpectationSetter_withSetter() throws IllegalStateExceptionWrapper { 151 | EasyMock.replay(behavior); 152 | 153 | ExpectationSettersImpl setter = EasyMock.createMock(ExpectationSettersImpl.class); 154 | EasyMock.replay(setter); 155 | 156 | recordState.currentSetter = setter; 157 | assertSame(setter, recordState.getExpectationSetter()); 158 | 159 | EasyMock.verify(setter); 160 | EasyMock.verify(behavior); 161 | } 162 | 163 | public void testInvoke() throws Throwable { 164 | EasyMock.replay(behavior); 165 | Call call = EasyMock.createMock(Call.class); 166 | EasyMock.expect(call.getDefaultReturnValue()).andReturn(0); 167 | EasyMock.expect(call.getArguments()).andReturn(new ArrayList()); 168 | EasyMock.replay(call); 169 | 170 | ExpectationSettersImpl setter = recordState.currentSetter; 171 | 172 | assertEquals(0, recordState.invoke(call).answer(null)); 173 | assertNull(recordState.argumentCaptures); 174 | assertNull(recordState.argumentMatchers); 175 | assertNotNull(recordState.currentSetter); 176 | assertNotSame(setter, recordState.currentSetter); 177 | 178 | EasyMock.verify(behavior); 179 | EasyMock.verify(call); 180 | } 181 | 182 | public void testInvoke_retirePrevious() { 183 | EasyMock.replay(behavior); 184 | Call call = EasyMock.createMock(Call.class); 185 | EasyMock.expect(call.getDefaultReturnValue()).andReturn(0); 186 | EasyMock.expect(call.getArguments()).andReturn(new ArrayList()); 187 | EasyMock.replay(call); 188 | 189 | ExpectationSettersImpl setter = EasyMock.createMock(ExpectationSettersImpl.class); 190 | setter.retire(); 191 | EasyMock.replay(setter); 192 | 193 | recordState.currentSetter = setter; 194 | 195 | recordState.invoke(call); 196 | assertNotSame(setter, recordState.currentSetter); 197 | 198 | EasyMock.verify(behavior); 199 | EasyMock.verify(call); 200 | EasyMock.verify(setter); 201 | } 202 | } 203 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/internal/ReplayStateJavaTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | import com.google.gwt.testing.easygwtmock.client.Answer; 20 | import com.google.gwt.testing.easygwtmock.client.internal.matchers.Any; 21 | 22 | import junit.framework.TestCase; 23 | 24 | import org.easymock.EasyMock; 25 | 26 | /** 27 | * Tests the ReplayState class 28 | * 29 | * @author Michael Goderbauer 30 | */ 31 | public class ReplayStateJavaTest extends TestCase { 32 | 33 | private MocksBehavior behavior; 34 | private ReplayState replayState; 35 | 36 | @Override 37 | public void setUp(){ 38 | behavior = EasyMock.createMock(MocksBehavior.class); 39 | replayState = new ReplayState(behavior); 40 | } 41 | 42 | public void testGetExpectationSetter() { 43 | EasyMock.replay(behavior); 44 | try { 45 | replayState.getExpectationSetter(); 46 | } catch (IllegalStateExceptionWrapper expected) { 47 | assertEquals("Cannot set expectations while in replay state", 48 | expected.getIllegalStateException().getMessage()); 49 | } 50 | EasyMock.verify(behavior); 51 | } 52 | 53 | public void testCheckCanSwitchToReplay() { 54 | EasyMock.replay(behavior); 55 | try { 56 | replayState.checkCanSwitchToReplay(); 57 | } catch (IllegalStateExceptionWrapper expected) { 58 | assertEquals("Cannot switch to replay mode while in replay state", 59 | expected.getIllegalStateException().getMessage()); 60 | } 61 | EasyMock.verify(behavior); 62 | } 63 | 64 | public void testVerify() throws AssertionErrorWrapper { 65 | behavior.verify(); 66 | EasyMock.replay(behavior); 67 | 68 | replayState.verify(); 69 | 70 | EasyMock.verify(behavior); 71 | } 72 | 73 | public void testInvoke() throws AssertionErrorWrapper { 74 | Call call = EasyMock.createMock(Call.class); 75 | EasyMock.replay(call); 76 | 77 | @SuppressWarnings("unchecked") 78 | Answer answer = EasyMock.createMock(Answer.class); 79 | EasyMock.replay(answer); 80 | 81 | EasyMock.>expect(behavior.addActual(call)).andReturn(answer); 82 | EasyMock.replay(behavior); 83 | 84 | assertSame(answer, replayState.invoke(call)); 85 | 86 | EasyMock.verify(behavior); 87 | EasyMock.verify(call); 88 | EasyMock.verify(answer); 89 | } 90 | 91 | public void testReportMatcher() { 92 | EasyMock.replay(behavior); 93 | try { 94 | replayState.reportMatcher(Any.ANY); 95 | } catch (IllegalStateExceptionWrapper expected) { 96 | assertEquals("Argument matchers must not be used in replay state", 97 | expected.getIllegalStateException().getMessage()); 98 | } 99 | EasyMock.verify(behavior); 100 | } 101 | 102 | public void testReportCapture() { 103 | EasyMock.replay(behavior); 104 | try { 105 | replayState.reportCapture(null); 106 | } catch (IllegalStateExceptionWrapper expected) { 107 | assertEquals("Captures must not be used in replay state", 108 | expected.getIllegalStateException().getMessage()); 109 | } 110 | EasyMock.verify(behavior); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/internal/UtilsJavaTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal; 18 | 19 | import junit.framework.TestCase; 20 | 21 | /** 22 | * Tests the Util class 23 | * 24 | * @author Michael Goderbauer 25 | */ 26 | public class UtilsJavaTest extends TestCase { 27 | 28 | private StringBuffer buffer; 29 | 30 | @Override 31 | public void setUp() { 32 | this.buffer = new StringBuffer(); 33 | } 34 | 35 | public void testAppendArgumentTo_null() { 36 | Utils.appendArgumentTo(null, buffer); 37 | assertEquals("null", buffer.toString()); 38 | } 39 | 40 | public void testAppendArgumentTo_primitive() { 41 | Utils.appendArgumentTo(4, buffer); 42 | assertEquals("4", buffer.toString()); 43 | } 44 | 45 | public void testAppendArgumentTo_primitiveArray() { 46 | double[] doubleArry = {1.2, 1.3, 1.4}; 47 | Utils.appendArgumentTo(doubleArry, buffer); 48 | assertEquals("[1.2, 1.3, 1.4]", buffer.toString()); 49 | } 50 | 51 | public void testAppendArgumentTo_objectArray() { 52 | String[] stringArray = {"Hallo", "Hi"}; 53 | Utils.appendArgumentTo(stringArray, buffer); 54 | assertEquals("[Hallo, Hi]", buffer.toString()); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/internal/matchers/AnyJavaTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal.matchers; 18 | 19 | import junit.framework.TestCase; 20 | 21 | import java.util.HashSet; 22 | 23 | /** 24 | * Tests the Any class 25 | * 26 | * @author Michael Goderbauer 27 | */ 28 | public class AnyJavaTest extends TestCase { 29 | 30 | private Any any; 31 | 32 | @Override 33 | public void setUp() { 34 | any = Any.ANY; 35 | } 36 | 37 | public void testMatches() { 38 | assertTrue(any.matches(null)); 39 | assertTrue(any.matches(23)); 40 | assertTrue(any.matches("Hallo")); 41 | assertTrue(any.matches(new Object())); 42 | assertTrue(any.matches(new HashSet())); 43 | } 44 | 45 | public void testAppend() { 46 | StringBuffer buffer = new StringBuffer(); 47 | any.appendTo(buffer); 48 | assertEquals("", buffer.toString()); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/internal/matchers/ArgumentCaptureJavaTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal.matchers; 18 | 19 | import com.google.gwt.testing.easygwtmock.client.Capture; 20 | 21 | import junit.framework.TestCase; 22 | 23 | /** 24 | * @author Michael Goderbauer 25 | */ 26 | public class ArgumentCaptureJavaTest extends TestCase { 27 | 28 | private ArgumentCapture argumentCapture; 29 | private Capture capture; 30 | 31 | @Override 32 | public void setUp() { 33 | capture = new Capture(); 34 | argumentCapture = new ArgumentCapture(capture); 35 | } 36 | 37 | public void testMatches() { 38 | assertTrue("should match", argumentCapture.matches(10)); 39 | assertTrue("should match", argumentCapture.matches(null)); 40 | assertTrue("should match", argumentCapture.matches("Hallo")); 41 | assertTrue("should match", argumentCapture.matches(new Object())); 42 | } 43 | 44 | public void testCaptureArgument() { 45 | argumentCapture.matches(10); 46 | argumentCapture.matches(20); 47 | 48 | assertFalse("should not have captured", capture.hasCaptured()); 49 | 50 | argumentCapture.captureArgument(); 51 | 52 | assertTrue("should have captured", capture.hasCaptured()); 53 | assertEquals(1, capture.getValues().size()); 54 | assertEquals(20, (int) capture.getFirstValue()); 55 | } 56 | 57 | public void testAppendTo() { 58 | StringBuffer buffer = new StringBuffer(); 59 | argumentCapture.appendTo(buffer); 60 | assertEquals("captured()", buffer.toString()); 61 | 62 | argumentCapture.matches(20); 63 | argumentCapture.captureArgument(); 64 | 65 | buffer = new StringBuffer(); 66 | argumentCapture.appendTo(buffer); 67 | assertEquals("captured(20)", buffer.toString()); 68 | 69 | argumentCapture.matches(30); 70 | argumentCapture.captureArgument(); 71 | 72 | buffer = new StringBuffer(); 73 | argumentCapture.appendTo(buffer); 74 | assertEquals("captured(20, 30)", buffer.toString()); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /javatests/com/google/gwt/testing/easygwtmock/client/internal/matchers/AsyncCallbackMatcherJavaTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.google.gwt.testing.easygwtmock.client.internal.matchers; 18 | 19 | import com.google.gwt.testing.easygwtmock.client.ArgumentMatcher; 20 | import com.google.gwt.user.client.rpc.AsyncCallback; 21 | 22 | import junit.framework.TestCase; 23 | 24 | import java.util.HashSet; 25 | 26 | /** 27 | * Tests the Any class 28 | * 29 | * @author Michael Goderbauer 30 | */ 31 | public class AsyncCallbackMatcherJavaTest extends TestCase { 32 | 33 | private ArgumentMatcher matcher; 34 | 35 | @Override 36 | public void setUp() { 37 | matcher = AsyncCallbackMatcher.MATCHER; 38 | } 39 | 40 | public void testMatches_true() { 41 | assertTrue(matcher.matches(new AsyncCallbackImpl())); 42 | assertTrue(matcher.matches(new AsyncCallbackImpl())); 43 | assertTrue(matcher.matches(new AsyncCallbackImpl())); 44 | } 45 | 46 | public void testMatches_false() { 47 | assertFalse(matcher.matches(null)); 48 | assertFalse(matcher.matches(23)); 49 | assertFalse(matcher.matches("Hallo")); 50 | assertFalse(matcher.matches(new Object())); 51 | assertFalse(matcher.matches(new HashSet())); 52 | } 53 | 54 | public void testAppend() { 55 | StringBuffer buffer = new StringBuffer(); 56 | matcher.appendTo(buffer); 57 | assertEquals("", buffer.toString()); 58 | } 59 | 60 | class AsyncCallbackImpl implements AsyncCallback { 61 | @Override 62 | public void onFailure(Throwable arg0) { 63 | } 64 | @Override 65 | public void onSuccess(T arg0) { 66 | } 67 | } 68 | } 69 | 70 | 71 | -------------------------------------------------------------------------------- /lib/README: -------------------------------------------------------------------------------- 1 | In order to run the tests you need to place the jars of the following libaries 2 | into this dir: 3 | 4 | - junit.jar 5 | - easymock.jar 6 | - asm.jar 7 | - cglib.jar 8 | - objenesis.jar 9 | 10 | --------------------------------------------------------------------------------