KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > project > ProjectUtilsTest


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.api.project;
21
22 import java.io.File JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.swing.event.ChangeListener JavaDoc;
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 /**
35  * Test {@link ProjectUtils}.
36  * @author Jesse Glick
37  */

38 public class ProjectUtilsTest extends NbTestCase {
39
40     static {
41         TestUtil.setLookup(new Object JavaDoc[] {
42             TestUtil.testProjectFactory(),
43         });
44     }
45     
46     public ProjectUtilsTest(String JavaDoc name) {
47         super(name);
48     }
49     
50     public void testHasSubprojectCycles() throws Exception JavaDoc {
51         // Check static cycle detection.
52
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         // Performance check.
96
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 JavaDoc {
112         clearWorkDir();
113         File JavaDoc topF = new File JavaDoc(getWorkDir(), "top");
114         assertTrue(new File JavaDoc(topF, "testproject").mkdirs());
115         assertTrue(new File JavaDoc(topF, "nested" + File.separator + "testproject").mkdirs());
116         assertTrue(new File JavaDoc(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         // XXX could also test contains(...) on unsharable files
136
}
137     
138     /**
139      * Fake project with subprojects.
140      */

141     private static final class TestProj implements Project, SubprojectProvider {
142         
143         private final String JavaDoc name;
144         /**
145          * Subproject list.
146          * Use null to not have a SubprojectProvider at all.
147          */

148         public Project[] subprojs = null;
149         private int getSubprojectsCalled;
150
151         /**
152          * Create a fake project.
153          * @param name a name for debugging purposes
154          */

155         public TestProj(String JavaDoc 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 JavaDoc<? extends Project> getSubprojects() {
168             getSubprojectsCalled++;
169             assert subprojs != null;
170             return new HashSet JavaDoc<Project>(Arrays.asList(subprojs));
171         }
172         
173         /**
174          * Number of times {@link #getSubprojects} was called since last check.
175          */

176         public int getSubprojectsCalled() {
177             int c = getSubprojectsCalled;
178             getSubprojectsCalled = 0;
179             return c;
180         }
181
182         public FileObject getProjectDirectory() {
183             // irrelevant
184
return FileUtil.createMemoryFileSystem().getRoot();
185         }
186         
187         public void addChangeListener(ChangeListener JavaDoc l) {}
188         
189         public void removeChangeListener(ChangeListener JavaDoc l) {}
190         
191         public String JavaDoc toString() {
192             return name;
193         }
194
195     }
196     
197 }
198
Popular Tags