From 62500c88492c2b03c85e80c31ab1e2ce1dd2a43d Mon Sep 17 00:00:00 2001 From: Carsten Hammer Date: Sat, 21 Sep 2024 11:34:35 +0200 Subject: [PATCH] copy tests back to usual test folder --- .../jdt/internal/common/HelperVisitor.java | 5 - org.eclipse.jdt.ui.tests/META-INF/MANIFEST.MF | 9 +- .../eclipse/jdt/ui/tests/AutomatedSuite.java | 4 + .../Java10/ExplicitEncodingCleanUpTest.java | 879 ++++++++++++++++++ .../Java10/QuickFixJava10TestSuite.java | 25 + .../Java8/ExplicitEncodingCleanUpTest.java | 866 +++++++++++++++++ .../Java8/QuickFixJava8TestSuite.java | 25 + .../quickfix/rules/AbstractEclipseJava.java | 482 ++++++++++ .../tests/quickfix/rules/EclipseJava10.java | 11 + .../tests/quickfix/rules/EclipseJava17.java | 11 + .../tests/quickfix/rules/EclipseJava18.java | 11 + .../ui/tests/quickfix/rules/EclipseJava8.java | 11 + .../ui/tests/quickfix/rules/EclipseJava9.java | 11 + 13 files changed, 2344 insertions(+), 6 deletions(-) create mode 100644 org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/ExplicitEncodingCleanUpTest.java create mode 100644 org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/QuickFixJava10TestSuite.java create mode 100644 org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java8/ExplicitEncodingCleanUpTest.java create mode 100644 org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java8/QuickFixJava8TestSuite.java create mode 100644 org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/AbstractEclipseJava.java create mode 100644 org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/EclipseJava10.java create mode 100644 org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/EclipseJava17.java create mode 100644 org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/EclipseJava18.java create mode 100644 org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/EclipseJava8.java create mode 100644 org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/EclipseJava9.java diff --git a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/common/HelperVisitor.java b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/common/HelperVisitor.java index 18e64087f5d..269eb1a1562 100644 --- a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/common/HelperVisitor.java +++ b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/common/HelperVisitor.java @@ -1519,11 +1519,6 @@ public BiConsumer addMethodInvocation(BiConsumer addMethodInvocation(String methodname, BiConsumer bc) { -// this.consumerdata.put(VisitorEnum.MethodInvocation, methodname); -// return consumermap.put(VisitorEnum.MethodInvocation, bc); -// } - public BiConsumer addMethodInvocation(String methodname, BiConsumer bc) { this.consumerdata.put(VisitorEnum.MethodInvocation, Map.ofEntries( new AbstractMap.SimpleEntry<>(METHODNAME, methodname) diff --git a/org.eclipse.jdt.ui.tests/META-INF/MANIFEST.MF b/org.eclipse.jdt.ui.tests/META-INF/MANIFEST.MF index 69dc24cc37d..8a298b01ea9 100644 --- a/org.eclipse.jdt.ui.tests/META-INF/MANIFEST.MF +++ b/org.eclipse.jdt.ui.tests/META-INF/MANIFEST.MF @@ -75,12 +75,19 @@ Require-Bundle: org.eclipse.jdt.astview, org.eclipse.compare.core, org.eclipse.compare, - org.eclipse.core.tests.harness + org.eclipse.core.tests.harness, + junit-jupiter-api Bundle-RequiredExecutionEnvironment: JavaSE-17 Eclipse-BundleShape: dir Bundle-ActivationPolicy: lazy Import-Package: org.assertj.core.api;version="3.24.2", org.junit.jupiter.api, + org.junit.jupiter.api.extension, + org.junit.jupiter.params, + org.junit.jupiter.params.aggregator, + org.junit.jupiter.params.converter, + org.junit.jupiter.params.provider, + org.junit.jupiter.params.support, org.junit.platform.suite.api, org.junit.platform.suite.commons;status=INTERNAL, org.junit.platform.suite.engine;status=INTERNAL diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/AutomatedSuite.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/AutomatedSuite.java index 740d3e6a6a8..ca360d2daa4 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/AutomatedSuite.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/AutomatedSuite.java @@ -38,6 +38,8 @@ import org.eclipse.jdt.ui.tests.model.ContentProviderTests; import org.eclipse.jdt.ui.tests.packageview.PackageExplorerTests; import org.eclipse.jdt.ui.tests.quickfix.QuickFixTestSuite; +import org.eclipse.jdt.ui.tests.quickfix.Java10.QuickFixJava10TestSuite; +import org.eclipse.jdt.ui.tests.quickfix.Java8.QuickFixJava8TestSuite; import org.eclipse.jdt.ui.tests.refactoring.RefactoringTests; import org.eclipse.jdt.ui.tests.search.SearchTest; import org.eclipse.jdt.ui.tests.views.SmokeViewsTest; @@ -56,6 +58,8 @@ CoreTests.class, CoreTestSuite.class, QuickFixTestSuite.class, + QuickFixJava8TestSuite.class, + QuickFixJava10TestSuite.class, NewJavaProjectWizardTest.class, NewTypeWizardTest.class, diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/ExplicitEncodingCleanUpTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/ExplicitEncodingCleanUpTest.java new file mode 100644 index 00000000000..8ffc88a33ff --- /dev/null +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/ExplicitEncodingCleanUpTest.java @@ -0,0 +1,879 @@ +package org.eclipse.jdt.ui.tests.quickfix.Java10; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IPackageFragment; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.eclipse.jdt.internal.corext.fix.CleanUpConstants; +import org.eclipse.jdt.ui.tests.quickfix.rules.AbstractEclipseJava; +import org.eclipse.jdt.ui.tests.quickfix.rules.EclipseJava10; + +public class ExplicitEncodingCleanUpTest { + + @RegisterExtension + AbstractEclipseJava context = new EclipseJava10(); + + enum ExplicitEncodingPatterns { + + CHARSET(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + Charset cs1= Charset.forName("UTF-8"); + Charset cs1b= Charset.forName("Utf-8"); + Charset cs2= Charset.forName("UTF-16"); + Charset cs3= Charset.forName("UTF-16BE"); + Charset cs4= Charset.forName("UTF-16LE"); + Charset cs5= Charset.forName("ISO-8859-1"); + Charset cs6= Charset.forName("US-ASCII"); + String result= cs1.toString(); + } + } + } + """, + +// """ +// package test1; +// +// import java.io.ByteArrayOutputStream; +// import java.io.InputStreamReader; +// import java.io.FileInputStream; +// import java.io.FileReader; +// import java.io.Reader; +// import java.nio.charset.Charset; +// import java.nio.charset.StandardCharsets; +// import java.io.FileNotFoundException; +// +// public class E1 { +// void method(String filename) { +// Charset cs1= StandardCharsets.UTF_8; +// Charset cs1b= StandardCharsets.UTF_8; +// Charset cs2= StandardCharsets.UTF_16; +// Charset cs3= StandardCharsets.UTF_16BE; +// Charset cs4= StandardCharsets.UTF_16LE; +// Charset cs5= StandardCharsets.ISO_8859_1; +// Charset cs6= StandardCharsets.US_ASCII; +// String result= cs1.toString(); +// } +// } +// } +// """), + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + Charset cs1= Charset.forName("UTF-8"); + Charset cs1b= Charset.forName("Utf-8"); + Charset cs2= Charset.forName("UTF-16"); + Charset cs3= Charset.forName("UTF-16BE"); + Charset cs4= Charset.forName("UTF-16LE"); + Charset cs5= Charset.forName("ISO-8859-1"); + Charset cs6= Charset.forName("US-ASCII"); + String result= cs1.toString(); + } + } + } + """), + BYTEARRAYOUTSTREAM(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + ByteArrayOutputStream ba2=new ByteArrayOutputStream(); + String result2=ba2.toString("UTF-8"); + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(Charset.defaultCharset()); + ByteArrayOutputStream ba2=new ByteArrayOutputStream(); + String result2=ba2.toString(StandardCharsets.UTF_8); + } + } + } + """), + FILEREADER(""" + package test1; + + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Reader is=new FileReader(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + FILEWRITER(""" + package test1; + + import java.io.FileWriter; + import java.io.Writer; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Writer fw=new FileWriter(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.FileWriter; + import java.io.OutputStreamWriter; + import java.io.Writer; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + import java.io.FileOutputStream; + + public class E1 { + void method(String filename) { + try { + Writer fw=new OutputStreamWriter(new FileOutputStream(filename), Charset.defaultCharset()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + INPUTSTREAMREADER( + """ + package test1; + + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + InputStreamReader is1=new InputStreamReader(new FileInputStream("file1.txt")); //$NON-NLS-1$ + InputStreamReader is2=new InputStreamReader(new FileInputStream("file2.txt"), "UTF-8"); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + InputStreamReader is1=new InputStreamReader(new FileInputStream("file1.txt"), Charset.defaultCharset()); //$NON-NLS-1$ + InputStreamReader is2=new InputStreamReader(new FileInputStream("file2.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + OUTPUTSTREAMWRITER( + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream("")); //$NON-NLS-1$ + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), "UTF-8"); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + CHANNELSNEWREADER(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.channels.ReadableByteChannel; + import java.nio.charset.StandardCharsets; + import java.nio.channels.Channels; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + ReadableByteChannel ch; + Reader r=Channels.newReader(ch,"UTF-8"); //$NON-NLS-1$ + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.channels.ReadableByteChannel; + import java.nio.charset.StandardCharsets; + import java.nio.channels.Channels; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + ReadableByteChannel ch; + Reader r=Channels.newReader(ch,StandardCharsets.UTF_8); //$NON-NLS-1$ + } + } + } + """), + CHANNELSNEWWRITER(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Writer; + import java.nio.channels.WritableByteChannel; + import java.nio.charset.StandardCharsets; + import java.nio.channels.Channels; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + WritableByteChannel ch; + Writer w=Channels.newWriter(ch,"UTF-8"); //$NON-NLS-1$ + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Writer; + import java.nio.channels.WritableByteChannel; + import java.nio.charset.StandardCharsets; + import java.nio.channels.Channels; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + WritableByteChannel ch; + Writer w=Channels.newWriter(ch,StandardCharsets.UTF_8); //$NON-NLS-1$ + } + } + } + """), + PRINTWRITER(""" + package test1; + + import java.io.PrintWriter; + import java.io.Writer; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Writer w=new PrintWriter(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.PrintWriter; + import java.io.Writer; + import java.nio.charset.Charset; + import java.io.BufferedWriter; + import java.io.FileNotFoundException; + import java.io.FileOutputStream; + import java.io.OutputStreamWriter; + + public class E1 { + void method(String filename) { + try { + Writer w=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), Charset.defaultCharset())); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + STRINGGETBYTES(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(); + byte[] bytes2= s.getBytes("UTF-8"); + System.out.println(bytes.length); + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(Charset.defaultCharset()); + byte[] bytes2= s.getBytes(StandardCharsets.UTF_8); + System.out.println(bytes.length); + } + } + } + """), + STRING(""" + package test1; + + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + + public class E5 { + + static void bla() throws FileNotFoundException, UnsupportedEncodingException { + byte[] b= {(byte)59}; + String s1=new String(b,"UTF-8"); + String s2=new String(b,0,1,"UTF-8"); + } + } + """, + + """ + package test1; + + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + import java.nio.charset.StandardCharsets; + + public class E5 { + + static void bla() throws FileNotFoundException, UnsupportedEncodingException { + byte[] b= {(byte)59}; + String s1=new String(b,StandardCharsets.UTF_8); + String s2=new String(b,0,1,StandardCharsets.UTF_8); + } + } + """), + PROPERTIESSTORETOXML(""" + package test1; + + import java.io.FileOutputStream; + import java.io.IOException; + import java.util.Properties; + + public class E1 { + static void blu() throws IOException { + Properties p=new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, null, "UTF-8"); + } + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, null); + } + } + } + """, + + """ + package test1; + + import java.io.FileOutputStream; + import java.io.IOException; + import java.nio.charset.StandardCharsets; + import java.util.Properties; + + public class E1 { + static void blu() throws IOException { + Properties p=new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, null, StandardCharsets.UTF_8); + } + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, null, StandardCharsets.UTF_8); + } + } + } + """), + URLDECODER(""" + package test1; + import java.io.UnsupportedEncodingException; + import java.net.URLDecoder; + + public class E2 { + + static void bla() throws UnsupportedEncodingException { + String url=URLDecoder.decode("asdf","UTF-8"); + String url2=URLDecoder.decode("asdf"); + } + } + """, """ + package test1; + import java.io.UnsupportedEncodingException; + import java.net.URLDecoder; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + + public class E2 { + + static void bla() throws UnsupportedEncodingException { + String url=URLDecoder.decode("asdf",StandardCharsets.UTF_8); + String url2=URLDecoder.decode("asdf", Charset.defaultCharset()); + } + } + """), + URLENCODER(""" + package test1; + import java.io.UnsupportedEncodingException; + import java.net.URLEncoder; + + public class E2 { + + static void bla() throws UnsupportedEncodingException { + String url=URLEncoder.encode("asdf","UTF-8"); + String url4=URLEncoder.encode("asdf"); + } + } + """, """ + package test1; + import java.io.UnsupportedEncodingException; + import java.net.URLEncoder; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + + public class E2 { + + static void bla() throws UnsupportedEncodingException { + String url=URLEncoder.encode("asdf",StandardCharsets.UTF_8); + String url4=URLEncoder.encode("asdf", Charset.defaultCharset()); + } + } + """), + SCANNER(""" + package test1; + import java.io.File; + import java.io.FileNotFoundException; + import java.util.Scanner; + + public class E3 { + + static void bla() throws FileNotFoundException { + Scanner s=new Scanner(new File("asdf"),"UTF-8"); + Scanner s2=new Scanner("asdf","UTF-8"); + Scanner s3=new Scanner("asdf"); + } + } + """, """ + package test1; + import java.io.File; + import java.io.FileNotFoundException; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.util.Scanner; + + public class E3 { + + static void bla() throws FileNotFoundException { + Scanner s=new Scanner(new File("asdf"),StandardCharsets.UTF_8); + Scanner s2=new Scanner("asdf",StandardCharsets.UTF_8); + Scanner s3=new Scanner("asdf", Charset.defaultCharset()); + } + } + """), + FORMATTER(""" + package test1; + import java.io.File; + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + import java.util.Formatter; + + public class E4 { + + static void bla() throws FileNotFoundException, UnsupportedEncodingException { + Formatter s=new Formatter(new File("asdf"),"UTF-8"); + } + } + """, """ + package test1; + import java.io.File; + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + import java.nio.charset.StandardCharsets; + import java.util.Formatter; + + public class E4 { + + static void bla() throws FileNotFoundException, UnsupportedEncodingException { + Formatter s=new Formatter(new File("asdf"),StandardCharsets.UTF_8); + } + } + """), + THREE(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream("")); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream("")); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new FileReader(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(Charset.defaultCharset()); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(Charset.defaultCharset()); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + ENCODINGASSTRINGPARAMETER( + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + //byte[] bytes= s.getBytes(StandardCharsets.UTF_8); + byte[] bytes= s.getBytes("Utf-8"); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream(""), "UTF-8"); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), "UTF-8"); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new FileReader(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + //byte[] bytes= s.getBytes(StandardCharsets.UTF_8); + byte[] bytes= s.getBytes(StandardCharsets.UTF_8); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(Charset.defaultCharset()); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """); + + String given; + String expected; + + ExplicitEncodingPatterns(String given, String expected) { + this.given = given; + this.expected = expected; + } + } + + @ParameterizedTest + @EnumSource(ExplicitEncodingPatterns.class) + public void testExplicitEncodingParametrized(ExplicitEncodingPatterns test) throws CoreException { + IPackageFragment pack = context.getfSourceFolder().createPackageFragment("test1", false, null); + ICompilationUnit cu = pack.createCompilationUnit("E1.java", test.given, false, null); + context.enable(CleanUpConstants.EXPLICITENCODING_CLEANUP); + context.assertRefactoringResultAsExpected(new ICompilationUnit[] { cu }, new String[] { test.expected }, null); + } + + @Test + public void testExplicitEncodingdonttouch() throws CoreException { + IPackageFragment pack = context.getfSourceFolder().createPackageFragment("test1", false, null); + ICompilationUnit cu = pack.createCompilationUnit("E2.java", + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.IOException; + import java.nio.charset.Charset; + import java.io.FileInputStream; + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + + public class E2 { + void method() throws UnsupportedEncodingException, IOException { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(Charset.defaultCharset()); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(Charset.defaultCharset().displayName()); + try ( + InputStreamReader is=new InputStreamReader(new FileInputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ + ){ } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + """, + false, null); + + context.enable(CleanUpConstants.EXPLICITENCODING_CLEANUP); + + context.assertRefactoringHasNoChange(new ICompilationUnit[] { cu }); + } +} diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/QuickFixJava10TestSuite.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/QuickFixJava10TestSuite.java new file mode 100644 index 00000000000..a4fbb2c5c60 --- /dev/null +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/QuickFixJava10TestSuite.java @@ -0,0 +1,25 @@ +/******************************************************************************* + * Copyright (c) 2000, 2023 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 + * Suite moved from QuickFixTest.java + *******************************************************************************/ +package org.eclipse.jdt.ui.tests.quickfix.Java10; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ + ExplicitEncodingCleanUpTest.class, +}) +public class QuickFixJava10TestSuite { +} diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java8/ExplicitEncodingCleanUpTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java8/ExplicitEncodingCleanUpTest.java new file mode 100644 index 00000000000..5e5e9421135 --- /dev/null +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java8/ExplicitEncodingCleanUpTest.java @@ -0,0 +1,866 @@ +package org.eclipse.jdt.ui.tests.quickfix.Java8; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IPackageFragment; +import org.eclipse.jdt.internal.corext.fix.CleanUpConstants; +import org.eclipse.jdt.ui.tests.quickfix.rules.AbstractEclipseJava; +import org.eclipse.jdt.ui.tests.quickfix.rules.EclipseJava8; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; + +public class ExplicitEncodingCleanUpTest { + + @RegisterExtension + AbstractEclipseJava context = new EclipseJava8(); + + enum ExplicitEncodingPatterns { + + CHARSET(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + Charset cs1= Charset.forName("UTF-8"); + Charset cs1b= Charset.forName("Utf-8"); + Charset cs2= Charset.forName("UTF-16"); + Charset cs3= Charset.forName("UTF-16BE"); + Charset cs4= Charset.forName("UTF-16LE"); + Charset cs5= Charset.forName("ISO-8859-1"); + Charset cs6= Charset.forName("US-ASCII"); + String result= cs1.toString(); + } + } + } + """, + +// """ +// package test1; +// +// import java.io.ByteArrayOutputStream; +// import java.io.InputStreamReader; +// import java.io.FileInputStream; +// import java.io.FileReader; +// import java.io.Reader; +// import java.nio.charset.Charset; +// import java.nio.charset.StandardCharsets; +// import java.io.FileNotFoundException; +// +// public class E1 { +// void method(String filename) { +// Charset cs1= StandardCharsets.UTF_8; +// Charset cs1b= StandardCharsets.UTF_8; +// Charset cs2= StandardCharsets.UTF_16; +// Charset cs3= StandardCharsets.UTF_16BE; +// Charset cs4= StandardCharsets.UTF_16LE; +// Charset cs5= StandardCharsets.ISO_8859_1; +// Charset cs6= StandardCharsets.US_ASCII; +// String result= cs1.toString(); +// } +// } +// } +// """), + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + Charset cs1= Charset.forName("UTF-8"); + Charset cs1b= Charset.forName("Utf-8"); + Charset cs2= Charset.forName("UTF-16"); + Charset cs3= Charset.forName("UTF-16BE"); + Charset cs4= Charset.forName("UTF-16LE"); + Charset cs5= Charset.forName("ISO-8859-1"); + Charset cs6= Charset.forName("US-ASCII"); + String result= cs1.toString(); + } + } + } + """), + BYTEARRAYOUTSTREAM(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + ByteArrayOutputStream ba2=new ByteArrayOutputStream(); + String result2=ba2.toString("UTF-8"); + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + ByteArrayOutputStream ba2=new ByteArrayOutputStream(); + String result2=ba2.toString("UTF-8"); + } + } + } + """), + FILEREADER(""" + package test1; + + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Reader is=new FileReader(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + FILEWRITER(""" + package test1; + + import java.io.FileWriter; + import java.io.Writer; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Writer fw=new FileWriter(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.FileWriter; + import java.io.OutputStreamWriter; + import java.io.Writer; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + import java.io.FileOutputStream; + + public class E1 { + void method(String filename) { + try { + Writer fw=new OutputStreamWriter(new FileOutputStream(filename), Charset.defaultCharset()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + INPUTSTREAMREADER( + """ + package test1; + + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + InputStreamReader is1=new InputStreamReader(new FileInputStream("file1.txt")); //$NON-NLS-1$ + InputStreamReader is2=new InputStreamReader(new FileInputStream("file2.txt"), "UTF-8"); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + InputStreamReader is1=new InputStreamReader(new FileInputStream("file1.txt"), Charset.defaultCharset()); //$NON-NLS-1$ + InputStreamReader is2=new InputStreamReader(new FileInputStream("file2.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + OUTPUTSTREAMWRITER( + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream("")); //$NON-NLS-1$ + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), "UTF-8"); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + CHANNELSNEWREADER(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.channels.ReadableByteChannel; + import java.nio.charset.StandardCharsets; + import java.nio.channels.Channels; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + ReadableByteChannel ch; + Reader r=Channels.newReader(ch,"UTF-8"); //$NON-NLS-1$ + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.channels.ReadableByteChannel; + import java.nio.charset.StandardCharsets; + import java.nio.channels.Channels; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + ReadableByteChannel ch; + Reader r=Channels.newReader(ch,"UTF-8"); //$NON-NLS-1$ + } + } + } + """), + CHANNELSNEWWRITER(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Writer; + import java.nio.channels.WritableByteChannel; + import java.nio.charset.StandardCharsets; + import java.nio.channels.Channels; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + WritableByteChannel ch; + Writer w=Channels.newWriter(ch,"UTF-8"); //$NON-NLS-1$ + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Writer; + import java.nio.channels.WritableByteChannel; + import java.nio.charset.StandardCharsets; + import java.nio.channels.Channels; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + WritableByteChannel ch; + Writer w=Channels.newWriter(ch,"UTF-8"); //$NON-NLS-1$ + } + } + } + """), + PRINTWRITER(""" + package test1; + + import java.io.PrintWriter; + import java.io.Writer; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Writer w=new PrintWriter(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.PrintWriter; + import java.io.Writer; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Writer w=new PrintWriter(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + STRINGGETBYTES(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(); + byte[] bytes2= s.getBytes("UTF-8"); + System.out.println(bytes.length); + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(Charset.defaultCharset()); + byte[] bytes2= s.getBytes(StandardCharsets.UTF_8); + System.out.println(bytes.length); + } + } + } + """), + STRING(""" + package test1; + + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + + public class E5 { + + static void bla() throws FileNotFoundException, UnsupportedEncodingException { + byte[] b= {(byte)59}; + String s1=new String(b,"UTF-8"); + String s2=new String(b,0,1,"UTF-8"); + } + } + """, + + """ + package test1; + + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + + public class E5 { + + static void bla() throws FileNotFoundException, UnsupportedEncodingException { + byte[] b= {(byte)59}; + String s1=new String(b,"UTF-8"); + String s2=new String(b,0,1,"UTF-8"); + } + } + """), + PROPERTIESSTORETOXML(""" + package test1; + + import java.io.FileOutputStream; + import java.io.IOException; + import java.util.Properties; + + public class E1 { + static void blu() throws IOException { + Properties p=new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, null, "UTF-8"); + } + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, null); + } + } + } + """, + + """ + package test1; + + import java.io.FileOutputStream; + import java.io.IOException; + import java.nio.charset.StandardCharsets; + import java.util.Properties; + + public class E1 { + static void blu() throws IOException { + Properties p=new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, null, StandardCharsets.UTF_8); + } + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, null, StandardCharsets.UTF_8); + } + } + } + """), + URLDECODER(""" + package test1; + import java.io.UnsupportedEncodingException; + import java.net.URLDecoder; + + public class E2 { + + static void bla() throws UnsupportedEncodingException { + String url=URLDecoder.decode("asdf","UTF-8"); + String url2=URLDecoder.decode("asdf"); + } + } + """, """ + package test1; + import java.io.UnsupportedEncodingException; + import java.net.URLDecoder; + + public class E2 { + + static void bla() throws UnsupportedEncodingException { + String url=URLDecoder.decode("asdf","UTF-8"); + String url2=URLDecoder.decode("asdf"); + } + } + """), + URLENCODER(""" + package test1; + import java.io.UnsupportedEncodingException; + import java.net.URLEncoder; + + public class E2 { + + static void bla() throws UnsupportedEncodingException { + String url=URLEncoder.encode("asdf","UTF-8"); + String url4=URLEncoder.encode("asdf"); + } + } + """, """ + package test1; + import java.io.UnsupportedEncodingException; + import java.net.URLEncoder; + + public class E2 { + + static void bla() throws UnsupportedEncodingException { + String url=URLEncoder.encode("asdf","UTF-8"); + String url4=URLEncoder.encode("asdf"); + } + } + """), + SCANNER(""" + package test1; + import java.io.File; + import java.io.FileNotFoundException; + import java.util.Scanner; + + public class E3 { + + static void bla() throws FileNotFoundException { + Scanner s=new Scanner(new File("asdf"),"UTF-8"); + Scanner s2=new Scanner("asdf","UTF-8"); + Scanner s3=new Scanner("asdf"); + } + } + """, """ + package test1; + import java.io.File; + import java.io.FileNotFoundException; + import java.util.Scanner; + + public class E3 { + + static void bla() throws FileNotFoundException { + Scanner s=new Scanner(new File("asdf"),"UTF-8"); + Scanner s2=new Scanner("asdf","UTF-8"); + Scanner s3=new Scanner("asdf"); + } + } + """), + FORMATTER(""" + package test1; + import java.io.File; + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + import java.util.Formatter; + + public class E4 { + + static void bla() throws FileNotFoundException, UnsupportedEncodingException { + Formatter s=new Formatter(new File("asdf"),"UTF-8"); + } + } + """, """ + package test1; + import java.io.File; + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + import java.nio.charset.StandardCharsets; + import java.util.Formatter; + + public class E4 { + + static void bla() throws FileNotFoundException, UnsupportedEncodingException { + Formatter s=new Formatter(new File("asdf"),StandardCharsets.UTF_8); + } + } + """), + THREE(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream("")); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream("")); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new FileReader(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(Charset.defaultCharset()); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + ENCODINGASSTRINGPARAMETER( + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + //byte[] bytes= s.getBytes(StandardCharsets.UTF_8); + byte[] bytes= s.getBytes("Utf-8"); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream(""), "UTF-8"); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), "UTF-8"); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new FileReader(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + //byte[] bytes= s.getBytes(StandardCharsets.UTF_8); + byte[] bytes= s.getBytes(StandardCharsets.UTF_8); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """); + + String given; + String expected; + + ExplicitEncodingPatterns(String given, String expected) { + this.given = given; + this.expected = expected; + } + } + + @ParameterizedTest + @EnumSource(ExplicitEncodingPatterns.class) + public void testExplicitEncodingParametrized(ExplicitEncodingPatterns test) throws CoreException { + IPackageFragment pack = context.getfSourceFolder().createPackageFragment("test1", false, null); + ICompilationUnit cu = pack.createCompilationUnit("E1.java", test.given, false, null); + context.enable(CleanUpConstants.EXPLICITENCODING_CLEANUP); + context.assertRefactoringResultAsExpected(new ICompilationUnit[] { cu }, new String[] { test.expected }, null); + } + + @Test + public void testExplicitEncodingdonttouch() throws CoreException { + IPackageFragment pack = context.getfSourceFolder().createPackageFragment("test1", false, null); + ICompilationUnit cu = pack.createCompilationUnit("E2.java", + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.IOException; + import java.nio.charset.Charset; + import java.io.FileInputStream; + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + + public class E2 { + void method() throws UnsupportedEncodingException, IOException { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(Charset.defaultCharset()); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(Charset.defaultCharset().displayName()); + try ( + InputStreamReader is=new InputStreamReader(new FileInputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ + ){ } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + """, + false, null); + + context.enable(CleanUpConstants.EXPLICITENCODING_CLEANUP); + + context.assertRefactoringHasNoChange(new ICompilationUnit[] { cu }); + } +} diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java8/QuickFixJava8TestSuite.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java8/QuickFixJava8TestSuite.java new file mode 100644 index 00000000000..a42e79b6807 --- /dev/null +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java8/QuickFixJava8TestSuite.java @@ -0,0 +1,25 @@ +/******************************************************************************* + * Copyright (c) 2000, 2023 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 + * Suite moved from QuickFixTest.java + *******************************************************************************/ +package org.eclipse.jdt.ui.tests.quickfix.Java8; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ + ExplicitEncodingCleanUpTest.class, +}) +public class QuickFixJava8TestSuite { +} diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/AbstractEclipseJava.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/AbstractEclipseJava.java new file mode 100644 index 00000000000..175e80e324e --- /dev/null +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/AbstractEclipseJava.java @@ -0,0 +1,482 @@ +package org.eclipse.jdt.ui.tests.quickfix.rules; + +import static org.eclipse.jdt.internal.corext.fix.CleanUpConstants.DEFAULT_CLEAN_UP_OPTIONS; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleReference; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.preferences.InstanceScope; + +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IProjectDescription; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IWorkspace; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.resources.IWorkspaceRunnable; +import org.eclipse.core.resources.ResourcesPlugin; + +import org.eclipse.ltk.core.refactoring.Change; +import org.eclipse.ltk.core.refactoring.CheckConditionsOperation; +import org.eclipse.ltk.core.refactoring.CompositeChange; +import org.eclipse.ltk.core.refactoring.CreateChangeOperation; +import org.eclipse.ltk.core.refactoring.GroupCategory; +import org.eclipse.ltk.core.refactoring.IUndoManager; +import org.eclipse.ltk.core.refactoring.PerformChangeOperation; +import org.eclipse.ltk.core.refactoring.RefactoringCore; +import org.eclipse.ltk.core.refactoring.RefactoringStatus; +import org.eclipse.ltk.core.refactoring.TextEditBasedChange; +import org.eclipse.ltk.core.refactoring.TextEditBasedChangeGroup; + +import org.eclipse.jdt.core.IClasspathAttribute; +import org.eclipse.jdt.core.IClasspathEntry; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IPackageFragmentRoot; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.compiler.IProblem; +import org.eclipse.jdt.core.dom.ASTParser; +import org.eclipse.jdt.core.dom.CompilationUnit; + +import org.eclipse.jdt.internal.corext.dom.IASTSharedValues; +import org.eclipse.jdt.internal.corext.fix.CleanUpConstants; +import org.eclipse.jdt.internal.corext.fix.CleanUpPreferenceUtil; +import org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring; + +import org.eclipse.jdt.ui.JavaUI; +import org.eclipse.jdt.ui.cleanup.CleanUpOptions; +import org.eclipse.jdt.ui.cleanup.ICleanUp; + +import org.eclipse.jdt.internal.ui.JavaPlugin; +import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; +import org.eclipse.jdt.internal.ui.preferences.cleanup.CleanUpProfileVersioner; +import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager; +import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.CustomProfile; +import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.Profile; +import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileStore; +import org.eclipse.jdt.internal.ui.util.CoreUtility; + +public class AbstractEclipseJava implements AfterEachCallback, BeforeEachCallback { + + private final String testresources_stubs; + private final String compliance; + private static final String TEST_SETUP_PROJECT= "TestSetupProject"; //$NON-NLS-1$ + private IPackageFragmentRoot fSourceFolder; + private CustomProfile fProfile; + + public AbstractEclipseJava(String stubs, String compilerversion) { + this.testresources_stubs= stubs; + this.compliance= compilerversion; + } + + @Override + public void beforeEach(ExtensionContext context) throws CoreException { + IJavaProject javaProject= createJavaProject(TEST_SETUP_PROJECT, "bin"); //$NON-NLS-1$ + javaProject.setRawClasspath(getDefaultClasspath(), null); + Map options= javaProject.getOptions(false); + JavaCore.setComplianceOptions(compliance, options); + javaProject.setOptions(options); + setfSourceFolder(addSourceContainer(getProject(TEST_SETUP_PROJECT), "src", new Path[0], //$NON-NLS-1$ + new Path[0], null, new IClasspathAttribute[0])); + Map settings= new HashMap<>(); + fProfile= new ProfileManager.CustomProfile("testProfile", settings, CleanUpProfileVersioner.CURRENT_VERSION, //$NON-NLS-1$ + CleanUpProfileVersioner.PROFILE_KIND); + InstanceScope.INSTANCE.getNode(JavaUI.ID_PLUGIN).put(CleanUpConstants.CLEANUP_PROFILE, fProfile.getID()); + InstanceScope.INSTANCE.getNode(JavaUI.ID_PLUGIN).put(CleanUpConstants.SAVE_PARTICIPANT_PROFILE, + fProfile.getID()); + disableAll(); + } + + @Override + public void afterEach(ExtensionContext context) throws CoreException { + delete(getfSourceFolder()); + } + + public IJavaProject getProject(String projectname) { + return JavaCore.create(ResourcesPlugin.getWorkspace().getRoot().getProject(projectname)); + } + + public IClasspathEntry[] getDefaultClasspath() throws CoreException { + IPath[] rtJarPath= findRtJar(new Path(testresources_stubs)); + return new IClasspathEntry[] { JavaCore.newLibraryEntry(rtJarPath[0], rtJarPath[1], rtJarPath[2], true) }; + } + + protected void disableAll() throws CoreException { + Map settings= fProfile.getSettings(); + JavaPlugin.getDefault().getCleanUpRegistry().getDefaultOptions(DEFAULT_CLEAN_UP_OPTIONS).getKeys() + .forEach(a -> settings.put(a, CleanUpOptions.FALSE)); + commitProfile(); + } + + /** + * Removes an IJavaElement's resource. Retries if deletion failed (e.g. because + * the indexer still locks the file). + * + * @param elem the element to delete + * @throws CoreException if operation failed + * @see #ASSERT_NO_MIXED_LINE_DELIMIERS + */ + public void delete(final IJavaElement elem) throws CoreException { + IWorkspaceRunnable runnable= monitor -> { + if (elem instanceof IJavaProject jproject) { + jproject.setRawClasspath(new IClasspathEntry[0], jproject.getProject().getFullPath(), null); + } + delete(elem.getResource()); + }; + ResourcesPlugin.getWorkspace().run(runnable, null); + } + + private static final int MAX_RETRY= 5; + private static final int RETRY_DELAY= 1000; + + /** + * Removes a resource. Retries if deletion failed (e.g. because the indexer + * still locks the file). + * + * @param resource the resource to delete + * @throws CoreException if operation failed + */ + public static void delete(IResource resource) throws CoreException { + for (int i= 0; i < MAX_RETRY; i++) { + try { + resource.delete(IResource.FORCE | IResource.ALWAYS_DELETE_PROJECT_CONTENT, null); + i= MAX_RETRY; + } catch (CoreException e) { + if (i == MAX_RETRY - 1) { + // JavaPlugin.log(e); + throw e; + } + try { + // JavaPlugin.log(new IllegalStateException("sleep before retrying JavaProjectHelper.delete() for " + resource.getLocationURI())); + Thread.sleep(RETRY_DELAY); // give other threads time to close the file + } catch (InterruptedException e1) { + } + } + } + } + + /** + * @param rtStubsPath the path to the RT stubs + * @return a rt.jar (stubs only) + * @throws CoreException + */ + @SuppressWarnings("javadoc") + public IPath[] findRtJar(IPath rtStubsPath) throws CoreException { + File rtStubs= rtStubsPath.toFile().getAbsoluteFile(); + assertNotNull(rtStubs); + assertTrue(rtStubs.exists()); + return new IPath[] { Path.fromOSString(rtStubs.getPath()), null, null }; + } + + /** + * Returns the bundle associated with this plug-in. + * + * @return the associated bundle + * @since 3.0 + */ + public final Bundle getBundle() { + ClassLoader cl= getClass().getClassLoader(); + if (cl instanceof BundleReference) { + return ((BundleReference) cl).getBundle(); + } + return null; + } + + /** + * Creates a IJavaProject. + * + * @param projectName The name of the project + * @param binFolderName Name of the output folder + * @return Returns the Java project handle + * @throws CoreException Project creation failed + */ + public static IJavaProject createJavaProject(String projectName, String binFolderName) throws CoreException { + IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); + IProject project= root.getProject(projectName); + if (!project.exists()) { + project.create(null); + } else { + project.refreshLocal(IResource.DEPTH_INFINITE, null); + } + if (!project.isOpen()) { + project.open(null); + } + IPath outputLocation; + if (binFolderName != null && binFolderName.length() > 0) { + IFolder binFolder= project.getFolder(binFolderName); + if (!binFolder.exists()) { + CoreUtility.createFolder(binFolder, false, true, null); + } + outputLocation= binFolder.getFullPath(); + } else { + outputLocation= project.getFullPath(); + } + if (!project.hasNature(JavaCore.NATURE_ID)) { + addNatureToProject(project, JavaCore.NATURE_ID, null); + } + IJavaProject jproject= JavaCore.create(project); + jproject.setOutputLocation(outputLocation, null); + jproject.setRawClasspath(new IClasspathEntry[0], null); + return jproject; + } + + private static void addNatureToProject(IProject proj, String natureId, IProgressMonitor monitor) + throws CoreException { + IProjectDescription description= proj.getDescription(); + String[] prevNatures= description.getNatureIds(); + String[] newNatures= new String[prevNatures.length + 1]; + System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length); + newNatures[prevNatures.length]= natureId; + description.setNatureIds(newNatures); + proj.setDescription(description, monitor); + } + + /** + * Adds a source container to a IJavaProject. + * + * @param jproject The parent project + * @param containerName The name of the new source container + * @param inclusionFilters Inclusion filters to set + * @param exclusionFilters Exclusion filters to set + * @param outputLocation The location where class files are written to, + * null for project output folder + * @param attributes The classpath attributes to set + * @return The handle to the new source container + * @throws CoreException Creation failed + */ + public static IPackageFragmentRoot addSourceContainer(IJavaProject jproject, String containerName, + IPath[] inclusionFilters, IPath[] exclusionFilters, String outputLocation, IClasspathAttribute[] attributes) + throws CoreException { + IProject project= jproject.getProject(); + IContainer container; + if (containerName == null || containerName.length() == 0) { + container= project; + } else { + IFolder folder= project.getFolder(containerName); + if (!folder.exists()) { + CoreUtility.createFolder(folder, false, true, null); + } + container= folder; + } + IPackageFragmentRoot root= jproject.getPackageFragmentRoot(container); + + IPath outputPath= null; + if (outputLocation != null) { + IFolder folder= project.getFolder(outputLocation); + if (!folder.exists()) { + CoreUtility.createFolder(folder, false, true, null); + } + outputPath= folder.getFullPath(); + } + IClasspathEntry cpe= JavaCore.newSourceEntry(root.getPath(), inclusionFilters, exclusionFilters, outputPath, + attributes); + addToClasspath(jproject, cpe); + return root; + } + + public static void addToClasspath(IJavaProject jproject, IClasspathEntry cpe) throws JavaModelException { + IClasspathEntry[] oldEntries= jproject.getRawClasspath(); + for (IClasspathEntry oldEntry : oldEntries) { + if (oldEntry.equals(cpe)) { + return; + } + } + int nEntries= oldEntries.length; + IClasspathEntry[] newEntries= new IClasspathEntry[nEntries + 1]; + System.arraycopy(oldEntries, 0, newEntries, 0, nEntries); + newEntries[nEntries]= cpe; + jproject.setRawClasspath(newEntries, null); + } + + public RefactoringStatus assertRefactoringResultAsExpected(ICompilationUnit[] cus, String[] expected, + Set setOfExpectedGroupCategories) throws CoreException { + RefactoringStatus status= performRefactoring(cus, setOfExpectedGroupCategories); + String[] previews= new String[cus.length]; + for (int i= 0; i < cus.length; i++) { + ICompilationUnit cu= cus[i]; + previews[i]= cu.getBuffer().getContents(); + } + assertEqualStringsIgnoreOrder(previews, expected); + return status; + } + + public RefactoringStatus assertRefactoringHasNoChange(ICompilationUnit[] cus) throws CoreException { + for (ICompilationUnit cu : cus) { + assertNoCompilationError(cu); + } + return assertRefactoringHasNoChangeEventWithError(cus); + } + + protected RefactoringStatus assertRefactoringHasNoChangeEventWithError(ICompilationUnit[] cus) + throws CoreException { + String[] expected= new String[cus.length]; + for (int i= 0; i < cus.length; i++) { + expected[i]= cus[i].getBuffer().getContents(); + } + return assertRefactoringResultAsExpected(cus, expected, null); + } + + protected CompilationUnit assertNoCompilationError(ICompilationUnit cu) { + ASTParser parser= ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL); + parser.setSource(cu); + parser.setResolveBindings(true); + CompilationUnit root= (CompilationUnit) parser.createAST(null); + IProblem[] problems= root.getProblems(); + boolean hasProblems= false; + for (IProblem prob : problems) { + if (!prob.isWarning() && !prob.isInfo()) { + hasProblems= true; + break; + } + } + if (hasProblems) { + StringBuilder builder= new StringBuilder(); + builder.append(cu.getElementName()).append(" has compilation problems: \n"); //$NON-NLS-1$ + for (IProblem prob : problems) { + builder.append(prob.getMessage()).append('\n'); + } + fail(builder.toString()); + } + return root; + } + + public static void assertEqualStringsIgnoreOrder(String[] actuals, String[] expecteds) { + ArrayList list1= new ArrayList<>(Arrays.asList(actuals)); + ArrayList list2= new ArrayList<>(Arrays.asList(expecteds)); + for (int i= list1.size() - 1; i >= 0; i--) { + if (list2.remove(list1.get(i))) { + list1.remove(i); + } + } + int n1= list1.size(); + int n2= list2.size(); + if (n1 + n2 > 0) { + if (n1 == 1 && n2 == 1) { + assertEquals(list2.get(0), list1.get(0)); + } + StringBuilder buf= new StringBuilder(); + for (int i= 0; i < n1; i++) { + String s1= list1.get(i); + if (s1 != null) { + buf.append(s1); + buf.append("\n"); //$NON-NLS-1$ + } + } + String actual= buf.toString(); + buf= new StringBuilder(); + for (int i= 0; i < n2; i++) { + String s2= list2.get(i); + if (s2 != null) { + buf.append(s2); + buf.append("\n"); //$NON-NLS-1$ + } + } + String expected= buf.toString(); + assertEquals(expected, actual); + } + } + + protected final RefactoringStatus performRefactoring(ICompilationUnit[] cus, + Set setOfExpectedGroupCategories) throws CoreException { + final CleanUpRefactoring ref= new CleanUpRefactoring(); + ref.setUseOptionsFromProfile(true); + return performRefactoring(ref, cus, JavaPlugin.getDefault().getCleanUpRegistry().createCleanUps(), + setOfExpectedGroupCategories); + } + + protected RefactoringStatus performRefactoring(final CleanUpRefactoring ref, ICompilationUnit[] cus, + ICleanUp[] cleanUps, Set setOfExpectedGroupCategories) throws CoreException { + for (ICompilationUnit cu : cus) { + ref.addCompilationUnit(cu); + } + for (ICleanUp cleanUp : cleanUps) { + ref.addCleanUp(cleanUp); + } + IUndoManager undoManager= RefactoringCore.getUndoManager(); + undoManager.flush(); + final CreateChangeOperation create= new CreateChangeOperation( + new CheckConditionsOperation(ref, CheckConditionsOperation.ALL_CONDITIONS), RefactoringStatus.FATAL); + final PerformChangeOperation perform= new PerformChangeOperation(create); + perform.setUndoManager(undoManager, ref.getName()); + IWorkspace workspace= ResourcesPlugin.getWorkspace(); + workspace.run(perform, new NullProgressMonitor()); + RefactoringStatus status= create.getConditionCheckingStatus(); + if (status.hasFatalError()) { + throw new CoreException( + new StatusInfo(status.getSeverity(), status.getMessageMatchingSeverity(status.getSeverity()))); + } + assertTrue(perform.changeExecuted(), "Change wasn't executed"); //$NON-NLS-1$ + Change undo= perform.getUndoChange(); + assertNotNull(undo, "Undo doesn't exist"); //$NON-NLS-1$ + assertTrue(undoManager.anythingToUndo(), "Undo manager is empty"); //$NON-NLS-1$ + if (setOfExpectedGroupCategories != null) { + Change change= create.getChange(); + Set actualCategories= new HashSet<>(); + collectGroupCategories(actualCategories, change); + actualCategories.forEach(actualCategory -> { + assertTrue(setOfExpectedGroupCategories.contains(actualCategory.getName()), + () -> "Unexpected group category: " + actualCategory.getName() + ", should find: " //$NON-NLS-1$ //$NON-NLS-2$ + + String.join(", ", setOfExpectedGroupCategories)); //$NON-NLS-1$ + }); + } + return status; + } + + private void collectGroupCategories(Set result, Change change) { + if (change instanceof TextEditBasedChange) { + for (TextEditBasedChangeGroup group : ((TextEditBasedChange) change).getChangeGroups()) { + result.addAll(group.getGroupCategorySet().asList()); + } + } else if (change instanceof CompositeChange) { + for (Change child : ((CompositeChange) change).getChildren()) { + collectGroupCategories(result, child); + } + } + } + + public void enable(String key) throws CoreException { + fProfile.getSettings().put(key, CleanUpOptions.TRUE); + commitProfile(); + } + + private void commitProfile() throws CoreException { + List profiles= CleanUpPreferenceUtil.getBuiltInProfiles(); + profiles.add(fProfile); + CleanUpProfileVersioner versioner= new CleanUpProfileVersioner(); + ProfileStore profileStore= new ProfileStore(CleanUpConstants.CLEANUP_PROFILES, versioner); + profileStore.writeProfiles(profiles, InstanceScope.INSTANCE); + CleanUpPreferenceUtil.saveSaveParticipantOptions(InstanceScope.INSTANCE, fProfile.getSettings()); + } + + public IPackageFragmentRoot getfSourceFolder() { + return fSourceFolder; + } + + public void setfSourceFolder(IPackageFragmentRoot fSourceFolder) { + this.fSourceFolder = fSourceFolder; + } +} diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/EclipseJava10.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/EclipseJava10.java new file mode 100644 index 00000000000..8d4b5bca02c --- /dev/null +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/EclipseJava10.java @@ -0,0 +1,11 @@ +package org.eclipse.jdt.ui.tests.quickfix.rules; + +import org.eclipse.jdt.core.JavaCore; + +public class EclipseJava10 extends AbstractEclipseJava { + private static final String TESTRESOURCES_RTSTUBS_10_JAR= "testresources/rtstubs10.jar"; //$NON-NLS-1$ + + public EclipseJava10() { + super(TESTRESOURCES_RTSTUBS_10_JAR, JavaCore.VERSION_10); + } +} diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/EclipseJava17.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/EclipseJava17.java new file mode 100644 index 00000000000..39070bce1da --- /dev/null +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/EclipseJava17.java @@ -0,0 +1,11 @@ +package org.eclipse.jdt.ui.tests.quickfix.rules; + +import org.eclipse.jdt.core.JavaCore; + +public class EclipseJava17 extends AbstractEclipseJava { + private static final String TESTRESOURCES_RTSTUBS_17_JAR= "testresources/rtstubs_17.jar"; //$NON-NLS-1$ + + public EclipseJava17() { + super(TESTRESOURCES_RTSTUBS_17_JAR, JavaCore.VERSION_17); + } +} diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/EclipseJava18.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/EclipseJava18.java new file mode 100644 index 00000000000..e793eef9d75 --- /dev/null +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/EclipseJava18.java @@ -0,0 +1,11 @@ +package org.eclipse.jdt.ui.tests.quickfix.rules; + +import org.eclipse.jdt.core.JavaCore; + +public class EclipseJava18 extends AbstractEclipseJava { + private static final String TESTRESOURCES_RTSTUBS_18_JAR= "testresources/rtstubs_17.jar"; //$NON-NLS-1$ + + public EclipseJava18() { + super(TESTRESOURCES_RTSTUBS_18_JAR, JavaCore.VERSION_17); + } +} diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/EclipseJava8.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/EclipseJava8.java new file mode 100644 index 00000000000..fb313d83ec9 --- /dev/null +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/EclipseJava8.java @@ -0,0 +1,11 @@ +package org.eclipse.jdt.ui.tests.quickfix.rules; + +import org.eclipse.jdt.core.JavaCore; + +public class EclipseJava8 extends AbstractEclipseJava { + private static final String TESTRESOURCES_RTSTUBS18_JAR= "testresources/rtstubs18.jar"; //$NON-NLS-1$ + + public EclipseJava8() { + super(TESTRESOURCES_RTSTUBS18_JAR, JavaCore.VERSION_1_8); + } +} diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/EclipseJava9.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/EclipseJava9.java new file mode 100644 index 00000000000..92cad609127 --- /dev/null +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/EclipseJava9.java @@ -0,0 +1,11 @@ +package org.eclipse.jdt.ui.tests.quickfix.rules; + +import org.eclipse.jdt.core.JavaCore; + +public class EclipseJava9 extends AbstractEclipseJava { + private static final String TESTRESOURCES_RTSTUBS_9_JAR= "testresources/rtstubs9.jar"; //$NON-NLS-1$ + + public EclipseJava9() { + super(TESTRESOURCES_RTSTUBS_9_JAR, JavaCore.VERSION_9); + } +}