private void assertStandardClassLoaderCannotFindClass(String className) { try { testObject.findClass(className); fail("Should not be able to find this class in standard ClassLoader"); } catch (Exception e) { } }
@Test public void findClassDoesNotTryToRedefineClassesTwice() throws Exception { assertStandardClassLoaderCannotFindClass(testClassName); final byte[] loadedClassContent = FileUtils.readFileToByteArray(testClassFile); InMemoryClassFile classFileObject = new InMemoryClassFile(testClassName) { @Override public byte[] getBytes() { return loadedClassContent; }; }; testObject.addClass(testClassName, classFileObject); testObject.findClass(testClassName); testObject.findClass(testClassName); }
@Test public void findClassTriesTheHashMapFirst() throws Exception { assertStandardClassLoaderCannotFindClass(testClassName); final byte[] loadedClassContent = FileUtils.readFileToByteArray(testClassFile); InMemoryClassFile classFileObject = new InMemoryClassFile(testClassName) { @Override public byte[] getBytes() { return loadedClassContent; }; }; testObject.addClass(testClassName, classFileObject); Class<?> clazz = testObject.findClass(testClassName); assertNotNull(clazz); }
@Test public void ifClassCannotBeFoundAtAllThrowARuntimeExceptionWrappedClassNotFoundException() throws Exception { try { testObject.findClass(testClassName); fail("Should have thrown a runtime exception"); } catch (RuntimeException e) { } }
@Test public void findClassTriesFilesystemSecond() throws Exception { File folderHierarchy = new File(currentDirectory, testPackageName.replace(".", "/")); folderHierarchy.mkdirs(); assertStandardClassLoaderCannotFindClass(testClassName); FileUtils.copyFile(testClassFile, new File(folderHierarchy, testFileName)); Class<?> clazz = testObject.findClass(testClassName); assertNotNull(clazz); }