KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > SortSuiteModulesTest


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.nbbuild;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.PrintStream JavaDoc;
25 import junit.framework.AssertionFailedError;
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.types.Path;
29 import org.netbeans.junit.*;
30
31 /**
32  * Test for SortSuiteModule
33  * @author pzajac
34  */

35 public class SortSuiteModulesTest extends NbTestCase {
36     private Project project;
37     String JavaDoc a = "a";
38     String JavaDoc b = "b";
39     String JavaDoc c = "c";
40     String JavaDoc d = "d";
41     String JavaDoc e = "e";
42     String JavaDoc f = "f";
43     String JavaDoc g = "g";
44     String JavaDoc SORTED_MODULES = "sorted_modules";
45     String JavaDoc NULL[] = new String JavaDoc[0];
46     
47     public SortSuiteModulesTest(java.lang.String JavaDoc testName) {
48         super(testName);
49     }
50
51     protected void setUp() throws Exception JavaDoc {
52         project = new Project();
53         project.setBaseDir(getWorkDir());
54     }
55
56   
57     public void testOnlyModuleDependencies() throws IOException JavaDoc {
58         
59         // a -> b means: a depends on b
60

61         // a-> b,c
62
// b -> d,e
63
// f -> g
64
createModule(g,NULL);
65         createModule(d,NULL);
66         createModule(c,NULL);
67         createModule(e,NULL);
68         createModule(a,new String JavaDoc[]{b,c});
69         createModule(b,new String JavaDoc[]{e,d});
70         createModule(f,new String JavaDoc[]{g});
71         
72         Path path = createPath(new String JavaDoc[]{a,b,c,d,e,f,g});
73         SortSuiteModules ssm = new SortSuiteModules();
74         ssm.setProject(project);
75         ssm.setUnsortedModules(path);
76         ssm.setSortedModulesProperty(SORTED_MODULES);
77         ssm.execute();
78         
79         String JavaDoc property = project.getProperty(SORTED_MODULES);
80         assertNotNull("null sorted modules path",property);
81         String JavaDoc paths[] = getSorted(property);
82         
83         assertEdge(paths,a,b);
84         assertEdge(paths,a,c);
85         assertEdge(paths,b,d);
86         assertEdge(paths,b,e);
87         assertEdge(paths,f,g);
88     }
89     public void testModuleDependenciesCycle() throws IOException JavaDoc {
90         createModule(a,new String JavaDoc[]{b});
91         createModule(b,new String JavaDoc[]{a});
92         Path path = createPath(new String JavaDoc[]{a,b});
93         SortSuiteModules ssm = new SortSuiteModules();
94         ssm.setProject(project);
95         ssm.setUnsortedModules(path);
96         ssm.setSortedModulesProperty(SORTED_MODULES);
97         try {
98             ssm.execute();
99             fail("Exception must be thrown");
100         } catch(BuildException be) {
101             // ok
102
}
103     }
104     public void testModuleAndTestDependenciesDisabledTestSort() throws IOException JavaDoc {
105         generateTestModules1(false);
106         
107         String JavaDoc property = project.getProperty(SORTED_MODULES);
108         assertNotNull("null sorted modules path",property);
109         String JavaDoc paths[] = getSorted(property);
110         
111         assertEdge(paths,a,b);
112         assertEdge(paths,a,c);
113         assertEdge(paths,b,d);
114         assertEdge(paths,b,e);
115         assertEdge(paths,f,g);
116         try {
117             assertEdge(paths,b,g);
118             fail("sort test deps disabled");
119         } catch (AssertionFailedError be) {};
120     }
121     public void testModuleAndTestDependenciesEnabledTestSort() throws IOException JavaDoc {
122         generateTestModules1(true);
123         
124         String JavaDoc property = project.getProperty(SORTED_MODULES);
125         assertNotNull("null sorted modules path",property);
126         String JavaDoc paths[] = getSorted(property);
127         
128         assertEdge(paths,a,b);
129         assertEdge(paths,a,c);
130         assertEdge(paths,b,d);
131         assertEdge(paths,b,e);
132         assertEdge(paths,b,g);
133         assertEdge(paths,f,g);
134     }
135
136     private void generateTestModules1(boolean sortTests) throws IOException JavaDoc, BuildException {
137         
138         // a -> b means: a depends on b
139

140         // a-> b,c
141
// b -> d,e, unittest g
142
// f -> g
143
createModule(g,NULL);
144         createModule(d,NULL);
145         createModule(c,NULL);
146         createModule(e,NULL);
147         createModule(a,new String JavaDoc[]{b,c});
148         createModule(b,new String JavaDoc[]{e,d},new String JavaDoc[]{g},NULL);
149         createModule(f,new String JavaDoc[]{g});
150         
151         Path path = createPath(new String JavaDoc[]{a,b,c,d,e,f,g});
152         SortSuiteModules ssm = new SortSuiteModules();
153         ssm.setProject(project);
154         ssm.setUnsortedModules(path);
155         ssm.setSortedModulesProperty(SORTED_MODULES);
156         if (sortTests) {
157             ssm.setSortTests(true);
158         }
159         ssm.execute();
160     }
161
162     private void createModule(String JavaDoc module, String JavaDoc[] mdeps) throws IOException JavaDoc {
163         createModule(module,mdeps,new String JavaDoc[0],new String JavaDoc[0]);
164     }
165
166     /** create module/nbbuild/project.xml
167      * @param module module and cnd
168      * @param mdeps runtime dependencies
169      * @param udeps test unit dependencies with tests
170      * @param qadeps qa-functional dependencies with tests
171      */

172     private void createModule(String JavaDoc module, String JavaDoc[] mdeps, String JavaDoc[] udeps, String JavaDoc[] qadeps) throws IOException JavaDoc {
173         File JavaDoc dir = new File JavaDoc(getWorkDir(),module + File.separator + "nbproject");
174         assertTrue("cannot create module dir",dir.mkdirs());
175         File JavaDoc xml = new File JavaDoc(dir,"project.xml");
176         PrintStream JavaDoc ps = new PrintStream JavaDoc(xml);
177         ps.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
178         ps.println("<project xmlns=\"http://www.netbeans.org/ns/project/1\">");
179         ps.println(" <type>org.netbeans.modules.apisupport.project</type>");
180         ps.println(" <configuration>");
181         ps.println(" <data xmlns=\"http://www.netbeans.org/ns/nb-module-project/2\">");
182         ps.println(" <code-name-base>" + module + "</code-name-base>");
183         ps.println(" <module-dependencies>");
184         for (int it = 0 ; it < mdeps.length ; it++) {
185             ps.println(" <dependency>");
186             ps.println(" <code-name-base>" + mdeps[it] + "</code-name-base>");
187             ps.println(" <build-prerequisite/>");
188             ps.println(" </dependency>");
189         }
190         ps.println(" </module-dependencies>");
191         ps.println(" <test-dependencies>");
192         ps.println(" <test-type>");
193         ps.println(" <name>unit</name>");
194         for (int it = 0 ; it < udeps.length ; it++ ) {
195             ps.println(" <test-dependency>");
196             ps.println(" <code-name-base>" + udeps[it] + "</code-name-base>");
197             ps.println(" <test/>");
198             ps.println(" </test-dependency>");
199         }
200         ps.println(" </test-type>");
201         ps.println(" <test-type>");
202         ps.println(" <name>qa-functional</name>");
203         for (int it = 0 ; it < qadeps.length ; it++ ) {
204             ps.println(" <test-dependency>");
205             ps.println(" <code-name-base>" + qadeps[it] + "</code-name-base>");
206             ps.println(" <test/>");
207             ps.println(" </test-dependency>");
208         }
209         ps.println(" </test-type>");
210         ps.println(" </test-dependencies>");
211         ps.println(" <public-packages/>");
212         ps.println(" </data>");
213         ps.println(" </configuration>");
214         ps.println("</project>");
215     }
216
217     private Path createPath(String JavaDoc[] paths) {
218         Path path = new Path(project);
219         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
220         for (int it = 0; it < paths.length; it++) {
221             if (sb.length() > 0) {
222                 sb.append(":");
223             }
224             sb.append(paths[it]);
225         }
226         path.setPath(sb.toString());
227         return path;
228     }
229
230     private String JavaDoc[] getSorted(String JavaDoc property) {
231         Path path = new Path(project);
232         path.setPath(property);
233         String JavaDoc paths[] = path.list();
234         
235         String JavaDoc rets [] = new String JavaDoc[paths.length];
236         for (int i = 0; i < paths.length; i++) {
237             rets[i] = new File JavaDoc(paths[i]).getName();
238             
239         }
240         return rets;
241     }
242
243     private void assertEdge(String JavaDoc[] names, String JavaDoc a, String JavaDoc b) {
244          assertTrue( a + " ->" + b, getIndex(names,a) > getIndex(names,b));
245     }
246
247     private int getIndex(String JavaDoc[] names, String JavaDoc a) {
248         for (int i = 0; i < names.length; i++) {
249             log(names[i]);
250             if (names[i].equals(a)) {
251                 return i;
252             }
253         }
254         fail("index " + a);
255         return -1;
256     }
257     
258     public void testTestDependenciesCycleEnabledTestSort() throws IOException JavaDoc {
259         createModule(a,new String JavaDoc[]{b},new String JavaDoc[]{b},NULL);
260         createModule(b,NULL,new String JavaDoc[]{a},NULL);
261         Path path = createPath(new String JavaDoc[]{a,b});
262         SortSuiteModules ssm = new SortSuiteModules();
263         ssm.setProject(project);
264         ssm.setUnsortedModules(path);
265         ssm.setSortedModulesProperty(SORTED_MODULES);
266         ssm.setSortTests(true);
267         try {
268             ssm.execute();
269             fail("Exception must be thrown");
270         } catch(BuildException be) {
271             // ok
272
}
273     }
274     public void testTestDependenciesCycleDisabledTestSort() throws IOException JavaDoc {
275         createModule(a,new String JavaDoc[]{b},new String JavaDoc[]{b},NULL);
276         createModule(b,NULL,new String JavaDoc[]{a},NULL);
277         Path path = createPath(new String JavaDoc[]{a,b});
278         SortSuiteModules ssm = new SortSuiteModules();
279         ssm.setProject(project);
280         ssm.setUnsortedModules(path);
281         ssm.setSortedModulesProperty(SORTED_MODULES);
282         // no exception
283
ssm.execute();
284     }
285 }
286
Popular Tags