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.ByteArrayOutputStream;
045: import java.io.File;
046: import java.io.FileOutputStream;
047: import java.io.FileReader;
048: import java.io.IOException;
049: import java.io.InputStream;
050: import java.util.Arrays;
051: import java.util.StringTokenizer;
052: import java.util.jar.JarEntry;
053: import java.util.jar.JarOutputStream;
054: import java.util.jar.Manifest;
055: import java.util.zip.ZipEntry;
056: import java.util.zip.ZipOutputStream;
057: import junit.framework.*;
058:
059: import org.netbeans.junit.*;
060:
061: /** Behaviour of fixing module dependencies. Knows how to replace old
062: * with new ones and remove those that are not needed for compilation.
063: *
064: * @author Jaroslav Tulach
065: */
066: public class FixDependenciesTest extends NbTestCase {
067: public FixDependenciesTest(String name) {
068: super (name);
069: }
070:
071: public void testReplaceOpenideDepWithSmallerOnes() throws Exception {
072: java.io.File xml = PublicPackagesInProjectizedXMLTest
073: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
074: + "<project>"
075: + " <module-dependencies>"
076: + " <dependency>"
077: + " <code-name-base>org.openide</code-name-base>"
078: + " <build-prerequisite/> "
079: + " <compile-dependency/> "
080: + " <run-dependency>"
081: + " <specification-version>3.17</specification-version> "
082: + " </run-dependency>"
083: + " </dependency>"
084: + " </module-dependencies>" + "</project>");
085:
086: java.io.File f = PublicPackagesInProjectizedXMLTest
087: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
088: + "<project name=\"Replace Openide\" basedir=\".\" default=\"all\" >"
089: + " <taskdef name=\"fix\" classname=\"org.netbeans.nbbuild.FixDependencies\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>"
090: + "<target name=\"all\" >"
091: + "<fix>"
092: + " <replace codenamebase=\"org.openide\" >"
093: + " <module codenamebase=\"org.openide.util\" spec=\"6.2\" />"
094: + " <module codenamebase=\"org.openide.awt\" spec=\"6.2\" />"
095: + " </replace>"
096: + " <fileset dir=\""
097: + xml.getParent()
098: + "\">"
099: + " <include name=\""
100: + xml.getName()
101: + "\" /> "
102: + " </fileset>"
103: + "</fix>"
104: + "</target>" + "</project>"
105:
106: );
107: PublicPackagesInProjectizedXMLTest.execute(f, new String[] {});
108:
109: String result = PublicPackagesInProjectizedXMLTest
110: .readFile(xml);
111:
112: if (result.indexOf("org.openide.util") == -1) {
113: fail("org.openide.util should be there: " + result);
114: }
115: if (result.indexOf("org.openide.awt") == -1) {
116: fail("org.openide.awt should be there: " + result);
117: }
118:
119: if (result
120: .indexOf("<specification-version>6.2</specification-version>") == -1) {
121: fail("Spec version must be updated to 6.2: " + result);
122: }
123: }
124:
125: public void testVerificationOfRemovedDependencies()
126: throws Exception {
127: java.io.File xml = PublicPackagesInProjectizedXMLTest
128: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
129: + "<project>"
130: + " <module-dependencies>"
131: + " <dependency>"
132: + " <code-name-base>org.openide.keep1</code-name-base>"
133: + " <build-prerequisite/> "
134: + " <compile-dependency/> "
135: + " <run-dependency>"
136: + " <specification-version>6.2</specification-version> "
137: + " </run-dependency>"
138: + " </dependency>"
139: + " <dependency>"
140: + " <code-name-base>org.openide.remove</code-name-base>"
141: + " <build-prerequisite/> "
142: + " <compile-dependency/> "
143: + " <run-dependency>"
144: + " <specification-version>6.2</specification-version> "
145: + " </run-dependency>"
146: + " </dependency>"
147: + " <dependency>"
148: + " <code-name-base>org.openide.keep2</code-name-base>"
149: + " <build-prerequisite/> "
150: + " <compile-dependency/> "
151: + " <run-dependency>"
152: + " <specification-version>6.2</specification-version> "
153: + " </run-dependency>"
154: + " </dependency>"
155: + " </module-dependencies>" + "</project>");
156:
157: java.io.File out = PublicPackagesInProjectizedXMLTest
158: .extractString("");
159:
160: java.io.File f = PublicPackagesInProjectizedXMLTest
161: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
162: + "<project name=\"Replace Openide\" basedir=\".\" default=\"all\" >"
163: + " <taskdef name=\"fix\" classname=\"org.netbeans.nbbuild.FixDependencies\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>"
164: + "<target name=\"all\" >"
165: + "<fix antfile=\"${buildscript}\" buildtarget=\"verify\" cleantarget=\"clean\" >"
166: + " <replace codenamebase=\"org.openide\" >"
167: + " <module codenamebase=\"org.openide.util\" spec=\"6.2\" />"
168: + " <module codenamebase=\"org.openide.awt\" spec=\"6.2\" />"
169: + " </replace>" + " <fileset dir=\""
170: + xml.getParent()
171: + "\">"
172: + " <include name=\""
173: + xml.getName()
174: + "\" /> "
175: + " </fileset>"
176: + "</fix>"
177: + "</target>"
178: + ""
179: + "<target name=\"verify\" >"
180: + " <echo message=\"v\" file=\""
181: + out.getPath()
182: + "\" append='true' />"
183: + " <loadfile property=\"p\" srcFile=\""
184: + xml.getPath()
185: + "\" />"
186: + " <condition property=\"remove\" >"
187: + " <and>"
188: + " <not>"
189: + " <and>"
190: + " <contains string=\"${p}\" substring=\"org.openide.keep1\" />"
191: + " <contains string=\"${p}\" substring=\"org.openide.keep2\" />"
192: + " </and>"
193: + " </not>"
194: + " <contains string=\"${p}\" substring=\"org.openide.remove\" />"
195: + " </and>"
196: + " </condition>"
197: +
198: // fail if there is org.openide.remove and at least one
199: // of org.openide.keep is missing
200: " <fail if=\"remove\" /> "
201: + "</target>"
202: + "<target name=\"clean\" >"
203: + " <echo message=\"c\" file=\""
204: + out.getPath()
205: + "\" append='true' />"
206: + "</target>" + "</project>"
207:
208: );
209: PublicPackagesInProjectizedXMLTest.execute(f,
210: new String[] { "-Dbuildscript=" + f.getPath() });
211:
212: String result = PublicPackagesInProjectizedXMLTest
213: .readFile(xml);
214:
215: if (result.indexOf("org.openide.keep") == -1) {
216: fail("org.openide.keep should be there: " + result);
217: }
218: if (result.indexOf("org.openide.remove") != -1) {
219: fail("org.openide.remove should not be there: " + result);
220: }
221:
222: String written = PublicPackagesInProjectizedXMLTest
223: .readFile(out);
224: assertEquals(
225: "First we do clean, test verify, then clean and verify three times as there are three <dependency> tags",
226: "cvcvcvcvcv", written);
227: }
228:
229: public void testBrokenCoreSettingsReplacement() throws Exception {
230:
231: String projectXML = "<?xml version='1.0' encoding='UTF-8'?>\n"
232: + "<!--\n"
233: + " CDDL Notice\n"
234: + "\n"
235: + "The Original Code is NetBeans. The Initial Developer of the Original\n"
236: + "Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun\n"
237: + "Microsystems, Inc. All Rights Reserved.\n"
238: + "-->\n"
239: + "<project xmlns='http://www.netbeans.org/ns/project/1'>\n"
240: + "<type>org.netbeans.modules.apisupport.project</type>\n"
241: + "<configuration>\n"
242: + "<data xmlns='http://www.netbeans.org/ns/nb-module-project/2'>\n"
243: + "<code-name-base>org.netbeans.modules.settings</code-name-base>\n"
244: + "<module-dependencies>\n"
245: + "<dependency>\n"
246: + "<code-name-base>org.openide</code-name-base>\n"
247: + "<build-prerequisite/>\n"
248: + "<compile-dependency/>\n"
249: + "<run-dependency>\n"
250: + "<release-version>1</release-version>\n"
251: + "<specification-version>3.17</specification-version>\n"
252: + "</run-dependency>\n"
253: + "</dependency>\n"
254: + "<dependency>\n"
255: + "<code-name-base>org.openide.loaders</code-name-base>\n"
256: + "<build-prerequisite/>\n" + "<compile-dependency/>\n"
257: + "<run-dependency/>\n" + "</dependency>\n"
258: + "</module-dependencies>\n" + "<public-packages>\n"
259: + "<package>org.netbeans.spi.settings</package>\n"
260: + "</public-packages>\n" + "<javadoc/>\n" + "</data>\n"
261: + "</configuration>\n" + "</project>\n";
262:
263: java.io.File xml = PublicPackagesInProjectizedXMLTest
264: .extractString(projectXML);
265:
266: java.io.File out = PublicPackagesInProjectizedXMLTest
267: .extractString("");
268:
269: java.io.File f = PublicPackagesInProjectizedXMLTest
270: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
271: + "<project name=\"Replace Openide\" basedir=\".\" default=\"all\" >"
272: + " <taskdef name=\"fix\" classname=\"org.netbeans.nbbuild.FixDependencies\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>"
273: + "<target name=\"all\" >"
274: + "<fix >"
275: + " <replace codenamebase='org.openide'>\n"
276: + " <module codenamebase='org.openide.filesystems' spec='6.2'/>\n"
277: + " <module codenamebase='org.openide.util' spec='6.2'/>\n"
278: + " <module codenamebase='org.openide.util.enumerations' spec='6.2'/>\n"
279: + " <module codenamebase='org.openide.modules' spec='6.2'/>\n"
280: + " <module codenamebase='org.openide.nodes' spec='6.2'/>\n"
281: + " <module codenamebase='org.openide.explorer' spec='6.2'/>\n"
282: + " <module codenamebase='org.openide.awt' spec='6.2'/>\n"
283: + " <module codenamebase='org.openide.dialogs' spec='6.2'/>\n"
284: + " <module codenamebase='org.openide.compat' spec='6.2'/>\n"
285: + " <module codenamebase='org.openide.options' spec='6.2'/>\n"
286: + " <module codenamebase='org.openide.windows' spec='6.2'/>\n"
287: + " <module codenamebase='org.openide.text' spec='6.2'/>\n"
288: + " <module codenamebase='org.openide.actions' spec='6.2'/>\n"
289: + " <module codenamebase='org.openide.loaders' spec='6.2'/>\n"
290: + " </replace>\n" + " <fileset dir=\""
291: + xml.getParent()
292: + "\">"
293: + " <include name=\""
294: + xml.getName()
295: + "\" /> "
296: + " </fileset>"
297: + "</fix>"
298: + "</target>"
299: + ""
300: + "<target name=\"verify\" >"
301: + " <echo message=\"v\" file=\""
302: + out.getPath()
303: + "\" append='true' />"
304: + " <loadfile property=\"p\" srcFile=\""
305: + xml.getPath()
306: + "\" />"
307: + "</target>"
308: + "<target name=\"clean\" >"
309: + " <echo message=\"c\" file=\""
310: + out.getPath()
311: + "\" append='true' />"
312: + "</target>" + "</project>"
313:
314: );
315: org.w3c.dom.Document doc;
316: doc = javax.xml.parsers.DocumentBuilderFactory.newInstance()
317: .newDocumentBuilder().parse(xml);
318: assertNotNull("Originally can be parsed", doc);
319:
320: PublicPackagesInProjectizedXMLTest.execute(f,
321: new String[] { "-Dbuildscript=" + f.getPath() });
322:
323: doc = javax.xml.parsers.DocumentBuilderFactory.newInstance()
324: .newDocumentBuilder().parse(xml);
325:
326: assertNotNull("Still can be parsed", doc);
327:
328: String r = PublicPackagesInProjectizedXMLTest.readFile(xml);
329: assertEquals(
330: "No release version used as modules do not have it",
331: -1, r.indexOf("release-version"));
332:
333: int idx = r
334: .indexOf("<code-name-base>org.openide.loaders</code-name-base>");
335: if (idx == -1) {
336: fail("One dep on loaders should be there: " + r);
337: }
338:
339: assertEquals("No next loader dep", -1, r.indexOf(
340: "<code-name-base>org.openide.loaders</code-name-base>",
341: idx + 10));
342: }
343:
344: public void testPropertiesAreNotInfluencedByPreviousExecution()
345: throws Exception {
346: java.io.File xml = PublicPackagesInProjectizedXMLTest
347: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
348: + "<project>"
349: + " <module-dependencies>"
350: + " <dependency>"
351: + " <code-name-base>org.openide.keep1</code-name-base>"
352: + " <build-prerequisite/> "
353: + " <compile-dependency/> "
354: + " <run-dependency>"
355: + " <specification-version>6.2</specification-version> "
356: + " </run-dependency>"
357: + " </dependency>"
358: + " <dependency>"
359: + " <code-name-base>org.openide.remove</code-name-base>"
360: + " <build-prerequisite/> "
361: + " <compile-dependency/> "
362: + " <run-dependency>"
363: + " <specification-version>6.2</specification-version> "
364: + " </run-dependency>"
365: + " </dependency>"
366: + " <dependency>"
367: + " <code-name-base>org.openide.keep2</code-name-base>"
368: + " <build-prerequisite/> "
369: + " <compile-dependency/> "
370: + " <run-dependency>"
371: + " <specification-version>6.2</specification-version> "
372: + " </run-dependency>"
373: + " </dependency>"
374: + " </module-dependencies>" + "</project>");
375:
376: java.io.File out = PublicPackagesInProjectizedXMLTest
377: .extractString("");
378:
379: java.io.File f = PublicPackagesInProjectizedXMLTest
380: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
381: + "<project name=\"Separate namespaces\" basedir=\".\" default=\"all\" >"
382: + " <taskdef name=\"fix\" classname=\"org.netbeans.nbbuild.FixDependencies\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>"
383: + "<target name=\"all\" >"
384: + "<fix antfile=\"${buildscript}\" buildtarget=\"verify\" cleantarget='verify'>"
385: + " <fileset dir=\""
386: + xml.getParent()
387: + "\">"
388: + " <include name=\""
389: + xml.getName()
390: + "\" /> "
391: + " </fileset>"
392: + "</fix>"
393: + "</target>"
394: + ""
395: + "<target name=\"verify\" >"
396: + " <fail if=\"remove\" /> "
397: + " <property name='remove' value='some' />"
398: + " <fail unless=\"remove\" /> "
399: + " <echo message=\"v\" file=\""
400: + out.getPath()
401: + "\" append='true' />"
402: + "</target>" + "</project>"
403:
404: );
405: PublicPackagesInProjectizedXMLTest.execute(f,
406: new String[] { "-Dbuildscript=" + f.getPath() });
407:
408: String result = PublicPackagesInProjectizedXMLTest
409: .readFile(xml);
410:
411: String written = PublicPackagesInProjectizedXMLTest
412: .readFile(out);
413: assertEquals("The property remove is never set", "vvvvvvvvvv",
414: written);
415: }
416:
417: public void testOnlyCompileTimeDependenciesCanBeRemoved()
418: throws Exception {
419: java.io.File xml = PublicPackagesInProjectizedXMLTest
420: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
421: + "<project>"
422: + " <module-dependencies>"
423: + " <dependency>"
424: + " <code-name-base>org.openide.keep1</code-name-base>"
425: + " <build-prerequisite/> "
426: + " <compile-dependency/> "
427: + " <run-dependency>"
428: + " <specification-version>6.2</specification-version> "
429: + " </run-dependency>"
430: + " </dependency>"
431: + " <dependency>"
432: + " <code-name-base>org.openide.remove</code-name-base>"
433: + " <build-prerequisite/> "
434: +
435: // This changes the meaning: " <compile-dependency/> " +
436: " <run-dependency>"
437: + " <specification-version>6.2</specification-version> "
438: + " </run-dependency>"
439: + " </dependency>"
440: + " <dependency>"
441: + " <code-name-base>org.openide.keep2</code-name-base>"
442: + " <build-prerequisite/> "
443: + " <compile-dependency/> "
444: + " <run-dependency>"
445: + " <specification-version>6.2</specification-version> "
446: + " </run-dependency>"
447: + " </dependency>"
448: + " </module-dependencies>" + "</project>");
449:
450: java.io.File out = PublicPackagesInProjectizedXMLTest
451: .extractString("");
452:
453: java.io.File f = PublicPackagesInProjectizedXMLTest
454: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
455: + "<project name=\"Replace Openide\" basedir=\".\" default=\"all\" >"
456: + " <taskdef name=\"fix\" classname=\"org.netbeans.nbbuild.FixDependencies\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>"
457: + "<target name=\"all\" >"
458: + "<fix antfile=\"${buildscript}\" buildtarget=\"verify\" cleantarget=\"clean\" >"
459: + " <replace codenamebase=\"org.openide\" >"
460: + " <module codenamebase=\"org.openide.util\" spec=\"6.2\" />"
461: + " <module codenamebase=\"org.openide.awt\" spec=\"6.2\" />"
462: + " </replace>" + " <fileset dir=\""
463: + xml.getParent()
464: + "\">"
465: + " <include name=\""
466: + xml.getName()
467: + "\" /> "
468: + " </fileset>"
469: + "</fix>"
470: + "</target>"
471: + ""
472: + "<target name=\"verify\" >"
473: + " <echo message=\"v\" file=\""
474: + out.getPath()
475: + "\" append='true' />"
476: + " <loadfile property=\"p\" srcFile=\""
477: + xml.getPath()
478: + "\" />"
479: + " <condition property=\"remove\" >"
480: + " <and>"
481: + " <not>"
482: + " <and>"
483: + " <contains string=\"${p}\" substring=\"org.openide.keep1\" />"
484: + " <contains string=\"${p}\" substring=\"org.openide.keep2\" />"
485: + " </and>"
486: + " </not>"
487: + " <contains string=\"${p}\" substring=\"org.openide.remove\" />"
488: + " </and>"
489: + " </condition>"
490: +
491: // fail if there is org.openide.remove and at least one
492: // of org.openide.keep is missing
493: " <fail if=\"remove\" /> "
494: + "</target>"
495: + "<target name=\"clean\" >"
496: + " <echo message=\"c\" file=\""
497: + out.getPath()
498: + "\" append='true' />"
499: + "</target>" + "</project>"
500:
501: );
502: PublicPackagesInProjectizedXMLTest.execute(f,
503: new String[] { "-Dbuildscript=" + f.getPath() });
504:
505: String result = PublicPackagesInProjectizedXMLTest
506: .readFile(xml);
507:
508: if (result.indexOf("org.openide.keep") == -1) {
509: fail("org.openide.keep should be there: " + result);
510: }
511: if (result.indexOf("org.openide.remove") == -1) {
512: fail("org.openide.remove should be there: " + result);
513: }
514:
515: String written = PublicPackagesInProjectizedXMLTest
516: .readFile(out);
517: assertEquals("The remove dependency is not even asked for",
518: "cvcvccvcv", written);
519: }
520:
521: public void testRuntimeDepOnOpenideIsSpecial() throws Exception {
522: java.io.File xml = PublicPackagesInProjectizedXMLTest
523: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
524: + "<project>"
525: + " <module-dependencies>"
526: + " <dependency>"
527: + " <code-name-base>org.openide</code-name-base>"
528: + " <run-dependency>"
529: + " <specification-version>5.1</specification-version> "
530: + " </run-dependency>"
531: + " </dependency>"
532: + " </module-dependencies>" + "</project>");
533:
534: java.io.File out = PublicPackagesInProjectizedXMLTest
535: .extractString("");
536:
537: java.io.File f = PublicPackagesInProjectizedXMLTest
538: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
539: + "<project name=\"Replace Openide\" basedir=\".\" default=\"all\" >"
540: + " <taskdef name=\"fix\" classname=\"org.netbeans.nbbuild.FixDependencies\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>"
541: + "<target name=\"all\" >"
542: + "<fix antfile=\"${buildscript}\" buildtarget=\"verify\" cleantarget=\"clean\" >"
543: + " <replace codenamebase=\"org.openide\" addcompiletime='true' >"
544: + " <module codenamebase=\"org.openide.util\" spec=\"6.2\" />"
545: + " <module codenamebase=\"org.openide.awt\" spec=\"6.2\" />"
546: + " </replace>"
547: + " <fileset dir=\""
548: + xml.getParent()
549: + "\">"
550: + " <include name=\""
551: + xml.getName()
552: + "\" /> "
553: + " </fileset>"
554: + "</fix>"
555: + "</target>"
556: + ""
557: + "<target name=\"verify\" >"
558: + // always succeed
559: "</target>"
560: + "<target name=\"clean\" >"
561: + " <echo message=\"c\" file=\""
562: + out.getPath()
563: + "\" append='true' />"
564: + "</target>" + "</project>"
565:
566: );
567: PublicPackagesInProjectizedXMLTest.execute(f,
568: new String[] { "-Dbuildscript=" + f.getPath() });
569:
570: String result = PublicPackagesInProjectizedXMLTest
571: .readFile(xml);
572:
573: if (result.indexOf("org.openide") > -1) {
574: fail("No org.openide should be there: " + result);
575: }
576: if (result.indexOf("<dependency>") > -1) {
577: fail("No dependency should be there: " + result);
578: }
579: }
580:
581: }
|