1 19 20 package org.netbeans.api.project; 21 22 import java.io.File ; 23 import java.util.Arrays ; 24 import java.util.HashSet ; 25 import java.util.Set ; 26 import javax.swing.event.ChangeListener ; 27 import org.netbeans.junit.NbTestCase; 28 import org.netbeans.spi.project.SubprojectProvider; 29 import org.openide.filesystems.FileObject; 30 import org.openide.filesystems.FileUtil; 31 import org.openide.util.Lookup; 32 import org.openide.util.lookup.Lookups; 33 34 38 public class ProjectUtilsTest extends NbTestCase { 39 40 static { 41 TestUtil.setLookup(new Object [] { 42 TestUtil.testProjectFactory(), 43 }); 44 } 45 46 public ProjectUtilsTest(String name) { 47 super(name); 48 } 49 50 public void testHasSubprojectCycles() throws Exception { 51 TestProj a = new TestProj("a"); 53 assertFalse("no cycles in a project with no declared subprojects", ProjectUtils.hasSubprojectCycles(a, null)); 54 a.subprojs = new Project[0]; 55 assertFalse("no cycles in a standalone project", ProjectUtils.hasSubprojectCycles(a, null)); 56 TestProj b = new TestProj("b"); 57 a.subprojs = new Project[] {b}; 58 b.subprojs = new Project[0]; 59 assertFalse("no cycles in a -> b", ProjectUtils.hasSubprojectCycles(a, null)); 60 TestProj c = new TestProj("c"); 61 c.subprojs = new Project[0]; 62 b.subprojs = new Project[] {c}; 63 assertFalse("no cycles in a -> b -> c", ProjectUtils.hasSubprojectCycles(a, null)); 64 TestProj d = new TestProj("d"); 65 d.subprojs = new Project[0]; 66 b.subprojs = new Project[] {d}; 67 c.subprojs = new Project[] {d}; 68 assertFalse("no cycles in a -> {b, c} -> d (DAG)", ProjectUtils.hasSubprojectCycles(a, null)); 69 a.subprojs = new Project[] {a}; 70 assertTrue("self-loop cycle in a -> a", ProjectUtils.hasSubprojectCycles(a, null)); 71 a.subprojs = new Project[] {b}; 72 b.subprojs = new Project[] {a}; 73 assertTrue("simple cycle in a -> b -> a", ProjectUtils.hasSubprojectCycles(a, null)); 74 b.subprojs = new Project[] {c}; 75 c.subprojs = new Project[] {b}; 76 assertTrue("simple cycle not involing master in a -> b -> c -> b", ProjectUtils.hasSubprojectCycles(a, null)); 77 c.subprojs = new Project[] {a}; 78 a.subprojs = new Project[] {b, d}; 79 d.subprojs = new Project[] {a}; 80 assertTrue("multiple cycles in a -> b -> c -> a, a -> d -> a", ProjectUtils.hasSubprojectCycles(a, null)); 81 a.subprojs = new Project[0]; 82 b.subprojs = new Project[0]; 83 assertFalse("no cycle introduced by a -> b in a, b", ProjectUtils.hasSubprojectCycles(a, b)); 84 c.subprojs = new Project[0]; 85 b.subprojs = new Project[] {c}; 86 assertFalse("no cycle introduced by a -> b in a, b -> c", ProjectUtils.hasSubprojectCycles(a, b)); 87 a.subprojs = new Project[] {b}; 88 assertFalse("no cycle introduced by no-op a -> b in a -> b -> c", ProjectUtils.hasSubprojectCycles(a, b)); 89 assertFalse("no cycle introduced by direct a -> c in a -> b -> c", ProjectUtils.hasSubprojectCycles(a, c)); 90 assertTrue("cycle introduced by a -> a in a -> b -> c", ProjectUtils.hasSubprojectCycles(a, a)); 91 assertTrue("cycle introduced by b -> a in a -> b -> c", ProjectUtils.hasSubprojectCycles(b, a)); 92 assertTrue("cycle introduced by c -> a in a -> b -> c", ProjectUtils.hasSubprojectCycles(c, a)); 93 c.subprojs = null; 94 assertTrue("cycle introduced by c -> a in a -> b -> c (no explicit subprojects in c)", ProjectUtils.hasSubprojectCycles(c, a)); 95 a = new TestProj("a"); 97 b = new TestProj("b"); 98 c = new TestProj("c"); 99 d = new TestProj("d"); 100 a.subprojs = new Project[] {b, c}; 101 b.subprojs = new Project[] {d}; 102 c.subprojs = new Project[] {d}; 103 d.subprojs = new Project[] {}; 104 assertFalse("diamond, no cycles", ProjectUtils.hasSubprojectCycles(a, null)); 105 assertEquals("A asked for subprojects just once", 1, a.getSubprojectsCalled()); 106 assertEquals("B asked for subprojects just once", 1, b.getSubprojectsCalled()); 107 assertEquals("C asked for subprojects just once", 1, c.getSubprojectsCalled()); 108 assertEquals("D asked for subprojects just once", 1, d.getSubprojectsCalled()); 109 } 110 111 public void testGenericSources() throws Exception { 112 clearWorkDir(); 113 File topF = new File (getWorkDir(), "top"); 114 assertTrue(new File (topF, "testproject").mkdirs()); 115 assertTrue(new File (topF, "nested" + File.separator + "testproject").mkdirs()); 116 assertTrue(new File (topF, "file").createNewFile()); 117 FileObject top = FileUtil.toFileObject(topF); 118 assertNotNull(top); 119 Project p = ProjectManager.getDefault().findProject(top); 120 assertNotNull(p); 121 Sources s = ProjectUtils.getSources(p); 122 SourceGroup[] grps = s.getSourceGroups(Sources.TYPE_GENERIC); 123 assertEquals(1, grps.length); 124 assertEquals(top, grps[0].getRootFolder()); 125 assertEquals("top", grps[0].getDisplayName()); 126 assertTrue(grps[0].contains(top)); 127 FileObject file = top.getFileObject("file"); 128 assertNotNull(file); 129 assertTrue(grps[0].contains(file)); 130 FileObject nested = top.getFileObject("nested"); 131 assertNotNull(nested); 132 assertFalse(grps[0].contains(nested)); 133 assertEquals(1, TestUtil.projectLoadCount(top)); 134 assertEquals("#67450: did not have to load nested project", 0, TestUtil.projectLoadCount(nested)); 135 } 137 138 141 private static final class TestProj implements Project, SubprojectProvider { 142 143 private final String name; 144 148 public Project[] subprojs = null; 149 private int getSubprojectsCalled; 150 151 155 public TestProj(String name) { 156 this.name = name; 157 } 158 159 public Lookup getLookup() { 160 if (subprojs == null) { 161 return Lookup.EMPTY; 162 } else { 163 return Lookups.singleton(this); 164 } 165 } 166 167 public Set <? extends Project> getSubprojects() { 168 getSubprojectsCalled++; 169 assert subprojs != null; 170 return new HashSet <Project>(Arrays.asList(subprojs)); 171 } 172 173 176 public int getSubprojectsCalled() { 177 int c = getSubprojectsCalled; 178 getSubprojectsCalled = 0; 179 return c; 180 } 181 182 public FileObject getProjectDirectory() { 183 return FileUtil.createMemoryFileSystem().getRoot(); 185 } 186 187 public void addChangeListener(ChangeListener l) {} 188 189 public void removeChangeListener(ChangeListener l) {} 190 191 public String toString() { 192 return name; 193 } 194 195 } 196 197 } 198 | Popular Tags |