[tests] improves not-null/pattern instrumenters' enclosing class tests

This commit is contained in:
Roman Shevchenko
2018-09-24 11:19:02 +02:00
parent 50eea286ac
commit e5b08cc0a9
12 changed files with 32 additions and 11 deletions
@@ -1,9 +1,18 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
import org.jetbrains.annotations.NotNull;
public class EnclosingClass {
public static Object main() {
public static Object fromStatic() {
return new Object() {
void foo(@NotNull String s) {}
void foo(@NotNull String s) { }
};
}
public Object fromInstance() {
return new Object() {
boolean foo(@NotNull String s) {
return s.contains(EnclosingClass.this.toString());
}
};
}
}
@@ -199,7 +199,7 @@ public class NotNullVerifyingInstrumenterTest {
Object instance = test.newInstance();
verifyCallThrowsException("@FooAnno method TypeUseOnlyAnnotations.foo1 must not return null", instance, test.getMethod("foo1"));
verifyCallThrowsException("Argument 0 for @FooAnno parameter of TypeUseOnlyAnnotations.foo2 must not be null", instance, test.getMethod("foo2", String.class), (String)null);
test.getMethod("foo3", List.class).invoke(instance, (List)null);
test.getMethod("foo3", List.class).invoke(instance, new Object[]{null});
}
@Test
@@ -231,8 +231,10 @@ public class NotNullVerifyingInstrumenterTest {
@Test
public void testEnclosingClass() throws Exception {
Class<?> testClass = prepareTest();
Object obj = testClass.getMethod("main").invoke(null);
assertEquals(testClass, obj.getClass().getEnclosingClass());
Object obj1 = testClass.getMethod("fromStatic").invoke(null);
assertEquals(testClass, obj1.getClass().getEnclosingClass());
Object obj2 = testClass.getMethod("fromInstance").invoke(testClass.newInstance());
assertEquals(testClass, obj2.getClass().getEnclosingClass());
}
@Test
@@ -270,7 +272,7 @@ public class NotNullVerifyingInstrumenterTest {
return prepareTest(false, AnnotationUtil.NOT_NULL);
}
private Class<?> prepareTest(boolean withDebugInfo, String... notNullAnnos) throws IOException {
private Class<?> prepareTest(boolean withDebugInfo, String... notNullAnnotations) throws IOException {
String base = JavaTestUtil.getJavaTestDataPath() + "/compiler/notNullVerification/";
String baseClassName = PlatformTestUtil.getTestName(testName.getMethodName(), false);
String javaPath = (base + baseClassName) + ".java";
@@ -296,7 +298,7 @@ public class NotNullVerifyingInstrumenterTest {
FailSafeClassReader reader = new FailSafeClassReader(content, 0, content.length);
ClassWriter writer = new ClassWriter(reader, ClassWriter.COMPUTE_FRAMES);
modified |= NotNullVerifyingInstrumenter.processClassFile(reader, writer, notNullAnnos);
modified |= NotNullVerifyingInstrumenter.processClassFile(reader, writer, notNullAnnotations);
byte[] instrumented = writer.toByteArray();
String className = FileUtil.getNameWithoutExtension(fileName);
@@ -95,8 +95,10 @@ class PatternInstrumenterTest {
@Test fun enclosingClass() {
val testClass = loadClass()
val obj = testClass.getMethod("enclosingClass").invoke(null)
assertEquals(testClass, obj::class.java.enclosingClass)
val obj1 = testClass.getMethod("enclosingStatic").invoke(null)
assertEquals(testClass, obj1::class.java.enclosingClass)
val obj2 = testClass.getMethod("enclosingInstance").invoke(testClass.newInstance())
assertEquals(testClass, obj2::class.java.enclosingClass)
}
@Test fun capturedParam() {
@@ -41,9 +41,17 @@ public class TestClass {
a.get("-");
}
public static Object enclosingClass() {
public static Object enclosingStatic() {
return new Object() {
void foo(@Pattern("\\d+") String s) { }
public void foo(@Pattern("\\d+") String s) { }
};
}
public Object enclosingInstance() {
return new Object() {
public boolean foo(@Pattern("\\d+") String s) {
return s.contains(TestClass.this.toString());
}
};
}