KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
20  * GroupTest2.java
21  *
22  * Created on March 1, 2004, 2:30 PM
23  */

24
25 package org.netbeans.jmi.javamodel.codegen;
26
27 import java.io.IOException JavaDoc;
28 import java.lang.reflect.Modifier JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31 import org.netbeans.jmi.javamodel.Field;
32 import org.netbeans.jmi.javamodel.FieldGroup;
33 import org.netbeans.jmi.javamodel.JavaClass;
34 import org.netbeans.jmi.javamodel.JavaModelPackage;
35 import org.netbeans.jmi.javamodel.MultipartId;
36 import org.netbeans.junit.NbTestCase;
37 import org.netbeans.junit.NbTestSuite;
38 import org.openide.filesystems.FileStateInvalidException;
39
40 /**
41  *
42  * @author Pavel Flaska
43  */

44 public class GroupTest2 extends NbTestCase {
45     
46     /** Creates a new instance of GroupTest2 */
47     public GroupTest2() {
48         super("GroupTest2");
49     }
50     
51     public static NbTestSuite suite() {
52         NbTestSuite suite = new NbTestSuite(GroupTest2.class);
53         return suite;
54     }
55     
56     JavaClass clazz;
57     JavaModelPackage pkg;
58     
59     protected void setUp() {
60         clazz = Utility.findClass("org.netbeans.test.codegen.GroupTest2Class");
61         pkg = (JavaModelPackage) clazz.refImmediatePackage();
62     }
63     
64     /**
65      * Creates new group with four fields.
66      * public String chicco, lego, fisherPrice = "nice", disney;
67      */

68     public void testGroupCreation() throws IOException JavaDoc, FileStateInvalidException {
69         boolean fail = true;
70         Utility.beginTrans(true);
71         try {
72             List JavaDoc fields = new ArrayList JavaDoc(4);
73             String JavaDoc[] fieldNames = { "chicco", "lego", "fisherPrice", "disney" };
74             String JavaDoc[] fieldInitials = { null, null, "\"nice\"", null };
75             for (int i = 0; i < 4; i++) {
76                 MultipartId id = pkg.getMultipartId().createMultipartId("String", null, null);
77                 fields.add(pkg.getField().createField(
78                     fieldNames[i],
79                     null,
80                     Modifier.PUBLIC,
81                     null,
82                     null,
83                     false,
84                     id,
85                     0,
86                     null,
87                     fieldInitials[i]
88                 ));
89             };
90             MultipartId id = pkg.getMultipartId().createMultipartId("String", null, null);
91             clazz.getContents().add(pkg.getFieldGroup().createFieldGroup(
92                 null,
93                 null,
94                 Modifier.PUBLIC,
95                 null,
96                 null,
97                 id,
98                 fields
99             ));
100             fail = false;
101         }
102         finally {
103             Utility.endTrans(fail);
104         }
105         assertFile("File is not correctly generated.",
106             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/GroupTest2Class.java"),
107             getGoldenFile("testGroupCreation_GroupTest2Class.pass"),
108             getWorkDir()
109         );
110     }
111     
112     /**
113      * Change the modifier of the third field and this should cause group
114      * split.
115      */

116     public void testGroupSplit1() throws IOException JavaDoc, FileStateInvalidException {
117         boolean fail = true;
118         Utility.beginTrans(true);
119         try {
120             FieldGroup group = (FieldGroup) clazz.getContents().get(1);
121             ((Field) group.getFields().get(2)).setModifiers(Modifier.PRIVATE);
122             fail = false;
123         }
124         finally {
125             Utility.endTrans(fail);
126         }
127         assertFile("File is not correctly generated.",
128             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/GroupTest2Class.java"),
129             getGoldenFile("testGroupSplit1_GroupTest2Class.pass"),
130             getWorkDir()
131         );
132     }
133     
134     /**
135      * Change the type of the first and the last
136      */

137     public void testGroupSplit2() throws IOException JavaDoc, FileStateInvalidException {
138         boolean fail = true;
139         Utility.beginTrans(true);
140         try {
141             MultipartId id = pkg.getMultipartId().createMultipartId("long", null, null);
142             FieldGroup group = (FieldGroup) clazz.getContents().get(0);
143             ((Field) group.getFields().get(2)).setTypeName(id);
144             fail = false;
145         }
146         finally {
147             Utility.endTrans(fail);
148         }
149         assertFile("File is not correctly generated.",
150             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/GroupTest2Class.java"),
151             getGoldenFile("testGroupSplit2_GroupTest2Class.pass"),
152             getWorkDir()
153         );
154     }
155     
156     /**
157      * @param args the command line arguments
158      */

159     public static void main(String JavaDoc[] args) {
160     }
161     
162 }
163
Popular Tags