public static <T> T newProxyInstance(Class<T> type, final InvocationHandler handler) { Invokable invokable = new Invokable() { @Override public Object invoke(Invocation invocation) throws Throwable { return handler.invoke(null, invocation.getInvokedMethod(), invocation.getParametersAsArray()); } }; return IMPOSTERISER.imposterise(invokable, type); } }
public <T> T imposterise(final Invokable mockObject, final Class<T> mockedType, Class<?>... ancilliaryTypes) { if (!canImposterise(mockedType)) { throw new IllegalArgumentException(mockedType.getName() + " cannot be imposterized (either a primitive, or a final type or has final toString method)"); } if(mockedType.isInterface()) { return reflectionImposteriser.imposterise(mockObject, mockedType, ancilliaryTypes); } try { setConstructorsAccessible(mockedType, true); final Class<?> proxyClass = proxyClass(mockedType, ancilliaryTypes); final Object proxy = proxy(proxyClass, mockObject); return mockedType.cast(proxy); } finally { setConstructorsAccessible(mockedType, false); } }
public Object invoke(Invocation invocation) throws Throwable { final Class<?> returnType = invocation.getInvokedMethod().getReturnType(); if(Object.class.equals(returnType) && invocation.isBuildingExpectation()) { return null; } if (resultValuesByType.containsKey(returnType)) { return resultValuesByType.get(returnType); } if (returnType.isArray()) { return Array.newInstance(returnType.getComponentType(), 0); } if (isCollectionOrMap(returnType)) { final Object instance = collectionOrMapInstanceFor(returnType); if (instance != null) return instance; } if (imposteriser.canImposterise(returnType) && !invocation.isBuildingExpectation()) { return imposteriser.imposterise(this, returnType); } return null; }
public Object captureExpectationTo(ExpectationCapture capture) { return imposteriser.imposterise( new ObjectMethodExpectationBouncer(new InvocationToExpectationTranslator(capture, defaultAction)), mockedType); } }
/** * Creates a mock object of type <var>typeToMock</var> with the given name. * @param <T> is the class of the mock * @param typeToMock is the class of the mock * @param name is the name of the mock object that will appear in failures * @return the mock of typeToMock */ public <T> T mock(Class<T> typeToMock, String name) { if (mockNames.contains(name)) { throw new IllegalArgumentException("a mock with name " + name + " already exists"); } final MockObject mock = new MockObject(typeToMock, name); mockNames.add(name); Invokable invokable = threadingPolicy.synchroniseAccessTo( new ProxiedObjectIdentity( new InvocationDiverter<CaptureControl>( CaptureControl.class, mock, mock))); return imposteriser.imposterise(invokable, typeToMock, CaptureControl.class); }
@Test public void worksAroundBugInCglibWhenAskedToImposteriseObject() { imposteriser.imposterise(new VoidAction(), Object.class); imposteriser.imposterise(new VoidAction(), Object.class, EmptyInterface.class); imposteriser.imposterise(new VoidAction(), Object.class, AnInterface2.class); }
@Test public void doesntDelegateFinalizeMethod() throws Exception { Invokable failIfInvokedAction = new Invokable() { @Override public Object invoke(Invocation invocation) throws Throwable { fail("invocation should not have happened"); return null; } }; Object imposter = imposteriser.imposterise(failIfInvokedAction, Object.class); invokeMethod(imposter, Object.class.getDeclaredMethod("finalize")); }
@Test public void happyCaseWhenClassInASignedJarFile() throws Exception { File jarFile = new File("src/test/resources/signed.jar"); assertTrue(jarFile.exists()); URL jarURL = jarFile.toURI().toURL(); ClassLoader loader = new URLClassLoader(new URL[]{jarURL}); Class<?> typeInSignedJar = loader.loadClass("TypeInSignedJar"); assertTrue(imposteriser.canImposterise(typeInSignedJar)); Object o = imposteriser.imposterise(new VoidAction(), typeInSignedJar); assertTrue(typeInSignedJar.isInstance(o)); }
@Test public void happyCaseWhenJdkInterface() { assertTrue(imposteriser.canImposterise(Runnable.class)); final Runnable imposter = imposteriser.imposterise(invokable, Runnable.class); assertNotNull(imposter); imposter.run(); }
@Test public void happyCaseWhenJdkClass() { assertTrue(imposteriser.canImposterise(Date.class)); final Date imposter = imposteriser.imposterise(invokable, Date.class); assertNotNull(imposter); imposter.toString(); }
@Test public void cannotImposteriseAClassWithAFinalToStringMethod() { assertFalse(imposteriser.canImposterise(ClassWithFinalToStringMethod.class)); try { imposteriser.imposterise(new VoidAction(), ClassWithFinalToStringMethod.class); fail("should have thrown IllegalArgumentException"); } catch (IllegalArgumentException expected) { } }
@Test public void happyCaseWhenNonFinalInstantiableClass() { assertTrue(imposteriser.canImposterise(AnInnerClass.class)); final AnInnerClass imposter = imposteriser.imposterise(invokable, AnInnerClass.class); assertNotNull(imposter); assertEquals("result", imposter.foo()); }
@Test public void happyCaseWhenClassWithNonPublicConstructor() { assertTrue(imposteriser.canImposterise(AClassWithAPrivateConstructor.class)); AClassWithAPrivateConstructor imposter = imposteriser.imposterise(invokable, AClassWithAPrivateConstructor.class); assertNotNull(imposter); assertEquals("result", imposter.foo()); }
@Test public void happyCaseWhenConcreteClassWithConstructorAndInitialisersThatShouldNotBeCalled() { assertTrue(imposteriser.canImposterise(ConcreteClassWithConstructorAndInstanceInitializer.class)); ConcreteClassWithConstructorAndInstanceInitializer imposter = imposteriser.imposterise(invokable, ConcreteClassWithConstructorAndInstanceInitializer.class); assertNotNull(imposter); assertEquals("result", imposter.foo()); }
@Test public void happyCaseWhenAbstractClass() { assertTrue(imposteriser.canImposterise(AnAbstractNestedClass.class)); final AnAbstractNestedClass imposter = imposteriser.imposterise(invokable, AnAbstractNestedClass.class); assertNotNull(imposter); assertEquals("result", imposter.foo()); }
@Test public void happyCaseWhenCustomInterface() { assertTrue(imposteriser.canImposterise(AnInterface.class)); AnInterface imposter = imposteriser.imposterise(invokable, AnInterface.class); assertNotNull(imposter); assertEquals("result", imposter.foo()); }
public static <T> T wrapWith(Class<T> type, Factory factory, Object delegate) { Invokable invokableChain = new MethodAccess(delegate, new MyProxiedObjectIdentity(type, new FieldAccess(delegate, factory, new Cacher( new FakeAccess(factory))))); return IMPOSTERISER.imposterise(invokableChain, type); }