KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > gen > TryTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.api.java.source.gen;
20
21 import com.sun.source.tree.*;
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import org.netbeans.api.java.source.CancellableTask;
25 import org.netbeans.api.java.source.JavaSource;
26 import org.netbeans.api.java.source.JavaSource.*;
27 import org.netbeans.api.java.source.TestUtilities;
28 import org.netbeans.api.java.source.TreeMaker;
29 import org.netbeans.api.java.source.WorkingCopy;
30 import org.netbeans.junit.NbTestSuite;
31 import org.openide.filesystems.FileUtil;
32
33 /**
34  * Test different modifications in try/catch/finally section.
35  *
36  * @author Pavel Flaska
37  */

38 public class TryTest extends GeneratorTest {
39     
40     /** Creates a new instance of TryTest */
41     public TryTest(String JavaDoc name) {
42         super(name);
43     }
44     
45     public static NbTestSuite suite() {
46         NbTestSuite suite = new NbTestSuite();
47         suite.addTestSuite(TryTest.class);
48         return suite;
49     }
50
51     /**
52      * Renames variable in try body.
53      */

54     public void testRenameInTryBody() throws Exception JavaDoc {
55         testFile = new File JavaDoc(getWorkDir(), "Test.java");
56         TestUtilities.copyStringToFile(testFile,
57             "package hierbas.del.litoral;\n\n" +
58             "import java.io.*;\n\n" +
59             "public class Test {\n" +
60             " public void taragui() {\n" +
61             " try {\n" +
62             " File f = new File(\"auto\");\n" +
63             " FileInputStream fis = new FileInputStream(f);\n" +
64             " } catch (FileNotFoundException ex) {\n" +
65             " } catch (NullPointerException ex) {\n" +
66             " }\n" +
67             " }\n" +
68             "}\n"
69             );
70         String JavaDoc golden =
71             "package hierbas.del.litoral;\n\n" +
72             "import java.io.*;\n\n" +
73             "public class Test {\n" +
74             " public void taragui() {\n" +
75             " try {\n" +
76             " File f = new File(\"auto\");\n" +
77             " FileInputStream input = new FileInputStream(f);\n" +
78             " } catch (FileNotFoundException ex) {\n" +
79             " } catch (NullPointerException ex) {\n" +
80             " }\n" +
81             " }\n" +
82             "}\n";
83         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
84         CancellableTask task = new CancellableTask<WorkingCopy>() {
85
86             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
87                 workingCopy.toPhase(Phase.RESOLVED);
88                 TreeMaker make = workingCopy.getTreeMaker();
89                 
90                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
91                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
92                 TryTree tt = (TryTree) method.getBody().getStatements().get(0);
93                 VariableTree var = (VariableTree) tt.getBlock().getStatements().get(1);
94                 workingCopy.rewrite(var, make.setLabel(var, "input"));
95             }
96             
97             public void cancel() {
98             }
99         };
100         testSource.runModificationTask(task).commit();
101         String JavaDoc res = TestUtilities.copyFileToString(testFile);
102         System.err.println(res);
103         assertEquals(golden, res);
104     }
105     
106     String JavaDoc getGoldenPckg() {
107         return "";
108     }
109
110     String JavaDoc getSourcePckg() {
111         return "";
112     }
113
114 }
115
Popular Tags