001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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 java.util.Hashtable;
013:
014: import junit.framework.Test;
015: import junit.framework.TestSuite;
016:
017: import org.eclipse.core.runtime.NullProgressMonitor;
018:
019: import org.eclipse.jface.preference.IPreferenceStore;
020:
021: import org.eclipse.jdt.core.ICompilationUnit;
022: import org.eclipse.jdt.core.IPackageFragment;
023: import org.eclipse.jdt.core.ISourceRange;
024: import org.eclipse.jdt.core.JavaCore;
025: import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
026:
027: import org.eclipse.jdt.ui.tests.refactoring.infra.TextRangeUtil;
028:
029: import org.eclipse.jdt.internal.corext.refactoring.base.RefactoringStatusCodes;
030: import org.eclipse.jdt.internal.corext.refactoring.code.ExtractConstantRefactoring;
031:
032: import org.eclipse.jdt.ui.PreferenceConstants;
033:
034: import org.eclipse.jdt.internal.ui.JavaPlugin;
035:
036: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
037:
038: public class ExtractConstantTests extends RefactoringTest {
039:
040: private static final Class clazz = ExtractConstantTests.class;
041: private static final String REFACTORING_PATH = "ExtractConstant/";
042:
043: private static final boolean BUG_86113_ImportRewrite = true;
044:
045: private Object fCompactPref;
046: private boolean fAddComments;
047:
048: public ExtractConstantTests(String name) {
049: super (name);
050: }
051:
052: protected String getRefactoringPath() {
053: return REFACTORING_PATH;
054: }
055:
056: public static Test suite() {
057: return new RefactoringTestSetup(new TestSuite(clazz));
058: }
059:
060: public static Test setUpTest(Test test) {
061: return new RefactoringTestSetup(test);
062: }
063:
064: private String getSimpleTestFileName(boolean canInline,
065: boolean input) {
066: String fileName = "A_" + getName();
067: if (canInline)
068: fileName += input ? "_in" : "_out";
069: return fileName + ".java";
070: }
071:
072: private String getTestFileName(boolean canExtract, boolean input) {
073: String fileName = TEST_PATH_PREFIX + getRefactoringPath();
074: fileName += (canExtract ? "canExtract/" : "cannotExtract/");
075: return fileName + getSimpleTestFileName(canExtract, input);
076: }
077:
078: protected ICompilationUnit createCUfromTestFile(
079: IPackageFragment pack, boolean canExtract, boolean input)
080: throws Exception {
081: return createCU(pack, getSimpleTestFileName(canExtract, input),
082: getFileContents(getTestFileName(canExtract, input)));
083: }
084:
085: protected void setUp() throws Exception {
086: super .setUp();
087: Hashtable options = JavaCore.getOptions();
088:
089: String setting = DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_ASSIGNMENT_OPERATOR;
090: fCompactPref = options.get(setting);
091: options.put(setting, DefaultCodeFormatterConstants.TRUE);
092: JavaCore.setOptions(options);
093:
094: IPreferenceStore store = JavaPlugin.getDefault()
095: .getPreferenceStore();
096: fAddComments = store
097: .getBoolean(PreferenceConstants.CODEGEN_ADD_COMMENTS);
098: store.setValue(PreferenceConstants.CODEGEN_ADD_COMMENTS, false);
099: }
100:
101: protected void tearDown() throws Exception {
102: super .tearDown();
103: Hashtable options = JavaCore.getOptions();
104: options
105: .put(
106: DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_ASSIGNMENT_OPERATOR,
107: fCompactPref);
108: JavaCore.setOptions(options);
109:
110: IPreferenceStore store = JavaPlugin.getDefault()
111: .getPreferenceStore();
112: store.setValue(PreferenceConstants.CODEGEN_ADD_COMMENTS,
113: fAddComments);
114: }
115:
116: private void guessHelper(int startLine, int startColumn,
117: int endLine, int endColumn, String expectedGuessedName)
118: throws Exception {
119: ICompilationUnit cu = createCU(getPackageP(), getName()
120: + ".java", getFileContents(TEST_PATH_PREFIX
121: + getRefactoringPath() + "nameGuessing/" + getName()
122: + ".java"));
123: ISourceRange selection = TextRangeUtil.getSelection(cu,
124: startLine, startColumn, endLine, endColumn);
125: ExtractConstantRefactoring ref = new ExtractConstantRefactoring(
126: cu, selection.getOffset(), selection.getLength());
127: RefactoringStatus preconditionResult = ref
128: .checkInitialConditions(new NullProgressMonitor());
129: assertTrue("activation was supposed to be successful"
130: + preconditionResult.toString(), preconditionResult
131: .isOK());
132: assertEquals("constant name not guessed", expectedGuessedName,
133: ref.guessConstantName());
134: }
135:
136: private void helper1(int startLine, int startColumn, int endLine,
137: int endColumn, boolean replaceAll, boolean allowLoadtime,
138: boolean qualifyReferencesWithConstantName,
139: String constantName, String guessedConstantName)
140: throws Exception {
141: ICompilationUnit cu = createCUfromTestFile(getPackageP(), true,
142: true);
143: ISourceRange selection = TextRangeUtil.getSelection(cu,
144: startLine, startColumn, endLine, endColumn);
145: ExtractConstantRefactoring ref = new ExtractConstantRefactoring(
146: cu, selection.getOffset(), selection.getLength());
147: RefactoringStatus preconditionResult = ref
148: .checkInitialConditions(new NullProgressMonitor());
149: assertTrue("activation was supposed to be successful"
150: + preconditionResult.toString(), preconditionResult
151: .isOK());
152:
153: if (!allowLoadtime)
154: assertTrue(
155: "The selected expression has been erroneously reported to contain references to non-static or non-final fields.",
156: ref.selectionAllStaticFinal());
157:
158: ref.setReplaceAllOccurrences(replaceAll);
159: ref
160: .setQualifyReferencesWithDeclaringClassName(qualifyReferencesWithConstantName);
161: ref.setConstantName(constantName);
162:
163: assertEquals("constant name incorrectly guessed",
164: guessedConstantName, ref.guessConstantName());
165:
166: RefactoringStatus checkInputResult = ref
167: .checkFinalConditions(new NullProgressMonitor());
168: assertTrue("precondition was supposed to pass",
169: checkInputResult.isOK());
170:
171: performChange(ref, false);
172:
173: IPackageFragment pack = (IPackageFragment) cu.getParent();
174: String newCuName = getSimpleTestFileName(true, true);
175: ICompilationUnit newcu = pack.getCompilationUnit(newCuName);
176: assertTrue(newCuName + " does not exist", newcu.exists());
177: assertEqualLines(getFileContents(getTestFileName(true, false)),
178: newcu.getSource());
179: }
180:
181: private void helper1(int startLine, int startColumn, int endLine,
182: int endColumn, boolean replaceAll, boolean allowLoadtime,
183: String constantName, String guessedConstantName)
184: throws Exception {
185: helper1(startLine, startColumn, endLine, endColumn, replaceAll,
186: allowLoadtime, false, constantName, guessedConstantName);
187: }
188:
189: private void helper1(int startLine, int startColumn, int endLine,
190: int endColumn, boolean replaceAll, boolean allowLoadtime,
191: boolean qualifyReferencesWithConstantName,
192: String constantName) throws Exception {
193: helper1(startLine, startColumn, endLine, endColumn, replaceAll,
194: allowLoadtime, qualifyReferencesWithConstantName,
195: constantName, constantName);
196: }
197:
198: private void helper1(int startLine, int startColumn, int endLine,
199: int endColumn, boolean replaceAll, boolean allowLoadtime,
200: String constantName) throws Exception {
201: helper1(startLine, startColumn, endLine, endColumn, replaceAll,
202: allowLoadtime, false, constantName);
203: }
204:
205: private void failHelper1(int startLine, int startColumn,
206: int endLine, int endColumn, boolean replaceAll,
207: boolean allowLoadtime, String constantName)
208: throws Exception {
209: failHelper1(startLine, startColumn, endLine, endColumn,
210: replaceAll, allowLoadtime, constantName, 0, false);
211: }
212:
213: private void failHelper1(int startLine, int startColumn,
214: int endLine, int endColumn, boolean replaceAll,
215: boolean allowLoadtime, String constantName, int errorCode,
216: boolean checkCode) throws Exception {
217: ICompilationUnit cu = createCUfromTestFile(getPackageP(),
218: false, true);
219: ISourceRange selection = TextRangeUtil.getSelection(cu,
220: startLine, startColumn, endLine, endColumn);
221: ExtractConstantRefactoring ref = new ExtractConstantRefactoring(
222: cu, selection.getOffset(), selection.getLength());
223: ref.setReplaceAllOccurrences(replaceAll);
224: ref.setConstantName(constantName);
225: RefactoringStatus result = performRefactoring(ref);
226:
227: if (!allowLoadtime && !ref.selectionAllStaticFinal())
228: return;
229:
230: assertNotNull("precondition was supposed to fail", result);
231: if (checkCode)
232: assertEquals(errorCode, result.getEntryMatchingSeverity(
233: RefactoringStatus.ERROR).getCode());
234: }
235:
236: //--- TESTS
237:
238: public void test0() throws Exception {
239: helper1(5, 16, 5, 17, true, false, "CONSTANT", "_0");
240: }
241:
242: public void test1() throws Exception {
243: helper1(5, 16, 5, 21, false, false, "CONSTANT", "INT");
244: }
245:
246: public void test2() throws Exception {
247: helper1(8, 16, 8, 27, false, false, "CONSTANT", "INT");
248: }
249:
250: public void test3() throws Exception {
251: helper1(8, 16, 8, 27, true, false, "CONSTANT", "INT");
252: }
253:
254: public void test4() throws Exception {
255: helper1(5, 23, 5, 34, true, false, "CONSTANT", "INT");
256: }
257:
258: public void test5() throws Exception {
259: helper1(11, 20, 11, 26, true, true, "CONSTANT", "RG");
260: }
261:
262: public void test6() throws Exception {
263: helper1(13, 20, 13, 35, true, true, "CONSTANT", "RF");
264: }
265:
266: public void test7() throws Exception {
267: helper1(12, 20, 12, 28, true, true, "CONSTANT", "RG");
268: }
269:
270: public void test8() throws Exception {
271: helper1(8, 16, 8, 22, true, true, "CONSTANT", "INT");
272: }
273:
274: public void test9() throws Exception {
275: helper1(6, 24, 6, 29, true, true, "CONSTANT", "INT");
276: }
277:
278: public void test10() throws Exception {
279: helper1(8, 17, 8, 22, true, true, "CONSTANT", "INT");
280: }
281:
282: public void test11() throws Exception {
283: helper1(10, 37, 10, 43, true, true, "CONSTANT", "INT");
284: }
285:
286: public void test12() throws Exception {
287: helper1(9, 19, 9, 24, true, true, "CONSTANT", "INT");
288: }
289:
290: public void test13() throws Exception {
291: helper1(9, 16, 9, 28, true, true, "CONSTANT", "F");
292: }
293:
294: public void test14() throws Exception {
295: helper1(10, 28, 10, 44, true, true, "CONSTANT", "INT");
296: }
297:
298: public void test15() throws Exception {
299: helper1(5, 16, 5, 25, true, false, "CONSTANT", "FRED");
300: }
301:
302: public void test16() throws Exception {
303: helper1(5, 20, 5, 27, true, false, "CONSTANT", "RED");
304: }
305:
306: public void test17() throws Exception {
307: helper1(5, 16, 5, 35, true, false, "CONSTANT",
308: "YET_ANOTHER_FRED");
309: }
310:
311: public void test18() throws Exception {
312: helper1(5, 16, 5, 17, true, false, true, "CONSTANT", "_0");
313: }
314:
315: public void test19() throws Exception {
316: helper1(5, 23, 5, 38, false, false, "CONSTANT", "STRING");
317: }
318:
319: public void test20() throws Exception {
320: helper1(7, 19, 7, 28, false, false, "CONSTANT", "STRING");
321: }
322:
323: public void test21() throws Exception {
324: helper1(4, 28, 4, 37, false, false, "CONSTANT", "STRING");
325: }
326:
327: public void test22() throws Exception {
328: helper1(9, 35, 9, 59, false, false, "ITEMS", "ARRAY_LIST");
329: }
330:
331: public void test23() throws Exception {
332: helper1(14, 12, 14, 15, true, false, "COLOR");
333: }
334:
335: public void test24() throws Exception {
336: if (BUG_86113_ImportRewrite) {
337: printTestDisabledMessage("BUG_86113_ImportRewrite");
338: return;
339: }
340: helper1(9, 28, 9, 36, true, false, "NUM", "ENUM");
341: }
342:
343: public void test25() throws Exception {
344: helper1(5, 27, 5, 40, false, false, "DEFAULT_NAME",
345: "JEAN_PIERRE");
346: }
347:
348: public void test26() throws Exception {
349: helper1(6, 16, 6, 32, true, false, true, "INT", "A");
350: }
351:
352: public void test27() throws Exception {
353: helper1(13, 14, 13, 19, true, false, false, "FOO", "FOO");
354: }
355:
356: public void test28() throws Exception {
357: helper1(13, 14, 13, 19, true, false, false, "FOO", "FOO");
358: }
359:
360: public void test29() throws Exception {
361: helper1(12, 19, 12, 28, false, true, "NUMBER", "NUMBER");
362: }
363:
364: public void test30() throws Exception {
365: helper1(12, 19, 12, 28, false, true, "INTEGER", "INTEGER");
366: }
367:
368: public void test31() throws Exception { //bug 104293
369: helper1(9, 32, 9, 44, true, false, "AS_LIST", "AS_LIST");
370: }
371:
372: public void test32() throws Exception { //bug 104293
373: helper1(7, 20, 7, 35, true, false, "STRING", "STRING");
374: }
375:
376: public void test33() throws Exception { //bug 108354
377: helper1(7, 20, 7, 35, true, false, "STRING", "STRING");
378: }
379:
380: public void test34() throws Exception { // syntax error
381: helper1(7, 20, 7, 35, true, false, "STRING", "STRING");
382: }
383:
384: public void testZeroLengthSelection0() throws Exception {
385: helper1(5, 18, 5, 18, false, false, "CONSTANT", "_100");
386: }
387:
388: // -- testing failing preconditions
389: public void testFail0() throws Exception {
390: failHelper1(8, 16, 8, 21, true, true, "CONSTANT");
391: }
392:
393: public void testFail1() throws Exception {
394: failHelper1(8, 16, 8, 26, true, true, "CONSTANT");
395: }
396:
397: public void testFail2() throws Exception {
398: failHelper1(9, 20, 9, 21, true, true, "CONSTANT");
399: }
400:
401: public void testFail3() throws Exception {
402: failHelper1(9, 18, 9, 25, true, true, "CONSTANT");
403: }
404:
405: public void testFail4() throws Exception {
406: failHelper1(6, 16, 6, 20, true, true, "CONSTANT");
407: }
408:
409: public void testFail5() throws Exception {
410: failHelper1(9, 16, 9, 25, true, true, "CONSTANT");
411: }
412:
413: public void testFail6() throws Exception {
414: failHelper1(11, 20, 11, 24, true, true, "CONSTANT");
415: }
416:
417: public void testFail7() throws Exception {
418: failHelper1(11, 20, 11, 34, true, true, "CONSTANT");
419: }
420:
421: public void testFail10() throws Exception {
422: failHelper1(15, 20, 15, 37, true, false, "CONSTANT");
423: }
424:
425: public void testFail11() throws Exception {
426: failHelper1(8, 16, 8, 22, true, false, "CONSTANT");
427: }
428:
429: public void testFail12() throws Exception {
430: failHelper1(4, 7, 4, 8, true, true, "CONSTANT",
431: RefactoringStatusCodes.EXPRESSION_NOT_RVALUE, true);
432: }
433:
434: public void testFail13() throws Exception {
435: failHelper1(2, 9, 2, 10, true, true, "CONSTANT",
436: RefactoringStatusCodes.EXPRESSION_NOT_RVALUE, true);
437: }
438:
439: public void testFail14() throws Exception {
440: failHelper1(5, 9, 5, 11, true, true, "CONSTANT");
441: }
442:
443: public void testFail15() throws Exception {
444: failHelper1(5, 10, 5, 13, true, true, "CONSTANT");
445: }
446:
447: public void testFail16() throws Exception {
448: failHelper1(9, 20, 9, 38, true, false, "CONSTANT");
449: }
450:
451: public void testFail17() throws Exception {
452: failHelper1(16, 18, 16, 21, true, true, "COLOR");
453: }
454:
455: public void testGuessStringLiteral0() throws Exception {
456: //test for bug 37377
457: guessHelper(4, 19, 4, 32, "FOO_HASH_MAP");
458: }
459:
460: public void testGuessStringLiteral1() throws Exception {
461: //test for bug 37377
462: guessHelper(4, 19, 4, 33, "FOO_HASH_MAP");
463: }
464:
465: public void testGuessStringLiteral2() throws Exception {
466: //test for bug 37377
467: guessHelper(4, 19, 4, 56, "HANS_IM_GLUECK123_34_BLA_BLA");
468: }
469:
470: public void testGuessStringLiteral3() throws Exception {
471: guessHelper(5, 16, 5, 16, "ASSUME_CAMEL_CASE");
472: }
473:
474: public void testGuessFromGetterName0() throws Exception {
475: guessHelper(4, 19, 4, 30, "FOO_BAR");
476: }
477:
478: public void testGuessFromGetterName1() throws Exception {
479: guessHelper(4, 23, 4, 33, "FOO_BAR");
480: }
481: }
|