Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSarika Sinha2020-07-23 12:19:41 -0400
committerSarika Sinha2020-07-23 12:19:41 -0400
commita54dd93dfd9cb86422fa0e73f4570cf2f5f42dd2 (patch)
tree753f35488b93c48a73cf73892ecd866d7fb4f93d
parent7f0b7a275edf8d5988fd058f6d1144e100d714e0 (diff)
downloadeclipse.jdt.core-a54dd93dfd9cb86422fa0e73f4570cf2f5f42dd2.tar.gz
eclipse.jdt.core-a54dd93dfd9cb86422fa0e73f4570cf2f5f42dd2.tar.xz
eclipse.jdt.core-a54dd93dfd9cb86422fa0e73f4570cf2f5f42dd2.zip
Bug 564904 - [15] [Tests] Building Annotation tests for recordY20200725-1200
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_15Test.java106
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingRecordDeclarationTest.java171
2 files changed, 259 insertions, 18 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_15Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_15Test.java
index c0b5ef76e6..1803fe4c23 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_15Test.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_15Test.java
@@ -17,6 +17,8 @@
*******************************************************************************/
package org.eclipse.jdt.core.tests.dom;
+import static org.junit.Assert.assertNotEquals;
+
import java.util.List;
import org.eclipse.core.runtime.CoreException;
@@ -360,11 +362,64 @@ public class ASTConverter_15Test extends ConverterTestSetup {
}
}
- public void _testRecord008() throws CoreException {
+ public void testRecord008() throws CoreException {
if (!isJRE15) {
System.err.println("Test "+getName()+" requires a JRE 15");
return;
}
+ String contents = "import java.lang.annotation.*;\n" +
+ "@Target (ElementType.METHOD)\n" +
+ "@interface MyAnnot {}\n" +
+ "public record X(@MyAnnot int lo) {\n" +
+ " public int lo() {\n" +
+ " return this.lo;\n" +
+ " }\n" +
+ "\n" +
+ "}\n";
+
+ this.workingCopy = getWorkingCopy("/Converter_15/src/X.java", true/*resolve*/);
+ IJavaProject javaProject = this.workingCopy.getJavaProject();
+ String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true);
+ try {
+ javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
+ javaProject.setOption(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE);
+ ASTNode node = buildAST(
+ contents,
+ this.workingCopy);
+ assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType());
+ CompilationUnit compilationUnit = (CompilationUnit) node;
+ assertProblemsSize(compilationUnit, 0);
+ node = ((AbstractTypeDeclaration)compilationUnit.types().get(0));
+ assertNotEquals("Not a Record Declaration", ASTNode.RECORD_DECLARATION, node.getNodeType());
+
+
+
+ } finally {
+ javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old);
+ }
+ }
+
+ public void testRecord009() throws CoreException {
+ if (!isJRE15) {
+ System.err.println("Test "+getName()+" requires a JRE 15");
+ return;
+ }
+ String elementType = "package java.lang.annotation;\n" +
+ "public enum ElementType {\n" +
+ " TYPE,\n" +
+ " FIELD,\n" +
+ " METHOD,\n" +
+ " PARAMETER,\n" +
+ " CONSTRUCTOR,\n" +
+ " LOCAL_VARIABLE,\n" +
+ " ANNOTATION_TYPE,\n" +
+ " PACKAGE,\n" +
+ " TYPE_PARAMETER,\n" +
+ " TYPE_USE,\n" +
+ " MODULE,\n" +
+ " RECORD_COMPONENT\n" +
+ "}\n";
+ ICompilationUnit workingCopy2 = getWorkingCopy("/Converter_15/src/java/lang/annotation/ElementType.java", true/*resolve*/);
String contents = "import java.lang.annotation.ElementType;\n" +
"import java.lang.annotation.Target;\n" +
"record X(@MyAnnot int lo) {\n" +
@@ -381,6 +436,9 @@ public class ASTConverter_15Test extends ConverterTestSetup {
try {
javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
javaProject.setOption(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE);
+ buildAST(
+ elementType,
+ workingCopy2);
ASTNode node = buildAST(
contents,
this.workingCopy);
@@ -393,25 +451,39 @@ public class ASTConverter_15Test extends ConverterTestSetup {
List<SingleVariableDeclaration> recordComponents = record.recordComponents();
assertEquals("There should be 1 record component", 1, recordComponents.size());
SingleVariableDeclaration recordComponent = recordComponents.get(0);
- assertEquals("Record component name should be lo","lo" , recordComponent.getName().toString());
- assertEquals("Record component type is int" , "int", recordComponent.getType().toString());
- IVariableBinding resolveBinding = recordComponent.resolveBinding();
+ SimpleName recordComponentName = recordComponent.getName();
+ assertEquals("Record component name should be lo","lo" , recordComponentName.toString());
+ ITypeBinding resolveTypeBinding = recordComponentName.resolveTypeBinding();
+ assertEquals("Record component type is int" , "int",resolveTypeBinding.getName() );
+ IVariableBinding resolveBinding = (IVariableBinding)recordComponentName.resolveBinding();
assertEquals("Record component binding" , true, resolveBinding.isRecordComponent());
- MarkerAnnotation annotation = (MarkerAnnotation)recordComponent.modifiers().get(0);
- assertEquals("Record component annotation name should be MyAnnot","@MyAnnot" , annotation.toString());
- assertEquals("Record component binding should not have annotation",0 , resolveBinding.getAnnotations().length);
-
} finally {
javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old);
}
}
- public void _testRecord009() throws CoreException {
+ public void _testRecord010() throws CoreException {
if (!isJRE15) {
System.err.println("Test "+getName()+" requires a JRE 15");
return;
}
+ String elementType = "package java.lang.annotation;\n" +
+ "public enum ElementType {\n" +
+ " TYPE,\n" +
+ " FIELD,\n" +
+ " METHOD,\n" +
+ " PARAMETER,\n" +
+ " CONSTRUCTOR,\n" +
+ " LOCAL_VARIABLE,\n" +
+ " ANNOTATION_TYPE,\n" +
+ " PACKAGE,\n" +
+ " TYPE_PARAMETER,\n" +
+ " TYPE_USE,\n" +
+ " MODULE,\n" +
+ " RECORD_COMPONENT\n" +
+ "}\n";
+ ICompilationUnit workingCopy2 = getWorkingCopy("/Converter_15/src/java/lang/annotation/ElementType.java", true/*resolve*/);
String contents = "import java.lang.annotation.ElementType;\n" +
"import java.lang.annotation.Target;\n" +
"record X(@MyAnnot int lo) {\n" +
@@ -429,16 +501,16 @@ public class ASTConverter_15Test extends ConverterTestSetup {
try {
javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
javaProject.setOption(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE);
- ASTNode node = buildAST(
+ buildAST(
+ elementType,
+ workingCopy2);
+ try {
+ buildAST(
contents,
this.workingCopy);
- assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType());
- CompilationUnit compilationUnit = (CompilationUnit) node;
- assertProblemsSize(compilationUnit, 0);
- node = ((AbstractTypeDeclaration)compilationUnit.types().get(0));
- assertEquals("Not a Record Declaration", ASTNode.RECORD_DECLARATION, node.getNodeType());
- //RecordDeclaration record = (RecordDeclaration)node;
- // test for error
+ } catch (Exception ex) {
+ // This can not be compiled
+ }
} finally {
javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old);
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingRecordDeclarationTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingRecordDeclarationTest.java
index 475b2058fc..25e5e511ba 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingRecordDeclarationTest.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingRecordDeclarationTest.java
@@ -29,6 +29,7 @@ import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.Javadoc;
+import org.eclipse.jdt.core.dom.MarkerAnnotation;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
@@ -1141,7 +1142,6 @@ public class ASTRewritingRecordDeclarationTest extends ASTRewritingTest {
assertTrue("Not a record", type instanceof RecordDeclaration);
RecordDeclaration record = (RecordDeclaration)type;
{ // change parameter type
- //rewrite.set((TypeParameter) record.typeParameters().get(0), TypeParameter.NAME_PROPERTY, astRoot.getAST().newSimpleName("Y"), null);
ListRewrite listRewrite= rewrite.getListRewrite(record, RecordDeclaration.TYPE_PARAMETERS_PROPERTY);
TypeParameter typeParameter= ast.newTypeParameter();
typeParameter.setName(ast.newSimpleName("Y"));
@@ -1212,6 +1212,175 @@ public class ASTRewritingRecordDeclarationTest extends ASTRewritingTest {
}
+ @SuppressWarnings("rawtypes")
+ public void testRecord_025() throws Exception {
+ if (checkAPILevel()) {
+ return;
+ }
+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("import java.lang.annotation.ElementType;\n");
+ buf.append("import java.lang.annotation.Target;\n");
+ buf.append("record X(@MyAnnot int lo) {\n");
+ buf.append(" public int lo() {\n");
+ buf.append(" return this.lo;\\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ buf.append("@Target({ElementType.FIELD})\n");
+ buf.append("@interface MyAnnot {}");
+ ICompilationUnit cu= pack1.createCompilationUnit("X.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= createAST(cu);
+ ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
+
+ assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
+ AbstractTypeDeclaration type= findAbstractTypeDeclaration(astRoot, "X");
+ assertTrue("Not a record", type instanceof RecordDeclaration);
+ RecordDeclaration record = (RecordDeclaration)type;
+ {
+ List recordComponents = record.recordComponents();
+ assertTrue("must be 1 parameter", recordComponents.size() == 1);
+ SingleVariableDeclaration recordComp = (SingleVariableDeclaration)recordComponents.get(0);
+ // remove annotation
+ ListRewrite listRewrite= rewrite.getListRewrite(recordComp, SingleVariableDeclaration.MODIFIERS2_PROPERTY);
+ listRewrite.remove((MarkerAnnotation)(recordComp.modifiers().get(0)), null);
+ }
+
+ String preview= evaluateRewrite(cu, rewrite);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("import java.lang.annotation.ElementType;\n");
+ buf.append("import java.lang.annotation.Target;\n");
+ buf.append("record X(int lo) {\n");
+ buf.append(" public int lo() {\n");
+ buf.append(" return this.lo;\\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ buf.append("@Target({ElementType.FIELD})\n");
+ buf.append("@interface MyAnnot {}");
+
+ assertEqualString(preview, buf.toString());
+
+ }
+
+ @SuppressWarnings("rawtypes")
+ public void testRecord_026() throws Exception {
+ if (checkAPILevel()) {
+ return;
+ }
+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("import java.lang.annotation.ElementType;\n");
+ buf.append("import java.lang.annotation.Target;\n");
+ buf.append("record X(@MyAnnot int lo) {\n");
+ buf.append(" public int lo() {\n");
+ buf.append(" return this.lo;\\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ buf.append("@Target({ElementType.FIELD})\n");
+ buf.append("@interface MyAnnot {}");
+ buf.append("@Target({ElementType.RECORD_COMPONENT})\n");
+ buf.append("@interface MyAnnotNew {}");
+ ICompilationUnit cu= pack1.createCompilationUnit("X.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= createAST(cu);
+ ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
+
+ assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
+ AbstractTypeDeclaration type= findAbstractTypeDeclaration(astRoot, "X");
+ assertTrue("Not a record", type instanceof RecordDeclaration);
+ RecordDeclaration record = (RecordDeclaration)type;
+ AST ast= astRoot.getAST();
+ {
+ List recordComponents = record.recordComponents();
+ assertTrue("must be 1 parameter", recordComponents.size() == 1);
+ SingleVariableDeclaration recordComp = (SingleVariableDeclaration)recordComponents.get(0);
+ // modify annotation
+ ListRewrite listRewrite= rewrite.getListRewrite(recordComp, SingleVariableDeclaration.MODIFIERS2_PROPERTY);
+ MarkerAnnotation annot= ast.newMarkerAnnotation();
+ annot.setTypeName(ast.newSimpleName("MyAnnotNew"));
+ listRewrite.replace((MarkerAnnotation)(recordComp.modifiers().get(0)), annot, null);
+ }
+
+ String preview= evaluateRewrite(cu, rewrite);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("import java.lang.annotation.ElementType;\n");
+ buf.append("import java.lang.annotation.Target;\n");
+ buf.append("record X(@MyAnnotNew int lo) {\n");
+ buf.append(" public int lo() {\n");
+ buf.append(" return this.lo;\\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ buf.append("@Target({ElementType.FIELD})\n");
+ buf.append("@interface MyAnnot {}");
+ buf.append("@Target({ElementType.RECORD_COMPONENT})\n");
+ buf.append("@interface MyAnnotNew {}");
+
+ assertEqualString(preview, buf.toString());
+
+ }
+
+ @SuppressWarnings("rawtypes")
+ public void testRecord_027() throws Exception {
+ if (checkAPILevel()) {
+ return;
+ }
+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("import java.lang.annotation.ElementType;\n");
+ buf.append("import java.lang.annotation.Target;\n");
+ buf.append("record X(int lo) {\n");
+ buf.append(" public int lo() {\n");
+ buf.append(" return this.lo;\\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ buf.append("@Target({ElementType.RECORD_COMPONENT})\n");
+ buf.append("@interface MyAnnot {}");
+ ICompilationUnit cu= pack1.createCompilationUnit("X.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= createAST(cu);
+ ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
+
+ assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
+ AbstractTypeDeclaration type= findAbstractTypeDeclaration(astRoot, "X");
+ assertTrue("Not a record", type instanceof RecordDeclaration);
+ RecordDeclaration record = (RecordDeclaration)type;
+ AST ast= astRoot.getAST();
+ {
+ List recordComponents = record.recordComponents();
+ assertTrue("must be 1 parameter", recordComponents.size() == 1);
+ SingleVariableDeclaration recordComp = (SingleVariableDeclaration)recordComponents.get(0);
+ // add annotation
+ ListRewrite listRewrite= rewrite.getListRewrite(recordComp, SingleVariableDeclaration.MODIFIERS2_PROPERTY);
+ MarkerAnnotation annot= ast.newMarkerAnnotation();
+ annot.setTypeName(ast.newSimpleName("MyAnnot"));
+ listRewrite.insertFirst(annot, null);
+ }
+
+ String preview= evaluateRewrite(cu, rewrite);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("import java.lang.annotation.ElementType;\n");
+ buf.append("import java.lang.annotation.Target;\n");
+ buf.append("record X(@MyAnnot int lo) {\n");
+ buf.append(" public int lo() {\n");
+ buf.append(" return this.lo;\\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ buf.append("@Target({ElementType.RECORD_COMPONENT})\n");
+ buf.append("@interface MyAnnot {}");
+
+ assertEqualString(preview, buf.toString());
+
+ }
+
}

Back to the top