KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import junit.framework.*;
29 import org.netbeans.api.java.classpath.ClassPath;
30 import org.netbeans.api.project.ProjectManager;
31 import org.netbeans.api.project.ProjectUtils;
32 import org.netbeans.api.project.Sources;
33 import org.netbeans.junit.NbTestCase;
34 import org.netbeans.modules.java.j2seproject.J2SEProject;
35 import org.netbeans.modules.java.j2seproject.SourceRoots;
36 import org.netbeans.spi.java.classpath.ClassPathProvider;
37 import org.netbeans.spi.project.SubprojectProvider;
38 import org.openide.filesystems.FileObject;
39 import org.openide.filesystems.FileUtil;
40
41
42
43 /**
44  *
45  * @author Radek Matous
46  */

47 public class ImportUtilsTest extends NbTestCase {
48     protected AbstractProject testProject;
49     
50     static {
51         System.setProperty("projectimport.logging.level", "FINEST");
52     }
53     
54     
55     public ImportUtilsTest(String JavaDoc testName) {
56         super(testName);
57     }
58     
59     protected void setUp() throws Exception JavaDoc {
60         clearWorkDir();
61         try {
62             testProject = new AbstractProject(getName(), FileUtil.toFileObject(getWorkDir()));
63         } catch(IOException JavaDoc iex) {
64             assert false : iex.getLocalizedMessage();
65             throw new IllegalStateException JavaDoc(iex.getLocalizedMessage());
66         }
67     }
68     
69     
70     protected void tearDown() throws Exception JavaDoc {
71     }
72     
73     public static Test suite() {
74         TestSuite suite = new TestSuite(ImportUtilsTest.class);
75         
76         return suite;
77     }
78     
79     /**
80      * Test of importProjectWithoutDependencies method, of class org.netbeans.modules.projectimport.jbuilder.j2seimport.ImportUtils.
81      */

82     public void testSourceRoots() throws Exception JavaDoc {
83         File JavaDoc src1 = new File JavaDoc(getWorkDir(), "src1");
84         assertTrue(src1.mkdir() );
85         
86         File JavaDoc src2 = new File JavaDoc(getWorkDir(), "src2");
87         assertTrue(src2.mkdir() );
88         
89         File JavaDoc projectDir = new File JavaDoc(getWorkDir(), "projectDir");
90         assertTrue(projectDir.mkdir() );
91         
92         
93         AbstractProject.SourceRoot srcE1 = new AbstractProject.SourceRoot(src1.getName(), src1);
94         assertTrue(srcE1.isValid());
95         AbstractProject.SourceRoot srcE2 = new AbstractProject.SourceRoot(src2.getName(), src2);
96         assertTrue(srcE2.isValid());
97         
98         testProject.addSourceRoot(srcE1);
99         testProject.addSourceRoot(srcE2);
100         
101         WarningContainer projectDefinitionWarnings = testProject.getWarnings();
102         assertTrue(projectDefinitionWarnings.isEmpty());
103         
104         
105         WarningContainer importingWarnings = new WarningContainer();
106         J2SEProject nbProject = ImportUtils.createInstance().importProjectWithoutDependencies(FileUtil.toFileObject(projectDir), testProject, importingWarnings, false);
107         assertTrue(importingWarnings.isEmpty());
108         
109         testProject.setAsImported();
110         ImportUtilsTest.testSourceRoots(testProject, nbProject);
111         ImportUtilsTest.testSourceRoots2(testProject, nbProject);
112         
113     }
114     
115     public void testSourceRootsWithWarning() throws Exception JavaDoc {
116         File JavaDoc src1 = new File JavaDoc(getWorkDir(), "src1");
117         assertTrue(src1.mkdir() );
118         
119         File JavaDoc src2 = new File JavaDoc(getWorkDir(), "src2");
120         
121         //not created to cause warning
122
//assertTrue(src2.mkdir() );
123

124         File JavaDoc projectDir = new File JavaDoc(getWorkDir(), "projectDir");
125         assertTrue(projectDir.mkdir() );
126         
127         
128         AbstractProject.SourceRoot srcE1 = new AbstractProject.SourceRoot(src1.getName(), src1);
129         assertTrue(srcE1.isValid());
130         AbstractProject.SourceRoot srcE2 = new AbstractProject.SourceRoot(src2.getName(), src2);
131         assertFalse(srcE2.isValid());
132         
133         testProject.addSourceRoot(srcE1);
134         testProject.addSourceRoot(srcE2);
135         
136         WarningContainer projectDefinitionWarnings = testProject.getWarnings();
137         assertTrue(projectDefinitionWarnings.size() == 1);
138         
139         
140         WarningContainer importingWarnings = new WarningContainer();
141         J2SEProject nbProject = ImportUtils.createInstance().importProjectWithoutDependencies(FileUtil.toFileObject(projectDir), testProject, importingWarnings, false);
142         assertTrue(importingWarnings.toString(), importingWarnings.isEmpty());
143         
144         testProject.setAsImported();
145         ImportUtilsTest.testSourceRoots(testProject, nbProject);
146         ImportUtilsTest.testSourceRoots2(testProject, nbProject);
147     }
148     
149     public void testLibraries() throws Exception JavaDoc {
150         File JavaDoc src1 = new File JavaDoc(getWorkDir(), "src1");
151         assertTrue(src1.mkdir() );
152         
153         AbstractProject.SourceRoot srcE1 = new AbstractProject.SourceRoot(src1.getName(), src1);
154         assertTrue(srcE1.isValid());
155         
156         AbstractProject.Library library1 =
157                 new AbstractProject.Library(AbstractProjectDefinitionTest.createArchivFile(getWorkDir(),"lib1.jar"));//NOI18N
158
assertTrue(library1.isValid());
159         
160         
161         AbstractProject.Library library2 =
162                 new AbstractProject.Library(AbstractProjectDefinitionTest.createArchivFile(getWorkDir(),"lib2.jar"));//NOI18N
163
assertTrue(library2.isValid());
164         
165         
166         File JavaDoc projectDir = new File JavaDoc(getWorkDir(), "projectDir");
167         assertTrue(projectDir.mkdir() );
168         
169         
170         testProject.addSourceRoot(srcE1);
171         testProject.addLibrary(library1);
172         testProject.addLibrary(library2);
173         
174         WarningContainer projectDefinitionWarnings = testProject.getWarnings();
175         assertTrue(projectDefinitionWarnings.isEmpty());
176         
177         
178         WarningContainer importingWarnings = new WarningContainer();
179         J2SEProject nbProject = ImportUtils.createInstance().importProjectWithoutDependencies(FileUtil.toFileObject(projectDir), testProject, importingWarnings, false);
180         assertTrue(importingWarnings.isEmpty());
181         
182         testProject.setAsImported();
183         
184         ImportUtilsTest.testSourceRoots(testProject, nbProject);
185         ImportUtilsTest.testSourceRoots2(testProject, nbProject);
186         ImportUtilsTest.testLibraries(testProject, nbProject);
187     }
188     
189     /**
190      * Test of addDependencies method, of class org.netbeans.modules.projectimport.jbuilder.j2seimport.ImportUtils.
191      */

192     public void testAddDependency() throws Exception JavaDoc{
193         File JavaDoc projectDir = new File JavaDoc(getWorkDir(), "projectDir");
194         File JavaDoc subPrjDir = new File JavaDoc(getWorkDir(), "subPrjDir");
195         assertTrue(subPrjDir.mkdir());
196         
197         testLibraries();//for setting src root and some libraries
198

199         AbstractProject subPrj = new AbstractProject("sub1", FileUtil.toFileObject(getWorkDir()));
200         assertNotNull(subPrj);
201
202         FileObject subPrjDirFo = FileUtil.toFileObject(subPrjDir);
203         assertNotNull(subPrjDirFo);
204         
205         ImportUtils importInstance = ImportUtils.createInstance();
206         J2SEProject subNbProject = importInstance.importProjectWithoutDependencies(subPrjDirFo, subPrj,new WarningContainer(), false);
207         assertNotNull(subNbProject);
208         
209         //testProject.addDependency(subPrj);
210

211         J2SEProject nbProject = (J2SEProject)ProjectManager.getDefault().findProject(FileUtil.toFileObject(projectDir));
212         assertNotNull(nbProject);
213
214         SubprojectProvider sProvider = (SubprojectProvider)nbProject.getLookup().lookup(SubprojectProvider.class);
215         assertNotNull(sProvider);
216         
217         
218         assertFalse(sProvider.getSubprojects().contains(subNbProject));
219         importInstance.addDependency(nbProject, subNbProject);
220         assertTrue(sProvider.getSubprojects().contains(subNbProject));
221     }
222     
223     
224     private static void testLibraries(ProjectModel projectDefinition, J2SEProject nbProject) throws Exception JavaDoc{
225         SourceRoots roots = nbProject.getSourceRoots();
226         Sources src = ProjectUtils.getSources(nbProject);
227         //workaround for unit code tests (isn't necessary for IDE run)
228
src.getSourceGroups(Sources.TYPE_GENERIC);
229         assertTrue(roots.getRoots().length > 0);
230         ClassPath cls = ClassPath.getClassPath(roots.getRoots()[0], ClassPath.COMPILE);
231         assertTrue(projectDefinition.getSourceRoots().size() > 0);
232         //ProjectDefinition.SourceRootEntry srcEntry = (ProjectDefinition.SourceRootEntry )projectDefinition.getSourceRootEntries().iterator().next();
233
//boolean isValid = ((AbstractProjectDefinition.AbstractSourceEntry)srcEntry).isValid();
234
//if (isValid) {
235
List JavaDoc rootList = Arrays.asList(cls.getRoots());
236         for (Iterator JavaDoc it2 = projectDefinition.getLibraries().iterator(); it2.hasNext(); ) {
237             ProjectModel.Library lEntry = (ProjectModel.Library)it2.next();
238             FileObject archive = FileUtil.toFileObject(lEntry.getArchiv());
239             assertNotNull(archive);
240             FileObject archiveRoot = FileUtil.getArchiveRoot(archive);
241             assertNotNull(archiveRoot);
242             assertTrue(rootList.contains(archiveRoot));
243         }
244         //}
245

246     }
247     
248     private static void testSourceRoots2(ProjectModel projectDefinition, J2SEProject nbProject) throws Exception JavaDoc{
249         ClassPathProvider cp = (ClassPathProvider)nbProject.getLookup().lookup(ClassPathProvider.class);
250         assertNotNull(cp);
251         
252         for (Iterator JavaDoc it = projectDefinition.getSourceRoots().iterator(); it.hasNext(); ) {
253             ProjectModel.SourceRoot srcEntry = (ProjectModel.SourceRoot)it.next();
254             boolean isValid = ((AbstractProject.SourceRoot)srcEntry).isValid();
255             if (isValid) {
256                 FileObject sourceFolder = FileUtil.toFileObject(srcEntry.getDirectory());
257                 assertNotNull(sourceFolder);
258                 ClassPath clsPath = cp.findClassPath(sourceFolder, ClassPath.SOURCE);
259                 assertNotNull(sourceFolder.getPath(),cp.findClassPath(sourceFolder, ClassPath.SOURCE));
260                 List JavaDoc roots = Arrays.asList(clsPath.getRoots());
261                 assertTrue(roots.contains(sourceFolder));
262                 
263             }
264         }
265     }
266     
267     //private static void testJavaPlatform(ProjectDefinition projectDefinition, J2SEProject nbProject) throws Exception{
268

269     
270     private static void testSourceRoots(ProjectModel projectDefinition, J2SEProject nbProject) throws Exception JavaDoc{
271         SourceRoots roots = nbProject.getSourceRoots();
272         
273         List JavaDoc rootFObjects = Arrays.asList(roots.getRoots());
274         List JavaDoc rootNames = Arrays.asList(roots.getRootNames());
275         List JavaDoc rootURLs = Arrays.asList(roots.getRootURLs());
276         
277         
278         for (Iterator JavaDoc it = projectDefinition.getSourceRoots().iterator(); it.hasNext(); ) {
279             ProjectModel.SourceRoot srcEntry = (ProjectModel.SourceRoot)it.next();
280             FileObject srcFolder = FileUtil.toFileObject(srcEntry.getDirectory());
281             boolean isValid = ((AbstractProject.SourceRoot)srcEntry).isValid();
282             assertEquals(isValid, srcFolder != null);
283             assertTrue(!isValid || rootFObjects.contains(srcFolder));
284             assertTrue(!isValid || rootURLs.contains(srcEntry.getDirectory().toURI().toURL()));
285             assertTrue(rootNames.contains(srcEntry.getLabel()));
286         }
287         
288         assertEquals(rootNames.size(),projectDefinition.getSourceRoots().size());
289         assertEquals(rootURLs.size(),projectDefinition.getSourceRoots().size());
290     }
291 }
292
Popular Tags