@SuppressWarnings("unchecked") private void doTestCache() { for (int i = 0; i < toConstruct.length; i++) { Class cl = toConstruct[i]; Object x = ReflectionUtils.newInstance(cl, null); Object y = ReflectionUtils.newInstance(cl, null); assertEquals(cl, x.getClass()); assertEquals(cl, y.getClass()); } }
@Test public void testCantCreate() { try { ReflectionUtils.newInstance(NoDefaultCtor.class, null); fail("invalid call should fail"); } catch (RuntimeException rte) { assertEquals(NoSuchMethodException.class, rte.getCause().getClass()); } }
@SuppressWarnings("unchecked") @Test public void testCacheDoesntLeak() throws Exception { int iterations = 9999; // very fast, but a bit less reliable - bigger numbers force GC for (int i = 0; i < iterations; i++) { URLClassLoader loader = new URLClassLoader(new URL[0], getClass().getClassLoader()); Class cl = Class.forName("org.apache.hadoop.util.TestReflectionUtils$LoadedInChild", false, loader); Object o = ReflectionUtils.newInstance(cl, null); assertEquals(cl, o.getClass()); } System.gc(); assertTrue(cacheSize() + " too big", cacheSize() < iterations); }