001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.projectimport.j2seimport;
043:
044: import java.io.File;
045: import java.io.FileOutputStream;
046: import java.io.IOException;
047: import java.util.jar.JarEntry;
048: import java.util.jar.JarOutputStream;
049: import junit.framework.*;
050: import org.netbeans.junit.NbTestCase;
051: import org.openide.filesystems.FileUtil;
052:
053: /**
054: *
055: * @author Radek Matous
056: */
057: public class AbstractProjectDefinitionTest extends NbTestCase {
058: protected AbstractProject testProject;
059:
060: static {
061: System.setProperty("projectimport.logging.level", "FINEST");
062: }
063:
064: public AbstractProjectDefinitionTest(String testName) {
065: super (testName);
066: }
067:
068: protected final void setUp() throws Exception {
069: clearWorkDir();
070: try {
071: testProject = new AbstractProject(getName(), FileUtil
072: .toFileObject(getWorkDir()));
073: } catch (IOException iex) {
074: assert false : iex.getLocalizedMessage();
075: throw new IllegalStateException(iex.getLocalizedMessage());
076: }
077:
078: specificSetUp(testProject);
079: }
080:
081: protected void specificSetUp(AbstractProject projectDefinition) {
082: }
083:
084: private AbstractProject generalSetUp(String testName) {
085: AbstractProject retVal = null;
086: try {
087: retVal = new AbstractProject(testName, FileUtil
088: .toFileObject(getWorkDir()));
089: } catch (IOException iex) {
090: assert false : iex.getLocalizedMessage();
091: throw new IllegalStateException(iex.getLocalizedMessage());
092: }
093:
094: return retVal;
095: }
096:
097: protected void tearDown() throws Exception {
098: clearWorkDir();
099: }
100:
101: public static Test suite() {
102: TestSuite suite = new TestSuite(
103: AbstractProjectDefinitionTest.class);
104:
105: return suite;
106: }
107:
108: /**
109: * Test of getName method, of class org.netbeans.modules.projectimport.jbuilder.j2seimport.AbstractProjectDefinition.
110: */
111: public void testGetName() {
112: assertEquals(testProject.getName(), this .getName());
113: }
114:
115: /**
116: * Test of getProjectDir method, of class org.netbeans.modules.projectimport.jbuilder.j2seimport.AbstractProjectDefinition.
117: */
118: public void testGetProjectDir() throws Exception {
119: assertEquals(testProject.getProjectDir(), FileUtil
120: .toFileObject(getWorkDir()));
121: }
122:
123: /**
124: * Test of isAlreadyImported method, of class org.netbeans.modules.projectimport.jbuilder.j2seimport.AbstractProjectDefinition.
125: */
126: public void testIsAlreadyImported() {
127: assertFalse(testProject.isAlreadyImported());
128: }
129:
130: /**
131: * Test of setAsImported method, of class org.netbeans.modules.projectimport.jbuilder.j2seimport.AbstractProjectDefinition.
132: */
133: public void testSetAsImported() {
134: testProject.setAsImported();
135: assertTrue(testProject.isAlreadyImported());
136: }
137:
138: /**
139: * Test of getLibraryEntries method, of class org.netbeans.modules.projectimport.jbuilder.j2seimport.AbstractProjectDefinition.
140: */
141: public void testGetLibraryEntries() {
142: assertNotNull(testProject.getLibraries());
143: assertEquals(testProject.getLibraries().size(), 0);
144: }
145:
146: /**
147: * Test of addLibraryEntry method, of class org.netbeans.modules.projectimport.jbuilder.j2seimport.AbstractProjectDefinition.
148: */
149: public void testAddLibraryEntry() throws Exception {
150: File archiv = createArchivFile(getWorkDir(), "lib.jar");
151:
152: assertEquals(testProject.getLibraries().size(), 0);
153: assertEquals(testProject.getWarnings().size(), 0);
154:
155: AbstractProject.Library library = new AbstractProject.Library(
156: archiv);
157: assertTrue(library.isValid());
158: testProject.addLibrary(library);
159: assertEquals(testProject.getLibraries().size(), 1);
160: assertEquals(testProject.getWarnings().size(), 0);
161: assertTrue(testProject.getLibraries().contains(library));
162:
163: library = new AbstractProject.Library(getWorkDir());
164: assertFalse(library.isValid());
165: testProject.addLibrary(library);
166: assertEquals(testProject.getLibraries().size(), 2);
167: assertEquals(testProject.getWarnings().size(), 1);
168: assertTrue(testProject.getLibraries().contains(library));
169: }
170:
171: /**
172: * Test of getUserLibraries method, of class org.netbeans.modules.projectimport.jbuilder.j2seimport.AbstractProjectDefinition.
173: */
174: public void testGetUserLibraries() {
175: assertNotNull(testProject.getUserLibraries());
176: assertEquals(testProject.getUserLibraries().size(), 0);
177: }
178:
179: /**
180: * Test of addLUserLibrary method, of class org.netbeans.modules.projectimport.jbuilder.j2seimport.AbstractProjectDefinition.
181: */
182: public void testAddLUserLibrary() throws Exception {
183: AbstractProject.UserLibrary uLibrary = new AbstractProject.UserLibrary(
184: this .getName());
185: assertFalse("UserLibrary is valid if isn't empty", uLibrary
186: .isValid());
187:
188: AbstractProject.Library library1 = new AbstractProject.Library(
189: createArchivFile(getWorkDir(), "lib1.jar"));//NOI18N
190:
191: uLibrary.addLibrary(library1);
192: assertTrue(uLibrary.isValid());
193: assertEquals(uLibrary.getLibraries().size(), 1);
194: assertTrue(uLibrary.getLibraries().contains(library1));
195:
196: testProject.addUserLibrary(uLibrary);
197: assertEquals(testProject.getUserLibraries().size(), 1);
198: assertEquals(testProject.getWarnings().size(), 0);
199: assertTrue(testProject.getUserLibraries().contains(uLibrary));
200: assertFalse("the same can be added just once", testProject
201: .addUserLibrary(uLibrary));
202: assertEquals("the same library isn't added again", testProject
203: .getUserLibraries().size(), 1);
204: assertEquals(testProject.getWarnings().size(), 1);
205:
206: AbstractProject.UserLibrary uLibrary2 = new AbstractProject.UserLibrary(
207: this .getName() + "2");
208: testProject.addUserLibrary(uLibrary2);
209:
210: //!!!!!
211: assertEquals(
212: "UserLibrary must be added in non empy state else warning is fired",
213: testProject.getWarnings().size(), 2);
214: }
215:
216: public static File createArchivFile(File workDir, String name)
217: throws Exception {
218: File archiv = new File(workDir, name);
219: assertTrue(archiv.createNewFile());
220: JarOutputStream jos = new JarOutputStream(new FileOutputStream(
221: archiv));
222: jos.setComment("Just for testing");//NOI18N
223: JarEntry je = new JarEntry("oneEntry");//NOI18N
224: jos.putNextEntry(je);
225: jos.close();
226:
227: return archiv;
228: }
229:
230: }
|