FileDocCategorySizeDatePackage
AsmAnalyzerTest.javaAPI DocAndroid 5.1 API9903Thu Mar 12 22:22:44 GMT 2015com.android.tools.layoutlib.create

AsmAnalyzerTest

public class AsmAnalyzerTest extends Object
Unit tests for some methods of {@link AsmAnalyzer}.

Fields Summary
private MockLog
mLog
private ArrayList
mOsJarPath
private AsmAnalyzer
mAa
Constructors Summary
Methods Summary
public voidsetUp()

        mLog = new MockLog();
        URL url = this.getClass().getClassLoader().getResource("data/mock_android.jar");

        mOsJarPath = new ArrayList<String>();
        mOsJarPath.add(url.getFile());

        Set<String> excludeClasses = new HashSet<String>(1);
        excludeClasses.add("java.lang.JavaClass");

        String[] includeFiles = new String[]{"mock_android/data/data*"};
        mAa = new AsmAnalyzer(mLog, mOsJarPath, null /* gen */, null /* deriveFrom */,
                null /* includeGlobs */, excludeClasses, includeFiles);
    
public voidtearDown()

    
public voidtestDependencyVisitor()


        Map<String, ClassReader> zipClasses = new TreeMap<String, ClassReader>();
        Map<String, InputStream> filesFound = new TreeMap<String, InputStream>();

        mAa.parseZip(mOsJarPath, zipClasses, filesFound);
        TreeMap<String, ClassReader> keep = new TreeMap<String, ClassReader>();
        TreeMap<String, ClassReader> new_keep = new TreeMap<String, ClassReader>();
        TreeMap<String, ClassReader> in_deps = new TreeMap<String, ClassReader>();
        TreeMap<String, ClassReader> out_deps = new TreeMap<String, ClassReader>();

        ClassReader cr = mAa.findClass("mock_android.widget.LinearLayout", zipClasses, keep);
        DependencyVisitor visitor = mAa.getVisitor(zipClasses, keep, new_keep, in_deps, out_deps);

        // get first level dependencies
        cr.accept(visitor, 0 /* flags */);

        assertArrayEquals(new String[] {
                "mock_android.util.EmptyArray",
                "mock_android.view.ViewGroup",
                "mock_android.widget.LinearLayout$LayoutParams",
            },
            out_deps.keySet().toArray());

        in_deps.putAll(out_deps);
        out_deps.clear();

        // get second level dependencies
        for (ClassReader cr2 : in_deps.values()) {
            cr2.accept(visitor, 0 /* flags */);
        }

        assertArrayEquals(new String[] {
                "mock_android.view.View",
                "mock_android.view.ViewGroup$LayoutParams",
                "mock_android.view.ViewGroup$MarginLayoutParams",
            },
            out_deps.keySet().toArray());

        in_deps.putAll(out_deps);
        out_deps.clear();

        // get third level dependencies (there are none)
        for (ClassReader cr2 : in_deps.values()) {
            cr2.accept(visitor, 0 /* flags */);
        }
        keep.putAll(new_keep);

        assertArrayEquals(new String[] { }, out_deps.keySet().toArray());
        assertArrayEquals(new String[] {
                "mock_android.widget.LinearLayout",
        }, keep.keySet().toArray());
    
public voidtestFindClass()


        Map<String, ClassReader> zipClasses = new TreeMap<String, ClassReader>();
        Map<String, InputStream> filesFound = new TreeMap<String, InputStream>();

        mAa.parseZip(mOsJarPath, zipClasses, filesFound);
        TreeMap<String, ClassReader> found = new TreeMap<String, ClassReader>();

        ClassReader cr = mAa.findClass("mock_android.view.ViewGroup$LayoutParams",
                zipClasses, found);

        assertNotNull(cr);
        assertEquals("mock_android/view/ViewGroup$LayoutParams", cr.getClassName());
        assertArrayEquals(new String[] { "mock_android.view.ViewGroup$LayoutParams" },
                found.keySet().toArray());
        assertArrayEquals(new ClassReader[] { cr }, found.values().toArray());
    
public voidtestFindClassesDerivingFrom()


        Map<String, ClassReader> zipClasses = new TreeMap<String, ClassReader>();
        Map<String, InputStream> filesFound = new TreeMap<String, InputStream>();

        mAa.parseZip(mOsJarPath, zipClasses, filesFound);
        TreeMap<String, ClassReader> found = new TreeMap<String, ClassReader>();

        mAa.findClassesDerivingFrom("mock_android.view.View", zipClasses, found);

        assertArrayEquals(new String[] {
                "mock_android.view.View",
                "mock_android.view.ViewGroup",
                "mock_android.widget.LinearLayout",
                "mock_android.widget.TableLayout",
            },
            found.keySet().toArray());

        for (String key : found.keySet()) {
            ClassReader value = found.get(key);
            assertNotNull("No value for " + key, value);
            assertEquals(key, AsmAnalyzer.classReaderToClassName(value));
        }
    
public voidtestFindGlobs()


        Map<String, ClassReader> zipClasses = new TreeMap<String, ClassReader>();
        Map<String, InputStream> filesFound = new TreeMap<String, InputStream>();

        mAa.parseZip(mOsJarPath, zipClasses, filesFound);
        TreeMap<String, ClassReader> found = new TreeMap<String, ClassReader>();

        // this matches classes, a package match returns nothing
        found.clear();
        mAa.findGlobs("mock_android.view", zipClasses, found);

        assertArrayEquals(new String[] { },
            found.keySet().toArray());

        // a complex glob search. * is a search pattern that matches names, not dots
        mAa.findGlobs("mock_android.*.*Group$*Layout*", zipClasses, found);

        assertArrayEquals(new String[] {
                "mock_android.view.ViewGroup$LayoutParams",
                "mock_android.view.ViewGroup$MarginLayoutParams"
            },
            found.keySet().toArray());

        // a complex glob search. ** is a search pattern that matches names including dots
        mAa.findGlobs("mock_android.**Group*", zipClasses, found);

        assertArrayEquals(new String[] {
                "mock_android.view.ViewGroup",
                "mock_android.view.ViewGroup$LayoutParams",
                "mock_android.view.ViewGroup$MarginLayoutParams"
            },
            found.keySet().toArray());

        // matches a single class
        found.clear();
        mAa.findGlobs("mock_android.view.View", zipClasses, found);

        assertArrayEquals(new String[] {
                "mock_android.view.View"
            },
            found.keySet().toArray());

        // matches everyting inside the given package but not sub-packages
        found.clear();
        mAa.findGlobs("mock_android.view.*", zipClasses, found);

        assertArrayEquals(new String[] {
                "mock_android.view.View",
                "mock_android.view.ViewGroup",
                "mock_android.view.ViewGroup$LayoutParams",
                "mock_android.view.ViewGroup$MarginLayoutParams"
            },
            found.keySet().toArray());

        for (String key : found.keySet()) {
            ClassReader value = found.get(key);
            assertNotNull("No value for " + key, value);
            assertEquals(key, AsmAnalyzer.classReaderToClassName(value));
        }
    
public voidtestParseZip()


        Map<String, ClassReader> map = new TreeMap<String, ClassReader>();
        Map<String, InputStream> filesFound = new TreeMap<String, InputStream>();

        mAa.parseZip(mOsJarPath, map, filesFound);

        assertArrayEquals(new String[] {
                "java.lang.JavaClass",
                "mock_android.dummy.InnerTest",
                "mock_android.dummy.InnerTest$DerivingClass",
                "mock_android.dummy.InnerTest$MyGenerics1",
                "mock_android.dummy.InnerTest$MyIntEnum",
                "mock_android.dummy.InnerTest$MyStaticInnerClass",
                "mock_android.dummy.InnerTest$NotStaticInner1",
                "mock_android.dummy.InnerTest$NotStaticInner2",
                "mock_android.util.EmptyArray",
                "mock_android.view.View",
                "mock_android.view.ViewGroup",
                "mock_android.view.ViewGroup$LayoutParams",
                "mock_android.view.ViewGroup$MarginLayoutParams",
                "mock_android.widget.LinearLayout",
                "mock_android.widget.LinearLayout$LayoutParams",
                "mock_android.widget.TableLayout",
                "mock_android.widget.TableLayout$LayoutParams"
            },
            map.keySet().toArray());
        assertArrayEquals(new String[] {"mock_android/data/dataFile"},
            filesFound.keySet().toArray());