001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.ui.tests.refactoring;
011:
012: import junit.framework.Test;
013: import junit.framework.TestSuite;
014:
015: import org.eclipse.core.runtime.Assert;
016: import org.eclipse.core.runtime.NullProgressMonitor;
017:
018: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
019:
020: import org.eclipse.jdt.core.ICompilationUnit;
021: import org.eclipse.jdt.core.ISourceRange;
022: import org.eclipse.jdt.core.dom.AST;
023:
024: import org.eclipse.jdt.internal.corext.refactoring.base.RefactoringStatusCodes;
025: import org.eclipse.jdt.internal.corext.refactoring.code.InlineConstantRefactoring;
026: import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
027:
028: import org.eclipse.jdt.ui.tests.refactoring.infra.TextRangeUtil;
029:
030: public class InlineConstantTests extends RefactoringTest {
031: private static final Class clazz = InlineConstantTests.class;
032: private static final String REFACTORING_PATH = "InlineConstant/";
033:
034: private boolean toSucceed;
035:
036: public InlineConstantTests(String name) {
037: super (name);
038: }
039:
040: protected String getRefactoringPath() {
041: return REFACTORING_PATH + successPath();
042: }
043:
044: private String successPath() {
045: return toSucceed ? "/canInline/" : "/cannotInline/";
046: }
047:
048: public static Test suite() {
049: return new RefactoringTestSetup(new TestSuite(clazz));
050: }
051:
052: public static Test setUpTest(Test test) {
053: return new RefactoringTestSetup(test);
054: }
055:
056: private String getSimpleName(String qualifiedName) {
057: return qualifiedName
058: .substring(qualifiedName.lastIndexOf('.') + 1);
059: }
060:
061: private String getQualifier(String qualifiedName) {
062: int dot = qualifiedName.lastIndexOf('.');
063: return qualifiedName.substring(0, dot != -1 ? dot : 0);
064: }
065:
066: private ICompilationUnit[] createCUs(String[] qualifiedNames)
067: throws Exception {
068: ICompilationUnit[] cus = new ICompilationUnit[qualifiedNames.length];
069: for (int i = 0; i < qualifiedNames.length; i++) {
070: Assert.isNotNull(qualifiedNames[i]);
071:
072: cus[i] = createCUfromTestFile(
073: getRoot()
074: .createPackageFragment(
075: getQualifier(qualifiedNames[i]),
076: true, null),
077: getSimpleName(qualifiedNames[i]));
078: }
079: return cus;
080: }
081:
082: private int firstIndexOf(String one, String[] others) {
083: for (int i = 0; i < others.length; i++)
084: if (one == null && others[i] == null
085: || one.equals(others[i]))
086: return i;
087: return -1;
088: }
089:
090: private void helper1(String cuQName, int startLine,
091: int startColumn, int endLine, int endColumn,
092: boolean replaceAll, boolean removeDeclaration)
093: throws Exception {
094: helper1(new String[] { cuQName }, cuQName, startLine,
095: startColumn, endLine, endColumn, replaceAll,
096: removeDeclaration);
097: }
098:
099: private void helper1(String[] cuQNames, String selectionCuQName,
100: int startLine, int startColumn, int endLine, int endColumn,
101: boolean replaceAll, boolean removeDeclaration)
102: throws Exception {
103: int selectionCuIndex = firstIndexOf(selectionCuQName, cuQNames);
104: Assert
105: .isTrue(selectionCuIndex != -1,
106: "parameter selectionCuQName must match some String in cuQNames.");
107: helper1(cuQNames, selectionCuIndex, startLine, startColumn,
108: endLine, endColumn, replaceAll, removeDeclaration);
109: }
110:
111: private void helper1(String[] cuQNames, int selectionCuIndex,
112: int startLine, int startColumn, int endLine, int endColumn,
113: boolean replaceAll, boolean removeDeclaration)
114: throws Exception {
115: Assert.isTrue(0 <= selectionCuIndex
116: && selectionCuIndex < cuQNames.length);
117:
118: toSucceed = true;
119:
120: ICompilationUnit[] cus = createCUs(cuQNames);
121: ICompilationUnit selectionCu = cus[selectionCuIndex];
122:
123: ISourceRange selection = TextRangeUtil
124: .getSelection(selectionCu, startLine, startColumn,
125: endLine, endColumn);
126: InlineConstantRefactoring ref = new InlineConstantRefactoring(
127: selectionCu, new RefactoringASTParser(AST.JLS3).parse(
128: selectionCu, true), selection.getOffset(),
129: selection.getLength());
130: if (ref.checkStaticFinalConstantNameSelected().hasFatalError())
131: ref = null;
132: RefactoringStatus preconditionResult = ref
133: .checkInitialConditions(new NullProgressMonitor());
134:
135: assertTrue("activation was supposed to be successful",
136: preconditionResult.isOK());
137:
138: ref.setReplaceAllReferences(replaceAll);
139: ref.setRemoveDeclaration(removeDeclaration);
140:
141: preconditionResult.merge(ref
142: .checkFinalConditions(new NullProgressMonitor()));
143:
144: assertTrue("precondition was supposed to pass",
145: preconditionResult.isOK());
146:
147: performChange(ref, false);
148:
149: for (int i = 0; i < cus.length; i++) {
150: String outputTestFileName = getOutputTestFileName(getSimpleName(cuQNames[i]));
151: assertEqualLines("Incorrect inline in "
152: + outputTestFileName,
153: getFileContents(outputTestFileName), cus[i]
154: .getSource());
155: }
156: }
157:
158: private void failHelper1(String cuQName, int startLine,
159: int startColumn, int endLine, int endColumn,
160: boolean replaceAll, boolean removeDeclaration, int errorCode)
161: throws Exception {
162: failHelper1(new String[] { cuQName }, cuQName, startLine,
163: startColumn, endLine, endColumn, replaceAll,
164: removeDeclaration, errorCode);
165: }
166:
167: private void failHelper1(String[] cuQNames,
168: String selectionCuQName, int startLine, int startColumn,
169: int endLine, int endColumn, boolean replaceAll,
170: boolean removeDeclaration, int errorCode) throws Exception {
171: int selectionCuIndex = firstIndexOf(selectionCuQName, cuQNames);
172: Assert
173: .isTrue(selectionCuIndex != -1,
174: "parameter selectionCuQName must match some String in cuQNames.");
175: failHelper1(cuQNames, selectionCuIndex, startLine, startColumn,
176: endLine, endColumn, replaceAll, removeDeclaration,
177: errorCode);
178: }
179:
180: private void failHelper1(String[] cuQNames, int selectionCuIndex,
181: int startLine, int startColumn, int endLine, int endColumn,
182: boolean replaceAll, boolean removeDeclaration, int errorCode)
183: throws Exception {
184: Assert.isTrue(0 <= selectionCuIndex
185: && selectionCuIndex < cuQNames.length);
186:
187: toSucceed = false;
188:
189: ICompilationUnit[] cus = createCUs(cuQNames);
190: ICompilationUnit selectionCu = cus[selectionCuIndex];
191:
192: ISourceRange selection = TextRangeUtil
193: .getSelection(selectionCu, startLine, startColumn,
194: endLine, endColumn);
195: InlineConstantRefactoring ref = new InlineConstantRefactoring(
196: selectionCu, new RefactoringASTParser(AST.JLS3).parse(
197: selectionCu, true), selection.getOffset(),
198: selection.getLength());
199: if (ref.checkStaticFinalConstantNameSelected().hasFatalError())
200: ref = null;
201: if (ref == null)
202: return;
203: RefactoringStatus result = ref
204: .checkInitialConditions(new NullProgressMonitor());
205:
206: if (!result.isOK()) {
207: assertEquals(errorCode, result.getEntryMatchingSeverity(
208: RefactoringStatus.ERROR).getCode());
209: return;
210: } else {
211:
212: ref.setReplaceAllReferences(replaceAll);
213: ref.setRemoveDeclaration(removeDeclaration);
214:
215: result.merge(ref
216: .checkFinalConditions(new NullProgressMonitor()));
217:
218: assertTrue("precondition checking is expected to fail.",
219: !result.isOK());
220: assertEquals(errorCode, result.getEntryMatchingSeverity(
221: RefactoringStatus.ERROR).getCode());
222: }
223: }
224:
225: //--- TESTS
226:
227: public void test0() throws Exception {
228: helper1("p.C", 5, 30, 5, 36, true, false);
229: }
230:
231: public void test1() throws Exception {
232: helper1("C", 3, 33, 3, 40, true, false);
233: }
234:
235: public void test2() throws Exception {
236: helper1("p.Klass", 10, 22, 10, 30, false, false);
237: }
238:
239: public void test3() throws Exception {
240: helper1("p.LeVinSuperieure", 5, 32, 5, 43, true, true);
241: }
242:
243: public void test4() throws Exception {
244: helper1("p.Klus", 5, 36, 5, 36, true, false);
245: }
246:
247: public void test5() throws Exception {
248: helper1("p.PartOfDeclNameSelected", 5, 32, 5, 34, true, true);
249: }
250:
251: public void test6() throws Exception {
252: helper1("p.CursorPositionedInReference", 8, 57, 8, 57, false,
253: false);
254: }
255:
256: public void test7() throws Exception {
257: helper1("p.PartOfReferenceSelected", 8, 52, 8, 62, false, false);
258: }
259:
260: public void test8() throws Exception {
261: helper1(new String[] { "p1.C", "p2.D" }, "p1.C", 5, 29, 5, 37,
262: true, false);
263: }
264:
265: public void test9() throws Exception {
266: helper1(new String[] { "p1.C", "p2.D", "p3.E" }, "p2.D", 8, 18,
267: 8, 26, true, true);
268: }
269:
270: public void test10() throws Exception {
271: helper1(new String[] { "p1.A", "p2.B" }, "p2.B", 9, 28, 9, 37,
272: false, false);
273: }
274:
275: public void test11() throws Exception {
276: helper1(new String[] { "p1.A", "p2.B", "p3.C" }, "p1.A", 8, 25,
277: 8, 25, false, false);
278: }
279:
280: public void test12() throws Exception {
281: helper1(new String[] { "p1.Declarer", "p2.InlineSite" },
282: "p2.InlineSite", 7, 37, 7, 43, true, false);
283: }
284:
285: public void test13() throws Exception {
286: helper1(new String[] { "p1.A", "p2.InlineSite" },
287: "p2.InlineSite", 8, 19, 8, 29, false, false);
288: }
289:
290: public void test14() throws Exception {
291: helper1("cantonzuerich.GrueziWohl", 7, 35, 7, 35, true, false);
292: }
293:
294: public void test15() throws Exception {
295: helper1("schweiz.zuerich.zuerich.Froehlichkeit", 14, 16, 14,
296: 32, true, false);
297: }
298:
299: public void test16() throws Exception {
300: helper1("p.IntegerMath", 8, 23, 8, 23, true, true);
301: }
302:
303: public void test17() throws Exception {
304: helper1("p.EnumRef", 4, 59, 4, 59, true, true);
305: }
306:
307: public void test18() throws Exception {
308: helper1("p.Annot", 5, 18, 5, 18, true, true);
309: }
310:
311: public void test19() throws Exception {
312: helper1("p.Test", 7, 36, 7, 36, true, false);
313: }
314:
315: public void test20() throws Exception {
316: helper1("p.Test", 10, 21, 10, 21, true, true);
317: }
318:
319: public void test21() throws Exception {
320: helper1(new String[] { "p.A", "q.Consts" }, "p.A", 8, 16, 8,
321: 19, true, false);
322: }
323:
324: public void test22() throws Exception {
325: helper1(new String[] { "p.A", "q.Consts", "r.Third" }, "p.A",
326: 11, 16, 11, 19, true, true);
327: }
328:
329: public void test23() throws Exception {
330: helper1("p.Test", 6, 26, 6, 26, false, false);
331: }
332:
333: public void test24() throws Exception {
334: helper1(new String[] { "p.A", "q.Consts" }, "p.A", 14, 17, 14,
335: 17, true, true);
336: }
337:
338: public void test25() throws Exception {
339: helper1("p.A", 5, 32, 5, 32, true, true);
340: }
341:
342: public void test26() throws Exception { // test for bug 93689
343: helper1("p.A", 5, 42, 5, 42, true, true);
344: }
345:
346: public void test27() throws Exception { // test for bug 109071
347: helper1("p.A", 4, 24, 4, 29, true, true);
348: }
349:
350: public void test28() throws Exception {
351: helper1(new String[] { "p.Const", "p.AnotherClass",
352: "q.UsedClass" }, "p.Const", 6, 35, 6, 43, true, true);
353: }
354:
355: public void test29() throws Exception { // test for bug 174327
356: helper1("p.A", 7, 44, 7, 44, true, true);
357: }
358:
359: // -- testing failing preconditions
360:
361: public void testFail0() throws Exception {
362: failHelper1("foo.NeueZuercherZeitung", 5, 27, 5, 28, true,
363: false, RefactoringStatusCodes.NOT_STATIC_FINAL_SELECTED);
364: }
365:
366: public void testFail1() throws Exception {
367: failHelper1("fun.Fun", 8, 35, 8, 35, false, false,
368: RefactoringStatusCodes.DECLARED_IN_CLASSFILE);
369: }
370:
371: public void testFail2() throws Exception {
372: failHelper1("p.EnumRef", 7, 22, 7, 22, true, true,
373: RefactoringStatusCodes.NOT_STATIC_FINAL_SELECTED);
374: }
375: }
|