KickJava   Java API By Example, From Geeks To Geeks.

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


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.BlockTree;
22 import com.sun.source.tree.ClassTree;
23 import com.sun.source.tree.CompilationUnitTree;
24 import com.sun.source.tree.ExpressionTree;
25 import com.sun.source.tree.ForLoopTree;
26 import com.sun.source.tree.MethodTree;
27 import com.sun.source.tree.VariableTree;
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30 import org.netbeans.api.java.source.CancellableTask;
31 import org.netbeans.api.java.source.JavaSource;
32 import org.netbeans.api.java.source.JavaSource.Phase;
33 import org.netbeans.api.java.source.TreeMaker;
34 import org.netbeans.api.java.source.WorkingCopy;
35 import org.netbeans.jackpot.test.TestUtilities;
36 import org.netbeans.junit.NbTestSuite;
37
38 /**
39  * Tests correct adding cast to statement.
40  *
41  * @author Pavel Flaska
42  */

43 public class AddCastTest extends GeneratorTestMDRCompat {
44     
45     /** Creates a new instance of AddCastTest */
46     public AddCastTest(String JavaDoc name) {
47         super(name);
48     }
49     
50     public static NbTestSuite suite() {
51         NbTestSuite suite = new NbTestSuite();
52         suite.addTestSuite(AddCastTest.class);
53         return suite;
54     }
55
56     public void testAddCastToDeclStmt() throws Exception JavaDoc {
57         testFile = new File JavaDoc(getWorkDir(), "Test.java");
58         TestUtilities.copyStringToFile(testFile,
59             "package hierbas.del.litoral;\n\n" +
60             "import java.util.*;\n\n" +
61             "public class Test<E> {\n" +
62             " public void taragui() {\n" +
63             " System.err.println(\"taragui() method\");\n" +
64             " String s = \"Oven.\";\n" +
65             "// line comment\n" +
66             " }\n" +
67             "}\n"
68             );
69         String JavaDoc golden =
70             "package hierbas.del.litoral;\n\n" +
71             "import java.util.*;\n\n" +
72             "public class Test<E> {\n" +
73             " public void taragui() {\n" +
74             " System.err.println(\"taragui() method\");\n" +
75             " String s = (String) \"Oven.\";\n" +
76             "// line comment\n" +
77             " }\n" +
78             "}\n";
79         JavaSource src = getJavaSource(testFile);
80         
81         CancellableTask task = new CancellableTask<WorkingCopy>() {
82
83             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
84                 // hovoovno
85
workingCopy.toPhase(Phase.RESOLVED);
86                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
87                 TreeMaker make = workingCopy.getTreeMaker();
88                 // coze?
89
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
90                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
91                 VariableTree var = (VariableTree) method.getBody().getStatements().get(1);
92                 ExpressionTree init = var.getInitializer();
93                 ExpressionTree cast = make.TypeCast(make.Identifier("String"), init);
94                 workingCopy.rewrite(init, cast);
95             }
96
97             public void cancel() {
98             }
99         };
100         src.runModificationTask(task).commit();
101         String JavaDoc res = TestUtilities.copyFileToString(testFile);
102         System.err.println(res);
103         assertEquals(golden, res);
104     }
105     
106     public void testAddCastInForWithoutSteps() throws Exception JavaDoc {
107         testFile = new File JavaDoc(getWorkDir(), "Test.java");
108         TestUtilities.copyStringToFile(testFile,
109             "package hierbas.del.litoral;\n\n" +
110             "import java.util.*;\n\n" +
111             "public class Test<E> {\n" +
112             " public void cast() {\n" +
113             " Object o = null;\n" +
114             " for (int i = 0; i < 5; ) {\n" +
115             " String s = o;\n" +
116             " }\n" +
117             " }\n" +
118             "}\n"
119             );
120         String JavaDoc golden =
121             "package hierbas.del.litoral;\n\n" +
122             "import java.util.*;\n\n" +
123             "public class Test<E> {\n" +
124             " public void cast() {\n" +
125             " Object o = null;\n" +
126             " for (int i = 0; i < 5; ) {\n" +
127             " String s = (String) o;\n" +
128             " }\n" +
129             " }\n" +
130             "}\n";
131         JavaSource src = getJavaSource(testFile);
132         
133         CancellableTask task = new CancellableTask<WorkingCopy>() {
134
135             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
136                 workingCopy.toPhase(Phase.RESOLVED);
137                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
138                 TreeMaker make = workingCopy.getTreeMaker();
139                 ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
140                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
141                 ForLoopTree forLoop = (ForLoopTree) method.getBody().getStatements().get(1);
142                 BlockTree block = (BlockTree) forLoop.getStatement();
143                 VariableTree vt = (VariableTree) block.getStatements().get(0);
144                 ExpressionTree init = vt.getInitializer();
145                 ExpressionTree cast = make.TypeCast(make.Identifier("String"), init);
146                 workingCopy.rewrite(init, cast);
147             }
148
149             public void cancel() {
150             }
151         };
152         src.runModificationTask(task).commit();
153         String JavaDoc res = TestUtilities.copyFileToString(testFile);
154         System.err.println(res);
155         assertEquals(golden, res);
156     }
157     
158     String JavaDoc getGoldenPckg() {
159         return "";
160     }
161     
162     String JavaDoc getSourcePckg() {
163         return "";
164     }
165     
166     
167 }
168
Popular Tags