### Question:
ProxyFactory { public static <T> T proxy(final TypedObjectFactory<T> objectFactory, final Class<T> genericType) { try { return new ProxyFactory<T>(objectFactory, genericType).create(); } catch (final RuntimeException e) { LOG.error("exception", e); throw e; } } private  ProxyFactory(final TypedObjectFactory<T> objectFactory, final Class<T> genericType); static T proxy(final TypedObjectFactory<T> objectFactory, final Class<T> genericType);  }### Answer:
@Test public void shouldCreateProxyForAnObjectWithNotAAnInterfaceType() { final Object proxy = ProxyFactory.proxy(new TypedObjectFactory<Object>() { public Object create() { return new Object(); } public Class<? extends Object> getObjectClass() { return Object.class; } }, Object.class); Validate.notNull(proxy); LOG.debug("Proxy: {}", proxy); }
@Test(expected = NullPointerException.class) public void cannotCreateProxyFromNullObjectFactory() { ProxyFactory.proxy(null, Object.class); }
@Test public void shouldCreateProxyForAValidObject() { final ReadOnlyContext object = Context.standaloneContext(); final ReadOnlyContext proxy = ProxyFactory.proxy(new TypedObjectFactory<ReadOnlyContext>() { public ReadOnlyContext create() { return object; } public Class<? extends ReadOnlyContext> getObjectClass() { return ReadOnlyContext.class; } }, ReadOnlyContext.class); assertNotNull(proxy); assertNotSame(object, proxy); }
@Test public void shouldInheritInterfacesOfTheObject() { final ResourceAuthorizationManager object = new DefaultResourceAuthorizationManager(); final ResourceAuthorizationManager proxy = ProxyFactory.proxy( new TypedObjectFactory<ResourceAuthorizationManager>() { public ResourceAuthorizationManager create() { return object; } public Class<? extends ResourceAuthorizationManager> getObjectClass() { return DefaultResourceAuthorizationManager.class; } }, ResourceAuthorizationManager.class); assertNotNull(proxy); assertNotSame(object, proxy); assertTrue(proxy instanceof MutableResourceAuthorizationManager); }