StopWatchProxy extends DefaultProxyBehavior { public static <T> T newJdkProxy(Object obj) { Class<?> clazz = obj.getClass(); List<Class<?>> interfaces = ClassUtils.getAllInterfaces(clazz); Object res = Proxy.newProxyInstance( clazz.getClassLoader(), interfaces.toArray(new Class[interfaces.size()]), new StopWatchProxy(obj)); return (T) res; } private  StopWatchProxy(Object obj); static T newJdkProxy(Object obj);  }
@Test public void newJdkProxy() throws Exception { WikipediaServiceJapiMock target = new WikipediaServiceJapiMock(); WikipediaServiceJapi wikipediaServiceJapi = StopWatchProxy.newJdkProxy(target); String article = wikipediaServiceJapi.getArticle("42"); assertThat(article, startsWith("{{Dieser Artikel|behandelt das Jahr 42")); assertThat(wikipediaServiceJapi, not(instanceOf(target.getClass()))); }
@Test public void proxyChaining() throws Exception { DummyServiceImpl target = new DummyServiceImpl(); DummyService dummyService = StopWatchProxy.newJdkProxy(StopWatchProxy.newJdkProxy(target)); int words = dummyService.countWords(parsedPage); assertThat("wordCount", words, is(27)); assertThat(dummyService, not(instanceOf(target.getClass()))); }
@Test public void testToString() throws Exception { DummyServiceImpl target = new DummyServiceImpl(); DummyService dummyService = StopWatchProxy.newJdkProxy(target); int words = dummyService.countWords(parsedPage); assertThat("wordCount", words, is(27)); assertThat(dummyService.toString(), is(target.toString())); assertThat(dummyService.hashCode(), is(target.hashCode())); }