001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2007 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.java.hints.errors;
041:
042: import com.sun.source.util.TreePath;
043: import java.util.List;
044: import org.netbeans.api.java.source.CompilationInfo;
045: import org.netbeans.modules.java.hints.infrastructure.ErrorHintsTestBase;
046: import org.netbeans.spi.editor.hints.Fix;
047:
048: /**
049: *
050: * @author Jan Lahoda
051: */
052: public class OrigSurroundWithTryCatchFixTest extends ErrorHintsTestBase {
053:
054: public OrigSurroundWithTryCatchFixTest(String testName) {
055: super (testName);
056: }
057:
058: @Override
059: protected void setUp() throws Exception {
060: super .setUp();
061: MagicSurroundWithTryCatchFix.DISABLE_JAVA_UTIL_LOGGER = true;
062: }
063:
064: public void testTryWrapper1() throws Exception {
065: performFixTest(
066: "test/Test.java",
067: "package test;\n"
068: + "import java.io.File;\n"
069: + "import java.io.FileInputStream;\n"
070: + "public abstract class Test {\n"
071: + " public Test() {\n"
072: + " FileInputStream fis = |new FileInputStream(new File(\"\"));\n"
073: + " }\n" + "}\n",
074: "FixImpl",
075: ("package test;\n"
076: + "import java.io.File;\n"
077: + "import java.io.FileInputStream;\n"
078: + "import java.io.FileNotFoundException;\n"
079: + "public abstract class Test {\n"
080: + " public Test() {\n"
081: + " try {\n"
082: + " FileInputStream fis = new FileInputStream(new File(\"\"));\n"
083: + " } catch (FileNotFoundException ex) {\n"
084: + " ex.printStackTrace();\n"
085: + " }" + " }\n" + "}\n").replaceAll(
086: "[ \t\n]+", " "));
087: }
088:
089: public void testTryWrapper2() throws Exception {
090: performFixTest(
091: "test/Test.java",
092: "package test;\n"
093: + "import java.io.File;\n"
094: + "import java.io.FileInputStream;\n"
095: + "public abstract class Test {\n"
096: + " public Test() {\n"
097: + " FileInputStream fis = |new FileInputStream(new File(\"\"));\n"
098: + " fis.read();\n" + " }\n" + "}\n",
099: "FixImpl",
100: ("package test;\n"
101: + "import java.io.File;\n"
102: + "import java.io.FileInputStream;\n"
103: + "import java.io.FileNotFoundException;\n"
104: + "public abstract class Test {\n"
105: + " public Test() {\n"
106: + " FileInputStream fis;\n"
107: + " try {\n"
108: + " fis = new FileInputStream(new File(\"\"));\n"
109: + " } catch (FileNotFoundException ex) {\n"
110: + " ex.printStackTrace();\n"
111: + " }" + " fis.read();\n"
112: + " }\n" + "}\n")
113: .replaceAll("[ \t\n]+", " "));
114: }
115:
116: public void testTryWrapper3() throws Exception {
117: performFixTest(
118: "test/Test.java",
119: "package test;\n"
120: + "import java.io.File;\n"
121: + "import java.io.FileInputStream;\n"
122: + "public abstract class Test {\n"
123: + " public Test() {\n"
124: + " {\n"
125: + " FileInputStream fis = |new FileInputStream(new File(\"\"));\n"
126: + " }\n"
127: + " {\n"
128: + " FileInputStream fis = new FileInputStream(new File(\"\"));\n"
129: + " fis.read();\n" + " }\n"
130: + " }\n" + "}\n",
131: "FixImpl",
132: ("package test;\n"
133: + "import java.io.File;\n"
134: + "import java.io.FileInputStream;\n"
135: + "import java.io.FileNotFoundException;\n"
136: + "public abstract class Test {\n"
137: + " public Test() {\n"
138: + " {\n"
139: + " try {\n"
140: + " FileInputStream fis = new FileInputStream(new File(\"\"));\n"
141: + " } catch (FileNotFoundException ex) {\n"
142: + " ex.printStackTrace();\n"
143: + " }\n"
144: + " }\n"
145: + " {\n"
146: + " FileInputStream fis = new FileInputStream(new File(\"\"));\n"
147: + " fis.read();\n" + " }\n"
148: + " }\n" + "}\n")
149: .replaceAll("[ \t\n]+", " "));
150: }
151:
152: public void testTryWrapper4() throws Exception {
153: performFixTest(
154: "test/Test.java",
155: "package test;\n"
156: + "import java.io.File;\n"
157: + "import java.io.FileInputStream;\n"
158: + "public abstract class Test {\n"
159: + " public Test() {\n"
160: + " FileInputStream a,b,c,fis = |new FileInputStream(new File(\"\"));\n"
161: + " fis.read();\n" + " }\n" + "}\n",
162: "FixImpl",
163: ("package test;\n"
164: + "import java.io.File;\n"
165: + "import java.io.FileInputStream;\n"
166: + "import java.io.FileNotFoundException;\n"
167: + "public abstract class Test {\n"
168: + " public Test() {\n"
169: + " FileInputStream a,b,c,fis;\n"
170: + " try {\n"
171: + " fis = new FileInputStream(new File(\"\"));\n"
172: + " } catch (FileNotFoundException ex) {\n"
173: + " ex.printStackTrace();\n"
174: + " }" + " fis.read();\n"
175: + " }\n" + "}\n")
176: .replaceAll("[ \t\n]+", " "));
177: }
178:
179: public void testTryWrapper5() throws Exception {
180: performFixTest(
181: "test/Test.java",
182: "package test;\n"
183: + "import java.io.File;\n"
184: + "import java.io.FileInputStream;\n"
185: + "public abstract class Test {\n"
186: + " public Test() {\n"
187: + " FileInputStream a,b,c,fis = |new FileInputStream(new File(\"\")),d,e,f;\n"
188: + " fis.read();\n" + " }\n" + "}\n",
189: "FixImpl",
190: ("package test;\n"
191: + "import java.io.File;\n"
192: + "import java.io.FileInputStream;\n"
193: + "import java.io.FileNotFoundException;\n"
194: + "public abstract class Test {\n"
195: + " public Test() {\n"
196: + " FileInputStream a,b,c,fis,d,e,f;\n"
197: + " try {\n"
198: + " fis = new FileInputStream(new File(\"\"));\n"
199: + " } catch (FileNotFoundException ex) {\n"
200: + " ex.printStackTrace();\n"
201: + " }" + " fis.read();\n"
202: + " }\n" + "}\n")
203: .replaceAll("[ \t\n]+", " "));
204: }
205:
206: public void testTryWrapper6() throws Exception {
207: performFixTest(
208: "test/Test.java",
209: "package test;\n"
210: + "import java.io.File;\n"
211: + "import java.io.FileInputStream;\n"
212: + "public abstract class Test {\n"
213: + " public Test() {\n"
214: + " FileInputStream fis = |new FileInputStream(new File(\"\")),a,b,c;\n"
215: + " fis.read();\n" + " }\n" + "}\n",
216: "FixImpl",
217: ("package test;\n"
218: + "import java.io.File;\n"
219: + "import java.io.FileInputStream;\n"
220: + "import java.io.FileNotFoundException;\n"
221: + "public abstract class Test {\n"
222: + " public Test() {\n"
223: + " FileInputStream fis,a,b,c;\n"
224: + " try {\n"
225: + " fis = new FileInputStream(new File(\"\"));\n"
226: + " } catch (FileNotFoundException ex) {\n"
227: + " ex.printStackTrace();\n"
228: + " }" + " fis.read();\n"
229: + " }\n" + "}\n")
230: .replaceAll("[ \t\n]+", " "));
231: }
232:
233: public void XtestTryWrapper7() throws Exception {
234: } //TODO: see the original test
235:
236: public void testTryWrapper8() throws Exception {
237: performFixTest(
238: "test/Test.java",
239: "package test;\n"
240: + "import java.io.File;\n"
241: + "import java.io.FileInputStream;\n"
242: + "public abstract class Test {\n"
243: + " public Test() {\n"
244: + " FileInputStream fis;\n"
245: + " fis = |new FileInputStream(new File(\"\"));\n"
246: + " fis.read();\n" + " }\n" + "}\n",
247: "FixImpl",
248: ("package test;\n"
249: + "import java.io.File;\n"
250: + "import java.io.FileInputStream;\n"
251: + "import java.io.FileNotFoundException;\n"
252: + "public abstract class Test {\n"
253: + " public Test() {\n"
254: + " FileInputStream fis;\n"
255: + " try {\n"
256: + " fis = new FileInputStream(new File(\"\"));\n"
257: + " } catch (FileNotFoundException ex) {\n"
258: + " ex.printStackTrace();\n"
259: + " }" + " fis.read();\n"
260: + " }\n" + "}\n")
261: .replaceAll("[ \t\n]+", " "));
262: }
263:
264: public void testTryWrapper9() throws Exception {
265: performFixTest("test/Test.java", "package test;\n"
266: + "import java.io.IOException;\n"
267: + "public abstract class Test {\n"
268: + " public Test() {\n"
269: + " |throw new IOException();\n" + " }\n"
270: + "}\n", "FixImpl", ("package test;\n"
271: + "import java.io.IOException;\n"
272: + "public abstract class Test {\n"
273: + " public Test() {\n" + " try {\n"
274: + " throw new IOException();\n"
275: + " } catch (IOException ex) {\n"
276: + " ex.printStackTrace();\n" + " }"
277: + " }\n" + "}\n").replaceAll("[ \t\n]+", " "));
278: }
279:
280: @Override
281: protected List<Fix> computeFixes(CompilationInfo info, int pos,
282: TreePath path) throws Exception {
283: return new UncaughtException().run(info, null, pos, path, null);
284: }
285:
286: @Override
287: protected String toDebugString(CompilationInfo info, Fix f) {
288: if (f instanceof OrigSurroundWithTryCatchFix) {
289: return "FixImpl";
290: }
291:
292: return super.toDebugString(info, f);
293: }
294:
295: }
|