| /******************************************************************************* |
| * Copyright (c) 2019, 2020 IBM Corporation and others. |
| * |
| * This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.jdt.ui.tests.quickfix; |
| |
| import java.util.ArrayList; |
| import java.util.Map; |
| |
| import org.junit.After; |
| import org.junit.Ignore; |
| import org.junit.Rule; |
| import org.junit.Test; |
| |
| import org.eclipse.jdt.testplugin.JavaProjectHelper; |
| |
| import org.eclipse.jface.text.contentassist.ICompletionProposal; |
| |
| import org.eclipse.jdt.core.ICompilationUnit; |
| import org.eclipse.jdt.core.IJavaProject; |
| import org.eclipse.jdt.core.IPackageFragment; |
| import org.eclipse.jdt.core.IPackageFragmentRoot; |
| import org.eclipse.jdt.core.JavaCore; |
| import org.eclipse.jdt.core.dom.CompilationUnit; |
| |
| import org.eclipse.jdt.internal.corext.util.Messages; |
| |
| import org.eclipse.jdt.ui.tests.core.rules.Java15ProjectTestSetup; |
| import org.eclipse.jdt.ui.tests.core.rules.ProjectTestSetup; |
| import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; |
| import org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal; |
| |
| import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages; |
| |
| public class QuickFixTestPreview extends QuickFixTest { |
| |
| @Rule |
| public ProjectTestSetup projectsetup= new Java15ProjectTestSetup(true); |
| |
| private IJavaProject fJProject1; |
| |
| private IPackageFragmentRoot fSourceFolder; |
| |
| private static String MODULE_INFO_FILE_CONTENT = "" |
| + "module test {\n" |
| + "}\n"; |
| |
| @After |
| public void tearDown() throws Exception { |
| if (fJProject1 != null) { |
| JavaProjectHelper.delete(fJProject1); |
| } |
| } |
| |
| @Test |
| public void testEnablePreviewsAndOpenCompilerPropertiesProposals() throws Exception { |
| fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin"); |
| fJProject1.setRawClasspath(projectsetup.getDefaultClasspath(), null); |
| JavaProjectHelper.set15CompilerOptions(fJProject1, false); |
| |
| fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src"); |
| |
| IPackageFragment def= fSourceFolder.createPackageFragment("", false, null); |
| def.createCompilationUnit("module-info.java", MODULE_INFO_FILE_CONTENT, false, null); |
| |
| IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); |
| String test= "" |
| + "package test;\n" |
| + "public record Rec1() {\n" |
| + "}\n"; |
| ICompilationUnit cu= pack.createCompilationUnit("Rec1.java", test, false, null); |
| |
| CompilationUnit astRoot= getASTRoot(cu); |
| ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot, 1, null); |
| |
| assertNumberOfProposals(proposals, 2); |
| String label1= CorrectionMessages.PreviewFeaturesSubProcessor_enable_preview_features; |
| assertProposalExists(proposals, label1); |
| String label2= CorrectionMessages.PreviewFeaturesSubProcessor_open_compliance_properties_page_enable_preview_features; |
| assertProposalExists(proposals, label2); |
| } |
| |
| @Test |
| public void testRecordSuppressWarningsProposals() throws Exception { |
| fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin"); |
| fJProject1.setRawClasspath(projectsetup.getDefaultClasspath(), null); |
| JavaProjectHelper.set15CompilerOptions(fJProject1, true); |
| |
| Map<String, String> options= fJProject1.getOptions(false); |
| options.put(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.WARNING); |
| fJProject1.setOptions(options); |
| |
| fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src"); |
| |
| |
| IPackageFragment def= fSourceFolder.createPackageFragment("", false, null); |
| def.createCompilationUnit("module-info.java", MODULE_INFO_FILE_CONTENT, false, null); |
| |
| IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); |
| String test= "" |
| + "package test;\n" |
| + "public record Rec1() {\n" |
| + "}\n"; |
| ICompilationUnit cu= pack.createCompilationUnit("Rec1.java", test, false, null); |
| |
| CompilationUnit astRoot= getASTRoot(cu); |
| ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot, 1, null); |
| |
| assertNumberOfProposals(proposals, 2); |
| String label= Messages.format(CorrectionMessages.SuppressWarningsSubProcessor_suppress_warnings_label, new String[] { "preview", "Rec1" }); |
| assertProposalExists(proposals, label); |
| |
| CUCorrectionProposal proposal= (CUCorrectionProposal) proposals.get(0); |
| String preview= getPreviewContent(proposal); |
| |
| String expected= "" |
| + "package test;\n" |
| + "@SuppressWarnings(\"preview\")\n" |
| + "public record Rec1() {\n" |
| + "}\n"; |
| |
| assertEqualStringsIgnoreOrder(new String[] { preview }, new String[] { expected }); |
| } |
| |
| @Ignore("See bug 562103 comment 4") |
| @Test |
| public void testGetNeedHigherComplianceProposalsAndEnablePreviewsProposal() throws Exception { |
| fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin"); |
| fJProject1.setRawClasspath(projectsetup.getDefaultClasspath(), null); |
| JavaProjectHelper.set15CompilerOptions(fJProject1, false); |
| |
| fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src"); |
| |
| |
| IPackageFragment def= fSourceFolder.createPackageFragment("", false, null); |
| def.createCompilationUnit("module-info.java", MODULE_INFO_FILE_CONTENT, false, null); |
| |
| IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); |
| String test= "" |
| + "package test;\n" |
| + "public record Rec1() {\n" |
| + "}\n"; |
| ICompilationUnit cu= pack.createCompilationUnit("Rec1.java", test, false, null); |
| |
| CompilationUnit astRoot= getASTRoot(cu); |
| ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot, 1, null); |
| |
| assertNumberOfProposals(proposals, 1); |
| String label1= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_change_project_compliance_description, "14"); |
| String label2= CorrectionMessages.PreviewFeaturesSubProcessor_enable_preview_features; |
| String label= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_combine_two_quickfixes, new String[] {label1, label2}); |
| assertProposalExists(proposals, label); |
| } |
| |
| @Test |
| public void testNoEnablePreviewProposal() throws Exception { |
| fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin"); |
| fJProject1.setRawClasspath(projectsetup.getDefaultClasspath(), null); |
| JavaProjectHelper.set15CompilerOptions(fJProject1, true); |
| |
| fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src"); |
| |
| |
| IPackageFragment def= fSourceFolder.createPackageFragment("", false, null); |
| def.createCompilationUnit("module-info.java", MODULE_INFO_FILE_CONTENT, false, null); |
| |
| IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); |
| String test= "" |
| + "package test;\n" |
| + "public record Rec() {\n" |
| + "}\n"; |
| ICompilationUnit cu= pack.createCompilationUnit("Rec.java", test, false, null); |
| |
| CompilationUnit astRoot= getASTRoot(cu); |
| ArrayList<ICompletionProposal> proposals= collectAllCorrections(cu, astRoot, 0); |
| |
| assertNumberOfProposals(proposals, 0); |
| } |
| |
| @Test |
| public void testAddSealedMissingClassModifierProposal() throws Exception { |
| fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin"); |
| fJProject1.setRawClasspath(projectsetup.getDefaultClasspath(), null); |
| JavaProjectHelper.set15CompilerOptions(fJProject1, true); |
| |
| Map<String, String> options= fJProject1.getOptions(false); |
| options.put(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE); |
| fJProject1.setOptions(options); |
| |
| fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src"); |
| IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null); |
| |
| String test= "" + |
| "package test;\n" + |
| "\n" + |
| "public sealed class Shape permits Square {}\n" + |
| "\n" + |
| "class Square extends Shape {}\n"; |
| ICompilationUnit cu= pack1.createCompilationUnit("Shape.java",test, false, null); |
| |
| CompilationUnit astRoot= getASTRoot(cu); |
| ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot, 1); |
| assertNumberOfProposals(proposals, 3); |
| assertCorrectLabels(proposals); |
| |
| CUCorrectionProposal proposal= (CUCorrectionProposal) proposals.get(0); |
| String preview1= getPreviewContent(proposal); |
| |
| String expected1= "" + |
| "package test;\n" + |
| "\n" + |
| "public sealed class Shape permits Square {}\n" + |
| "\n" + |
| "final class Square extends Shape {}\n"; |
| |
| proposal= (CUCorrectionProposal) proposals.get(1); |
| String preview2= getPreviewContent(proposal); |
| |
| String expected2= "" + |
| "package test;\n" + |
| "\n" + |
| "public sealed class Shape permits Square {}\n" + |
| "\n" + |
| "non-sealed class Square extends Shape {}\n"; |
| |
| proposal= (CUCorrectionProposal) proposals.get(2); |
| String preview3= getPreviewContent(proposal); |
| |
| String expected3= "" + |
| "package test;\n" + |
| "\n" + |
| "public sealed class Shape permits Square {}\n" + |
| "\n" + |
| "sealed class Square extends Shape {}\n"; |
| |
| assertEqualStringsIgnoreOrder(new String[] { preview1, preview2, preview3 }, new String[] { expected1, expected2, expected3 }); |
| |
| } |
| |
| @Test |
| public void testAddSealedMissingInterfaceModifierProposal() throws Exception { |
| fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin"); |
| fJProject1.setRawClasspath(projectsetup.getDefaultClasspath(), null); |
| JavaProjectHelper.set15CompilerOptions(fJProject1, true); |
| |
| Map<String, String> options= fJProject1.getOptions(false); |
| options.put(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE); |
| fJProject1.setOptions(options); |
| |
| fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src"); |
| IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null); |
| |
| String test = "" + |
| "package test;\n" + |
| "\n" + |
| "public sealed interface Shape permits Square {}\n" + |
| "\n" + |
| "interface Square extends Shape {}\n"; |
| ICompilationUnit cu= pack1.createCompilationUnit("Shape.java", test, false, null); |
| |
| CompilationUnit astRoot= getASTRoot(cu); |
| ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot, 1); |
| assertNumberOfProposals(proposals, 2); |
| assertCorrectLabels(proposals); |
| |
| CUCorrectionProposal proposal= (CUCorrectionProposal) proposals.get(0); |
| String preview1= getPreviewContent(proposal); |
| |
| String expected1= "" + |
| "package test;\n" + |
| "\n" + |
| "public sealed interface Shape permits Square {}\n" + |
| "\n" + |
| "sealed interface Square extends Shape {}\n"; |
| |
| proposal= (CUCorrectionProposal) proposals.get(1); |
| String preview2= getPreviewContent(proposal); |
| |
| String expected2= "" + |
| "package test;\n" + |
| "\n" + |
| "public sealed interface Shape permits Square {}\n" + |
| "\n" + |
| "non-sealed interface Square extends Shape {}\n"; |
| |
| assertEqualStringsIgnoreOrder(new String[] { preview1, preview2 }, new String[] { expected1, expected2 }); |
| |
| } |
| |
| @Test |
| public void testRecordConstructorIncorrectParamsProposal1() throws Exception { |
| fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin"); |
| fJProject1.setRawClasspath(projectsetup.getDefaultClasspath(), null); |
| JavaProjectHelper.set15CompilerOptions(fJProject1, true); |
| |
| Map<String, String> options= fJProject1.getOptions(false); |
| options.put(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE); |
| fJProject1.setOptions(options); |
| |
| fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src"); |
| IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null); |
| |
| String test= "" + |
| "package test;\n" + |
| "\n" + |
| "public record Rec1(int a){\n" + |
| "\n" + |
| " public static void main(String[] args) {\n" + |
| " Rec1 abc = new Rec1();\n" + |
| " }\n" + |
| "}\n"; |
| ICompilationUnit cu= pack1.createCompilationUnit("Rec1.java", test, false, null); |
| |
| CompilationUnit astRoot= getASTRoot(cu); |
| ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot, 1); |
| assertNumberOfProposals(proposals, 2); |
| assertCorrectLabels(proposals); |
| |
| CUCorrectionProposal proposal= (CUCorrectionProposal) proposals.get(0); |
| String preview1= getPreviewContent(proposal); |
| |
| String expected1= "" + |
| "package test;\n" + |
| "\n" + |
| "public record Rec1(int a){\n" + |
| "\n" + |
| " public static void main(String[] args) {\n" + |
| " Rec1 abc = new Rec1(a);\n" + |
| " }\n" + |
| "}\n"; |
| |
| proposal= (CUCorrectionProposal) proposals.get(1); |
| String preview2= getPreviewContent(proposal); |
| |
| String expected2= "" + |
| "package test;\n" + |
| "\n" + |
| "public record Rec1(int a){\n" + |
| "\n" + |
| " /**\n" + |
| " * \n" + |
| " */\n" + |
| " public Rec1() {\n" + |
| " // TODO Auto-generated constructor stub\n" + |
| " }\n" + |
| "\n" + |
| " public static void main(String[] args) {\n" + |
| " Rec1 abc = new Rec1();\n" + |
| " }\n" + |
| "}\n"; |
| |
| assertEqualStringsIgnoreOrder(new String[] { preview1, preview2 }, new String[] { expected1, expected2 }); |
| } |
| |
| @Test |
| public void testRecordConstructorIncorrectParamsProposal2() throws Exception { |
| fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin"); |
| fJProject1.setRawClasspath(projectsetup.getDefaultClasspath(), null); |
| JavaProjectHelper.set15CompilerOptions(fJProject1, true); |
| |
| Map<String, String> options= fJProject1.getOptions(false); |
| options.put(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE); |
| fJProject1.setOptions(options); |
| |
| fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src"); |
| IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null); |
| |
| String test= "" + |
| "package test;\n" + |
| "\n" + |
| "public record Rec1(int a){\n" + |
| "\n" + |
| " public static void main(String[] args) {\n" + |
| " Rec1 abc = new Rec1(10, 20);\n" + |
| " }\n" + |
| "}\n"; |
| ICompilationUnit cu= pack1.createCompilationUnit("Rec1.java", test, false, null); |
| |
| CompilationUnit astRoot= getASTRoot(cu); |
| ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot, 1); |
| assertNumberOfProposals(proposals, 2); |
| assertCorrectLabels(proposals); |
| |
| CUCorrectionProposal proposal= (CUCorrectionProposal) proposals.get(0); |
| String preview1= getPreviewContent(proposal); |
| |
| String expected1= "" + |
| "package test;\n" + |
| "\n" + |
| "public record Rec1(int a){\n" + |
| "\n" + |
| " public static void main(String[] args) {\n" + |
| " Rec1 abc = new Rec1(10);\n" + |
| " }\n" + |
| "}\n"; |
| |
| proposal= (CUCorrectionProposal) proposals.get(1); |
| String preview2= getPreviewContent(proposal); |
| |
| String expected2= "" + |
| "package test;\n" + |
| "\n" + |
| "public record Rec1(int a){\n" + |
| "\n" + |
| " /**\n" + |
| " * @param i\n" + |
| " * @param j\n" + |
| " */\n" + |
| " public Rec1(int i, int j) {\n" + |
| " // TODO Auto-generated constructor stub\n" + |
| " }\n" + |
| "\n" + |
| " public static void main(String[] args) {\n" + |
| " Rec1 abc = new Rec1(10, 20);\n" + |
| " }\n" + |
| "}\n"; |
| |
| assertEqualStringsIgnoreOrder(new String[] { preview1, preview2 }, new String[] { expected1, expected2 }); |
| } |
| |
| @Test |
| public void testRecordConstructorIncorrectParamsProposal3() throws Exception { |
| fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin"); |
| fJProject1.setRawClasspath(projectsetup.getDefaultClasspath(), null); |
| JavaProjectHelper.set15CompilerOptions(fJProject1, true); |
| |
| Map<String, String> options= fJProject1.getOptions(false); |
| options.put(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE); |
| fJProject1.setOptions(options); |
| |
| fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src"); |
| IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null); |
| |
| String test= "" + |
| "package test;\n" + |
| "\n" + |
| "public record Rec1(int a){\n" + |
| "\n" + |
| " public static void main(String[] args) {\n" + |
| " Rec1 abc = new Rec1(\"str\");\n" + |
| " }\n" + |
| "}\n"; |
| ICompilationUnit cu= pack1.createCompilationUnit("Rec1.java", test, false, null); |
| |
| CompilationUnit astRoot= getASTRoot(cu); |
| ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot, 1); |
| assertNumberOfProposals(proposals, 1); |
| assertCorrectLabels(proposals); |
| |
| CUCorrectionProposal proposal= (CUCorrectionProposal) proposals.get(0); |
| String preview1= getPreviewContent(proposal); |
| |
| String expected1= "" + |
| "package test;\n" + |
| "\n" + |
| "public record Rec1(int a){\n" + |
| "\n" + |
| " /**\n" + |
| " * @param string\n" + |
| " */\n" + |
| " public Rec1(String string) {\n" + |
| " // TODO Auto-generated constructor stub\n" + |
| " }\n" + |
| "\n" + |
| " public static void main(String[] args) {\n" + |
| " Rec1 abc = new Rec1(\"str\");\n" + |
| " }\n" + |
| "}\n"; |
| |
| assertEqualStringsIgnoreOrder(new String[] { preview1 }, new String[] { expected1 }); |
| } |
| |
| @Test |
| public void testRecordCanonicalConstructordUninitializedFieldProposal() throws Exception { |
| fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin"); |
| fJProject1.setRawClasspath(projectsetup.getDefaultClasspath(), null); |
| JavaProjectHelper.set15CompilerOptions(fJProject1, true); |
| |
| Map<String, String> options= fJProject1.getOptions(false); |
| options.put(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE); |
| fJProject1.setOptions(options); |
| |
| fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src"); |
| IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null); |
| |
| String test= "" + |
| "package test;\n" + |
| "\n" + |
| "public record Rec1(int a, int b){\n" + |
| "\n" + |
| " public Rec1(int a, int b) {\n" + |
| " \n" + |
| " }\n\n" + |
| " public Rec1(int a) {\n" + |
| " this(a, a);\n" + |
| " }\n\n" + |
| " public Rec1(int a, int b, int c) {\n" + |
| " this(a, b+c);\n" + |
| " }\n\n" + |
| "}\n"; |
| ICompilationUnit cu= pack1.createCompilationUnit("Rec1.java", test, false, null); |
| |
| CompilationUnit astRoot= getASTRoot(cu); |
| ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot, 2); |
| assertNumberOfProposals(proposals, 1); |
| assertCorrectLabels(proposals); |
| |
| CUCorrectionProposal proposal= (CUCorrectionProposal) proposals.get(0); |
| String preview1= getPreviewContent(proposal); |
| |
| String expected1= "" + |
| "package test;\n" + |
| "\n" + |
| "public record Rec1(int a, int b){\n" + |
| "\n" + |
| " public Rec1(int a, int b) {\n" + |
| " this.a = 0;\n" + |
| " \n" + |
| " }\n\n" + |
| " public Rec1(int a) {\n" + |
| " this(a, a);\n" + |
| " }\n\n" + |
| " public Rec1(int a, int b, int c) {\n" + |
| " this(a, b+c);\n" + |
| " }\n\n" + |
| "}\n"; |
| |
| assertEqualStringsIgnoreOrder(new String[] { preview1 }, new String[] { expected1 }); |
| } |
| } |