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

AsmGeneratorTest

public class AsmGeneratorTest extends Object
Unit tests for some methods of {@link AsmGenerator}.

Fields Summary
private MockLog
mLog
private ArrayList
mOsJarPath
private String
mOsDestJar
private File
mTempFile
private static final String
JAVA_CLASS_NAME
Constructors Summary
Methods Summary
private java.lang.StringclassReaderToClassName(org.objectweb.asm.ClassReader classReader)

        if (classReader == null) {
            return null;
        } else {
            return classReader.getClassName().replace('/", '.");
        }
    
private voidparseZip(java.lang.String jarPath, java.util.Map classes, java.util.Map filesFound)


            ZipFile zip = new ZipFile(jarPath);
            Enumeration<? extends ZipEntry> entries = zip.entries();
            ZipEntry entry;
            while (entries.hasMoreElements()) {
                entry = entries.nextElement();
                if (entry.getName().endsWith(".class")) {
                    ClassReader cr = new ClassReader(zip.getInputStream(entry));
                    String className = classReaderToClassName(cr);
                    classes.put(className, cr);
                } else {
                    filesFound.put(entry.getName(), zip.getInputStream(entry));
                }
            }

    
public voidsetUp()


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

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

        mTempFile = File.createTempFile("mock", ".jar");
        mOsDestJar = mTempFile.getAbsolutePath();
        mTempFile.deleteOnExit();
    
public voidtearDown()

        if (mTempFile != null) {
            mTempFile.delete();
            mTempFile = null;
        }
    
public voidtestClassExclusion()

        ICreateInfo ci = new ICreateInfo() {
            @Override
            public Class<?>[] getInjectedClasses() {
                return new Class<?>[0];
            }

            @Override
            public String[] getDelegateMethods() {
                return new String[0];
            }

            @Override
            public String[] getDelegateClassNatives() {
                return new String[0];
            }

            @Override
            public String[] getOverriddenMethods() {
                // methods to force override
                return new String[0];
            }

            @Override
            public String[] getRenamedClasses() {
                // classes to rename (so that we can replace them)
                return new String[0];
            }

            @Override
            public String[] getJavaPkgClasses() {
                // classes to refactor (so that we can replace them)
                return new String[0];
            }

            @Override
            public Set<String> getExcludedClasses() {
                Set<String> set = new HashSet<String>(2);
                set.add("mock_android.dummy.InnerTest");
                set.add("java.lang.JavaClass");
                return set;
            }

            @Override
            public String[] getDeleteReturns() {
                // methods deleted from their return type.
                return new String[0];
            }
        };

        AsmGenerator agen = new AsmGenerator(mLog, mOsDestJar, ci);
        Set<String> excludedClasses = ci.getExcludedClasses();
        AsmAnalyzer aa = new AsmAnalyzer(mLog, mOsJarPath, agen,
                null,                 // derived from
                new String[] {        // include classes
                        "**"
                },
                excludedClasses,
                new String[] {        /* include files */
                        "mock_android/data/data*"
                });
        aa.analyze();
        agen.generate();
        Map<String, ClassReader> output = new TreeMap<String, ClassReader>();
        Map<String, InputStream> filesFound = new TreeMap<String, InputStream>();
        parseZip(mOsDestJar, output, filesFound);
        for (String s : output.keySet()) {
            assertFalse(excludedClasses.contains(s));
        }
        assertArrayEquals(new String[] {"mock_android/data/dataFile"},
                filesFound.keySet().toArray());
    
public voidtestClassRefactoring()

        ICreateInfo ci = new ICreateInfo() {
            @Override
            public Class<?>[] getInjectedClasses() {
                // classes to inject in the final JAR
                return new Class<?>[] {
                        com.android.tools.layoutlib.create.dataclass.JavaClass.class
                };
            }

            @Override
            public String[] getDelegateMethods() {
                return new String[0];
            }

            @Override
            public String[] getDelegateClassNatives() {
                return new String[0];
            }

            @Override
            public String[] getOverriddenMethods() {
                // methods to force override
                return new String[0];
            }

            @Override
            public String[] getRenamedClasses() {
                // classes to rename (so that we can replace them)
                return new String[0];
            }

            @Override
            public String[] getJavaPkgClasses() {
             // classes to refactor (so that we can replace them)
                return new String[] {
                        "java.lang.JavaClass", "com.android.tools.layoutlib.create.dataclass.JavaClass",
                };
            }

            @Override
            public Set<String> getExcludedClasses() {
                return Collections.singleton("java.lang.JavaClass");
            }

            @Override
            public String[] getDeleteReturns() {
                 // methods deleted from their return type.
                return new String[0];
            }
        };

        AsmGenerator agen = new AsmGenerator(mLog, mOsDestJar, ci);

        AsmAnalyzer aa = new AsmAnalyzer(mLog, mOsJarPath, agen,
                null,                 // derived from
                new String[] {        // include classes
                    "**"
                },
                new HashSet<String>(1),
                new String[] {        /* include files */
                    "mock_android/data/data*"
                });
        aa.analyze();
        agen.generate();
        Map<String, ClassReader> output = new TreeMap<String, ClassReader>();
        Map<String, InputStream> filesFound = new TreeMap<String, InputStream>();
        parseZip(mOsDestJar, output, filesFound);
        boolean injectedClassFound = false;
        for (ClassReader cr: output.values()) {
            TestClassVisitor cv = new TestClassVisitor();
            cr.accept(cv, 0);
            injectedClassFound |= cv.mInjectedClassFound;
        }
        assertTrue(injectedClassFound);
        assertArrayEquals(new String[] {"mock_android/data/dataFile"},
                filesFound.keySet().toArray());
    
public voidtestClassRenaming()


        ICreateInfo ci = new ICreateInfo() {
            @Override
            public Class<?>[] getInjectedClasses() {
                // classes to inject in the final JAR
                return new Class<?>[0];
            }

            @Override
            public String[] getDelegateMethods() {
                return new String[0];
            }

            @Override
            public String[] getDelegateClassNatives() {
                return new String[0];
            }

            @Override
            public String[] getOverriddenMethods() {
                // methods to force override
                return new String[0];
            }

            @Override
            public String[] getRenamedClasses() {
                // classes to rename (so that we can replace them)
                return new String[] {
                        "mock_android.view.View", "mock_android.view._Original_View",
                        "not.an.actual.ClassName", "anoter.fake.NewClassName",
                };
            }

            @Override
            public String[] getJavaPkgClasses() {
              return new String[0];
            }

            @Override
            public Set<String> getExcludedClasses() {
                return null;
            }

            @Override
            public String[] getDeleteReturns() {
                 // methods deleted from their return type.
                return new String[0];
            }
        };

        AsmGenerator agen = new AsmGenerator(mLog, mOsDestJar, ci);

        AsmAnalyzer aa = new AsmAnalyzer(mLog, mOsJarPath, agen,
                null,                 // derived from
                new String[] {        // include classes
                    "**"
                },
                new HashSet<String>(0) /* excluded classes */,
                new String[]{} /* include files */);
        aa.analyze();
        agen.generate();

        Set<String> notRenamed = agen.getClassesNotRenamed();
        assertArrayEquals(new String[] { "not/an/actual/ClassName" }, notRenamed.toArray());