1 19 20 package org.netbeans.modules.projectimport.j2seimport; 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.util.Arrays ; 24 import java.util.Collection ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.List ; 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 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 testName) { 56 super(testName); 57 } 58 59 protected void setUp() throws Exception { 60 clearWorkDir(); 61 try { 62 testProject = new AbstractProject(getName(), FileUtil.toFileObject(getWorkDir())); 63 } catch(IOException iex) { 64 assert false : iex.getLocalizedMessage(); 65 throw new IllegalStateException (iex.getLocalizedMessage()); 66 } 67 } 68 69 70 protected void tearDown() throws Exception { 71 } 72 73 public static Test suite() { 74 TestSuite suite = new TestSuite(ImportUtilsTest.class); 75 76 return suite; 77 } 78 79 82 public void testSourceRoots() throws Exception { 83 File src1 = new File (getWorkDir(), "src1"); 84 assertTrue(src1.mkdir() ); 85 86 File src2 = new File (getWorkDir(), "src2"); 87 assertTrue(src2.mkdir() ); 88 89 File projectDir = new File (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 { 116 File src1 = new File (getWorkDir(), "src1"); 117 assertTrue(src1.mkdir() ); 118 119 File src2 = new File (getWorkDir(), "src2"); 120 121 124 File projectDir = new File (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 { 150 File src1 = new File (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")); assertTrue(library1.isValid()); 159 160 161 AbstractProject.Library library2 = 162 new AbstractProject.Library(AbstractProjectDefinitionTest.createArchivFile(getWorkDir(),"lib2.jar")); assertTrue(library2.isValid()); 164 165 166 File projectDir = new File (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 192 public void testAddDependency() throws Exception { 193 File projectDir = new File (getWorkDir(), "projectDir"); 194 File subPrjDir = new File (getWorkDir(), "subPrjDir"); 195 assertTrue(subPrjDir.mkdir()); 196 197 testLibraries(); 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 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 { 225 SourceRoots roots = nbProject.getSourceRoots(); 226 Sources src = ProjectUtils.getSources(nbProject); 227 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 List rootList = Arrays.asList(cls.getRoots()); 236 for (Iterator 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 246 } 247 248 private static void testSourceRoots2(ProjectModel projectDefinition, J2SEProject nbProject) throws Exception { 249 ClassPathProvider cp = (ClassPathProvider)nbProject.getLookup().lookup(ClassPathProvider.class); 250 assertNotNull(cp); 251 252 for (Iterator 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 roots = Arrays.asList(clsPath.getRoots()); 261 assertTrue(roots.contains(sourceFolder)); 262 263 } 264 } 265 } 266 267 269 270 private static void testSourceRoots(ProjectModel projectDefinition, J2SEProject nbProject) throws Exception { 271 SourceRoots roots = nbProject.getSourceRoots(); 272 273 List rootFObjects = Arrays.asList(roots.getRoots()); 274 List rootNames = Arrays.asList(roots.getRootNames()); 275 List rootURLs = Arrays.asList(roots.getRootURLs()); 276 277 278 for (Iterator 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 |