1 /*******************************************************************************
2  * Copyright (c) 2014, 2020 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.jdt.ui.tests.refactoring;
15 
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertNotNull;
18 import static org.junit.Assert.assertNull;
19 
20 import java.util.ArrayList;
21 import java.util.List;
22 
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 
27 import org.eclipse.ltk.core.refactoring.Refactoring;
28 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
29 import org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring;
30 
31 import org.eclipse.jdt.core.ICompilationUnit;
32 import org.eclipse.jdt.core.IField;
33 import org.eclipse.jdt.core.IMember;
34 import org.eclipse.jdt.core.IPackageFragment;
35 import org.eclipse.jdt.core.IPackageFragmentRoot;
36 import org.eclipse.jdt.core.IType;
37 
38 import org.eclipse.jdt.internal.corext.refactoring.structure.ExtractInterfaceProcessor;
39 
40 import org.eclipse.jdt.ui.tests.CustomBaseRunner;
41 import org.eclipse.jdt.ui.tests.IgnoreInheritedTests;
42 import org.eclipse.jdt.ui.tests.refactoring.rules.Java1d8Setup;
43 import org.eclipse.jdt.ui.tests.refactoring.rules.RefactoringTestSetup;
44 
45 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
46 
47 /**
48  * Those tests are made to run on Java Spider 1.8 .
49  */
50 @IgnoreInheritedTests
51 @RunWith(CustomBaseRunner.class)
52 public class ExtractInterfaceTests1d8 extends ExtractInterfaceTests {
53 	private static final String REFACTORING_PATH= "ExtractInterface18/";
54 
55 	@Rule
56 	public RefactoringTestSetup js= new Java1d8Setup();
57 
58 	@Override
getRefactoringPath()59 	protected String getRefactoringPath() {
60 		return REFACTORING_PATH;
61 	}
62 
63 	@Test
testExtractInterfaceFromInterface1()64 	public void testExtractInterfaceFromInterface1() throws Exception {
65 		validatePassingTest("A", "B", true, true);
66 	}
67 
68 	@Test
testExtractInterfaceFromInterface2()69 	public void testExtractInterfaceFromInterface2() throws Exception {
70 		String className= "A";
71 		String extendingInterfaceName= "I1";
72 		String newInterfaceName= "B";
73 
74 		IType clas= getType(createCUfromTestFile(getPackageP(), getTopLevelTypeName(className)), className);
75 		ICompilationUnit cu= clas.getCompilationUnit();
76 		IPackageFragment pack= (IPackageFragment)cu.getParent();
77 
78 		getType(createCUfromTestFile(getPackageP(), getTopLevelTypeName(extendingInterfaceName)), extendingInterfaceName);
79 
80 		IPackageFragmentRoot root= RefactoringTestSetup.getDefaultSourceFolder();
81 		assertNotNull(root);
82 		IPackageFragment p2= root.createPackageFragment("p2", true, null);
83 		getType(createCUfromTestFile(p2, getTopLevelTypeName("I2")), "I2");
84 
85 		ExtractInterfaceProcessor processor= new ExtractInterfaceProcessor(clas, JavaPreferencesSettings.getCodeGenerationSettings(clas.getJavaProject()));
86 		Refactoring ref= new ProcessorBasedRefactoring(processor);
87 
88 		processor.setTypeName(newInterfaceName);
89 		assertEquals("interface name should be accepted", RefactoringStatus.OK, processor.checkTypeName(newInterfaceName).getSeverity());
90 
91 		IMember[] extractableMembers= processor.getExtractableMembers();
92 		final IMember[] members= new IMember[extractableMembers.length - 1];
93 		List<IMember> list= new ArrayList<>();
94 		for (IMember iMember : extractableMembers) {
95 			if (!(iMember instanceof IField)) {
96 				list.add(iMember);
97 			}
98 		}
99 		processor.setExtractedMembers(list.toArray(members));
100 		processor.setReplace(true);
101 		processor.setAnnotations(false);
102 		RefactoringStatus performRefactoring= performRefactoring(ref);
103 		assertNull("was supposed to pass", performRefactoring);
104 		assertEqualLines("incorrect changes in " + className,
105 				getFileContents(getOutputTestFileName(className)),
106 				cu.getSource());
107 
108 		ICompilationUnit interfaceCu= pack.getCompilationUnit(newInterfaceName + ".java");
109 		assertEqualLines("incorrect interface created",
110 				getFileContents(getOutputTestFileName(newInterfaceName)),
111 				interfaceCu.getSource());
112 
113 	}
114 
115 	@Test
testExtractInterfaceFromClass()116 	public void testExtractInterfaceFromClass() throws Exception {
117 		validatePassingTest("A", "B", true, true);
118 	}
119 
120 	// bug 394551
121 	@Test
testExtractInterfaceFromClass2()122 	public void testExtractInterfaceFromClass2() throws Exception {
123 		fGenerateAnnotations= true;
124 		String[] names= new String[] { "m" };
125 		String[][] signatures= new String[][] { new String[0], new String[0] };
126 		validatePassingTest("A", new String[] { "A" }, "I", true, names, signatures, null);
127 	}
128 
129 	@Test
testExtractInterfaceFromAbstractClass()130 	public void testExtractInterfaceFromAbstractClass() throws Exception {
131 		validatePassingTest("A", "B", true, true);
132 	}
133 
134 	@Test
testLambda1()135 	public void testLambda1() throws Exception {
136 		// bug 488420
137 		String[] names= new String[] { "m1" };
138 		String[][] signatures= new String[][] { new String[0] };
139 		validatePassingTest("X", new String[] { "X", "Util" }, "I", true, names, signatures, null);
140 	}
141 
142 	@Test
testLambda2()143 	public void testLambda2() throws Exception {
144 		// bug 488420
145 		String[] names= new String[] { "m1" };
146 		String[][] signatures= new String[][] { new String[0] };
147 		validatePassingTest("X", new String[] { "X", "Util" }, "I", true, names, signatures, null);
148 	}
149 
150 	@Test
testMethodRef1()151 	public void testMethodRef1() throws Exception {
152 		// bug 489170
153 		String[] names= new String[] { "methodN" };
154 		String[][] signatures= new String[][] { new String[0] };
155 		validatePassingTest("X", new String[] { "X", "Util" }, "I", true, names, signatures, null);
156 	}
157 
158 	@Test
testMethodRef2()159 	public void testMethodRef2() throws Exception {
160 		// bug 489170
161 		String[] names= new String[] { "m1" };
162 		String[][] signatures= new String[][] { new String[0] };
163 		validatePassingTest("X", new String[] { "X", "Util" }, "I", true, names, signatures, null);
164 	}
165 
166 	@Test
testMethodRef3()167 	public void testMethodRef3() throws Exception {
168 		// bug 489170
169 		String[] names= new String[] { "m1" };
170 		String[][] signatures= new String[][] { new String[0] };
171 		validatePassingTest("X", new String[] { "X", "Util" }, "I", true, names, signatures, null);
172 	}
173 }
174