FileDocCategorySizeDatePackage
AndroidJarLoaderTest.javaAPI DocAndroid 1.5 API7149Wed May 06 22:41:10 BST 2009com.android.ide.eclipse.adt.sdk

AndroidJarLoaderTest

public class AndroidJarLoaderTest extends TestCase
Unit Test for {@link AndroidJarLoader}. Uses the classes jar.example.Class1/Class2 stored in tests/data/jar_example.jar.

Fields Summary
private AndroidJarLoader
mFrameworkClassLoader
Constructors Summary
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.HashMapgetPrivateClassCache()
Retrieves the private mFrameworkClassLoader.mClassCache field using reflection.

throws
NoSuchFieldException
throws
SecurityException
throws
IllegalAccessException
throws
IllegalArgumentException

        Field field = AndroidJarLoader.class.getDeclaredField("mClassCache");  //$NON-NLS-1$
        field.setAccessible(true);
        return (HashMap<String, Class<?>>) field.get(mFrameworkClassLoader);
    
private java.util.HashMapgetPrivateEntryCache()
Retrieves the private mFrameworkClassLoader.mEntryCache field using reflection.

throws
NoSuchFieldException
throws
SecurityException
throws
IllegalAccessException
throws
IllegalArgumentException

        Field field = AndroidJarLoader.class.getDeclaredField("mEntryCache");  //$NON-NLS-1$
        field.setAccessible(true);
        return (HashMap<String, byte[]>) field.get(mFrameworkClassLoader);
    
public voidsetUp()
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 voidtearDown()

        mFrameworkClassLoader = null;
        System.gc();
    
public final voidtestFindClass_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 voidtestFindClass_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 voidtestFindClassesDerivingFrom()

        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 voidtestPreLoadClasses()
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 voidtestPreLoadClasses_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());