Skip to main content
summaryrefslogtreecommitdiffstats
blob: e44d775f0a1e4b03b47708065506ee639d970a2a (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*******************************************************************************
 * Copyright (c) 2000, 2010 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
 * 	   Fraunhofer FIRST - Initial API and implementation
 * 	   Technical University Berlin - Initial API and implementation
 *******************************************************************************/

package org.eclipse.objectteams.otdt.ui.tests.refactoring.rename;

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.IMethod;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.Signature;
import org.eclipse.ltk.core.refactoring.RefactoringCore;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.participants.RenameRefactoring;
import org.eclipse.objectteams.otdt.ui.tests.refactoring.MySetup;
import org.eclipse.objectteams.otdt.ui.tests.refactoring.RefactoringTest;
import org.eclipse.jdt.internal.corext.refactoring.rename.RenameMethodProcessor;
import org.eclipse.jdt.internal.corext.refactoring.rename.RenameNonVirtualMethodProcessor;

/**
 * The tests in this class have initially been copied from the original class
 * <code>RenameStaticMethodTests</code> in the test suite
 *<code>org.eclipse.jdt.ui.tests.refactoring</code> provided by Eclipse. 
 * 
 * @author brcan
 */
@SuppressWarnings("restriction")
public class RenameStaticMethodTests extends RefactoringTest
{
    private static final String REFACTORING_PATH = "RenameStaticMethod/";

    public RenameStaticMethodTests(String name)
    {
        super(name);
    }

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

    protected String getRefactoringPath()
    {
        return REFACTORING_PATH;
    }

    private RenameRefactoring createRefactoring(RenameMethodProcessor processor)
    {
        return new RenameRefactoring(processor);
    }

    private RenameMethodProcessor createProcessor(IMethod method)
    {
        return new RenameNonVirtualMethodProcessor(method);
    }

    private void performRenamingMtoK_failing() throws Exception
    {
        performRenameRefactoring_failing("m", "k", new String[0]);
    }

    private void performRenameRefactoring_failing(
            String methodName,
            String newMethodName,
            String[] signatures)
    	throws Exception
    {
        IType classA = getType(createCUfromTestFile(getPackageP(), "A"), "A");
        try
        {
            RenameMethodProcessor processor =
                createProcessor(classA.getMethod(methodName, signatures));
            RenameRefactoring refactoring = createRefactoring(processor);
            processor.setNewElementName(newMethodName);
            RefactoringStatus result = performRefactoring(refactoring);
            assertNotNull("precondition was supposed to fail", result);
        }
        finally
        {
            performDummySearch();
            classA.getCompilationUnit().delete(true, null);
        }
    }

    private void performRenaming_passing() throws Exception
    {
        performRenamingMtoK_passing(true);
    }

    /**
     * Rename method m to k and update all references.
     */
    private void performRenamingMtoK_passing(boolean updateReferences)
    	throws Exception
    {
        performRenameRefactoring_passing1("m", "k", new String[0], updateReferences);
    }

	private void performRenameRefactoring_passing2(
	        String methodName,
	        String newMethodName,
	        String[] signatures)
		throws Exception
	{
	    performRenameRefactoring_passing1(methodName, newMethodName, signatures, true);
	}

    private void performRenameRefactoring_passing1(
            String methodName,
            String newMethodName,
            String[] signatures,
            boolean updateReferences)
    	throws Exception
    {
        ICompilationUnit cu = createCUfromTestFile(getPackageP(), "A");
        try
        {
            IType classA = getType(cu, "A");
            RenameMethodProcessor processor =
                createProcessor(classA.getMethod(methodName, signatures));
            RenameRefactoring refactoring = createRefactoring(processor);
            processor.setUpdateReferences(updateReferences);
            processor.setNewElementName(newMethodName);
            assertEquals("was supposed to pass", null,
                    performRefactoring(refactoring));
            assertEqualLines("invalid renaming",
                    getFileContents(getOutputTestFileName("A")), cu.getSource());

            assertTrue("anythingToUndo", RefactoringCore.getUndoManager()
                    .anythingToUndo());
            assertTrue("! anythingToRedo", !RefactoringCore.getUndoManager()
                    .anythingToRedo());

            RefactoringCore.getUndoManager().performUndo(null,
                    new NullProgressMonitor());
            assertEqualLines("invalid undo",
                    getFileContents(getInputTestFileName("A")), cu.getSource());

            assertTrue("! anythingToUndo", !RefactoringCore.getUndoManager()
                    .anythingToUndo());
            assertTrue("anythingToRedo", RefactoringCore.getUndoManager()
                    .anythingToRedo());

            RefactoringCore.getUndoManager().performRedo(null,
                    new NullProgressMonitor());
            assertEqualLines("invalid redo",
                    getFileContents(getOutputTestFileName("A")), cu.getSource());
        }
        finally
        {
            performDummySearch();
            cu.delete(true, null);
        }
    }
	private void performRenameRefactoring_passing(String[] cuNames, String declaringTypeName, String methodName, String newMethodName, String[] signatures,
			boolean shouldPass, boolean updateReferences) throws Exception {
		ICompilationUnit[] cus = createCUs(cuNames);
		IType declaringType = getType(cus[0], declaringTypeName);
		IMethod method = declaringType.getMethod(methodName, signatures);
		RenameMethodProcessor processor = createProcessor(method);
		RenameRefactoring ref = createRefactoring(processor);
		processor.setUpdateReferences(updateReferences);
		processor.setNewElementName(newMethodName);
		RefactoringStatus status = performRefactoring(ref);

		assertEquals("was supposed to pass!", null, status);
		if (!shouldPass) {
			for (int idx = 0; idx < cus.length; idx++) {
				assertTrue("incorrect renaming because of java model!", !getFileContents(createOutputTestFileName(cus, idx)).equals(cus[idx].getSource()));
			}
			return;
		}

		for (int idx = 0; idx < cus.length; idx++) {
			String expectedRenaming = getFileContents(createOutputTestFileName(cus, idx));
			String actualRenaming = cus[idx].getSource();
			assertEqualLines("incorrect renaming!", expectedRenaming, actualRenaming);
		}
		assertTrue("anythingToUndo", RefactoringCore.getUndoManager().anythingToUndo());
		assertTrue("! anythingToRedo", !RefactoringCore.getUndoManager().anythingToRedo());

		RefactoringCore.getUndoManager().performUndo(null, new NullProgressMonitor());

		for (int idx = 0; idx < cus.length; idx++) {
			assertEqualLines("invalid undo!", getFileContents(createInputTestFileName(cus, idx)), cus[idx].getSource());
		}
		assertTrue("! anythingToUndo", !RefactoringCore.getUndoManager().anythingToUndo());
		assertTrue("anythingToRedo", RefactoringCore.getUndoManager().anythingToRedo());

		RefactoringCore.getUndoManager().performRedo(null, new NullProgressMonitor());
		for (int idx = 0; idx < cus.length; idx++) {
			assertEqualLines("invalid redo", getFileContents(createOutputTestFileName(cus, idx)), cus[idx].getSource());
		}
	}

    /********** tests **********/
// test method template
//	public void test0() throws Exception
//	{
//		helper0_passing();
//	}
    
	public void testFail0() throws Exception{
	    performRenamingMtoK_failing();
	}
	
	public void testFail1() throws Exception{
	    performRenamingMtoK_failing();
	}
	
	public void testFail2() throws Exception{
	    performRenamingMtoK_failing();
	}
	
	//testFail3 deleted
	
	public void testFail4() throws Exception{
	    performRenamingMtoK_failing();
	}
	
	public void testFail5() throws Exception{
	    performRenamingMtoK_failing();
	}
	
	public void testFail6() throws Exception{
	    performRenamingMtoK_failing();
	}
	
	public void testFail7() throws Exception{
	    performRenamingMtoK_failing();
	}
	
	public void testFail8() throws Exception{
	    performRenamingMtoK_failing();
	}
	
	public void test0() throws Exception{
	    performRenaming_passing();
	}
	
	public void test1() throws Exception{
	    performRenaming_passing();
	}
	
	public void test2() throws Exception{
	    performRenaming_passing();
	}
	
	public void test3() throws Exception{
	    performRenaming_passing();
	}
	
	public void test4() throws Exception{
	    performRenaming_passing();
	}
	
	public void test5() throws Exception{
	    performRenaming_passing();
	}
	
	public void test6() throws Exception{
	    performRenaming_passing();
	}
	
	public void test7() throws Exception{
	    performRenameRefactoring_passing2("m", "k", new String[]{Signature.SIG_INT});
	}
	
	public void test8() throws Exception{
	    performRenameRefactoring_passing2("m", "k", new String[]{Signature.SIG_INT});
	}
	
	public void test9() throws Exception{
	    performRenameRefactoring_passing1("m", "k", new String[]{Signature.SIG_INT}, false);
	}
	
	public void test10() throws Exception
	{
		ICompilationUnit cuA = createCUfromTestFile(getPackageP(), "A");
		ICompilationUnit cuB = createCUfromTestFile(getPackageP(), "B");

		IType classB = getType(cuB, "B");
		RenameMethodProcessor processor =
		    createProcessor(classB.getMethod("method", new String[0]));
		RenameRefactoring refactoring = createRefactoring(processor);
		processor.setUpdateReferences(true);
		processor.setNewElementName("newmethod");
		assertEquals("was supposed to pass", null, performRefactoring(refactoring));
		assertEqualLines("invalid renaming in A", getFileContents(
		        getOutputTestFileName("A")), cuA.getSource());
		assertEqualLines("invalid renaming in B", getFileContents(
		        getOutputTestFileName("B")), cuB.getSource());
	}

	public void test11() throws Exception
	{
		IPackageFragment packageA =
		    getRoot().createPackageFragment("a", false, new NullProgressMonitor());
		IPackageFragment packageB =
		    getRoot().createPackageFragment("b", false, new NullProgressMonitor());
		try
		{
			ICompilationUnit cuA = createCUfromTestFile(packageA, "A");
			ICompilationUnit cuB = createCUfromTestFile(packageB, "B");
	
			IType classA = getType(cuA, "A");
			RenameMethodProcessor processor =
			    createProcessor(classA.getMethod("method2", new String[0]));
			RenameRefactoring refactoring = createRefactoring(processor);
			processor.setUpdateReferences(true);
			processor.setNewElementName("fred");
			assertEquals("was supposed to pass", null, performRefactoring(refactoring));
			assertEqualLines("invalid renaming in A", getFileContents(
			        getOutputTestFileName("A")), cuA.getSource());
			assertEqualLines("invalid renaming in B", getFileContents(
			        getOutputTestFileName("B")), cuB.getSource());
		}
		finally
		{
			packageA.delete(true, new NullProgressMonitor());
			packageB.delete(true, new NullProgressMonitor());
		}
	}
	
	public void testUpdateReferenceToBaseMethodInCalloutBinding1() throws Exception {
		performRenameRefactoring_passing(new String[] { "B", "T" }, "B", "getAmount", "getQuantity", new String[0], true, true);
	}
}

Back to the top