Skip to main content
summaryrefslogtreecommitdiffstats
blob: 99dae77b9738dc5a2b43b83bf403d0c2fdd8c950 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**********************************************************************
 * This file is part of "Object Teams Development Tooling"-Software
 * 
 * Copyright 2004, 2010 Fraunhofer Gesellschaft, Munich, Germany,
 * for its Fraunhofer Institute and Computer Architecture and Software
 * Technology (FIRST), Berlin, Germany and Technical University Berlin,
 * Germany.
 * 
 * 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
 * $Id$
 * 
 * Please visit http://www.eclipse.org/objectteams for updates and contact.
 * 
 * Contributors:
 * 	  Fraunhofer FIRST - Initial API and implementation
 * 	  Technical University Berlin - Initial API and implementation
 **********************************************************************/
package org.eclipse.objectteams.otdt.ui.tests.refactoring.pushdown;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
import org.eclipse.jdt.internal.corext.refactoring.structure.PushDownRefactoringProcessor;
import org.eclipse.ltk.core.refactoring.Refactoring;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring;
import org.eclipse.objectteams.otdt.ui.tests.refactoring.MySetup;
import org.eclipse.objectteams.otdt.ui.tests.refactoring.RefactoringTest;

/**
 * Copy of {@link org.eclipse.jdt.ui.tests.refactoring.PushDownTests}.
 * 
 * @author Johannes Gebauer
 * 
 */
@SuppressWarnings({ "nls", "restriction" })
public class PushDownTests extends RefactoringTest {

	private static final String REFACTORING_PATH= "PushDown/";

	public PushDownTests(String name) {
		super(name);
	}
	
	protected String getRefactoringPath() {
		return REFACTORING_PATH;
	}
	

	public static Test suite() {
		return new MySetup(new TestSuite(PushDownTests.class));
	}

	private void performPushDown_pass(String[] cuNames, String[] methodNames, String[][] signatures, String[] fieldNames, String nameOfDeclaringType)
			throws Exception {
		ICompilationUnit[] cus = createCUs(cuNames);
		try {

			IType declaringType = getType(cus[0], nameOfDeclaringType);
			IMethod[] methods = getMethods(declaringType, methodNames, signatures);
			IField[] fields = getFields(declaringType, fieldNames);
			IMember[] members = merge(methods, fields);

			PushDownRefactoringProcessor processor = createRefactoringProcessor(members);
			Refactoring ref = processor.getRefactoring();

			assertTrue("activation", ref.checkInitialConditions(new NullProgressMonitor()).isOK());

			RefactoringStatus checkInputResult = ref.checkFinalConditions(new NullProgressMonitor());
			assertTrue("precondition was supposed to pass", !checkInputResult.hasError());
			performChange(ref, false);

			for (int i = 0; i < cus.length; i++) {
				String expected = getFileContents(getOutputTestFileName(cuNames[i]));
				String actual = cus[i].getSource();
				assertEqualLines(expected, actual);
			}
		} finally {
			performDummySearch();
			for (int i = 0; i < cus.length; i++) {
				cus[i].delete(false, null);
			}

		}
	}
	
	private void performPushDown_fail(String[] cuNames, String[] methodNames, String[][] signatures, String[] fieldNames, String nameOfDeclaringType)
			throws Exception {
		ICompilationUnit[] cus = createCUs(cuNames);
		try {

			IType declaringType = getType(cus[0], nameOfDeclaringType);
			IMethod[] methods = getMethods(declaringType, methodNames, signatures);
			IField[] fields = getFields(declaringType, fieldNames);
			IMember[] members = merge(methods, fields);

			PushDownRefactoringProcessor processor = createRefactoringProcessor(members);
			Refactoring ref = processor.getRefactoring();

			assertTrue("activation", ref.checkInitialConditions(new NullProgressMonitor()).isOK());
			
			RefactoringStatus result = performRefactoring(ref);
			assertNotNull("precondition was supposed to fail.", result);
			assertTrue("precondition was supposed to fail.", !result.isOK());
			assertNotNull("precondition result is expected to contain an error.", result.getEntryMatchingSeverity(RefactoringStatus.ERROR));

		} finally {
			performDummySearch();
			for (int i = 0; i < cus.length; i++) {
				cus[i].delete(false, null);
			}

		}
	}
	
	private static PushDownRefactoringProcessor createRefactoringProcessor(IMember[] methods) throws JavaModelException {
		if (RefactoringAvailabilityTester.isPullUpAvailable(methods)) {
			PushDownRefactoringProcessor processor = new PushDownRefactoringProcessor(methods);
			new ProcessorBasedRefactoring(processor);
			return processor;
		}
		return null;
	}
	
	//--------------------------------------------------------
	public void testPushDownMethodToImplicitSubclass() throws Exception {
		performPushDown_pass(new String[] { "TSuper", "T", "B" }, new String[] { "rm" }, new String[][] { new String[0] }, new String[] {}, "R");
	}
	
	public void testDeclaringTypeHasADirectPhantomSubRole() throws Exception {
		performPushDown_fail(new String[] { "TSuper", "T1", "T2" }, new String[] { "rm" }, new String[][] { new String[0] }, new String[] {}, "R");
	}
	
	public void testPushedDownMethodReferencedInMethodBinding1() throws Exception {
		performPushDown_fail(new String[] { "B1", "B2", "T" }, new String[] { "bm" }, new String[][] { new String[0] }, new String[] {}, "B1");
	}
	
	public void testPushedDownMethodReferencedInMethodBinding2() throws Exception {
		performPushDown_fail(new String[] { "B1", "B2", "T" }, new String[] { "bm" }, new String[][] { new String[0] }, new String[] {}, "B1");
	}
	
	public void testPushedDownMethodReferencedInMethodBinding3() throws Exception {
		performPushDown_fail(new String[] { "T1", "T2", "B" }, new String[] { "rm" }, new String[][] { new String[0] }, new String[] {}, "Role");
	}	
	
	public void testPushedDownMethodIsReferencedInRegularGuard1() throws Exception {
		performPushDown_fail(new String[] { "T", "B" }, new String[] { "b" }, new String[][] { new String[0] }, new String[] {}, "R1");
	}	
	
	public void testPushedDownMethodIsReferencedInRegularGuard2() throws Exception {
		performPushDown_fail(new String[] { "T", "B" }, new String[] { "b" }, new String[][] { new String[0] }, new String[] {}, "R1");
	}	
	
	public void testPushedDownMethodIsReferencedInBaseGuard1() throws Exception {
		performPushDown_fail(new String[] { "B1", "B2", "T" }, new String[] { "b" }, new String[][] { new String[0] }, new String[] {}, "B1");
	}
	
	public void testPushedDownMethodIsReferencedInBaseGuard2() throws Exception {
		performPushDown_fail(new String[] { "B1", "B2", "T" }, new String[] { "b" }, new String[][] { new String[0] }, new String[] {}, "B1");
	}

	public void testPushedDownMethodIsReferencedInBaseGuard3() throws Exception {
		performPushDown_fail(new String[] { "B1", "B2", "T" }, new String[] { "b" }, new String[][] { new String[0] }, new String[] {}, "B1");
	}
	
	public void testPushedDownMethodIsReferencedInParameterMapping1() throws Exception {
		performPushDown_fail(new String[] { "T", "B" }, new String[] { "f" }, new String[][] { new String[0] }, new String[] {}, "R1");
	}

	public void testPushedDownMethodIsReferencedInParameterMapping2() throws Exception {
		performPushDown_fail(new String[] { "T", "B" }, new String[] { "f" }, new String[][] { new String[0] }, new String[] {}, "R1");
	}

	public void testPushedDownMethodIsReferencedInParameterMapping3() throws Exception {
		performPushDown_fail(new String[] { "T", "B" }, new String[] { "f" }, new String[][] { new String[0] }, new String[] {}, "R1");
	}
	
	/* Push Down Field Tests */
	public void testPushDownFieldToImplicitSubclass() throws Exception {
		performPushDown_pass(new String[] { "TSuper", "T", "B" }, new String[] {}, new String[][] { new String[0] }, new String[] { "f" }, "R");
	}
	
	public void testPushedDownFieldReferencedInCalloutToField() throws Exception {
		performPushDown_fail(new String[] { "B1", "B2", "T" }, new String[] {}, new String[][] { new String[0] }, new String[] { "f" }, "B1");
	}
	
	public void testPushDownTypeAnchorInstance() throws Exception {
		performPushDown_fail(new String[] { "T1", "T2", "T3" }, new String[] {}, new String[][] { new String[0] }, new String[] { "anchor" }, "T1");
	}
	
	public void testPushedDownFieldWouldShadowAnInheritedField() throws Exception {
		performPushDown_fail(new String[] { "B", "T1", "T2" }, new String[] {}, new String[][] { new String[0] }, new String[] { "f" }, "B");
	}
	
	public void testPushedDownFieldIsReferencedInRegularGuard1() throws Exception {
		performPushDown_fail(new String[] { "T", "B" }, new String[] {}, new String[][] { new String[0] }, new String[] { "b" }, "R1");
	}

	public void testPushedDownFieldIsReferencedInRegularGuard2() throws Exception {
		performPushDown_fail(new String[] { "T", "B" }, new String[] {}, new String[][] { new String[0] }, new String[] { "b" }, "R1");
	}

	public void testPushedDownFieldIsReferencedInBaseGuard1() throws Exception {
		performPushDown_fail(new String[] { "B1", "B2", "T" }, new String[] {}, new String[][] { new String[0] }, new String[] { "b" }, "B1");
	}

	public void testPushedDownFieldIsReferencedInBaseGuard2() throws Exception {
		performPushDown_fail(new String[] { "B1", "B2", "T" }, new String[] {}, new String[][] { new String[0] }, new String[] { "b" }, "B1");
	}

	public void testPushedDownFieldIsReferencedInBaseGuard3() throws Exception {
		performPushDown_fail(new String[] { "B1", "B2", "T" }, new String[] {}, new String[][] { new String[0] }, new String[] { "b" }, "B1");
	}
	
	public void testPushedDownFieldIsReferencedInParameterMapping1() throws Exception {
		performPushDown_fail(new String[] { "T", "B" }, new String[] {}, new String[][] { new String[0] }, new String[] { "f" }, "R1");
	}
	
	public void testPushedDownFieldIsReferencedInParameterMapping2() throws Exception {
		performPushDown_fail(new String[] { "T", "B" }, new String[] {}, new String[][] { new String[0] }, new String[] { "f" }, "R1");
	}
	
	public void testOverrideImplicitInheritedMethod() throws Exception {
		performPushDown_fail(new String[] { "A", "T1", "T2" }, new String[] { "m" }, new String[][] { new String[0] }, new String[] {}, "A");
	}
	
	public void testOverrideExplicitInheritedMethod() throws Exception {
		performPushDown_pass(new String[] { "T2", "T1", "A" }, new String[] { "m" }, new String[][] { new String[0] }, new String[] {}, "R");
	}
	
	// See Ticket #286
	public void testPushedDownFieldIsReferencedInParameterMapping3() throws Exception {
		performPushDown_fail(new String[] { "T", "B" }, new String[] {}, new String[][] { new String[0] }, new String[] { "f" }, "R1");
	}

}

Back to the top