KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jmi > javamodel > codegen > FieldTest1


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.jmi.javamodel.codegen.FieldTest1;
20
21 import java.lang.reflect.Modifier JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import junit.textui.TestRunner;
25 import org.netbeans.jmi.javamodel.*;
26 import org.netbeans.jmi.javamodel.codegen.CodegenTestCase;
27 import org.netbeans.jmi.javamodel.codegen.Utility;
28 import org.netbeans.junit.NbTestSuite;
29
30 /**
31  * Test the field generator.
32  *
33  * @author Pavel Flaska
34  */

35 public class FieldTest1 extends CodegenTestCase {
36
37     /** Need to be defined because of JUnit */
38     public FieldTest1(String JavaDoc name) {
39         super(name, "FieldTest1");
40     }
41
42     public static NbTestSuite suite() {
43         NbTestSuite suite = new NbTestSuite();
44         suite.addTest(new FieldTest1("testFieldModifiers"));
45         suite.addTest(new FieldTest1("testFieldType"));
46         suite.addTest(new FieldTest1("testFieldName"));
47         suite.addTest(new FieldTest1("testFieldInitialValueText"));
48         suite.addTest(new FieldTest1("testFieldInitialValue"));
49         suite.addTest(new FieldTest1("testFieldChangeInInitValue"));
50         suite.addTest(new FieldTest1("testFieldInitialValueRemoval"));
51         suite.addTest(new FieldTest1("testFieldAdd"));
52         return suite;
53     }
54     
55     JavaClass clazz;
56     JavaModelPackage pkg;
57     Field[] field = new Field[7];
58     
59     protected void setUp() {
60         clazz = Utility.findClass("org.netbeans.test.codegen.FieldTest1");
61         pkg = (JavaModelPackage) clazz.refImmediatePackage();
62         // initialize methods
63
int i = 0;
64         for (Iterator JavaDoc mIt = clazz.getFeatures().iterator(); mIt.hasNext(); i++) {
65             field[i] = (Field) mIt.next();
66         }
67     }
68
69     /**
70      * Removes all the modififers from the field.
71      */

72     public void testFieldModifiers() {
73         boolean fail = true;
74         Utility.beginTrans(true);
75         try {
76             int modifiers = field[0].getModifiers();
77             modifiers &= ~(Modifier.PRIVATE | Modifier.FINAL | Modifier.STATIC);
78             field[0].setModifiers(modifiers);
79             fail = false;
80         }
81         finally {
82             Utility.endTrans(fail);
83         }
84         makeDiff("testFieldModifiers");
85     }
86     
87     /**
88      * Changes long type of the field to short type.
89      */

90     public void testFieldType() {
91         boolean fail = true;
92         Utility.beginTrans(true);
93         try {
94             field[1].setTypeName(pkg.getMultipartId().createMultipartId("short", null, null));
95             fail = false;
96         }
97         finally {
98             Utility.endTrans(fail);
99         }
100         makeDiff("testFieldType");
101     }
102     
103     /**
104      * Changes field name to thisIsTheNewName.
105      */

106     public void testFieldName() {
107         boolean fail = true;
108         Utility.beginTrans(true);
109         try {
110             field[2].setName("thisIsTheNewName");
111             fail = false;
112         }
113         finally {
114             Utility.endTrans(fail);
115         }
116         makeDiff("testFieldName");
117     }
118     
119     /**
120      * Changes the initial value text.
121      */

122     public void testFieldInitialValueText() {
123         boolean fail = true;
124         Utility.beginTrans(true);
125         try {
126             field[3].setInitialValueText("\"This is a new text.\"");
127             fail = false;
128         }
129         finally {
130             Utility.endTrans(fail);
131         }
132         makeDiff("testFieldInitialValueText");
133     }
134     
135     /**
136      * Change whole initial value expression to literal 78.
137      */

138     public void testFieldInitialValue() {
139         boolean fail = true;
140         Utility.beginTrans(true);
141         try {
142             Literal l = pkg.getLongLiteral().createLongLiteral((long) 78);
143             field[4].setInitialValue(l);
144             fail = false;
145         }
146         finally {
147             Utility.endTrans(fail);
148         }
149         makeDiff("testFieldInitialValue");
150     }
151     
152     /**
153      * Changes the initialization 'new String("NetBeers")' to 'new String()'.
154      * It tests only change in expression, no expression swap.
155      */

156     public void testFieldChangeInInitValue() {
157         boolean fail = true;
158         Utility.beginTrans(true);
159         try {
160             NewClassExpression nce = (NewClassExpression) field[5].getInitialValue();
161             List JavaDoc parameters = nce.getParameters();
162             Object JavaDoc o = parameters.remove(0);
163             parameters.add(pkg.getStringLiteral().createStringLiteral("NetBeans"));
164             fail = false;
165         }
166         finally {
167             Utility.endTrans(fail);
168         }
169         makeDiff("testFieldChangeInInitValue");
170     }
171
172     /**
173      * Removes the inital value from the field.
174      */

175     public void testFieldInitialValueRemoval() {
176         boolean fail = true;
177         Utility.beginTrans(true);
178         try {
179             field[6].setInitialValue(null);
180             fail = false;
181         }
182         finally {
183             Utility.endTrans(fail);
184         }
185         makeDiff("testFieldInitialValueRemoval");
186     }
187     
188     /**
189      * Removes the inital value from the field.
190      */

191     public void testFieldAdd() {
192         boolean fail = true;
193         Field field;
194         Utility.beginTrans(true);
195         try {
196               field=pkg.getField().createField();
197               field.setType(pkg.getType().resolve("java.math.BigDecimal"));
198               field.setName("fooo");
199               field.setModifiers(Modifier.PRIVATE);
200               assert "java.math.BigDecimal".equals(field.getType().getName());
201               clazz.getContents().add(field);
202               assert "java.math.BigDecimal".equals(field.getType().getName());
203               fail=false;
204         }
205         finally {
206             Utility.endTrans(fail);
207         }
208           assert "java.math.BigDecimal".equals(field.getType().getName());
209     }
210
211     
212     /**
213      * @param args the command line arguments
214      */

215     public static void main(String JavaDoc[] args) {
216         TestRunner.run(suite());
217     }
218     
219 }
220
Popular Tags