KickJava   Java API By Example, From Geeks To Geeks.

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


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;
20
21 import java.lang.reflect.Modifier JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import junit.textui.TestRunner;
24 import org.netbeans.jmi.javamodel.*;
25 import org.netbeans.junit.NbTestCase;
26 import org.netbeans.junit.NbTestSuite;
27 import org.openide.filesystems.FileStateInvalidException;
28
29 /**
30  *
31  * @author Martin Matula
32  */

33 public class EnumTest extends NbTestCase {
34
35     public EnumTest() {
36         super("EnumTest");
37     }
38
39     public static NbTestSuite suite() {
40         NbTestSuite suite = new NbTestSuite(EnumTest.class);
41         return suite;
42     }
43     
44     JavaEnum clazz;
45     JavaModelPackage pkg;
46     
47     protected void setUp() {
48         clazz = (JavaEnum) Utility.findClass("org.netbeans.test.codegen.EnumTest");
49         pkg = (JavaModelPackage) clazz.refImmediatePackage();
50     }
51
52     public void testAddConstant() throws java.io.IOException JavaDoc, FileStateInvalidException {
53         boolean fail = true;
54         Utility.beginTrans(true);
55         try {
56             EnumConstant literal = pkg.getEnumConstant().createEnumConstant("LITERAL4", null, 0, null, null, false, null, 0, null, null, null);
57             clazz.getConstants().add(literal);
58             fail = false;
59         }
60         finally {
61             Utility.endTrans(fail);
62         }
63         assertFile("File is not correctly generated.",
64             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/EnumTest.java"),
65             getGoldenFile("EnumTest.pass"),
66             getWorkDir()
67         );
68     }
69
70     public void testNewEnum() throws java.io.IOException JavaDoc, FileStateInvalidException {
71         boolean fail = true;
72         Utility.beginTrans(true);
73         try {
74             JavaEnum javaEnum = pkg.getJavaEnum().createJavaEnum(
75                 "InnerEnum", // name
76
null, // annotations
77
Modifier.PUBLIC, // modifiers
78
null, // javadocText
79
null, // javadoc
80
null, // contents
81
null, // superClassName
82
null, // interfaceNames
83
null, // typeParameters
84
null // constants
85
);
86             EnumConstant literal = pkg.getEnumConstant().createEnumConstant(
87                 "FIRST_CONSTANT",
88                 null,
89                 0,
90                 null,
91                 null,
92                 false,
93                 null,
94                 0,
95                 null,
96                 null,
97                 null
98             );
99             clazz.getContents().add(javaEnum);
100             javaEnum.getConstants().add(literal);
101             fail = false;
102         }
103         finally {
104             Utility.endTrans(fail);
105         }
106         assertFile("File is not correctly generated.",
107             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/EnumTest.java"),
108             getGoldenFile("testNewEnum_EnumTest.pass"),
109             getWorkDir()
110         );
111     }
112     
113     public void testNewEmptyEnum() throws java.io.IOException JavaDoc, FileStateInvalidException {
114         boolean fail = true;
115         Utility.beginTrans(true);
116         try {
117             JavaEnum javaEnum = pkg.getJavaEnum().createJavaEnum(
118                 "InnerOriginallyEmptyEnum", // name
119
null, // annotations
120
Modifier.PUBLIC, // modifiers
121
null, // javadocText
122
null, // javadoc
123
null, // contents
124
null, // superClassName
125
null, // interfaceNames
126
null, // typeParameters
127
null // constants
128
);
129             clazz.getContents().add(javaEnum);
130             fail = false;
131         }
132         finally {
133             Utility.endTrans(fail);
134         }
135         assertFile("File is not correctly generated.",
136             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/EnumTest.java"),
137             getGoldenFile("testNewEmptyEnum_EnumTest.pass"),
138             getWorkDir()
139         );
140     }
141
142     public void testAddConstantToEmptyEnum() throws java.io.IOException JavaDoc, FileStateInvalidException {
143         boolean fail = true;
144         Utility.beginTrans(true);
145         try {
146             JavaEnum javaEnum = (JavaEnum) clazz.getContents().get(1);
147             EnumConstant literal = pkg.getEnumConstant().createEnumConstant(
148                 null,
149                 null,
150                 0,
151                 null,
152                 null,
153                 false,
154                 null,
155                 0,
156                 null,
157                 null,
158                 null
159             );
160             literal.setName("TO_NEW_ENUM_CONSTANT");
161             javaEnum.getConstants().add(literal);
162             fail = false;
163         }
164         finally {
165             Utility.endTrans(fail);
166         }
167         assertFile("File is not correctly generated.",
168             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/EnumTest.java"),
169             getGoldenFile("testAddConstantToEmptyEnum_EnumTest.pass"),
170             getWorkDir()
171         );
172     }
173     
174     /**
175      * @param args the command line arguments
176      */

177     public static void main(String JavaDoc[] args) {
178         TestRunner.run(suite());
179     }
180     
181 }
182
Popular Tags