Methods Summary |
---|
private java.lang.Class | _findClass(AndroidJarLoader jarLoader, java.lang.String name)call the protected method findClass
Method findClassMethod = AndroidJarLoader.class.getDeclaredMethod(
"findClass", String.class); //$NON-NLS-1$
findClassMethod.setAccessible(true);
try {
return (Class<?>)findClassMethod.invoke(jarLoader, name);
}
catch (InvocationTargetException e) {
throw (Exception)e.getCause();
}
|
private java.util.HashMap | getPrivateClassCache()Retrieves the private mFrameworkClassLoader.mClassCache field using reflection.
Field field = AndroidJarLoader.class.getDeclaredField("mClassCache"); //$NON-NLS-1$
field.setAccessible(true);
return (HashMap<String, Class<?>>) field.get(mFrameworkClassLoader);
|
private java.util.HashMap | getPrivateEntryCache()Retrieves the private mFrameworkClassLoader.mEntryCache field using reflection.
Field field = AndroidJarLoader.class.getDeclaredField("mEntryCache"); //$NON-NLS-1$
field.setAccessible(true);
return (HashMap<String, byte[]>) field.get(mFrameworkClassLoader);
|
public void | setUp()Creates an instance of {@link AndroidJarLoader} on our test data JAR
String jarfilePath = AdtTestData.getInstance().getTestFilePath("jar_example.jar"); //$NON-NLS-1$
mFrameworkClassLoader = new AndroidJarLoader(jarfilePath);
|
public void | tearDown()
mFrameworkClassLoader = null;
System.gc();
|
public final void | testFindClass_classFound()Finds a class we just preloaded. It should work.
Class<?> c = _findClass(mFrameworkClassLoader, "jar.example.Class2"); //$NON-NLS-1$
assertEquals("jar.example.Class2", c.getName()); //$NON-NLS-1$
HashMap<String, Class<?>> map = getPrivateClassCache();
assertTrue(map.containsKey("jar.example.Class1")); //$NON-NLS-1$
assertTrue(map.containsKey("jar.example.Class2")); //$NON-NLS-1$
assertEquals(2, map.size());
|
public final void | testFindClass_classNotFound()Trying to find a class that we fail to preload should throw a CNFE.
try {
// Will throw ClassNotFoundException
_findClass(mFrameworkClassLoader, "not.a.valid.ClassName"); //$NON-NLS-1$
} catch (ClassNotFoundException e) {
// check the message in the CNFE
assertEquals("not.a.valid.ClassName", e.getMessage()); //$NON-NLS-1$
return;
}
// Exception not thrown - this is a failure
fail("Expected ClassNotFoundException not thrown");
|
public final void | testFindClassesDerivingFrom()
HashMap<String, ArrayList<IClassDescriptor>> found =
mFrameworkClassLoader.findClassesDerivingFrom("jar.example.", new String[] { //$NON-NLS-1$
"jar.example.Class1", //$NON-NLS-1$
"jar.example.Class2" }); //$NON-NLS-1$
assertTrue(found.containsKey("jar.example.Class1")); //$NON-NLS-1$
assertTrue(found.containsKey("jar.example.Class2")); //$NON-NLS-1$
assertEquals(2, found.size());
// Only Class2 derives from Class1..
// Class1 and Class1$InnerStaticClass1 derive from Object and are thus ignored.
// Class1$InnerClass2 should never be seen either.
assertEquals("jar.example.Class2", //$NON-NLS-1$
found.get("jar.example.Class1").get(0).getCanonicalName()); //$NON-NLS-1$
assertEquals(1, found.get("jar.example.Class1").size()); //$NON-NLS-1$
assertEquals(0, found.get("jar.example.Class2").size()); //$NON-NLS-1$
|
public final void | testPreLoadClasses()Preloads classes. They should load just fine.
mFrameworkClassLoader.preLoadClasses("jar.example.", null, null); //$NON-NLS-1$
HashMap<String, Class<?>> map = getPrivateClassCache();
assertEquals(0, map.size());
HashMap<String,byte[]> data = getPrivateEntryCache();
assertTrue(data.containsKey("jar.example.Class1")); //$NON-NLS-1$
assertTrue(data.containsKey("jar.example.Class2")); //$NON-NLS-1$
assertTrue(data.containsKey("jar.example.Class1$InnerStaticClass1")); //$NON-NLS-1$
assertTrue(data.containsKey("jar.example.Class1$InnerClass2")); //$NON-NLS-1$
assertEquals(4, data.size());
|
public final void | testPreLoadClasses_classNotFound()Preloads a class not in the JAR. Preloading does nothing in this case.
mFrameworkClassLoader.preLoadClasses("not.a.package.", null, null); //$NON-NLS-1$
HashMap<String, Class<?>> map = getPrivateClassCache();
assertEquals(0, map.size());
HashMap<String,byte[]> data = getPrivateEntryCache();
assertEquals(0, data.size());
|