KickJava   Java API By Example, From Geeks To Geeks.

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


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-2007 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 java.util.Collections JavaDoc;
25 import org.netbeans.api.java.source.CancellableTask;
26 import org.netbeans.api.java.source.JavaSource;
27 import org.netbeans.api.java.source.JavaSource.*;
28 import org.netbeans.api.java.source.TestUtilities;
29 import org.netbeans.api.java.source.TreeMaker;
30 import org.netbeans.api.java.source.WorkingCopy;
31 import org.netbeans.junit.NbTestSuite;
32 import org.openide.filesystems.FileUtil;
33
34 /**
35  * Test modifications of NewClassTree.
36  *
37  * @author Jan Lahoda and Pavel Flaska
38  */

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

55     public void testRemoveClassBody() throws Exception JavaDoc {
56         testFile = new File JavaDoc(getWorkDir(), "Test.java");
57         TestUtilities.copyStringToFile(testFile,
58             "package hierbas.del.litoral;\n\n" +
59             "public class Test {\n" +
60             " public void taragui() {\n" +
61             " new Runnable() {\n" +
62             " public void run() {}\n" +
63             " };\n" +
64             " }\n" +
65             " private static class X {}\n" +
66             "}\n"
67             );
68         String JavaDoc golden =
69             "package hierbas.del.litoral;\n\n" +
70             "public class Test {\n" +
71             " public void taragui() {\n" +
72             " new X() ;\n" + //XXX: there should be no space between '()' and ';'
73
" }\n" +
74             " private static class X {}\n" +
75             "}\n";
76         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
77         CancellableTask task = new CancellableTask<WorkingCopy>() {
78
79             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
80                 workingCopy.toPhase(Phase.RESOLVED);
81                 TreeMaker make = workingCopy.getTreeMaker();
82                 
83                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
84                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
85                 ExpressionStatementTree st = (ExpressionStatementTree) method.getBody().getStatements().get(0);
86                 NewClassTree nct = (NewClassTree) st.getExpression();
87                 workingCopy.rewrite(nct, make.NewClass(null, Collections.<ExpressionTree>emptyList(), make.Identifier("X"), nct.getArguments(), null));
88             }
89             
90             public void cancel() {
91             }
92         };
93         testSource.runModificationTask(task).commit();
94         String JavaDoc res = TestUtilities.copyFileToString(testFile);
95         System.err.println(res);
96         assertEquals(golden, res);
97     }
98     
99     String JavaDoc getGoldenPckg() {
100         return "";
101     }
102
103     String JavaDoc getSourcePckg() {
104         return "";
105     }
106
107 }
108
Popular Tags