CachedComponentClassLoader extends ClassLoader { public Class<?> loadClass(String name) throws ClassNotFoundException { synchronized (getClassLoadingLock(name)) { String packageName = name.replaceAll(".[^.]*$", ""); if(super.getPackage(packageName) == null) { super.definePackage(packageName, null, null, null, null, null, null, null); } Class<?> clazz = findLoadedClass(name); if(clazz != null) return clazz; clazz = cachedClasses.get(name); if(clazz != null) { return clazz; } if(byteCode.containsKey(name)) clazz = findClass(name); if(clazz != null) { return clazz; } clazz = super.loadClass(name); if(clazz == null) throw new ClassNotFoundException("Class '" + name + "' not found"); return clazz; } } CachedComponentClassLoader(final ClassLoader parentClassLoader); Class<?> loadClass(String name); InputStream getResourceAsStream(String name); void initialize(final String componentFolder); MicroPipelineComponent newInstance(final String id, final String name, final String version, final Properties properties); boolean isManagedComponent(final String name, final String version); String getManagedComponentKey(final String name, final String version); Map<String, ComponentDescriptor> getManagedComponents();  }
@Test public void testConstructor_withNullParentClassloader() throws ClassNotFoundException { CachedComponentClassLoader cl = new CachedComponentClassLoader(null); Assert.assertNotNull("The instance must not be null", cl); Class<?> c = cl.loadClass("java.lang.String"); Assert.assertEquals("Values must be equal as the classloader switches to bootstrap loader if parent is missing", c.getName(), "java.lang.String"); }
@Test public void testLoadClass_withEmptyInput() { CachedComponentClassLoader cl = new CachedComponentClassLoader(CachedComponentClassLoader.class.getClassLoader()); try { cl.loadClass(""); Assert.fail("No such class exist"); } catch(ClassNotFoundException e) { } }
@Test public void testLoadClass_withInvalidClassReference() { CachedComponentClassLoader cl = new CachedComponentClassLoader(CachedComponentClassLoader.class.getClassLoader()); try { cl.loadClass("java.lang.StringUnknown"); Assert.fail("No such class exist"); } catch(ClassNotFoundException e) { } }
@Test public void testLoadClass_withStandardClassReference() throws ClassNotFoundException { CachedComponentClassLoader cl = new CachedComponentClassLoader(CachedComponentClassLoader.class.getClassLoader()); Class<?> c = cl.loadClass("java.lang.String"); Assert.assertEquals("Values must be equal", c.getName(), "java.lang.String"); }
@Test public void testPackage_withStandardClassReference() throws ClassNotFoundException { CachedComponentClassLoader cl = new CachedComponentClassLoader(CachedComponentClassLoader.class.getClassLoader()); Class<?> c = cl.loadClass("java.lang.String"); Assert.assertEquals("Values must be equal", c.getPackage().getName(), "java.lang"); }
@Test public void testPackage_withExternalClassReference() throws ClassNotFoundException { CachedComponentClassLoader cl = new CachedComponentClassLoader(CachedComponentClassLoader.class.getClassLoader()); Class<?> c = cl.loadClass("com.fasterxml.jackson.databind.JsonNode"); Assert.assertEquals("Values must be equal", c.getPackage().getName(), "com.fasterxml.jackson.databind"); }