KickJava   Java API By Example, From Geeks To Geeks.

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


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.ClassTree;
22 import com.sun.source.tree.ExpressionTree;
23 import com.sun.source.tree.LiteralTree;
24 import com.sun.source.tree.Tree;
25 import com.sun.source.tree.VariableTree;
26 import java.io.IOException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.List JavaDoc;
30 import javax.lang.model.element.Modifier;
31 import junit.textui.TestRunner;
32 import org.netbeans.api.java.source.transform.Transformer;
33 import org.netbeans.junit.NbTestCase;
34 import org.netbeans.junit.NbTestSuite;
35 import org.openide.filesystems.FileStateInvalidException;
36
37 /**
38  * Tests more transaction on file with fields. Tests fields generating and
39  * also update.
40  *
41  * @author Pavel Flaska
42  */

43 public class FieldTest4 extends GeneratorTest {
44     
45     /** Creates a new instance of FieldTest4 */
46     public FieldTest4(String JavaDoc name) {
47         super(name);
48     }
49     
50     public static NbTestSuite suite() {
51         NbTestSuite suite = new NbTestSuite(FieldTest4.class);
52         return suite;
53     }
54     
55     protected void setUp() throws Exception JavaDoc {
56         super.setUp();
57         testFile = getFile(getSourceDir(), getSourcePckg() + "FieldTest4.java");
58     }
59
60     /**
61      * Tests inital value for fields in field group.
62      */

63     public void testAddField() throws IOException JavaDoc {
64         process(
65             new Transformer<Void JavaDoc, Object JavaDoc>() {
66                 public Void JavaDoc visitClass(ClassTree node, Object JavaDoc p) {
67                     super.visitClass(node, p);
68                     VariableTree vtecko = make.Variable(
69                             make.Modifiers(Collections.singleton(Modifier.PROTECTED)),
70                             "newField",
71                             make.Identifier("String"),
72                             null
73                     );
74                     List JavaDoc<Tree> memberDecl = new ArrayList JavaDoc<Tree>(node.getMembers());
75                     memberDecl.add(vtecko);
76                     ClassTree ct = make.Class(node.getModifiers(),
77                             node.getSimpleName(),
78                             node.getTypeParameters(),
79                             node.getExtendsClause(),
80                             (List JavaDoc<ExpressionTree>) node.getImplementsClause(),
81                             memberDecl);
82                     model.setElement(ct, model.getElement(node));
83                     model.setType(ct, model.getType(node));
84                     copyCommentTo(node, ct);
85                     changes.rewrite(node, ct);
86                     return null;
87                 }
88             }
89         );
90         assertFiles("testAddField_FieldTest4.pass");
91     }
92     
93     /**
94      * @param args the command line arguments
95      */

96     public static void main(String JavaDoc[] args) {
97         TestRunner.run(suite());
98     }
99     
100     String JavaDoc getSourcePckg() {
101         return "org/netbeans/test/codegen/";
102     }
103
104     String JavaDoc getGoldenPckg() {
105         return "org/netbeans/jmi/javamodel/codegen/FieldTest4/";
106     }
107 }
108
Popular Tags