blob: 8a61c7a27ca048547a3d0adef4c68294d5c43fe8 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2016, 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 static org.junit.Assert.assertNotEquals;
import org.junit.Rule;
import org.junit.Test;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
import org.eclipse.jdt.ui.tests.core.rules.Java1d6ProjectTestSetup;
import org.eclipse.jdt.ui.tests.core.rules.ProjectTestSetup;
/**
* Tests the cleanup features related to Java 6 (i.e. Mustang).
*/
public class CleanUpTest1d6 extends CleanUpTestCase {
@Rule
public ProjectTestSetup projectSetup= new Java1d6ProjectTestSetup();
@Override
protected IJavaProject getProject() {
return projectSetup.getProject();
}
@Override
protected IClasspathEntry[] getDefaultClasspath() throws CoreException {
return projectSetup.getDefaultClasspath();
}
/**
* Tests if CleanUp works when the number of problems in a single CU is greater than the
* Compiler option {@link JavaCore#COMPILER_PB_MAX_PER_UNIT} which has a default value of 100,
* see http://bugs.eclipse.org/322543 for details.
*
* @throws Exception if the something fails while executing this test
* @since 3.7
*/
@Test
public void testCleanUpWithCUProblemsGreaterThanMaxProblemsPerCUPreference() throws Exception {
final int PROBLEMS_COUNT= 101;
IPackageFragment pack= fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder bld= new StringBuilder();
bld.append("package test1;\n");
bld.append("interface I {\n");
for (int count= 0; count < PROBLEMS_COUNT; count++) {
bld.append(" void m").append(count).append("();\n");
}
bld.append("}\n");
bld.append("class X implements I {\n");
for (int count= 0; count < PROBLEMS_COUNT; count++) {
bld.append(" public void m").append(count).append("() {} // @Override error in 1.5, not in 1.6\n");
}
bld.append("}\n");
String given= bld.toString();
ICompilationUnit cu= pack.createCompilationUnit("I.java", given, false, null);
enable(CleanUpConstants.ADD_MISSING_ANNOTATIONS);
enable(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE);
enable(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE_FOR_INTERFACE_METHOD_IMPLEMENTATION);
bld= new StringBuilder();
bld.append("package test1;\n");
bld.append("interface I {\n");
for (int count= 0; count < PROBLEMS_COUNT; count++) {
bld.append(" void m").append(count).append("();\n");
}
bld.append("}\n");
bld.append("class X implements I {\n");
for (int count= 0; count < PROBLEMS_COUNT; count++) {
bld.append(" @Override\n");
bld.append(" public void m").append(count).append("() {} // @Override error in 1.5, not in 1.6\n");
}
bld.append("}\n");
String expected= bld.toString();
assertNotEquals("The class must be changed", expected, given);
assertRefactoringResultAsExpected(new ICompilationUnit[] { cu }, new String[] { expected });
}
@Test
public void testAddOverride1d6() throws Exception {
IPackageFragment pack= fSourceFolder.createPackageFragment("test1", false, null);
String given= "" //
+ "package test1;\n" //
+ "\n" //
+ "interface I {\n" //
+ " void m();\n" //
+ " boolean equals(Object obj);\n" //
+ "}\n" //
+ "\n" //
+ "interface J extends I {\n" //
+ " void m(); // @Override error in 1.5, not in 1.6\n" //
+ "}\n" //
+ "\n" //
+ "class X implements J {\n" //
+ " public void m() {} // @Override error in 1.5, not in 1.6\n" //
+ " public int hashCode() { return 0; }\n" //
+ "}\n";
ICompilationUnit cu= pack.createCompilationUnit("I.java", given, false, null);
enable(CleanUpConstants.ADD_MISSING_ANNOTATIONS);
enable(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE);
enable(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE_FOR_INTERFACE_METHOD_IMPLEMENTATION);
String expected= "" //
+ "package test1;\n" //
+ "\n" //
+ "interface I {\n" //
+ " void m();\n" //
+ " @Override\n" //
+ " boolean equals(Object obj);\n" //
+ "}\n" //
+ "\n" //
+ "interface J extends I {\n" //
+ " @Override\n" //
+ " void m(); // @Override error in 1.5, not in 1.6\n" //
+ "}\n" //
+ "\n" //
+ "class X implements J {\n" //
+ " @Override\n" //
+ " public void m() {} // @Override error in 1.5, not in 1.6\n" //
+ " @Override\n" //
+ " public int hashCode() { return 0; }\n" //
+ "}\n";
assertNotEquals("The class must be changed", expected, given);
assertRefactoringResultAsExpected(new ICompilationUnit[] {cu}, new String[] {expected});
}
@Test
public void testAddOverride1d6NoInterfaceMethods() throws Exception {
IPackageFragment pack= fSourceFolder.createPackageFragment("test1", false, null);
String given= "" //
+ "package test1;\n" //
+ "\n" //
+ "interface I {\n" //
+ " void m();\n" //
+ " boolean equals(Object obj);\n" //
+ "}\n" //
+ "\n" //
+ "interface J extends I {\n" //
+ " void m(); // @Override error in 1.5, not in 1.6\n" //
+ "}\n" //
+ "\n" //
+ "class X implements J {\n" //
+ " public void m() {} // @Override error in 1.5, not in 1.6\n" //
+ " public int hashCode() { return 0; }\n" //
+ "}\n";
ICompilationUnit cu= pack.createCompilationUnit("I.java", given, false, null);
enable(CleanUpConstants.ADD_MISSING_ANNOTATIONS);
enable(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE);
String expected= "" //
+ "package test1;\n" //
+ "\n" //
+ "interface I {\n" //
+ " void m();\n" //
+ " boolean equals(Object obj);\n" //
+ "}\n" //
+ "\n" //
+ "interface J extends I {\n" //
+ " void m(); // @Override error in 1.5, not in 1.6\n" //
+ "}\n" //
+ "\n" //
+ "class X implements J {\n" //
+ " public void m() {} // @Override error in 1.5, not in 1.6\n" //
+ " @Override\n" //
+ " public int hashCode() { return 0; }\n" //
+ "}\n";
assertNotEquals("The class must be changed", expected, given);
assertRefactoringResultAsExpected(new ICompilationUnit[] {cu}, new String[] {expected});
}
}