1 /* 2 * Copyright 2010 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 package com.google.android.testing.mocking; 17 18 import javassist.CannotCompileException; 19 import javassist.ClassPool; 20 import javassist.CtClass; 21 import javassist.CtMethod; 22 import javassist.Modifier; 23 import javassist.NotFoundException; 24 import javassist.expr.ExprEditor; 25 import javassist.expr.MethodCall; 26 27 import junit.framework.TestCase; 28 29 import org.easymock.Capture; 30 import org.easymock.IAnswer; 31 import org.easymock.LogicalOperator; 32 import org.easymock.internal.matchers.Equals; 33 34 import java.io.IOException; 35 import java.io.InputStream; 36 import java.io.ObjectInput; 37 import java.util.ArrayList; 38 import java.util.Arrays; 39 import java.util.Calendar; 40 import java.util.List; 41 import java.util.Locale; 42 import java.util.Map; 43 import java.util.SimpleTimeZone; 44 import java.util.Vector; 45 46 47 /** 48 * Tests for the AndroidMock class. 49 * 50 * @author swoodward (at) google.com (Stephen Woodward) 51 */ 52 public class AndroidMockTest extends TestCase { 53 private List<String> notForwardedMethods = 54 new ArrayList<String>(Arrays.asList(new String[] { 55 "com.google.android.testing.mocking.AndroidMock.getInterfaceFor(java.lang.Class)", 56 "com.google.android.testing.mocking.AndroidMock.getSubclassNameFor(java.lang.Class)", 57 "com.google.android.testing.mocking.AndroidMock.getSubclassFor(java.lang.Class," 58 + "java.lang.Class,java.lang.Object)", 59 "com.google.android.testing.mocking.AndroidMock.getInterfaceNameFor(java.lang.Class)", 60 "com.google.android.testing.mocking.AndroidMock.createStrictMock(" 61 + "java.lang.Class,java.lang.Object[])", 62 "com.google.android.testing.mocking.AndroidMock.createStrictMock(" 63 + "java.lang.String,java.lang.Class,java.lang.Object[])", 64 "com.google.android.testing.mocking.AndroidMock.createMock(" 65 + "java.lang.Class,java.lang.Object[])", 66 "com.google.android.testing.mocking.AndroidMock.createMock(" 67 + "java.lang.String,java.lang.Class,java.lang.Object[])", 68 "com.google.android.testing.mocking.AndroidMock.createNiceMock(" 69 + "java.lang.Class,java.lang.Object[])", 70 "com.google.android.testing.mocking.AndroidMock.createNiceMock(" 71 + "java.lang.String,java.lang.Class,java.lang.Object[])"})); 72 73 private CtMethod[] getForwardedMethods() throws NotFoundException { 74 List<CtMethod> methods = 75 new ArrayList<CtMethod>(Arrays.asList(getAndroidMockCtClass().getDeclaredMethods())); 76 // Get a copy for safe removal of elements during iteration. 77 for (CtMethod method : Arrays.asList(methods.toArray(new CtMethod[0]))) { 78 if (notForwardedMethods.contains(method.getLongName()) 79 || !Modifier.isPublic(method.getModifiers())) { 80 methods.remove(method); 81 } 82 } 83 return methods.toArray(new CtMethod[0]); 84 } 85 86 private CtClass getAndroidMockCtClass() throws NotFoundException { 87 return ClassPool.getDefault().get("com.google.android.testing.mocking.AndroidMock"); 88 } 89 90 private void compileClasses(List<GeneratedClassFile> mockClasses) throws NotFoundException { 91 for (GeneratedClassFile clazz : mockClasses) { 92 CtClass ctClass; 93 ctClass = ClassPool.getDefault().get(clazz.getClassName()); 94 try { 95 ctClass.toClass(); 96 } catch (CannotCompileException e) { 97 // Just ignore -- this will happen for every class used in more than one test. 98 } 99 } 100 } 101 102 public void testIsUnboxableToPrimitiveAllPrimitives() { 103 assertTrue(AndroidMock.isUnboxableToPrimitive(Integer.TYPE, new Integer(42), true)); 104 assertTrue(AndroidMock.isUnboxableToPrimitive(Long.TYPE, new Long(42L), true)); 105 assertTrue(AndroidMock.isUnboxableToPrimitive(Short.TYPE, new Short((short) 42), true)); 106 assertTrue(AndroidMock.isUnboxableToPrimitive(Byte.TYPE, new Byte((byte) 42), true)); 107 assertTrue(AndroidMock.isUnboxableToPrimitive(Boolean.TYPE, Boolean.TRUE, true)); 108 assertTrue(AndroidMock.isUnboxableToPrimitive(Float.TYPE, new Float(42.0f), true)); 109 assertTrue(AndroidMock.isUnboxableToPrimitive(Double.TYPE, new Double(42.0), true)); 110 assertTrue(AndroidMock.isUnboxableToPrimitive(Character.TYPE, new Character('a'), true)); 111 112 assertTrue(AndroidMock.isUnboxableToPrimitive(Integer.TYPE, 42, true)); 113 assertTrue(AndroidMock.isUnboxableToPrimitive(Long.TYPE, 42L, true)); 114 assertTrue(AndroidMock.isUnboxableToPrimitive(Short.TYPE, (short) 42, true)); 115 assertTrue(AndroidMock.isUnboxableToPrimitive(Byte.TYPE, (byte) 42, true)); 116 assertTrue(AndroidMock.isUnboxableToPrimitive(Boolean.TYPE, true, true)); 117 assertTrue(AndroidMock.isUnboxableToPrimitive(Float.TYPE, 42.0f, true)); 118 assertTrue(AndroidMock.isUnboxableToPrimitive(Double.TYPE, 42.0, true)); 119 assertTrue(AndroidMock.isUnboxableToPrimitive(Character.TYPE, 'a', true)); 120 } 121 122 public void testIsUnboxableToPrimitiveIsObject() { 123 assertFalse(AndroidMock.isUnboxableToPrimitive(Integer.TYPE, new Object(), false)); 124 } 125 126 public void testIsUnboxableToPrimitiveAllWideningPrimitives() { 127 Object[] testValues = 128 new Object[] {new Byte((byte) 42), new Short((short) 42), new Integer(42), new Long(42L), 129 new Float(42.0f), new Double(42.0), new Character('a'), Boolean.TRUE}; 130 boolean[] byteExpected = new boolean[] {true, false, false, false, false, false, false, false}; 131 boolean[] shortExpected = new boolean[] {true, true, false, false, false, false, true, false}; 132 boolean[] intExpected = new boolean[] {true, true, true, false, false, false, true, false}; 133 boolean[] longExpected = new boolean[] {true, true, true, true, false, false, true, false}; 134 boolean[] floatExpected = new boolean[] {true, true, true, false, true, false, true, false}; 135 boolean[] doubleExpected = new boolean[] {true, true, true, true, true, true, true, false}; 136 boolean[] charExpected = new boolean[] {true, true, true, false, false, false, true, false}; 137 boolean[] booleanExpected = 138 new boolean[] {false, false, false, false, false, false, false, true}; 139 140 for (int i = 0; i < testValues.length; ++i) { 141 assertEquals("Convert byte from " + testValues[i].getClass(), byteExpected[i], AndroidMock 142 .isUnboxableToPrimitive(Byte.TYPE, testValues[i], false)); 143 assertEquals("Convert short from " + testValues[i].getClass(), shortExpected[i], AndroidMock 144 .isUnboxableToPrimitive(Short.TYPE, testValues[i], false)); 145 assertEquals("Convert int from " + testValues[i].getClass(), intExpected[i], AndroidMock 146 .isUnboxableToPrimitive(Integer.TYPE, testValues[i], false)); 147 assertEquals("Convert long from " + testValues[i].getClass(), longExpected[i], AndroidMock 148 .isUnboxableToPrimitive(Long.TYPE, testValues[i], false)); 149 assertEquals("Convert float from " + testValues[i].getClass(), floatExpected[i], AndroidMock 150 .isUnboxableToPrimitive(Float.TYPE, testValues[i], false)); 151 assertEquals("Convert double from " + testValues[i].getClass(), doubleExpected[i], 152 AndroidMock.isUnboxableToPrimitive(Double.TYPE, testValues[i], false)); 153 assertEquals("Convert char from " + testValues[i].getClass(), charExpected[i], AndroidMock 154 .isUnboxableToPrimitive(Character.TYPE, testValues[i], false)); 155 assertEquals("Convert boolean from " + testValues[i].getClass(), booleanExpected[i], 156 AndroidMock.isUnboxableToPrimitive(Boolean.TYPE, testValues[i], false)); 157 } 158 } 159 160 161 public void testIsUnboxableToPrimitiveNotPrimitive() { 162 try { 163 AndroidMock.isUnboxableToPrimitive(Object.class, Object.class, false); 164 fail("Exception should have been thrown"); 165 } catch (IllegalArgumentException e) { 166 // expected 167 } 168 } 169 170 public void testCreateMock() throws ClassNotFoundException, IOException, CannotCompileException, 171 NotFoundException { 172 List<GeneratedClassFile> mockClasses = 173 new AndroidMockGenerator().createMocksForClass(Vector.class); 174 compileClasses(mockClasses); 175 Vector<String> mockVector = AndroidMock.createMock(Vector.class); 176 AndroidMock.expect(mockVector.get(0)).andReturn("Hello World"); 177 AndroidMock.replay(mockVector); 178 assertEquals("Hello World", mockVector.get(0).toString()); 179 AndroidMock.verify(mockVector); 180 } 181 182 public void testCreateMockUsingParameters() throws ClassNotFoundException, IOException, 183 CannotCompileException, NotFoundException { 184 List<GeneratedClassFile> mockClasses = 185 new AndroidMockGenerator().createMocksForClass(SimpleTimeZone.class); 186 compileClasses(mockClasses); 187 SimpleTimeZone mockTimeZone = AndroidMock.createMock(SimpleTimeZone.class, 0, "GMT"); 188 AndroidMock.expect(mockTimeZone.getRawOffset()).andReturn(42); 189 AndroidMock.replay(mockTimeZone); 190 assertEquals(42, mockTimeZone.getRawOffset()); 191 AndroidMock.verify(mockTimeZone); 192 } 193 194 public void testCreateMockUsingProtectedConstructors() throws ClassNotFoundException, 195 IOException, CannotCompileException, NotFoundException { 196 List<GeneratedClassFile> mockClasses = 197 new AndroidMockGenerator().createMocksForClass(Calendar.class); 198 compileClasses(mockClasses); 199 Calendar mockCalendar = AndroidMock.createMock(Calendar.class); 200 AndroidMock.expect(mockCalendar.getGreatestMinimum(1)).andReturn(42); 201 AndroidMock.replay(mockCalendar); 202 assertEquals(42, mockCalendar.getGreatestMinimum(1)); 203 AndroidMock.verify(mockCalendar); 204 205 // Just don't explode 206 Calendar newMockCalendar = 207 AndroidMock.createMock(Calendar.class, new SimpleTimeZone(1, "GMT"), Locale.UK); 208 } 209 210 public void testCreateMockUsingCastableParameters() throws ClassNotFoundException, IOException, 211 CannotCompileException, NotFoundException { 212 List<GeneratedClassFile> mockClasses = 213 new AndroidMockGenerator().createMocksForClass(SimpleTimeZone.class); 214 compileClasses(mockClasses); 215 SimpleTimeZone mockTimeZone = AndroidMock.createMock(SimpleTimeZone.class, 'a', "GMT"); 216 AndroidMock.expect(mockTimeZone.getRawOffset()).andReturn(42); 217 AndroidMock.replay(mockTimeZone); 218 assertEquals(42, mockTimeZone.getRawOffset()); 219 AndroidMock.verify(mockTimeZone); 220 } 221 222 public void testCreateMockUsingUnusableParameters() throws ClassNotFoundException, IOException, 223 CannotCompileException, NotFoundException { 224 List<GeneratedClassFile> mockClasses = 225 new AndroidMockGenerator().createMocksForClass(SimpleTimeZone.class); 226 compileClasses(mockClasses); 227 try { 228 SimpleTimeZone mockTimeZone = AndroidMock.createMock(SimpleTimeZone.class, "GMT"); 229 fail("Excepted an IllegalArgumentException for incorrect number of constructor parameters"); 230 } catch (IllegalArgumentException e) { 231 // Expected 232 } 233 try { 234 SimpleTimeZone mockTimeZone = AndroidMock.createMock(SimpleTimeZone.class, 0, null); 235 fail("Excepted an IllegalArgumentException for indeterminate null constructor parameters"); 236 } catch (IllegalArgumentException e) { 237 // Expected 238 } 239 try { 240 SimpleTimeZone mockTimeZone = AndroidMock.createMock(SimpleTimeZone.class, 0, new Object()); 241 fail("Excepted an IllegalArgumentException for incorrect constructor parameters"); 242 } catch (IllegalArgumentException e) { 243 // Expected 244 } 245 } 246 247 public void testCreateMockUsingInterface() throws ClassNotFoundException, IOException, 248 CannotCompileException, NotFoundException { 249 List<GeneratedClassFile> mockClasses = 250 new AndroidMockGenerator().createMocksForClass(Map.class); 251 compileClasses(mockClasses); 252 Map<String, String> mockMap = AndroidMock.createMock(Map.class); 253 AndroidMock.expect(mockMap.get("key")).andReturn("Hello World"); 254 AndroidMock.replay(mockMap); 255 assertEquals("Hello World", mockMap.get("key")); 256 AndroidMock.verify(mockMap); 257 } 258 259 public void testCreateMockUsingClass() throws ClassNotFoundException, IOException, 260 CannotCompileException, NotFoundException { 261 List<GeneratedClassFile> mockClasses = 262 new AndroidMockGenerator().createMocksForClass(Vector.class); 263 compileClasses(mockClasses); 264 Vector<String> mockVector = AndroidMock.createMock(Vector.class); 265 AndroidMock.expect(mockVector.get(0)).andReturn("Hello World"); 266 AndroidMock.replay(mockVector); 267 assertEquals("Hello World", mockVector.get(0).toString()); 268 AndroidMock.verify(mockVector); 269 } 270 271 public void testCreateNiceMock() throws ClassNotFoundException, IOException, 272 CannotCompileException, NotFoundException { 273 List<GeneratedClassFile> mockClasses = 274 new AndroidMockGenerator().createMocksForClass(Vector.class); 275 compileClasses(mockClasses); 276 Vector<String> mockVector = AndroidMock.createNiceMock(Vector.class); 277 AndroidMock.expect(mockVector.get(0)).andReturn("Hello World"); 278 AndroidMock.replay(mockVector); 279 assertEquals("Hello World", mockVector.get(0).toString()); 280 AndroidMock.verify(mockVector); 281 } 282 283 public void testCreateNiceMockUsingUnusableParameters() throws ClassNotFoundException, 284 IOException, CannotCompileException, NotFoundException { 285 List<GeneratedClassFile> mockClasses = 286 new AndroidMockGenerator().createMocksForClass(SimpleTimeZone.class); 287 compileClasses(mockClasses); 288 try { 289 SimpleTimeZone mockTimeZone = AndroidMock.createNiceMock(SimpleTimeZone.class, "GMT"); 290 fail("Excepted an IllegalArgumentException for incorrect number of constructor parameters"); 291 } catch (IllegalArgumentException e) { 292 // Expected 293 } 294 try { 295 SimpleTimeZone mockTimeZone = AndroidMock.createNiceMock(SimpleTimeZone.class, 0, null); 296 fail("Excepted an IllegalArgumentException for indeterminate null constructor parameters"); 297 } catch (IllegalArgumentException e) { 298 // Expected 299 } 300 try { 301 SimpleTimeZone mockTimeZone = 302 AndroidMock.createNiceMock(SimpleTimeZone.class, 0, new Object()); 303 fail("Excepted an IllegalArgumentException for incorrect constructor parameters"); 304 } catch (IllegalArgumentException e) { 305 // Expected 306 } 307 } 308 309 public void testCreateNiceMockUsingParameters() throws ClassNotFoundException, IOException, 310 CannotCompileException, NotFoundException { 311 List<GeneratedClassFile> mockClasses = 312 new AndroidMockGenerator().createMocksForClass(SimpleTimeZone.class); 313 314 compileClasses(mockClasses); 315 SimpleTimeZone mockTimeZone = AndroidMock.createNiceMock(SimpleTimeZone.class, 0, "GMT"); 316 AndroidMock.expect(mockTimeZone.getRawOffset()).andReturn(42); 317 AndroidMock.replay(mockTimeZone); 318 assertEquals(42, mockTimeZone.getRawOffset()); 319 AndroidMock.verify(mockTimeZone); 320 } 321 322 public void testCreateNiceMockUsingCastableParameters() throws ClassNotFoundException, 323 IOException, CannotCompileException, NotFoundException { 324 List<GeneratedClassFile> mockClasses = 325 new AndroidMockGenerator().createMocksForClass(SimpleTimeZone.class); 326 327 compileClasses(mockClasses); 328 SimpleTimeZone mockTimeZone = AndroidMock.createNiceMock(SimpleTimeZone.class, 'a', "GMT"); 329 AndroidMock.expect(mockTimeZone.getRawOffset()).andReturn(42); 330 AndroidMock.replay(mockTimeZone); 331 assertEquals(42, mockTimeZone.getRawOffset()); 332 AndroidMock.verify(mockTimeZone); 333 } 334 335 public void testCreateNiceMockUsingInterface() throws ClassNotFoundException, IOException, 336 CannotCompileException, NotFoundException { 337 List<GeneratedClassFile> mockClasses = 338 new AndroidMockGenerator().createMocksForClass(Map.class); 339 340 compileClasses(mockClasses); 341 Map<String, String> mockMap = AndroidMock.createNiceMock(Map.class); 342 AndroidMock.expect(mockMap.get("key")).andReturn("Hello World"); 343 AndroidMock.replay(mockMap); 344 assertEquals("Hello World", mockMap.get("key")); 345 AndroidMock.verify(mockMap); 346 } 347 348 public void testCreateNiceMockUsingClass() throws ClassNotFoundException, IOException, 349 CannotCompileException, NotFoundException { 350 List<GeneratedClassFile> mockClasses = 351 new AndroidMockGenerator().createMocksForClass(Vector.class); 352 353 compileClasses(mockClasses); 354 Vector<String> mockVector = AndroidMock.createNiceMock(Vector.class); 355 AndroidMock.expect(mockVector.get(0)).andReturn("Hello World"); 356 AndroidMock.replay(mockVector); 357 assertEquals("Hello World", mockVector.get(0).toString()); 358 AndroidMock.verify(mockVector); 359 } 360 361 public void testCreateStrictMock() throws ClassNotFoundException, IOException, 362 CannotCompileException, NotFoundException { 363 List<GeneratedClassFile> mockClasses = 364 new AndroidMockGenerator().createMocksForClass(Vector.class); 365 366 compileClasses(mockClasses); 367 Vector<String> mockVector = AndroidMock.createStrictMock(Vector.class); 368 AndroidMock.expect(mockVector.get(0)).andReturn("Hello World"); 369 AndroidMock.replay(mockVector); 370 assertEquals("Hello World", mockVector.get(0).toString()); 371 AndroidMock.verify(mockVector); 372 } 373 374 public void testCreateStrictMockUsingUnusableParameters() throws ClassNotFoundException, 375 IOException, CannotCompileException, NotFoundException { 376 List<GeneratedClassFile> mockClasses = 377 new AndroidMockGenerator().createMocksForClass(SimpleTimeZone.class); 378 379 compileClasses(mockClasses); 380 try { 381 SimpleTimeZone mockTimeZone = AndroidMock.createStrictMock(SimpleTimeZone.class, "GMT"); 382 fail("Excepted an IllegalArgumentException for incorrect number of constructor parameters"); 383 } catch (IllegalArgumentException e) { 384 // Expected 385 } 386 try { 387 SimpleTimeZone mockTimeZone = AndroidMock.createStrictMock(SimpleTimeZone.class, 0, null); 388 fail("Excepted an IllegalArgumentException for indeterminate null constructor parameters"); 389 } catch (IllegalArgumentException e) { 390 // Expected 391 } 392 try { 393 SimpleTimeZone mockTimeZone = 394 AndroidMock.createStrictMock(SimpleTimeZone.class, 0, new Object()); 395 fail("Excepted an IllegalArgumentException for incorrect constructor parameters"); 396 } catch (IllegalArgumentException e) { 397 // Expected 398 } 399 } 400 401 public void testCreateStrictMockUsingParameters() throws ClassNotFoundException, IOException, 402 CannotCompileException, NotFoundException { 403 List<GeneratedClassFile> mockClasses = 404 new AndroidMockGenerator().createMocksForClass(SimpleTimeZone.class); 405 406 compileClasses(mockClasses); 407 SimpleTimeZone mockTimeZone = AndroidMock.createStrictMock(SimpleTimeZone.class, 0, "GMT"); 408 AndroidMock.expect(mockTimeZone.getRawOffset()).andReturn(42); 409 AndroidMock.replay(mockTimeZone); 410 assertEquals(42, mockTimeZone.getRawOffset()); 411 AndroidMock.verify(mockTimeZone); 412 } 413 414 public void testCreateStrictMockUsingCastableParameters() throws ClassNotFoundException, 415 IOException, CannotCompileException, NotFoundException { 416 List<GeneratedClassFile> mockClasses = 417 new AndroidMockGenerator().createMocksForClass(SimpleTimeZone.class); 418 419 compileClasses(mockClasses); 420 SimpleTimeZone mockTimeZone = AndroidMock.createStrictMock(SimpleTimeZone.class, 'a', "GMT"); 421 AndroidMock.expect(mockTimeZone.getRawOffset()).andReturn(42); 422 AndroidMock.replay(mockTimeZone); 423 assertEquals(42, mockTimeZone.getRawOffset()); 424 AndroidMock.verify(mockTimeZone); 425 } 426 427 public void testCreateStrictMockUsingInterface() throws ClassNotFoundException, IOException, 428 CannotCompileException, NotFoundException { 429 List<GeneratedClassFile> mockClasses = 430 new AndroidMockGenerator().createMocksForClass(Map.class); 431 432 compileClasses(mockClasses); 433 Map<String, String> mockMap = AndroidMock.createStrictMock(Map.class); 434 AndroidMock.expect(mockMap.get("key")).andReturn("Hello World"); 435 AndroidMock.replay(mockMap); 436 assertEquals("Hello World", mockMap.get("key")); 437 AndroidMock.verify(mockMap); 438 } 439 440 public void testCreateStrictMockUsingClass() throws ClassNotFoundException, IOException, 441 CannotCompileException, NotFoundException { 442 List<GeneratedClassFile> mockClasses = 443 new AndroidMockGenerator().createMocksForClass(Vector.class); 444 compileClasses(mockClasses); 445 Vector<String> mockVector = AndroidMock.createStrictMock(Vector.class); 446 AndroidMock.expect(mockVector.get(0)).andReturn("Hello World"); 447 AndroidMock.replay(mockVector); 448 assertEquals("Hello World", mockVector.get(0).toString()); 449 AndroidMock.verify(mockVector); 450 } 451 452 public void testCreateMockConstructorDoesWorkOnAllReturnTypes() throws ClassNotFoundException, 453 IOException, CannotCompileException, NotFoundException { 454 List<GeneratedClassFile> mockClasses = 455 new AndroidMockGenerator().createMocksForClass(ClassDoesWorkInConstructor.class); 456 compileClasses(mockClasses); 457 ClassDoesWorkInConstructor mock = AndroidMock.createMock(ClassDoesWorkInConstructor.class); 458 } 459 460 public void testAllForwardedMethods() throws CannotCompileException, NotFoundException { 461 for (CtMethod method : getForwardedMethods()) { 462 MethodVerifier verifier = new MethodVerifier(method); 463 // CtMethod.instrument Causes every instruction in the method to be 464 // inspected, and passed to 465 // the MethodVerifier callback (extends javassist.expr.ExprEditor). We 466 // want to verify that 467 // the expected EasyMock method is called at least once in each 468 // AndroidMock method. 469 method.instrument(verifier); 470 assertTrue(method.getLongName() + " not called.", verifier.expectedMethodCalled()); 471 } 472 } 473 474 public void testCheckOrder() throws ClassNotFoundException, IOException, CannotCompileException, 475 NotFoundException { 476 List<GeneratedClassFile> mockClasses = 477 new AndroidMockGenerator().createMocksForClass(Vector.class); 478 compileClasses(mockClasses); 479 Vector<?> mockVector = AndroidMock.createMock(Vector.class); 480 AndroidMock.checkOrder(mockVector, false); 481 AndroidMock.checkOrder(AndroidMock.createMock(Map.class), false); 482 } 483 484 public void testVerify() throws ClassNotFoundException, IOException, CannotCompileException, 485 NotFoundException { 486 List<GeneratedClassFile> mockClasses = 487 new AndroidMockGenerator().createMocksForClass(Vector.class); 488 compileClasses(mockClasses); 489 Vector<?> mockVector = AndroidMock.createMock(Vector.class); 490 AndroidMock.replay(mockVector); 491 AndroidMock.verify(mockVector); 492 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 493 AndroidMock.replay(mockMap); 494 AndroidMock.verify(mockMap); 495 } 496 497 public void testResetToStrict() throws ClassNotFoundException, IOException, 498 CannotCompileException, NotFoundException { 499 List<GeneratedClassFile> mockClasses = 500 new AndroidMockGenerator().createMocksForClass(Vector.class); 501 compileClasses(mockClasses); 502 Vector<?> mockVector = AndroidMock.createMock(Vector.class); 503 AndroidMock.resetToStrict(mockVector); 504 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 505 AndroidMock.resetToStrict(mockMap); 506 } 507 508 public void testResetToDefault() throws ClassNotFoundException, IOException, 509 CannotCompileException, NotFoundException { 510 List<GeneratedClassFile> mockClasses = 511 new AndroidMockGenerator().createMocksForClass(Vector.class); 512 compileClasses(mockClasses); 513 Vector<?> mockVector = AndroidMock.createMock(Vector.class); 514 AndroidMock.resetToDefault(mockVector); 515 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 516 AndroidMock.resetToDefault(mockMap); 517 } 518 519 public void testResetToNice() throws ClassNotFoundException, IOException, 520 CannotCompileException, NotFoundException { 521 List<GeneratedClassFile> mockClasses = 522 new AndroidMockGenerator().createMocksForClass(Vector.class); 523 compileClasses(mockClasses); 524 Vector<?> mockVector = AndroidMock.createMock(Vector.class); 525 AndroidMock.resetToNice(mockVector); 526 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 527 AndroidMock.resetToNice(mockMap); 528 } 529 530 public void testReset() throws ClassNotFoundException, IOException, CannotCompileException, 531 NotFoundException { 532 List<GeneratedClassFile> mockClasses = 533 new AndroidMockGenerator().createMocksForClass(Vector.class); 534 compileClasses(mockClasses); 535 Vector<?> mockVector = AndroidMock.createMock(Vector.class); 536 AndroidMock.reset(mockVector); 537 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 538 AndroidMock.reset(mockMap); 539 540 } 541 542 public void testReplay() throws ClassNotFoundException, IOException, CannotCompileException, 543 NotFoundException { 544 List<GeneratedClassFile> mockClasses = 545 new AndroidMockGenerator().createMocksForClass(Vector.class); 546 compileClasses(mockClasses); 547 Vector<?> mockVector = AndroidMock.createMock(Vector.class); 548 AndroidMock.replay(mockVector); 549 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 550 AndroidMock.replay(mockMap); 551 } 552 553 public void testExpect() { 554 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 555 mockMap.clear(); 556 AndroidMock.expect(null); 557 AndroidMock.replay(mockMap); 558 } 559 560 public void testExpectLastCall() { 561 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 562 mockMap.clear(); 563 AndroidMock.expectLastCall(); 564 AndroidMock.replay(mockMap); 565 } 566 567 public void testAnyBoolean() { 568 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 569 AndroidMock.expect(mockMap.get(AndroidMock.anyBoolean())).andReturn(null); 570 AndroidMock.replay(mockMap); 571 } 572 573 public void testAnyByte() { 574 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 575 AndroidMock.expect(mockMap.get(AndroidMock.anyByte())).andReturn(null); 576 AndroidMock.replay(mockMap); 577 } 578 579 public void testAnyChar() { 580 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 581 AndroidMock.expect(mockMap.get(AndroidMock.anyChar())).andReturn(null); 582 AndroidMock.replay(mockMap); 583 } 584 585 public void testAnyInt() { 586 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 587 AndroidMock.expect(mockMap.get(AndroidMock.anyInt())).andReturn(null); 588 AndroidMock.replay(mockMap); 589 } 590 591 public void testAnyLong() { 592 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 593 AndroidMock.expect(mockMap.get(AndroidMock.anyLong())).andReturn(null); 594 AndroidMock.replay(mockMap); 595 } 596 597 public void testAnyFloat() { 598 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 599 AndroidMock.expect(mockMap.get(AndroidMock.anyFloat())).andReturn(null); 600 AndroidMock.replay(mockMap); 601 } 602 603 public void testAnyDouble() { 604 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 605 AndroidMock.expect(mockMap.get(AndroidMock.anyDouble())).andReturn(null); 606 AndroidMock.replay(mockMap); 607 } 608 609 public void testAnyShort() { 610 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 611 AndroidMock.expect(mockMap.get(AndroidMock.anyShort())).andReturn(null); 612 AndroidMock.replay(mockMap); 613 } 614 615 public void testAnyObject() { 616 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 617 AndroidMock.expect(mockMap.get(AndroidMock.anyObject())).andReturn(null); 618 AndroidMock.replay(mockMap); 619 } 620 621 public void testGeq() { 622 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 623 AndroidMock.expect(mockMap.get(AndroidMock.geq((byte) 0))).andReturn(null); 624 AndroidMock.expect(mockMap.get(AndroidMock.geq((short) 0))).andReturn(null); 625 AndroidMock.expect(mockMap.get(AndroidMock.geq(0))).andReturn(null); 626 AndroidMock.expect(mockMap.get(AndroidMock.geq(0L))).andReturn(null); 627 AndroidMock.expect(mockMap.get(AndroidMock.geq(0.0))).andReturn(null); 628 AndroidMock.expect(mockMap.get(AndroidMock.geq(0.0f))).andReturn(null); 629 AndroidMock.expect(mockMap.get(AndroidMock.geq("Hi"))).andReturn(null); 630 AndroidMock.replay(mockMap); 631 } 632 633 public void testLeq() { 634 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 635 AndroidMock.expect(mockMap.get(AndroidMock.leq((byte) 0))).andReturn(null); 636 AndroidMock.expect(mockMap.get(AndroidMock.leq((short) 0))).andReturn(null); 637 AndroidMock.expect(mockMap.get(AndroidMock.leq(0))).andReturn(null); 638 AndroidMock.expect(mockMap.get(AndroidMock.leq(0L))).andReturn(null); 639 AndroidMock.expect(mockMap.get(AndroidMock.leq(0.0))).andReturn(null); 640 AndroidMock.expect(mockMap.get(AndroidMock.leq(0.0f))).andReturn(null); 641 AndroidMock.expect(mockMap.get(AndroidMock.leq("Hi"))).andReturn(null); 642 AndroidMock.replay(mockMap); 643 } 644 645 public void testGt() { 646 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 647 AndroidMock.expect(mockMap.get(AndroidMock.gt((byte) 0))).andReturn(null); 648 AndroidMock.expect(mockMap.get(AndroidMock.gt((short) 0))).andReturn(null); 649 AndroidMock.expect(mockMap.get(AndroidMock.gt(0))).andReturn(null); 650 AndroidMock.expect(mockMap.get(AndroidMock.gt(0L))).andReturn(null); 651 AndroidMock.expect(mockMap.get(AndroidMock.gt(0.0))).andReturn(null); 652 AndroidMock.expect(mockMap.get(AndroidMock.gt(0.0f))).andReturn(null); 653 AndroidMock.expect(mockMap.get(AndroidMock.gt("Hi"))).andReturn(null); 654 AndroidMock.replay(mockMap); 655 } 656 657 public void testLt() { 658 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 659 AndroidMock.expect(mockMap.get(AndroidMock.lt((byte) 0))).andReturn(null); 660 AndroidMock.expect(mockMap.get(AndroidMock.lt((short) 0))).andReturn(null); 661 AndroidMock.expect(mockMap.get(AndroidMock.lt(0))).andReturn(null); 662 AndroidMock.expect(mockMap.get(AndroidMock.lt(0L))).andReturn(null); 663 AndroidMock.expect(mockMap.get(AndroidMock.lt(0.0))).andReturn(null); 664 AndroidMock.expect(mockMap.get(AndroidMock.lt(0.0f))).andReturn(null); 665 AndroidMock.expect(mockMap.get(AndroidMock.lt("Hi"))).andReturn(null); 666 AndroidMock.replay(mockMap); 667 } 668 669 public void testIsA() { 670 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 671 AndroidMock.expect(mockMap.get(AndroidMock.isA(String.class))).andReturn(null); 672 AndroidMock.replay(mockMap); 673 } 674 675 public void testContains() { 676 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 677 AndroidMock.expect(mockMap.get(AndroidMock.contains("hi"))).andReturn(null); 678 AndroidMock.replay(mockMap); 679 } 680 681 public void testAnd() { 682 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 683 AndroidMock.expect(mockMap.get(AndroidMock.and(AndroidMock.eq(true), AndroidMock.eq(true)))) 684 .andReturn(null); 685 AndroidMock.expect( 686 mockMap.get(AndroidMock.and(AndroidMock.eq((byte) 0), AndroidMock.eq((byte) 0)))) 687 .andReturn(null); 688 AndroidMock.expect( 689 mockMap.get(AndroidMock.and(AndroidMock.eq((short) 0), AndroidMock.eq((short) 0)))) 690 .andReturn(null); 691 AndroidMock.expect(mockMap.get(AndroidMock.and(AndroidMock.eq(0), AndroidMock.eq(0)))) 692 .andReturn(null); 693 AndroidMock.expect(mockMap.get(AndroidMock.and(AndroidMock.eq(0L), AndroidMock.eq(0L)))) 694 .andReturn(null); 695 AndroidMock.expect(mockMap.get(AndroidMock.and(AndroidMock.eq(0.0), AndroidMock.eq(0.0)))) 696 .andReturn(null); 697 AndroidMock.expect(mockMap.get(AndroidMock.and(AndroidMock.eq(0.0f), AndroidMock.eq(0.0f)))) 698 .andReturn(null); 699 AndroidMock.expect(mockMap.get(AndroidMock.and(AndroidMock.eq("hi"), AndroidMock.eq("hi")))) 700 .andReturn(null); 701 AndroidMock.expect(mockMap.get(AndroidMock.and(AndroidMock.eq('a'), AndroidMock.eq('a')))) 702 .andReturn(null); 703 AndroidMock.replay(mockMap); 704 } 705 706 public void testOr() { 707 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 708 AndroidMock.expect(mockMap.get(AndroidMock.or(AndroidMock.eq(true), AndroidMock.eq(true)))) 709 .andReturn(null); 710 AndroidMock.expect( 711 mockMap.get(AndroidMock.or(AndroidMock.eq((byte) 0), AndroidMock.eq((byte) 0)))) 712 .andReturn(null); 713 AndroidMock.expect( 714 mockMap.get(AndroidMock.or(AndroidMock.eq((short) 0), AndroidMock.eq((short) 0)))) 715 .andReturn(null); 716 AndroidMock.expect(mockMap.get(AndroidMock.or(AndroidMock.eq(0), AndroidMock.eq(0)))) 717 .andReturn(null); 718 AndroidMock.expect(mockMap.get(AndroidMock.or(AndroidMock.eq(0L), AndroidMock.eq(0L)))) 719 .andReturn(null); 720 AndroidMock.expect(mockMap.get(AndroidMock.or(AndroidMock.eq(0.0), AndroidMock.eq(0.0)))) 721 .andReturn(null); 722 AndroidMock.expect(mockMap.get(AndroidMock.or(AndroidMock.eq(0.0f), AndroidMock.eq(0.0f)))) 723 .andReturn(null); 724 AndroidMock.expect(mockMap.get(AndroidMock.or(AndroidMock.eq("hi"), AndroidMock.eq("hi")))) 725 .andReturn(null); 726 AndroidMock.expect(mockMap.get(AndroidMock.or(AndroidMock.eq('a'), AndroidMock.eq('a')))) 727 .andReturn(null); 728 AndroidMock.replay(mockMap); 729 } 730 731 public void testNot() { 732 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 733 AndroidMock.expect(mockMap.get(AndroidMock.not(AndroidMock.eq(true)))).andReturn(null); 734 AndroidMock.expect(mockMap.get(AndroidMock.not(AndroidMock.eq((byte) 0)))).andReturn(null); 735 AndroidMock.expect(mockMap.get(AndroidMock.not(AndroidMock.eq((short) 0)))).andReturn(null); 736 AndroidMock.expect(mockMap.get(AndroidMock.not(AndroidMock.eq(0)))).andReturn(null); 737 AndroidMock.expect(mockMap.get(AndroidMock.not(AndroidMock.eq(0L)))).andReturn(null); 738 AndroidMock.expect(mockMap.get(AndroidMock.not(AndroidMock.eq(0.0)))).andReturn(null); 739 AndroidMock.expect(mockMap.get(AndroidMock.not(AndroidMock.eq(0.0f)))).andReturn(null); 740 AndroidMock.expect(mockMap.get(AndroidMock.not(AndroidMock.eq("hi")))).andReturn(null); 741 AndroidMock.expect(mockMap.get(AndroidMock.not(AndroidMock.eq('a')))).andReturn(null); 742 AndroidMock.replay(mockMap); 743 } 744 745 public void testEq() { 746 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 747 AndroidMock.expect(mockMap.get(AndroidMock.eq(true))).andReturn(null); 748 AndroidMock.expect(mockMap.get(AndroidMock.eq((byte) 0))).andReturn(null); 749 AndroidMock.expect(mockMap.get(AndroidMock.eq((short) 0))).andReturn(null); 750 AndroidMock.expect(mockMap.get(AndroidMock.eq(0))).andReturn(null); 751 AndroidMock.expect(mockMap.get(AndroidMock.eq(0L))).andReturn(null); 752 AndroidMock.expect(mockMap.get(AndroidMock.eq(0.0))).andReturn(null); 753 AndroidMock.expect(mockMap.get(AndroidMock.eq(0.0f))).andReturn(null); 754 AndroidMock.expect(mockMap.get(AndroidMock.eq(0.0, 0.1))).andReturn(null); 755 AndroidMock.expect(mockMap.get(AndroidMock.eq(0.0f, 0.1f))).andReturn(null); 756 AndroidMock.expect(mockMap.get(AndroidMock.eq("hi"))).andReturn(null); 757 AndroidMock.expect(mockMap.get(AndroidMock.eq('a'))).andReturn(null); 758 AndroidMock.replay(mockMap); 759 } 760 761 public void testAryEq() { 762 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 763 AndroidMock.expect(mockMap.get(AndroidMock.aryEq(new boolean[] {true}))).andReturn(null); 764 AndroidMock.expect(mockMap.get(AndroidMock.aryEq(new byte[] {(byte) 0}))).andReturn(null); 765 AndroidMock.expect(mockMap.get(AndroidMock.aryEq(new short[] {(short) 0}))).andReturn(null); 766 AndroidMock.expect(mockMap.get(AndroidMock.aryEq(new int[] {0}))).andReturn(null); 767 AndroidMock.expect(mockMap.get(AndroidMock.aryEq(new long[] {0L}))).andReturn(null); 768 AndroidMock.expect(mockMap.get(AndroidMock.aryEq(new double[] {0.0}))).andReturn(null); 769 AndroidMock.expect(mockMap.get(AndroidMock.aryEq(new float[] {0.0f}))).andReturn(null); 770 AndroidMock.expect(mockMap.get(AndroidMock.aryEq(new String[] {"hi"}))).andReturn(null); 771 AndroidMock.expect(mockMap.get(AndroidMock.aryEq(new char[] {'a'}))).andReturn(null); 772 AndroidMock.replay(mockMap); 773 } 774 775 public void testIsNull() { 776 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 777 AndroidMock.expect(mockMap.get(AndroidMock.isNull())).andReturn(null); 778 AndroidMock.replay(mockMap); 779 } 780 781 public void testNotNull() { 782 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 783 AndroidMock.expect(mockMap.get(AndroidMock.notNull())).andReturn(null); 784 AndroidMock.replay(mockMap); 785 } 786 787 public void testFind() { 788 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 789 AndroidMock.expect(mockMap.get(AndroidMock.find("hi"))).andReturn(null); 790 AndroidMock.replay(mockMap); 791 } 792 793 public void testMatches() { 794 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 795 AndroidMock.expect(mockMap.get(AndroidMock.matches("hi"))).andReturn(null); 796 AndroidMock.replay(mockMap); 797 } 798 799 public void testStartsWith() { 800 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 801 AndroidMock.expect(mockMap.get(AndroidMock.startsWith("hi"))).andReturn(null); 802 AndroidMock.replay(mockMap); 803 } 804 805 public void testEndsWith() { 806 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 807 AndroidMock.expect(mockMap.get(AndroidMock.endsWith("hi"))).andReturn(null); 808 AndroidMock.replay(mockMap); 809 } 810 811 public void testSame() { 812 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 813 AndroidMock.expect(mockMap.get(AndroidMock.same("hi"))).andReturn(null); 814 AndroidMock.replay(mockMap); 815 } 816 817 public void testCmpEq() { 818 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 819 AndroidMock.expect(mockMap.get(AndroidMock.cmpEq("hi"))).andReturn(null); 820 AndroidMock.replay(mockMap); 821 } 822 823 public void testCmp() { 824 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 825 AndroidMock.expect( 826 mockMap.get(AndroidMock.cmp("hi", String.CASE_INSENSITIVE_ORDER, LogicalOperator.EQUAL))) 827 .andReturn(null); 828 AndroidMock.replay(mockMap); 829 } 830 831 public void testCapture() { 832 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 833 AndroidMock.expect(mockMap.get(AndroidMock.capture(new Capture<Byte>()))).andReturn(null); 834 AndroidMock.expect(mockMap.get(AndroidMock.capture(new Capture<Character>()))).andReturn(null); 835 AndroidMock.expect(mockMap.get(AndroidMock.capture(new Capture<Double>()))).andReturn(null); 836 AndroidMock.expect(mockMap.get(AndroidMock.capture(new Capture<Float>()))).andReturn(null); 837 AndroidMock.expect(mockMap.get(AndroidMock.capture(new Capture<Integer>()))).andReturn(null); 838 AndroidMock.expect(mockMap.get(AndroidMock.capture(new Capture<Long>()))).andReturn(null); 839 AndroidMock.expect(mockMap.get(AndroidMock.capture(new Capture<String>()))).andReturn(null); 840 AndroidMock.replay(mockMap); 841 } 842 843 public void testReportMatcher() { 844 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 845 AndroidMock.reportMatcher(new Equals(null)); 846 AndroidMock.expect(mockMap.get(null)).andReturn(null); 847 AndroidMock.replay(mockMap); 848 } 849 850 public void testGetCurrentArguments() { 851 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 852 AndroidMock.expect(mockMap.get(null)).andAnswer(new IAnswer() { 853 @Override 854 public Object answer() { 855 AndroidMock.getCurrentArguments(); 856 return null; 857 } 858 }); 859 AndroidMock.replay(mockMap); 860 mockMap.get(null); 861 } 862 863 public void testMakeThreadSafe() { 864 Map<?, ?> mockMap = AndroidMock.createMock(Map.class); 865 AndroidMock.makeThreadSafe(mockMap, false); 866 AndroidMock.replay(mockMap); 867 } 868 869 public void testAndThrowsOnMockedInterface() throws IOException { 870 ObjectInput mockInStream = AndroidMock.createMock(ObjectInput.class); 871 AndroidMock.expect(mockInStream.read()).andThrow(new IOException("foo")); 872 AndroidMock.replay(mockInStream); 873 try { 874 mockInStream.read(); 875 fail("IOException not thrown"); 876 } catch (IOException e) { 877 assertEquals("foo", e.getMessage()); 878 } 879 AndroidMock.verify(mockInStream); 880 } 881 882 public void testAndThrowsOnMockedClass() throws IOException, ClassNotFoundException, 883 CannotCompileException, NotFoundException { 884 List<GeneratedClassFile> mockClasses = 885 new AndroidMockGenerator().createMocksForClass(InputStream.class); 886 compileClasses(mockClasses); 887 InputStream mockInStream = AndroidMock.createMock(InputStream.class); 888 AndroidMock.expect(mockInStream.read()).andThrow(new IOException("foo")); 889 AndroidMock.replay(mockInStream); 890 try { 891 mockInStream.read(); 892 fail("IOException not thrown"); 893 } catch (IOException e) { 894 assertEquals("foo", e.getMessage()); 895 } 896 AndroidMock.verify(mockInStream); 897 } 898 899 /** 900 * Used for testing that a given method on Android Mock calls the equivalent 901 * method on EasyMock, to ensure that the method-wiring of Android Mock is 902 * correct. 903 * 904 * @author swoodward (at) google.com (Stephen Woodward) 905 */ 906 class MethodVerifier extends ExprEditor { 907 private CtMethod expectedMethod; 908 private boolean methodCalled; 909 910 MethodVerifier(CtMethod expectedMethod) { 911 this.expectedMethod = expectedMethod; 912 } 913 914 @Override 915 public void edit(MethodCall calledMethod) { 916 try { 917 methodCalled = methodCalled || expectedMethod.equals(calledMethod.getMethod()); 918 } catch (NotFoundException e) { 919 throw new RuntimeException(e); 920 } 921 } 922 923 public boolean expectedMethodCalled() { 924 return methodCalled; 925 } 926 } 927 } 928