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.quickfix;
011:
012: import java.util.ArrayList;
013: import java.util.HashMap;
014:
015: import junit.framework.Test;
016: import junit.framework.TestSuite;
017:
018: import org.eclipse.jdt.core.ICompilationUnit;
019: import org.eclipse.jdt.core.IJavaProject;
020: import org.eclipse.jdt.core.IPackageFragment;
021: import org.eclipse.jdt.core.IPackageFragmentRoot;
022: import org.eclipse.jdt.core.JavaCore;
023: import org.eclipse.jdt.core.dom.CompilationUnit;
024:
025: import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
026:
027: import org.eclipse.jdt.testplugin.JavaProjectHelper;
028: import org.eclipse.jdt.testplugin.TestOptions;
029:
030: import org.eclipse.jdt.ui.tests.core.ProjectTestSetup;
031:
032: public class QuickFixEnablementTest extends QuickFixTest {
033:
034: private static final Class THIS = QuickFixEnablementTest.class;
035:
036: private IJavaProject fJProject1;
037: private IPackageFragmentRoot fSourceFolder;
038:
039: public QuickFixEnablementTest(String name) {
040: super (name);
041: }
042:
043: public static Test allTests() {
044: return setUpTest(new TestSuite(THIS));
045: }
046:
047: public static Test setUpTest(Test test) {
048: return new ProjectTestSetup(test);
049: }
050:
051: public static Test suite() {
052: return allTests();
053: }
054:
055: protected void setUp() throws Exception {
056: fJProject1 = ProjectTestSetup.getProject();
057:
058: fSourceFolder = JavaProjectHelper.addSourceContainer(
059: fJProject1, "src");
060: }
061:
062: protected void tearDown() throws Exception {
063: TestOptions.initializeProjectOptions(fJProject1);
064: JavaProjectHelper.clear(fJProject1, ProjectTestSetup
065: .getDefaultClasspath());
066: }
067:
068: public void testContributedQuickFix1() throws Exception {
069:
070: HashMap options = new HashMap();
071: JavaModelUtil.setCompilanceOptions(options,
072: JavaCore.VERSION_1_5);
073: fJProject1.setOptions(options);
074:
075: // quick fix is contributed only for files with name 'A.java' in a 1.5 project
076: IPackageFragment pack1 = fSourceFolder.createPackageFragment(
077: "test1", false, null);
078: StringBuffer buf = new StringBuffer();
079: buf.append("package test1;\n");
080: buf.append("public class A {\n");
081: buf.append(" public void foo() {\n");
082: buf.append(" int x= 9999999999999999999999;\n");
083: buf.append(" }\n");
084: buf.append("}\n");
085: ICompilationUnit cu = pack1.createCompilationUnit("A.java", buf
086: .toString(), false, null);
087:
088: CompilationUnit astRoot = getASTRoot(cu);
089: ArrayList proposals = collectCorrections(cu, astRoot);
090: assertNumberOfProposals(proposals, 1);
091: assertCorrectLabels(proposals);
092:
093: String[] previewContents = getPreviewContents(proposals);
094:
095: buf = new StringBuffer();
096: buf.append("package test1;\n");
097: buf.append("public class A {\n");
098: buf.append(" public void foo() {\n");
099: buf.append(" int x= 0;\n");
100: buf.append(" }\n");
101: buf.append("}\n");
102: String expected = buf.toString();
103: assertEqualString(previewContents[0], expected);
104: }
105:
106: public void testContributedQuickFix2() throws Exception {
107: // quick fix is contributed only for files with name 'A.java' in a 1.5 project
108: IPackageFragment pack1 = fSourceFolder.createPackageFragment(
109: "test1", false, null);
110: StringBuffer buf = new StringBuffer();
111: buf.append("package test1;\n");
112: buf.append("public class A {\n");
113: buf.append(" public void foo() {\n");
114: buf.append(" int x= 9999999999999999999999;\n");
115: buf.append(" }\n");
116: buf.append("}\n");
117: ICompilationUnit cu = pack1.createCompilationUnit("A.java", buf
118: .toString(), false, null);
119:
120: HashMap options = new HashMap();
121: JavaModelUtil.setCompilanceOptions(options,
122: JavaCore.VERSION_1_6);
123: fJProject1.setOptions(options);
124:
125: assertNumberOfProposals(collectCorrections(cu, getASTRoot(cu)),
126: 1); // ok
127:
128: options = new HashMap();
129: JavaModelUtil.setCompilanceOptions(options,
130: JavaCore.VERSION_1_4);
131: fJProject1.setOptions(options);
132:
133: assertNumberOfProposals(collectCorrections(cu, getASTRoot(cu)),
134: 0); // wrong version
135:
136: buf = new StringBuffer();
137: buf.append("package test1;\n");
138: buf.append("public class B {\n");
139: buf.append(" public void foo() {\n");
140: buf.append(" int x= 9999999999999999999999;\n");
141: buf.append(" }\n");
142: buf.append("}\n");
143: cu = pack1.createCompilationUnit("B.java", buf.toString(),
144: false, null);
145:
146: options = new HashMap();
147: JavaModelUtil.setCompilanceOptions(options,
148: JavaCore.VERSION_1_5);
149: fJProject1.setOptions(options);
150:
151: assertNumberOfProposals(collectCorrections(cu, getASTRoot(cu)),
152: 0); // wrong name
153: }
154:
155: }
|