Skip to content

Commit

Permalink
Broke up PackageInfoLookup test, added tests for package-info.class f…
Browse files Browse the repository at this point in the history
…iles.

	Change on 2017/07/28 by tball <tball@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=163514225
  • Loading branch information
tomball authored and kstanger committed Aug 16, 2017
1 parent 4d97901 commit 642d8dc
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 25 deletions.
Expand Up @@ -217,7 +217,47 @@ protected CompilationUnit compileAsClassFile(String name, String source,
String... flags) throws IOException { String... flags) throws IOException {
assertTrue("Classfile translation not enabled", options.translateClassfiles()); assertTrue("Classfile translation not enabled", options.translateClassfiles());


String path = name.replace('.', '/') + ".java"; InputFile input = createClassFile(name, source, flags);
if (input == null) {
// Class file compilation failed.
return null;
}
int errors = ErrorUtil.errorCount();
CompilationUnit unit = parser.parse(input);
if (ErrorUtil.errorCount() > errors) {
int newErrorCount = ErrorUtil.errorCount() - errors;
String info = String.format(
"%d test compilation error%s", newErrorCount, (newErrorCount == 1 ? "" : "s"));
failWithMessages(info, ErrorUtil.getErrorMessages().subList(errors, ErrorUtil.errorCount()));
}
return unit;
}

/**
* Compiles Java source to a JVM class file.
*
* @param typeName the name of the type being declared
* @param source the source code
* @return the InputFile defining the class file
*/
protected InputFile createClassFile(String typeName, String source) throws IOException {
String tempPath = tempDir.getAbsolutePath();
List<String> classpath = getComGoogleDevtoolsJ2objcPath();
classpath.add(0, tempPath);
return createClassFile(typeName, source, "-d", tempPath, "-cp", String.join(":", classpath));
}

/**
* Compiles Java source to a JVM class file.
*
* @param typeName the name of the type being declared
* @param source the source code
* @param flags which javac flags to use
* @return the InputFile defining the class file
*/
protected InputFile createClassFile(String typeName, String source, String... flags)
throws IOException {
String path = typeName.replace('.', '/') + ".java";
File srcFile = new File(tempDir, path); File srcFile = new File(tempDir, path);
srcFile.getParentFile().mkdirs(); srcFile.getParentFile().mkdirs();
try (FileWriter fw = new FileWriter(srcFile)) { try (FileWriter fw = new FileWriter(srcFile)) {
Expand All @@ -241,16 +281,7 @@ protected CompilationUnit compileAsClassFile(String name, String source,
File classFile = new File(tempDir, path.replace(".java", ".class")); File classFile = new File(tempDir, path.replace(".java", ".class"));
assertTrue(classFile.exists()); assertTrue(classFile.exists());


InputFile input = new RegularInputFile(classFile.getAbsolutePath(), name); return new RegularInputFile(classFile.getAbsolutePath(), typeName);
int errors = ErrorUtil.errorCount();
CompilationUnit unit = parser.parse(input);
if (ErrorUtil.errorCount() > errors) {
int newErrorCount = ErrorUtil.errorCount() - errors;
String info = String.format(
"%d test compilation error%s", newErrorCount, (newErrorCount == 1 ? "" : "s"));
failWithMessages(info, ErrorUtil.getErrorMessages().subList(errors, ErrorUtil.errorCount()));
}
return unit;
} }


protected static List<String> getComGoogleDevtoolsJ2objcPath() { protected static List<String> getComGoogleDevtoolsJ2objcPath() {
Expand Down
Expand Up @@ -26,34 +26,92 @@
*/ */
public class PackageInfoLookupTest extends GenerationTest { public class PackageInfoLookupTest extends GenerationTest {


public void testReflectionSupportAnnotation() throws IOException { public void testFullReflectionSupportSetValue() throws IOException {
addSourceFile("@ReflectionSupport(value = ReflectionSupport.Level.FULL) package foo;" addSourceFile("@ReflectionSupport(value = ReflectionSupport.Level.FULL) package foo;"
+ "import com.google.j2objc.annotations.ReflectionSupport;", "foo/package-info.java"); + "import com.google.j2objc.annotations.ReflectionSupport;", "foo/package-info.java");
CompilationUnit unit = translateType("foo.A", "package foo; public class A {}"); CompilationUnit unit = translateType("foo.A", "package foo; public class A {}");
PackageInfoLookup packageInfoLookup = unit.getEnv().options().getPackageInfoLookup(); PackageInfoLookup packageInfoLookup = unit.getEnv().options().getPackageInfoLookup();
assert packageInfoLookup.getReflectionSupportLevel("foo") == ReflectionSupport.Level.FULL; assertSame(ReflectionSupport.Level.FULL, packageInfoLookup.getReflectionSupportLevel("foo"));
}


public void testFullReflectionSupport() throws IOException {
addSourceFile("@ReflectionSupport(ReflectionSupport.Level.FULL) package bar;" addSourceFile("@ReflectionSupport(ReflectionSupport.Level.FULL) package bar;"
+ "import com.google.j2objc.annotations.*;", "bar/package-info.java"); + "import com.google.j2objc.annotations.*;", "bar/package-info.java");
unit = translateType("bar.A", "package bar; public class A {}"); CompilationUnit unit = translateType("bar.A", "package bar; public class A {}");
packageInfoLookup = unit.getEnv().options().getPackageInfoLookup(); PackageInfoLookup packageInfoLookup = unit.getEnv().options().getPackageInfoLookup();
assert packageInfoLookup.getReflectionSupportLevel("bar") == ReflectionSupport.Level.FULL; assertSame(ReflectionSupport.Level.FULL, packageInfoLookup.getReflectionSupportLevel("bar"));
}


public void testNativeOnlyReflectionSupport() throws IOException {
addSourceFile("@com.google.j2objc.annotations.ReflectionSupport" addSourceFile("@com.google.j2objc.annotations.ReflectionSupport"
+ "(com.google.j2objc.annotations.ReflectionSupport.Level.NATIVE_ONLY) package baz;", + "(com.google.j2objc.annotations.ReflectionSupport.Level.NATIVE_ONLY) package baz;",
"baz/package-info.java"); "baz/package-info.java");
unit = translateType("baz.A", "package baz; public class A {}"); CompilationUnit unit = translateType("baz.A", "package baz; public class A {}");
packageInfoLookup = unit.getEnv().options().getPackageInfoLookup(); PackageInfoLookup packageInfoLookup = unit.getEnv().options().getPackageInfoLookup();
assert assertSame(ReflectionSupport.Level.NATIVE_ONLY,
packageInfoLookup.getReflectionSupportLevel("baz") == ReflectionSupport.Level.NATIVE_ONLY; packageInfoLookup.getReflectionSupportLevel("baz"));
}


// Verify that ReflectionSupport annotation can be parsed from class files // Verify that ReflectionSupport annotation can be parsed from a compiled jar file.
public void testReflectionSupportInJarFile() throws IOException {
String jarFilePath = getResourceAsFile("packageInfoLookupTest.jar"); String jarFilePath = getResourceAsFile("packageInfoLookupTest.jar");
options.fileUtil().getClassPathEntries().add(jarFilePath); options.fileUtil().getClassPathEntries().add(jarFilePath);
unit = translateType("com.google.test.packageInfoLookupTest.A", CompilationUnit unit = translateType("com.google.test.packageInfoLookupTest.A",
"package com.google.test.packageInfoLookupTest; public class A {}"); "package com.google.test.packageInfoLookupTest; public class A {}");
packageInfoLookup = unit.getEnv().options().getPackageInfoLookup(); PackageInfoLookup packageInfoLookup = unit.getEnv().options().getPackageInfoLookup();
assert packageInfoLookup.getReflectionSupportLevel(unit.getPackage().getName().toString()) assertSame(ReflectionSupport.Level.FULL,
== ReflectionSupport.Level.FULL; packageInfoLookup.getReflectionSupportLevel(unit.getPackage().getName().toString()));
}

public void testFullReflectionSupportSetValueCompiled() throws IOException {
createClassFile("foo.package-info",
"@ReflectionSupport(value = ReflectionSupport.Level.FULL) package foo;"
+ "import com.google.j2objc.annotations.ReflectionSupport;");
CompilationUnit unit = translateType("foo.A", "package foo; public class A {}");
PackageInfoLookup packageInfoLookup = unit.getEnv().options().getPackageInfoLookup();
assertSame(ReflectionSupport.Level.FULL, packageInfoLookup.getReflectionSupportLevel("foo"));
}

public void testFullReflectionSupportCompiled() throws IOException {
createClassFile("bar.package-info",
"@ReflectionSupport(ReflectionSupport.Level.FULL) package bar;"
+ "import com.google.j2objc.annotations.ReflectionSupport;");
CompilationUnit unit = translateType("bar.A", "package bar; public class A {}");
PackageInfoLookup packageInfoLookup = unit.getEnv().options().getPackageInfoLookup();
assertSame(ReflectionSupport.Level.FULL, packageInfoLookup.getReflectionSupportLevel("bar"));
}

public void testNativeOnlyReflectionSupportCompiled() throws IOException {
createClassFile("baz.package-info",
"@com.google.j2objc.annotations.ReflectionSupport"
+ "(com.google.j2objc.annotations.ReflectionSupport.Level.NATIVE_ONLY) package baz;");
CompilationUnit unit = translateType("baz.A", "package baz; public class A {}");
PackageInfoLookup packageInfoLookup = unit.getEnv().options().getPackageInfoLookup();
assertSame(ReflectionSupport.Level.NATIVE_ONLY,
packageInfoLookup.getReflectionSupportLevel("baz"));
}

public void testPackageRenameCompiled() throws IOException {
createClassFile("foo.package-info",
"@ObjectiveCName(\"XYZ\") package foo; "
+ "import com.google.j2objc.annotations.ObjectiveCName;");
String translation = translateSourceFile("package foo; public class A {}", "foo.A", "foo/A.h");
assertTranslation(translation, "@interface XYZA");
}

// Verify that ParametersAreNonnullByDefault is not set on packages by default.
public void testParametersAreNonnullByDefaultNotSet() throws IOException {
CompilationUnit unit = translateType("foo.A", "package foo; public class A {}");
PackageInfoLookup packageInfoLookup = unit.getEnv().options().getPackageInfoLookup();
assertFalse(packageInfoLookup.hasParametersAreNonnullByDefault("foo"));
}

public void testParametersAreNonnullByDefault() throws IOException {
createClassFile("bar.package-info",
"@ParametersAreNonnullByDefault package bar;"
+ "import javax.annotation.ParametersAreNonnullByDefault;");
CompilationUnit unit = translateType("bar.A", "package bar; public class A {}");
PackageInfoLookup packageInfoLookup = unit.getEnv().options().getPackageInfoLookup();
assertTrue(packageInfoLookup.hasParametersAreNonnullByDefault("bar"));
} }
} }

0 comments on commit 642d8dc

Please sign in to comment.