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.IOException;
046: import java.util.Arrays;
047: import java.util.Collection;
048: import java.util.HashSet;
049: import java.util.Iterator;
050: import java.util.List;
051: import junit.framework.*;
052: import org.netbeans.api.java.classpath.ClassPath;
053: import org.netbeans.api.project.ProjectManager;
054: import org.netbeans.api.project.ProjectUtils;
055: import org.netbeans.api.project.Sources;
056: import org.netbeans.junit.NbTestCase;
057: import org.netbeans.modules.java.api.common.SourceRoots;
058: import org.netbeans.modules.java.j2seproject.J2SEProject;
059: import org.netbeans.spi.java.classpath.ClassPathProvider;
060: import org.netbeans.spi.project.SubprojectProvider;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.FileUtil;
063:
064: /**
065: *
066: * @author Radek Matous
067: */
068: public class ImportUtilsTest extends NbTestCase {
069: protected AbstractProject testProject;
070:
071: static {
072: System.setProperty("projectimport.logging.level", "FINEST");
073: }
074:
075: public ImportUtilsTest(String testName) {
076: super (testName);
077: }
078:
079: protected void setUp() throws Exception {
080: clearWorkDir();
081: try {
082: testProject = new AbstractProject(getName(), FileUtil
083: .toFileObject(getWorkDir()));
084: } catch (IOException iex) {
085: assert false : iex.getLocalizedMessage();
086: throw new IllegalStateException(iex.getLocalizedMessage());
087: }
088: }
089:
090: protected void tearDown() throws Exception {
091: }
092:
093: public static Test suite() {
094: TestSuite suite = new TestSuite(ImportUtilsTest.class);
095:
096: return suite;
097: }
098:
099: /**
100: * Test of importProjectWithoutDependencies method, of class org.netbeans.modules.projectimport.jbuilder.j2seimport.ImportUtils.
101: */
102: public void testSourceRoots() throws Exception {
103: File src1 = new File(getWorkDir(), "src1");
104: assertTrue(src1.mkdir());
105:
106: File src2 = new File(getWorkDir(), "src2");
107: assertTrue(src2.mkdir());
108:
109: File projectDir = new File(getWorkDir(), "projectDir");
110: assertTrue(projectDir.mkdir());
111:
112: AbstractProject.SourceRoot srcE1 = new AbstractProject.SourceRoot(
113: src1.getName(), src1);
114: assertTrue(srcE1.isValid());
115: AbstractProject.SourceRoot srcE2 = new AbstractProject.SourceRoot(
116: src2.getName(), src2);
117: assertTrue(srcE2.isValid());
118:
119: testProject.addSourceRoot(srcE1);
120: testProject.addSourceRoot(srcE2);
121:
122: WarningContainer projectDefinitionWarnings = testProject
123: .getWarnings();
124: assertTrue(projectDefinitionWarnings.isEmpty());
125:
126: WarningContainer importingWarnings = new WarningContainer();
127: J2SEProject nbProject = ImportUtils.createInstance()
128: .importProjectWithoutDependencies(
129: FileUtil.toFileObject(projectDir), testProject,
130: importingWarnings, false);
131: assertTrue(importingWarnings.isEmpty());
132:
133: testProject.setAsImported();
134: ImportUtilsTest.testSourceRoots(testProject, nbProject);
135: ImportUtilsTest.testSourceRoots2(testProject, nbProject);
136:
137: }
138:
139: public void testSourceRootsWithWarning() throws Exception {
140: File src1 = new File(getWorkDir(), "src1");
141: assertTrue(src1.mkdir());
142:
143: File src2 = new File(getWorkDir(), "src2");
144:
145: //not created to cause warning
146: //assertTrue(src2.mkdir() );
147:
148: File projectDir = new File(getWorkDir(), "projectDir");
149: assertTrue(projectDir.mkdir());
150:
151: AbstractProject.SourceRoot srcE1 = new AbstractProject.SourceRoot(
152: src1.getName(), src1);
153: assertTrue(srcE1.isValid());
154: AbstractProject.SourceRoot srcE2 = new AbstractProject.SourceRoot(
155: src2.getName(), src2);
156: assertFalse(srcE2.isValid());
157:
158: testProject.addSourceRoot(srcE1);
159: testProject.addSourceRoot(srcE2);
160:
161: WarningContainer projectDefinitionWarnings = testProject
162: .getWarnings();
163: assertTrue(projectDefinitionWarnings.size() == 1);
164:
165: WarningContainer importingWarnings = new WarningContainer();
166: J2SEProject nbProject = ImportUtils.createInstance()
167: .importProjectWithoutDependencies(
168: FileUtil.toFileObject(projectDir), testProject,
169: importingWarnings, false);
170: assertTrue(importingWarnings.toString(), importingWarnings
171: .isEmpty());
172:
173: testProject.setAsImported();
174: ImportUtilsTest.testSourceRoots(testProject, nbProject);
175: ImportUtilsTest.testSourceRoots2(testProject, nbProject);
176: }
177:
178: public void testLibraries() throws Exception {
179: File src1 = new File(getWorkDir(), "src1");
180: assertTrue(src1.mkdir());
181:
182: AbstractProject.SourceRoot srcE1 = new AbstractProject.SourceRoot(
183: src1.getName(), src1);
184: assertTrue(srcE1.isValid());
185:
186: AbstractProject.Library library1 = new AbstractProject.Library(
187: AbstractProjectDefinitionTest.createArchivFile(
188: getWorkDir(), "lib1.jar"));//NOI18N
189: assertTrue(library1.isValid());
190:
191: AbstractProject.Library library2 = new AbstractProject.Library(
192: AbstractProjectDefinitionTest.createArchivFile(
193: getWorkDir(), "lib2.jar"));//NOI18N
194: assertTrue(library2.isValid());
195:
196: File projectDir = new File(getWorkDir(), "projectDir");
197: assertTrue(projectDir.mkdir());
198:
199: testProject.addSourceRoot(srcE1);
200: testProject.addLibrary(library1);
201: testProject.addLibrary(library2);
202:
203: WarningContainer projectDefinitionWarnings = testProject
204: .getWarnings();
205: assertTrue(projectDefinitionWarnings.isEmpty());
206:
207: WarningContainer importingWarnings = new WarningContainer();
208: J2SEProject nbProject = ImportUtils.createInstance()
209: .importProjectWithoutDependencies(
210: FileUtil.toFileObject(projectDir), testProject,
211: importingWarnings, false);
212: assertTrue(importingWarnings.isEmpty());
213:
214: testProject.setAsImported();
215:
216: ImportUtilsTest.testSourceRoots(testProject, nbProject);
217: ImportUtilsTest.testSourceRoots2(testProject, nbProject);
218: ImportUtilsTest.testLibraries(testProject, nbProject);
219: }
220:
221: /**
222: * Test of addDependencies method, of class org.netbeans.modules.projectimport.jbuilder.j2seimport.ImportUtils.
223: */
224: public void testAddDependency() throws Exception {
225: File projectDir = new File(getWorkDir(), "projectDir");
226: File subPrjDir = new File(getWorkDir(), "subPrjDir");
227: assertTrue(subPrjDir.mkdir());
228:
229: testLibraries();//for setting src root and some libraries
230:
231: AbstractProject subPrj = new AbstractProject("sub1", FileUtil
232: .toFileObject(getWorkDir()));
233: assertNotNull(subPrj);
234:
235: FileObject subPrjDirFo = FileUtil.toFileObject(subPrjDir);
236: assertNotNull(subPrjDirFo);
237:
238: ImportUtils importInstance = ImportUtils.createInstance();
239: J2SEProject subNbProject = importInstance
240: .importProjectWithoutDependencies(subPrjDirFo, subPrj,
241: new WarningContainer(), false);
242: assertNotNull(subNbProject);
243:
244: //testProject.addDependency(subPrj);
245:
246: J2SEProject nbProject = (J2SEProject) ProjectManager
247: .getDefault().findProject(
248: FileUtil.toFileObject(projectDir));
249: assertNotNull(nbProject);
250:
251: SubprojectProvider sProvider = (SubprojectProvider) nbProject
252: .getLookup().lookup(SubprojectProvider.class);
253: assertNotNull(sProvider);
254:
255: assertFalse(sProvider.getSubprojects().contains(subNbProject));
256: importInstance.addDependency(nbProject, subNbProject);
257: assertTrue(sProvider.getSubprojects().contains(subNbProject));
258: }
259:
260: private static void testLibraries(ProjectModel projectDefinition,
261: J2SEProject nbProject) throws Exception {
262: SourceRoots roots = nbProject.getSourceRoots();
263: Sources src = ProjectUtils.getSources(nbProject);
264: //workaround for unit code tests (isn't necessary for IDE run)
265: src.getSourceGroups(Sources.TYPE_GENERIC);
266: assertTrue(roots.getRoots().length > 0);
267: ClassPath cls = ClassPath.getClassPath(roots.getRoots()[0],
268: ClassPath.COMPILE);
269: assertTrue(projectDefinition.getSourceRoots().size() > 0);
270: //ProjectDefinition.SourceRootEntry srcEntry = (ProjectDefinition.SourceRootEntry )projectDefinition.getSourceRootEntries().iterator().next();
271: //boolean isValid = ((AbstractProjectDefinition.AbstractSourceEntry)srcEntry).isValid();
272: //if (isValid) {
273: List rootList = Arrays.asList(cls.getRoots());
274: for (Iterator it2 = projectDefinition.getLibraries().iterator(); it2
275: .hasNext();) {
276: ProjectModel.Library lEntry = (ProjectModel.Library) it2
277: .next();
278: FileObject archive = FileUtil.toFileObject(lEntry
279: .getArchiv());
280: assertNotNull(archive);
281: FileObject archiveRoot = FileUtil.getArchiveRoot(archive);
282: assertNotNull(archiveRoot);
283: assertTrue(rootList.contains(archiveRoot));
284: }
285: //}
286:
287: }
288:
289: private static void testSourceRoots2(
290: ProjectModel projectDefinition, J2SEProject nbProject)
291: throws Exception {
292: ClassPathProvider cp = (ClassPathProvider) nbProject
293: .getLookup().lookup(ClassPathProvider.class);
294: assertNotNull(cp);
295:
296: for (Iterator it = projectDefinition.getSourceRoots()
297: .iterator(); it.hasNext();) {
298: ProjectModel.SourceRoot srcEntry = (ProjectModel.SourceRoot) it
299: .next();
300: boolean isValid = ((AbstractProject.SourceRoot) srcEntry)
301: .isValid();
302: if (isValid) {
303: FileObject sourceFolder = FileUtil
304: .toFileObject(srcEntry.getDirectory());
305: assertNotNull(sourceFolder);
306: ClassPath clsPath = cp.findClassPath(sourceFolder,
307: ClassPath.SOURCE);
308: assertNotNull(sourceFolder.getPath(), cp.findClassPath(
309: sourceFolder, ClassPath.SOURCE));
310: List roots = Arrays.asList(clsPath.getRoots());
311: assertTrue(roots.contains(sourceFolder));
312:
313: }
314: }
315: }
316:
317: //private static void testJavaPlatform(ProjectDefinition projectDefinition, J2SEProject nbProject) throws Exception{
318:
319: private static void testSourceRoots(ProjectModel projectDefinition,
320: J2SEProject nbProject) throws Exception {
321: SourceRoots roots = nbProject.getSourceRoots();
322:
323: List rootFObjects = Arrays.asList(roots.getRoots());
324: List rootNames = Arrays.asList(roots.getRootNames());
325: List rootURLs = Arrays.asList(roots.getRootURLs());
326:
327: for (Iterator it = projectDefinition.getSourceRoots()
328: .iterator(); it.hasNext();) {
329: ProjectModel.SourceRoot srcEntry = (ProjectModel.SourceRoot) it
330: .next();
331: FileObject srcFolder = FileUtil.toFileObject(srcEntry
332: .getDirectory());
333: boolean isValid = ((AbstractProject.SourceRoot) srcEntry)
334: .isValid();
335: assertEquals(isValid, srcFolder != null);
336: assertTrue(!isValid || rootFObjects.contains(srcFolder));
337: assertTrue(!isValid
338: || rootURLs.contains(srcEntry.getDirectory()
339: .toURI().toURL()));
340: assertTrue(rootNames.contains(srcEntry.getLabel()));
341: }
342:
343: assertEquals(rootNames.size(), projectDefinition
344: .getSourceRoots().size());
345: assertEquals(rootURLs.size(), projectDefinition
346: .getSourceRoots().size());
347: }
348: }
|