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: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.java.hints.errors;
029:
030: import com.sun.source.util.TreePath;
031: import java.util.List;
032: import org.netbeans.api.java.source.CompilationInfo;
033: import org.netbeans.modules.java.hints.infrastructure.ErrorHintsTestBase;
034: import org.netbeans.spi.editor.hints.Fix;
035: import org.openide.filesystems.FileObject;
036: import org.openide.filesystems.FileUtil;
037: import org.openide.filesystems.URLMapper;
038: import org.openide.util.Exceptions;
039:
040: /**
041: *
042: * @author Jan Lahoda
043: */
044: public class MagicSurroundWithTryCatchFixTest extends
045: ErrorHintsTestBase {
046:
047: public MagicSurroundWithTryCatchFixTest(String testName) {
048: super (testName);
049: }
050:
051: public void test104085() throws Exception {
052: performFixTest(
053: "test/Test.java",
054: "package test; public class Test {public void test() {try {}finally{new java.io.FileInputStream(\"\");}}}",
055: 127 - 43,
056: "FixImpl",
057: "package test; import java.io.FileNotFoundException; import java.util.logging.Level; import java.util.logging.Logger; public class Test {public void test() {try {}finally{try { new java.io.FileInputStream(\"\"); } catch (FileNotFoundException ex) { Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); } }}}");
058: }
059:
060: public void testLogStatementLogger() throws Exception {
061: performFixTest(
062: "test/Test.java",
063: "package test; public class Test {public void test() {|new java.io.FileInputStream(\"\");}}",
064: "FixImpl",
065: "package test; import java.io.FileNotFoundException; import java.util.logging.Level; import java.util.logging.Logger; public class Test {public void test() {try { new java.io.FileInputStream(\"\"); } catch (FileNotFoundException ex) { Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); } }}");
066: }
067:
068: public void testLogStatementExceptions() throws Exception {
069: performFixTest(
070: "test/Test.java",
071: "package test; public class Test {public void test() {|new java.io.FileInputStream(\"\");}}",
072: "FixImpl",
073: "package test; import java.io.FileNotFoundException; import org.openide.util.Exceptions; public class Test {public void test() {try { new java.io.FileInputStream(\"\"); } catch (FileNotFoundException ex) { Exceptions.printStackTrace(ex); } }}");
074: }
075:
076: public void testLogPrint() throws Exception {
077: boolean orig = ErrorFixesFakeHint.isUseLogger();
078:
079: try {
080: ErrorFixesFakeHint.setUseLogger(false);
081:
082: performFixTest(
083: "test/Test.java",
084: "package test; public class Test {public void test() {|new java.io.FileInputStream(\"\");}}",
085: "FixImpl",
086: "package test; import java.io.FileNotFoundException; public class Test {public void test() {try { new java.io.FileInputStream(\"\"); } catch (FileNotFoundException ex) { ex.printStackTrace(); } }}");
087: } finally {
088: ErrorFixesFakeHint.setUseLogger(orig);
089: }
090: }
091:
092: public void test117085a() throws Exception {
093: performFixTest(
094: "test/Test.java",
095: "package test; public class Test {public void test(Exception ex) {|x();} private void x() throws Exception {}}",
096: "FixImpl",
097: "package test; import java.util.logging.Level; import java.util.logging.Logger; public class Test {public void test(Exception ex) {try { x(); } catch (Exception ex1) { Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex1); } } private void x() throws Exception {}}");
098: }
099:
100: public void test117085b() throws Exception {
101: performFixTest(
102: "test/Test.java",
103: "package test; public class Test {public void test(Exception ex) {|x();} private void x() throws Exception {}}",
104: "FixImpl",
105: "package test; import org.openide.util.Exceptions; public class Test {public void test(Exception ex) {try { x(); } catch (Exception ex1) { Exceptions.printStackTrace(ex1); } } private void x() throws Exception {}}");
106: }
107:
108: public void test117085c() throws Exception {
109: boolean orig = ErrorFixesFakeHint.isUseLogger();
110:
111: try {
112: ErrorFixesFakeHint.setUseLogger(false);
113:
114: performFixTest(
115: "test/Test.java",
116: "package test; public class Test {public void test(Exception ex) {|x();} private void x() throws Exception {}}",
117: "FixImpl",
118: "package test; public class Test {public void test(Exception ex) {try { x(); } catch (Exception ex1) { ex1.printStackTrace(); } } private void x() throws Exception {}}");
119: } finally {
120: ErrorFixesFakeHint.setUseLogger(orig);
121: }
122: }
123:
124: public void test117085d() throws Exception {
125: performFixTest(
126: "test/Test.java",
127: "package test; public class Test {public void test(Exception ex, Exception ex1) {|x();} private void x() throws Exception {}}",
128: "FixImpl",
129: "package test; import java.util.logging.Level; import java.util.logging.Logger; public class Test {public void test(Exception ex, Exception ex1) {try { x(); } catch (Exception ex2) { Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex2); } } private void x() throws Exception {}}");
130: }
131:
132: public void test117085e() throws Exception {
133: performFixTest(
134: "test/Test.java",
135: "package test; public class Test {public void test(Exception ex) {while (true) {Exception ex1; if (1 != 1) {|x();}}} private void x() throws Exception {}}",
136: "FixImpl",
137: "package test; import java.util.logging.Level; import java.util.logging.Logger; public class Test {public void test(Exception ex) {while (true) {Exception ex1; if (1 != 1) {try { x(); } catch (Exception ex2) { Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex2); } }}} private void x() throws Exception {}}");
138: }
139:
140: // public void test86483() throws Exception {
141: // performFixTest("test/Test.java",
142: // "package test; import java.io.IOException; import java.io.FileNotFoundException; import org.openide.util.Exceptions; public class Test {public void test(int a) {try {if (a == 1) |throw new IOException(); } catch (FileNotFoundException e) { Exceptions.printStacktrace(e); } } }",
143: // "FixImpl",
144: // "package test; import java.io.IOException; import java.io.FileNotFoundException; import org.openide.util.Exceptions; public class Test {public void test(int a) {try {if (a == 1) throw new IOException(); } catch (FileNotFoundException e) { Exceptions.printStacktrace(e); } catch (IOException e) { Exceptions.printStacktrace(e); } } }");
145: // }
146:
147: protected List<Fix> computeFixes(CompilationInfo info, int pos,
148: TreePath path) throws Exception {
149: return new UncaughtException().run(info, null, pos, path, null);
150: }
151:
152: @Override
153: protected String toDebugString(CompilationInfo info, Fix f) {
154: if (f instanceof MagicSurroundWithTryCatchFix) {
155: return "FixImpl";
156: }
157:
158: return super .toDebugString(info, f);
159: }
160:
161: @Override
162: protected FileObject[] getExtraClassPathElements() {
163: if ("testLogStatementExceptions".equals(getName())
164: || "test117085b".equals(getName())
165: || "test86483".equals(getName())) {
166: FileObject ooutils = URLMapper
167: .findFileObject(Exceptions.class
168: .getProtectionDomain().getCodeSource()
169: .getLocation());
170:
171: assertNotNull(ooutils);
172: assertTrue(FileUtil.isArchiveFile(ooutils));
173:
174: return new FileObject[] { FileUtil.getArchiveRoot(ooutils) };
175: }
176:
177: return super.getExtraClassPathElements();
178: }
179:
180: }
|