001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.nbbuild;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.io.PrintStream;
047: import java.util.Arrays;
048: import junit.framework.AssertionFailedError;
049: import org.apache.tools.ant.BuildException;
050: import org.apache.tools.ant.Project;
051: import org.apache.tools.ant.types.Path;
052: import org.netbeans.junit.*;
053:
054: /**
055: * Test for SortSuiteModule
056: * @author pzajac
057: */
058: public class SortSuiteModulesTest extends NbTestCase {
059: private Project project;
060: String a = "a";
061: String b = "b";
062: String c = "c";
063: String d = "d";
064: String e = "e";
065: String f = "f";
066: String g = "g";
067: String SORTED_MODULES = "sorted_modules";
068: String NULL[] = new String[0];
069:
070: public SortSuiteModulesTest(java.lang.String testName) {
071: super (testName);
072: }
073:
074: protected @Override
075: void setUp() throws Exception {
076: project = new Project();
077: project.setBaseDir(getWorkDir());
078: }
079:
080: public void testOnlyModuleDependencies() throws IOException {
081:
082: // a -> b means: a depends on b
083:
084: // a-> b,c
085: // b -> d,e
086: // f -> g
087: createModule(g, NULL);
088: createModule(d, NULL);
089: createModule(c, NULL);
090: createModule(e, NULL);
091: createModule(a, new String[] { b, c });
092: createModule(b, new String[] { e, d });
093: createModule(f, new String[] { g });
094:
095: Path path = createPath(new String[] { a, b, c, d, e, f, g });
096: SortSuiteModules ssm = new SortSuiteModules();
097: ssm.setProject(project);
098: ssm.setUnsortedModules(path);
099: ssm.setSortedModulesProperty(SORTED_MODULES);
100: ssm.execute();
101:
102: String property = project.getProperty(SORTED_MODULES);
103: assertNotNull("null sorted modules path", property);
104: String paths[] = getSorted(property);
105:
106: assertEdge(paths, a, b);
107: assertEdge(paths, a, c);
108: assertEdge(paths, b, d);
109: assertEdge(paths, b, e);
110: assertEdge(paths, f, g);
111: }
112:
113: public void testModuleDependenciesCycle() throws IOException {
114: createModule(a, new String[] { b });
115: createModule(b, new String[] { a });
116: Path path = createPath(new String[] { a, b });
117: SortSuiteModules ssm = new SortSuiteModules();
118: ssm.setProject(project);
119: ssm.setUnsortedModules(path);
120: ssm.setSortedModulesProperty(SORTED_MODULES);
121: try {
122: ssm.execute();
123: fail("Exception must be thrown");
124: } catch (BuildException be) {
125: // ok
126: }
127: }
128:
129: public void testModuleAndTestDependenciesDisabledTestSort()
130: throws IOException {
131: generateTestModules1(false);
132:
133: String property = project.getProperty(SORTED_MODULES);
134: assertNotNull("null sorted modules path", property);
135: String paths[] = getSorted(property);
136:
137: assertEdge(paths, a, b);
138: assertEdge(paths, a, c);
139: assertEdge(paths, b, d);
140: assertEdge(paths, b, e);
141: assertEdge(paths, f, g);
142: try {
143: assertEdge(paths, b, g);
144: fail("sort test deps disabled");
145: } catch (AssertionFailedError be) {
146: }
147: }
148:
149: public void testModuleAndTestDependenciesEnabledTestSort()
150: throws IOException {
151: generateTestModules1(true);
152:
153: String property = project.getProperty(SORTED_MODULES);
154: assertNotNull("null sorted modules path", property);
155: String paths[] = getSorted(property);
156:
157: assertEdge(paths, a, b);
158: assertEdge(paths, a, c);
159: assertEdge(paths, b, d);
160: assertEdge(paths, b, e);
161: assertEdge(paths, b, g);
162: assertEdge(paths, f, g);
163: }
164:
165: private void generateTestModules1(boolean sortTests)
166: throws IOException, BuildException {
167:
168: // a -> b means: a depends on b
169:
170: // a-> b,c
171: // b -> d,e, unittest g
172: // f -> g
173: createModule(g, NULL);
174: createModule(d, NULL);
175: createModule(c, NULL);
176: createModule(e, NULL);
177: createModule(a, new String[] { b, c });
178: createModule(b, new String[] { e, d }, new String[] { g }, NULL);
179: createModule(f, new String[] { g });
180:
181: Path path = createPath(new String[] { a, b, c, d, e, f, g });
182: SortSuiteModules ssm = new SortSuiteModules();
183: ssm.setProject(project);
184: ssm.setUnsortedModules(path);
185: ssm.setSortedModulesProperty(SORTED_MODULES);
186: if (sortTests) {
187: ssm.setSortTests(true);
188: }
189: ssm.execute();
190: }
191:
192: private void createModule(String module, String[] mdeps)
193: throws IOException {
194: createModule(module, mdeps, new String[0], new String[0]);
195: }
196:
197: /** create module/nbbuild/project.xml
198: * @param module module and cnd
199: * @param mdeps runtime dependencies
200: * @param udeps test unit dependencies with tests
201: * @param qadeps qa-functional dependencies with tests
202: */
203: private void createModule(String module, String[] mdeps,
204: String[] udeps, String[] qadeps) throws IOException {
205: File dir = new File(getWorkDir(), module + File.separator
206: + "nbproject");
207: assertTrue("cannot create module dir", dir.mkdirs());
208: File xml = new File(dir, "project.xml");
209: PrintStream ps = new PrintStream(xml);
210: ps.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
211: ps
212: .println("<project xmlns=\"http://www.netbeans.org/ns/project/1\">");
213: ps
214: .println(" <type>org.netbeans.modules.apisupport.project</type>");
215: ps.println(" <configuration>");
216: ps
217: .println(" <data xmlns=\"http://www.netbeans.org/ns/nb-module-project/2\">");
218: ps.println(" <code-name-base>" + module
219: + "</code-name-base>");
220: ps.println(" <module-dependencies>");
221: for (int it = 0; it < mdeps.length; it++) {
222: ps.println(" <dependency>");
223: ps.println(" <code-name-base>"
224: + mdeps[it] + "</code-name-base>");
225: ps.println(" <build-prerequisite/>");
226: ps.println(" </dependency>");
227: }
228: ps.println(" </module-dependencies>");
229: ps.println(" <test-dependencies>");
230: ps.println(" <test-type>");
231: ps.println(" <name>unit</name>");
232: for (int it = 0; it < udeps.length; it++) {
233: ps.println(" <test-dependency>");
234: ps.println(" <code-name-base>"
235: + udeps[it] + "</code-name-base>");
236: ps.println(" <test/>");
237: ps.println(" </test-dependency>");
238: }
239: ps.println(" </test-type>");
240: ps.println(" <test-type>");
241: ps.println(" <name>qa-functional</name>");
242: for (int it = 0; it < qadeps.length; it++) {
243: ps.println(" <test-dependency>");
244: ps.println(" <code-name-base>"
245: + qadeps[it] + "</code-name-base>");
246: ps.println(" <test/>");
247: ps.println(" </test-dependency>");
248: }
249: ps.println(" </test-type>");
250: ps.println(" </test-dependencies>");
251: ps.println(" <public-packages/>");
252: ps.println(" </data>");
253: ps.println(" </configuration>");
254: ps.println("</project>");
255: }
256:
257: private Path createPath(String[] paths) {
258: Path path = new Path(project);
259: StringBuffer sb = new StringBuffer();
260: for (int it = 0; it < paths.length; it++) {
261: if (sb.length() > 0) {
262: sb.append(":");
263: }
264: sb.append(paths[it]);
265: }
266: path.setPath(sb.toString());
267: return path;
268: }
269:
270: private String[] getSorted(String property) {
271: Path path = new Path(project);
272: path.setPath(property);
273: String paths[] = path.list();
274:
275: String rets[] = new String[paths.length];
276: for (int i = 0; i < paths.length; i++) {
277: rets[i] = new File(paths[i]).getName();
278:
279: }
280: return rets;
281: }
282:
283: private void assertEdge(String[] names, String a, String b) {
284: assertTrue(a + " after " + b + " in " + Arrays.toString(names),
285: getIndex(names, a) > getIndex(names, b));
286: }
287:
288: private int getIndex(String[] names, String a) {
289: for (int i = 0; i < names.length; i++) {
290: log(names[i]);
291: if (names[i].equals(a)) {
292: return i;
293: }
294: }
295: fail("index " + a);
296: return -1;
297: }
298:
299: public void testTestDependenciesCycleEnabledTestSort()
300: throws IOException {
301: createModule(a, new String[] { b }, new String[] { b }, NULL);
302: createModule(b, NULL, new String[] { a }, NULL);
303: Path path = createPath(new String[] { a, b });
304: SortSuiteModules ssm = new SortSuiteModules();
305: ssm.setProject(project);
306: ssm.setUnsortedModules(path);
307: ssm.setSortedModulesProperty(SORTED_MODULES);
308: ssm.setSortTests(true);
309: try {
310: ssm.execute();
311: fail("Exception must be thrown");
312: } catch (BuildException be) {
313: // ok
314: }
315: }
316:
317: public void testTestDependenciesCycleDisabledTestSort()
318: throws IOException {
319: createModule(a, new String[] { b }, new String[] { b }, NULL);
320: createModule(b, NULL, new String[] { a }, NULL);
321: Path path = createPath(new String[] { a, b });
322: SortSuiteModules ssm = new SortSuiteModules();
323: ssm.setProject(project);
324: ssm.setUnsortedModules(path);
325: ssm.setSortedModulesProperty(SORTED_MODULES);
326: // no exception
327: ssm.execute();
328: }
329: }
|