KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > projectimport > j2seimport > AbstractProjectDefinitionTest


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.modules.projectimport.j2seimport;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.jar.JarEntry JavaDoc;
26 import java.util.jar.JarOutputStream JavaDoc;
27 import junit.framework.*;
28 import org.netbeans.junit.NbTestCase;
29 import org.openide.filesystems.FileUtil;
30
31
32 /**
33  *
34  * @author Radek Matous
35  */

36 public class AbstractProjectDefinitionTest extends NbTestCase {
37     protected AbstractProject testProject;
38
39     static {
40         System.setProperty("projectimport.logging.level", "FINEST");
41     }
42
43     public AbstractProjectDefinitionTest(String JavaDoc testName) {
44         super(testName);
45     }
46
47     protected final void setUp() throws Exception JavaDoc {
48         clearWorkDir();
49         try {
50             testProject = new AbstractProject(getName(), FileUtil.toFileObject(getWorkDir()));
51         } catch(IOException JavaDoc iex) {
52             assert false : iex.getLocalizedMessage();
53             throw new IllegalStateException JavaDoc(iex.getLocalizedMessage());
54         }
55         
56         specificSetUp(testProject);
57     }
58
59
60     protected void specificSetUp(AbstractProject projectDefinition) {}
61     
62     private AbstractProject generalSetUp(String JavaDoc testName) {
63         AbstractProject retVal = null;
64         try {
65             retVal = new AbstractProject(testName, FileUtil.toFileObject(getWorkDir()));
66         } catch(IOException JavaDoc iex) {
67             assert false : iex.getLocalizedMessage();
68             throw new IllegalStateException JavaDoc(iex.getLocalizedMessage());
69         }
70      
71         return retVal;
72     }
73     
74     protected void tearDown() throws Exception JavaDoc {
75         clearWorkDir();
76     }
77
78
79     public static Test suite() {
80         TestSuite suite = new TestSuite(AbstractProjectDefinitionTest.class);
81         
82         return suite;
83     }
84
85     /**
86      * Test of getName method, of class org.netbeans.modules.projectimport.jbuilder.j2seimport.AbstractProjectDefinition.
87      */

88     public void testGetName() {
89         assertEquals(testProject.getName(), this.getName());
90     }
91
92     /**
93      * Test of getProjectDir method, of class org.netbeans.modules.projectimport.jbuilder.j2seimport.AbstractProjectDefinition.
94      */

95     public void testGetProjectDir() throws Exception JavaDoc {
96         assertEquals(testProject.getProjectDir(), FileUtil.toFileObject(getWorkDir()));
97     }
98
99     /**
100      * Test of isAlreadyImported method, of class org.netbeans.modules.projectimport.jbuilder.j2seimport.AbstractProjectDefinition.
101      */

102     public void testIsAlreadyImported() {
103         assertFalse(testProject.isAlreadyImported());
104     }
105
106     /**
107      * Test of setAsImported method, of class org.netbeans.modules.projectimport.jbuilder.j2seimport.AbstractProjectDefinition.
108      */

109     public void testSetAsImported() {
110         testProject.setAsImported();
111         assertTrue(testProject.isAlreadyImported());
112     }
113
114     /**
115      * Test of getLibraryEntries method, of class org.netbeans.modules.projectimport.jbuilder.j2seimport.AbstractProjectDefinition.
116      */

117     public void testGetLibraryEntries() {
118         assertNotNull(testProject.getLibraries());
119         assertEquals(testProject.getLibraries().size(),0);
120     }
121
122     /**
123      * Test of addLibraryEntry method, of class org.netbeans.modules.projectimport.jbuilder.j2seimport.AbstractProjectDefinition.
124      */

125     public void testAddLibraryEntry() throws Exception JavaDoc {
126         File JavaDoc archiv = createArchivFile(getWorkDir(),"lib.jar");
127         
128         assertEquals(testProject.getLibraries().size(),0);
129         assertEquals(testProject.getWarnings().size(),0);
130         
131         AbstractProject.Library library = new AbstractProject.Library(archiv);
132         assertTrue(library.isValid());
133         testProject.addLibrary(library);
134         assertEquals(testProject.getLibraries().size(),1);
135         assertEquals(testProject.getWarnings().size(),0);
136         assertTrue(testProject.getLibraries().contains(library));
137         
138         
139
140         library = new AbstractProject.Library(getWorkDir());
141         assertFalse(library.isValid());
142         testProject.addLibrary(library);
143         assertEquals(testProject.getLibraries().size(),2);
144         assertEquals(testProject.getWarnings().size(),1);
145         assertTrue(testProject.getLibraries().contains(library));
146     }
147
148     /**
149      * Test of getUserLibraries method, of class org.netbeans.modules.projectimport.jbuilder.j2seimport.AbstractProjectDefinition.
150      */

151     public void testGetUserLibraries() {
152         assertNotNull(testProject.getUserLibraries());
153         assertEquals(testProject.getUserLibraries().size(),0);
154     }
155
156     /**
157      * Test of addLUserLibrary method, of class org.netbeans.modules.projectimport.jbuilder.j2seimport.AbstractProjectDefinition.
158      */

159     public void testAddLUserLibrary() throws Exception JavaDoc{
160         AbstractProject.UserLibrary uLibrary = new AbstractProject.UserLibrary(this.getName());
161         assertFalse("UserLibrary is valid if isn't empty",uLibrary.isValid());
162
163         
164         
165         AbstractProject.Library library1 =
166                 new AbstractProject.Library(createArchivFile(getWorkDir(),"lib1.jar"));//NOI18N
167

168         
169         uLibrary.addLibrary(library1);
170         assertTrue(uLibrary.isValid());
171         assertEquals(uLibrary.getLibraries().size(),1);
172         assertTrue(uLibrary.getLibraries().contains(library1));
173         
174         
175         testProject.addUserLibrary(uLibrary);
176         assertEquals(testProject.getUserLibraries().size(),1);
177         assertEquals(testProject.getWarnings().size(),0);
178         assertTrue(testProject.getUserLibraries().contains(uLibrary));
179         assertFalse("the same can be added just once",testProject.addUserLibrary(uLibrary));
180         assertEquals("the same library isn't added again",testProject.getUserLibraries().size(),1);
181         assertEquals(testProject.getWarnings().size(),1);
182         
183
184         AbstractProject.UserLibrary uLibrary2 = new AbstractProject.UserLibrary(this.getName()+"2");
185         testProject.addUserLibrary(uLibrary2);
186         
187         //!!!!!
188
assertEquals("UserLibrary must be added in non empy state else warning is fired",testProject.getWarnings().size(),2);
189     }
190
191
192     public static File JavaDoc createArchivFile(File JavaDoc workDir,String JavaDoc name) throws Exception JavaDoc {
193         File JavaDoc archiv = new File JavaDoc(workDir, name);
194         assertTrue(archiv.createNewFile());
195         JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(archiv));
196         jos.setComment("Just for testing");//NOI18N
197
JarEntry JavaDoc je = new JarEntry JavaDoc("oneEntry");//NOI18N
198
jos.putNextEntry(je);
199         jos.close();
200         
201         return archiv;
202     }
203     
204 }
205
Popular Tags