KickJava   Java API By Example, From Geeks To Geeks.

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


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 static com.sun.source.tree.Tree.Kind.*;
23 import java.util.*;
24 import java.io.IOException JavaDoc;
25 import java.util.EnumSet JavaDoc;
26 import javax.lang.model.element.Modifier;
27 import javax.lang.model.type.TypeKind;
28 import org.netbeans.junit.NbTestSuite;
29 import junit.textui.TestRunner;
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
36 /**
37  * Tests the method generator.
38  *
39  * @author Pavel Flaska
40  */

41 public class ConstructorTest extends GeneratorTest {
42     
43     /** Need to be defined because of JUnit */
44     public ConstructorTest(String JavaDoc name) {
45         super(name);
46     }
47     
48     public static NbTestSuite suite() {
49         NbTestSuite suite = new NbTestSuite(ConstructorTest.class);
50         return suite;
51     }
52     
53     protected void setUp() throws Exception JavaDoc {
54         super.setUp();
55     }
56
57     public void testAddConstructor() throws IOException JavaDoc {
58         testFile = getFile(getSourceDir(), getSourcePckg() + "ConstructorTest.java");
59         
60         JavaSource src = getJavaSource(testFile);
61         CancellableTask task = new CancellableTask<WorkingCopy>() {
62
63             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
64                 workingCopy.toPhase(Phase.RESOLVED);
65                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
66                 TreeMaker make = workingCopy.getTreeMaker();
67                 // exactly one class in compilation unit
68
ClassTree topLevel = (ClassTree) cut.getTypeDecls().iterator().next();
69                 for (Tree member : topLevel.getMembers()) {
70                     // for the first inner class in top level
71
if (CLASS == member.getKind()) {
72                         
73                         ModifiersTree mods = make.Modifiers(EnumSet.of(Modifier.PUBLIC));
74                         
75                         List<VariableTree> arguments = new ArrayList<VariableTree>();
76                         arguments.add(make.Variable(
77                                 make.Modifiers(EnumSet.noneOf(Modifier.class)),
78                                 "a",
79                                 make.PrimitiveType(TypeKind.BOOLEAN), null)
80                         );
81                         
82                         MethodTree newConstructor = make.Constructor(
83                                 mods,
84                                 Collections.<TypeParameterTree>emptyList(),
85                                 arguments,
86                                 Collections.<ExpressionTree>emptyList(),
87                                 make.Block(Collections.<StatementTree>emptyList(), false)
88                         );
89                         ClassTree newInner = make.addClassMember((ClassTree) member, newConstructor);
90                         workingCopy.rewrite(member, newInner);
91                     }
92                 }
93             }
94                 
95             public void cancel() {
96             }
97         };
98         src.runModificationTask(task).commit();
99         assertFiles("testAddConstructor.pass");
100     }
101         
102     public void testAddConstructor2() throws IOException JavaDoc {
103         testFile = getFile(getSourceDir(), getSourcePckg() + "ConstructorTest2.java");
104         
105         JavaSource src = getJavaSource(testFile);
106         CancellableTask task = new CancellableTask<WorkingCopy>() {
107
108             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
109                 workingCopy.toPhase(Phase.RESOLVED);
110                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
111                 TreeMaker make = workingCopy.getTreeMaker();
112                 // exactly one class in compilation unit
113
ClassTree topLevel = (ClassTree) cut.getTypeDecls().iterator().next();
114                 
115                 ModifiersTree mods = make.Modifiers(EnumSet.of(Modifier.PUBLIC));
116                 List<VariableTree> arguments = new ArrayList<VariableTree>();
117                 arguments.add(make.Variable(
118                         make.Modifiers(EnumSet.noneOf(Modifier.class)),
119                         "a",
120                         make.PrimitiveType(TypeKind.BOOLEAN), null)
121                 );
122                 MethodTree newConstructor = make.Constructor(
123                         mods,
124                         Collections.<TypeParameterTree>emptyList(),
125                         arguments,
126                         Collections.<ExpressionTree>emptyList(),
127                         make.Block(Collections.<StatementTree>emptyList(), false)
128                 );
129
130                 ClassTree newClass = make.addClassMember(topLevel, newConstructor);
131                 workingCopy.rewrite(topLevel, newClass);
132             }
133                 
134             public void cancel() {
135             }
136         };
137         src.runModificationTask(task).commit();
138         assertFiles("testAddConstructor2.pass");
139     }
140     
141     ////////////////////////////////////////////////////////////////////////////
142
/**
143      * @param args the command line arguments
144      */

145     public static void main(String JavaDoc[] args) {
146         TestRunner.run(suite());
147     }
148
149     String JavaDoc getSourcePckg() {
150         return "org/netbeans/test/codegen/";
151     }
152
153     String JavaDoc getGoldenPckg() {
154         return "org/netbeans/jmi/javamodel/codegen/ConstructorTest/ConstructorTest/";
155     }
156
157 }
158
Popular Tags