KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.jmi.javamodel.codegen;
21
22 import org.netbeans.junit.NbTestCase;
23 import org.netbeans.junit.NbTestSuite;
24 import org.netbeans.jmi.javamodel.JavaModelPackage;
25 import org.netbeans.jmi.javamodel.Import;
26 import org.netbeans.jmi.javamodel.Resource;
27 import org.netbeans.jmi.javamodel.JavaClass;
28 import java.io.IOException JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import junit.textui.TestRunner;
31 import org.openide.filesystems.FileStateInvalidException;
32
33 /**
34  * Tests the static imports as they are defined in JSR201 and implemented
35  * in JDK 1.5.
36  *
37  * @author Pavel Flaska
38  */

39 public class StaticImportTest extends NbTestCase {
40     
41     /** Creates a new instance of StaticImportTest */
42     public StaticImportTest() {
43         super("StaticImportTest");
44     }
45
46     public static NbTestSuite suite() {
47         NbTestSuite suite = new NbTestSuite(StaticImportTest.class);
48         return suite;
49     }
50     
51     JavaModelPackage pkg;
52     JavaClass clazz;
53     Import arraysImport;
54     
55     protected void setUp() {
56         clazz = Utility.findClass("org.netbeans.test.codegen.StaticImport");
57         pkg = (JavaModelPackage) clazz.refImmediatePackage();
58         for (Iterator JavaDoc it = clazz.getResource().getImports().iterator(); it.hasNext(); ) {
59             arraysImport = (Import) it.next();
60         }
61     }
62     
63     public void testCreation() throws IOException JavaDoc, FileStateInvalidException {
64         boolean fail = true;
65         Utility.beginTrans(true);
66         try {
67             Resource res = clazz.getResource();
68             Import i = pkg.getImport().createImport("java.util.Collections", null, true, false);
69             res.getImports().add(i);
70             fail = false;
71         } finally {
72             Utility.endTrans(fail);
73         }
74         assertFile("File is not correctly generated.",
75             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/StaticImport.java"),
76             getGoldenFile("testCreation_StaticImport.pass"),
77             getWorkDir()
78         );
79     }
80     
81     public void testCreation2() throws IOException JavaDoc, FileStateInvalidException {
82         boolean fail = true;
83         Utility.beginTrans(true);
84         try {
85             Resource res = clazz.getResource();
86             arraysImport = pkg.getImport().createImport("java.util.Arrays", null, false, false);
87             res.getImports().add(arraysImport);
88             fail = false;
89         } finally {
90             Utility.endTrans(fail);
91         }
92         assertFile("File is not correctly generated.",
93             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/StaticImport.java"),
94             getGoldenFile("testCreation2_StaticImport.pass"),
95             getWorkDir()
96         );
97     }
98     
99     public void testSetStatic() throws IOException JavaDoc, FileStateInvalidException {
100         boolean fail = true;
101         Utility.beginTrans(true);
102         try {
103             arraysImport.setStatic(true);
104             fail = false;
105         } finally {
106             Utility.endTrans(fail);
107         }
108         assertFile("File is not correctly generated.",
109             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/StaticImport.java"),
110             getGoldenFile("testSetStatic_StaticImport.pass"),
111             getWorkDir()
112         );
113     }
114     
115     /**
116      * @param args the command line arguments
117      */

118     public static void main(String JavaDoc[] args) {
119         TestRunner.run(suite());
120     }
121 }
122
Popular Tags