KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByteArrayOutputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.FileReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30 import java.util.jar.JarEntry JavaDoc;
31 import java.util.jar.JarOutputStream JavaDoc;
32 import java.util.jar.Manifest JavaDoc;
33 import java.util.zip.ZipEntry JavaDoc;
34 import java.util.zip.ZipOutputStream JavaDoc;
35 import junit.framework.*;
36
37 import org.netbeans.junit.*;
38
39
40 /** Behaviour of fixing module dependencies. Knows how to replace old
41  * with new ones and remove those that are not needed for compilation.
42  *
43  * @author Jaroslav Tulach
44  */

45 public class FixDependenciesTest extends NbTestCase {
46     public FixDependenciesTest (String JavaDoc name) {
47         super (name);
48     }
49     public void testReplaceOpenideDepWithSmallerOnes () throws Exception JavaDoc {
50         java.io.File JavaDoc xml = PublicPackagesInProjectizedXMLTest.extractString (
51             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
52             "<project>" +
53             " <module-dependencies>" +
54             " <dependency>" +
55             " <code-name-base>org.openide</code-name-base>" +
56             " <build-prerequisite/> " +
57             " <compile-dependency/> " +
58             " <run-dependency>" +
59             " <specification-version>3.17</specification-version> " +
60             " </run-dependency>" +
61             " </dependency>" +
62             " </module-dependencies>" +
63             "</project>"
64         );
65         
66         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
67             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
68             "<project name=\"Replace Openide\" basedir=\".\" default=\"all\" >" +
69             " <taskdef name=\"fix\" classname=\"org.netbeans.nbbuild.FixDependencies\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
70             "<target name=\"all\" >" +
71             "<fix>" +
72             " <replace codenamebase=\"org.openide\" >" +
73             " <module codenamebase=\"org.openide.util\" spec=\"6.2\" />" +
74             " <module codenamebase=\"org.openide.awt\" spec=\"6.2\" />" +
75             " </replace>" +
76             " <fileset dir=\"" + xml.getParent () + "\">" +
77             " <include name=\"" + xml.getName () + "\" /> " +
78             " </fileset>" +
79             "</fix>" +
80             "</target>" +
81             "</project>"
82
83         );
84         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { });
85         
86         String JavaDoc result = PublicPackagesInProjectizedXMLTest.readFile (xml);
87         
88         if (result.indexOf ("org.openide.util") == -1) {
89             fail ("org.openide.util should be there: " + result);
90         }
91         if (result.indexOf ("org.openide.awt") == -1) {
92             fail ("org.openide.awt should be there: " + result);
93         }
94         
95         if (result.indexOf ("<specification-version>6.2</specification-version>") == -1) {
96             fail ("Spec version must be updated to 6.2: " + result);
97         }
98     }
99     
100     
101     public void testVerificationOfRemovedDependencies () throws Exception JavaDoc {
102         java.io.File JavaDoc xml = PublicPackagesInProjectizedXMLTest.extractString (
103             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
104             "<project>" +
105             " <module-dependencies>" +
106             " <dependency>" +
107             " <code-name-base>org.openide.keep1</code-name-base>" +
108             " <build-prerequisite/> " +
109             " <compile-dependency/> " +
110             " <run-dependency>" +
111             " <specification-version>6.2</specification-version> " +
112             " </run-dependency>" +
113             " </dependency>" +
114             " <dependency>" +
115             " <code-name-base>org.openide.remove</code-name-base>" +
116             " <build-prerequisite/> " +
117             " <compile-dependency/> " +
118             " <run-dependency>" +
119             " <specification-version>6.2</specification-version> " +
120             " </run-dependency>" +
121             " </dependency>" +
122             " <dependency>" +
123             " <code-name-base>org.openide.keep2</code-name-base>" +
124             " <build-prerequisite/> " +
125             " <compile-dependency/> " +
126             " <run-dependency>" +
127             " <specification-version>6.2</specification-version> " +
128             " </run-dependency>" +
129             " </dependency>" +
130             " </module-dependencies>" +
131             "</project>"
132         );
133         
134         java.io.File JavaDoc out = PublicPackagesInProjectizedXMLTest.extractString ("");
135         
136         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
137             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
138             "<project name=\"Replace Openide\" basedir=\".\" default=\"all\" >" +
139             " <taskdef name=\"fix\" classname=\"org.netbeans.nbbuild.FixDependencies\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
140             "<target name=\"all\" >" +
141             "<fix antfile=\"${buildscript}\" buildtarget=\"verify\" cleantarget=\"clean\" >" +
142             " <replace codenamebase=\"org.openide\" >" +
143             " <module codenamebase=\"org.openide.util\" spec=\"6.2\" />" +
144             " <module codenamebase=\"org.openide.awt\" spec=\"6.2\" />" +
145             " </replace>" +
146             " <fileset dir=\"" + xml.getParent () + "\">" +
147             " <include name=\"" + xml.getName () + "\" /> " +
148             " </fileset>" +
149             "</fix>" +
150             "</target>" +
151             "" +
152             "<target name=\"verify\" >" +
153             " <echo message=\"v\" file=\"" + out.getPath () + "\" append='true' />" +
154             " <loadfile property=\"p\" srcFile=\"" + xml.getPath () + "\" />" +
155             " <condition property=\"remove\" >" +
156             " <and>" +
157             " <not>" +
158             " <and>" +
159             " <contains string=\"${p}\" substring=\"org.openide.keep1\" />" +
160             " <contains string=\"${p}\" substring=\"org.openide.keep2\" />" +
161             " </and>" +
162             " </not>" +
163             " <contains string=\"${p}\" substring=\"org.openide.remove\" />" +
164             " </and>" +
165             " </condition>" +
166             // fail if there is org.openide.remove and at least one
167
// of org.openide.keep is missing
168
" <fail if=\"remove\" /> " +
169             "</target>" +
170             "<target name=\"clean\" >" +
171             " <echo message=\"c\" file=\"" + out.getPath () + "\" append='true' />" +
172             "</target>" +
173             "</project>"
174
175         );
176         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { "-Dbuildscript=" + f.getPath () });
177         
178         String JavaDoc result = PublicPackagesInProjectizedXMLTest.readFile (xml);
179         
180         if (result.indexOf ("org.openide.keep") == -1) {
181             fail ("org.openide.keep should be there: " + result);
182         }
183         if (result.indexOf ("org.openide.remove") != -1) {
184             fail ("org.openide.remove should not be there: " + result);
185         }
186
187         String JavaDoc written = PublicPackagesInProjectizedXMLTest.readFile (out);
188         assertEquals ("First we do clean, test verify, then clean and verify three times as there are three <dependency> tags"
189                 , "cvcvcvcvcv", written);
190     }
191
192     public void testBrokenCoreSettingsReplacement () throws Exception JavaDoc {
193         
194         String JavaDoc projectXML =
195 "<?xml version='1.0' encoding='UTF-8'?>\n" +
196 "<!--\n" +
197 " CDDL Notice\n" +
198 "\n" +
199 "The Original Code is NetBeans. The Initial Developer of the Original\n" +
200 "Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun\n" +
201 "Microsystems, Inc. All Rights Reserved.\n" +
202 "-->\n" +
203 "<project xmlns='http://www.netbeans.org/ns/project/1'>\n" +
204     "<type>org.netbeans.modules.apisupport.project</type>\n" +
205     "<configuration>\n" +
206         "<data xmlns='http://www.netbeans.org/ns/nb-module-project/2'>\n" +
207             "<code-name-base>org.netbeans.modules.settings</code-name-base>\n" +
208             "<module-dependencies>\n" +
209                 "<dependency>\n" +
210                     "<code-name-base>org.openide</code-name-base>\n" +
211                     "<build-prerequisite/>\n" +
212                     "<compile-dependency/>\n" +
213                     "<run-dependency>\n" +
214                         "<release-version>1</release-version>\n" +
215                         "<specification-version>3.17</specification-version>\n" +
216                     "</run-dependency>\n" +
217                 "</dependency>\n" +
218                 "<dependency>\n" +
219                     "<code-name-base>org.openide.loaders</code-name-base>\n" +
220                     "<build-prerequisite/>\n" +
221                     "<compile-dependency/>\n" +
222                     "<run-dependency/>\n" +
223                 "</dependency>\n" +
224             "</module-dependencies>\n" +
225             "<public-packages>\n" +
226                 "<package>org.netbeans.spi.settings</package>\n" +
227             "</public-packages>\n" +
228             "<javadoc/>\n" +
229         "</data>\n" +
230     "</configuration>\n" +
231 "</project>\n";
232         
233         
234         
235         java.io.File JavaDoc xml = PublicPackagesInProjectizedXMLTest.extractString (projectXML);
236         
237         
238         java.io.File JavaDoc out = PublicPackagesInProjectizedXMLTest.extractString ("");
239         
240         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
241             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
242             "<project name=\"Replace Openide\" basedir=\".\" default=\"all\" >" +
243             " <taskdef name=\"fix\" classname=\"org.netbeans.nbbuild.FixDependencies\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
244             "<target name=\"all\" >" +
245             "<fix >" +
246             " <replace codenamebase='org.openide'>\n" +
247             " <module codenamebase='org.openide.filesystems' spec='6.2'/>\n" +
248             " <module codenamebase='org.openide.util' spec='6.2'/>\n" +
249             " <module codenamebase='org.openide.util.enumerations' spec='6.2'/>\n" +
250             " <module codenamebase='org.openide.modules' spec='6.2'/>\n" +
251             " <module codenamebase='org.openide.nodes' spec='6.2'/>\n" +
252             " <module codenamebase='org.openide.explorer' spec='6.2'/>\n" +
253             " <module codenamebase='org.openide.awt' spec='6.2'/>\n" +
254             " <module codenamebase='org.openide.dialogs' spec='6.2'/>\n" +
255             " <module codenamebase='org.openide.compat' spec='6.2'/>\n" +
256             " <module codenamebase='org.openide.options' spec='6.2'/>\n" +
257             " <module codenamebase='org.openide.windows' spec='6.2'/>\n" +
258             " <module codenamebase='org.openide.text' spec='6.2'/>\n" +
259             " <module codenamebase='org.openide.actions' spec='6.2'/>\n" +
260             " <module codenamebase='org.openide.loaders' spec='6.2'/>\n" +
261             " </replace>\n" +
262             " <fileset dir=\"" + xml.getParent () + "\">" +
263             " <include name=\"" + xml.getName () + "\" /> " +
264             " </fileset>" +
265             "</fix>" +
266             "</target>" +
267             "" +
268             "<target name=\"verify\" >" +
269             " <echo message=\"v\" file=\"" + out.getPath () + "\" append='true' />" +
270             " <loadfile property=\"p\" srcFile=\"" + xml.getPath () + "\" />" +
271             "</target>" +
272             "<target name=\"clean\" >" +
273             " <echo message=\"c\" file=\"" + out.getPath () + "\" append='true' />" +
274             "</target>" +
275             "</project>"
276
277         );
278         org.w3c.dom.Document JavaDoc doc;
279         doc = javax.xml.parsers.DocumentBuilderFactory.newInstance ().newDocumentBuilder ().parse (xml);
280         assertNotNull ("Originally can be parsed", doc);
281         
282         
283         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { "-Dbuildscript=" + f.getPath () });
284         
285         doc = javax.xml.parsers.DocumentBuilderFactory.newInstance ().newDocumentBuilder ().parse (xml);
286         
287         assertNotNull ("Still can be parsed", doc);
288         
289         String JavaDoc r = PublicPackagesInProjectizedXMLTest.readFile (xml);
290         assertEquals ("No release version used as modules do not have it", -1, r.indexOf ("release-version"));
291         
292         
293         int idx = r.indexOf ("<code-name-base>org.openide.loaders</code-name-base>");
294         if (idx == -1) {
295             fail ("One dep on loaders should be there: " + r);
296         }
297         
298         assertEquals ("No next loader dep", -1, r.indexOf ("<code-name-base>org.openide.loaders</code-name-base>", idx + 10));
299     }
300
301     public void testPropertiesAreNotInfluencedByPreviousExecution () throws Exception JavaDoc {
302         java.io.File JavaDoc xml = PublicPackagesInProjectizedXMLTest.extractString (
303             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
304             "<project>" +
305             " <module-dependencies>" +
306             " <dependency>" +
307             " <code-name-base>org.openide.keep1</code-name-base>" +
308             " <build-prerequisite/> " +
309             " <compile-dependency/> " +
310             " <run-dependency>" +
311             " <specification-version>6.2</specification-version> " +
312             " </run-dependency>" +
313             " </dependency>" +
314             " <dependency>" +
315             " <code-name-base>org.openide.remove</code-name-base>" +
316             " <build-prerequisite/> " +
317             " <compile-dependency/> " +
318             " <run-dependency>" +
319             " <specification-version>6.2</specification-version> " +
320             " </run-dependency>" +
321             " </dependency>" +
322             " <dependency>" +
323             " <code-name-base>org.openide.keep2</code-name-base>" +
324             " <build-prerequisite/> " +
325             " <compile-dependency/> " +
326             " <run-dependency>" +
327             " <specification-version>6.2</specification-version> " +
328             " </run-dependency>" +
329             " </dependency>" +
330             " </module-dependencies>" +
331             "</project>"
332         );
333         
334         java.io.File JavaDoc out = PublicPackagesInProjectizedXMLTest.extractString ("");
335         
336         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
337             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
338             "<project name=\"Separate namespaces\" basedir=\".\" default=\"all\" >" +
339             " <taskdef name=\"fix\" classname=\"org.netbeans.nbbuild.FixDependencies\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
340             "<target name=\"all\" >" +
341             "<fix antfile=\"${buildscript}\" buildtarget=\"verify\" cleantarget='verify'>" +
342             " <fileset dir=\"" + xml.getParent () + "\">" +
343             " <include name=\"" + xml.getName () + "\" /> " +
344             " </fileset>" +
345             "</fix>" +
346             "</target>" +
347             "" +
348             "<target name=\"verify\" >" +
349             " <fail if=\"remove\" /> " +
350             " <property name='remove' value='some' />" +
351             " <fail unless=\"remove\" /> " +
352             " <echo message=\"v\" file=\"" + out.getPath () + "\" append='true' />" +
353             "</target>" +
354             "</project>"
355
356         );
357         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { "-Dbuildscript=" + f.getPath () });
358         
359         String JavaDoc result = PublicPackagesInProjectizedXMLTest.readFile (xml);
360
361         String JavaDoc written = PublicPackagesInProjectizedXMLTest.readFile (out);
362         assertEquals ("The property remove is never set", "vvvvvvvvvv", written);
363     }
364
365     
366   public void testOnlyCompileTimeDependenciesCanBeRemoved () throws Exception JavaDoc {
367         java.io.File JavaDoc xml = PublicPackagesInProjectizedXMLTest.extractString (
368             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
369             "<project>" +
370             " <module-dependencies>" +
371             " <dependency>" +
372             " <code-name-base>org.openide.keep1</code-name-base>" +
373             " <build-prerequisite/> " +
374             " <compile-dependency/> " +
375             " <run-dependency>" +
376             " <specification-version>6.2</specification-version> " +
377             " </run-dependency>" +
378             " </dependency>" +
379             " <dependency>" +
380             " <code-name-base>org.openide.remove</code-name-base>" +
381             " <build-prerequisite/> " +
382 // This changes the meaning: " <compile-dependency/> " +
383
" <run-dependency>" +
384             " <specification-version>6.2</specification-version> " +
385             " </run-dependency>" +
386             " </dependency>" +
387             " <dependency>" +
388             " <code-name-base>org.openide.keep2</code-name-base>" +
389             " <build-prerequisite/> " +
390             " <compile-dependency/> " +
391             " <run-dependency>" +
392             " <specification-version>6.2</specification-version> " +
393             " </run-dependency>" +
394             " </dependency>" +
395             " </module-dependencies>" +
396             "</project>"
397         );
398         
399         java.io.File JavaDoc out = PublicPackagesInProjectizedXMLTest.extractString ("");
400         
401         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
402             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
403             "<project name=\"Replace Openide\" basedir=\".\" default=\"all\" >" +
404             " <taskdef name=\"fix\" classname=\"org.netbeans.nbbuild.FixDependencies\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
405             "<target name=\"all\" >" +
406             "<fix antfile=\"${buildscript}\" buildtarget=\"verify\" cleantarget=\"clean\" >" +
407             " <replace codenamebase=\"org.openide\" >" +
408             " <module codenamebase=\"org.openide.util\" spec=\"6.2\" />" +
409             " <module codenamebase=\"org.openide.awt\" spec=\"6.2\" />" +
410             " </replace>" +
411             " <fileset dir=\"" + xml.getParent () + "\">" +
412             " <include name=\"" + xml.getName () + "\" /> " +
413             " </fileset>" +
414             "</fix>" +
415             "</target>" +
416             "" +
417             "<target name=\"verify\" >" +
418             " <echo message=\"v\" file=\"" + out.getPath () + "\" append='true' />" +
419             " <loadfile property=\"p\" srcFile=\"" + xml.getPath () + "\" />" +
420             " <condition property=\"remove\" >" +
421             " <and>" +
422             " <not>" +
423             " <and>" +
424             " <contains string=\"${p}\" substring=\"org.openide.keep1\" />" +
425             " <contains string=\"${p}\" substring=\"org.openide.keep2\" />" +
426             " </and>" +
427             " </not>" +
428             " <contains string=\"${p}\" substring=\"org.openide.remove\" />" +
429             " </and>" +
430             " </condition>" +
431             // fail if there is org.openide.remove and at least one
432
// of org.openide.keep is missing
433
" <fail if=\"remove\" /> " +
434             "</target>" +
435             "<target name=\"clean\" >" +
436             " <echo message=\"c\" file=\"" + out.getPath () + "\" append='true' />" +
437             "</target>" +
438             "</project>"
439
440         );
441         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { "-Dbuildscript=" + f.getPath () });
442         
443         String JavaDoc result = PublicPackagesInProjectizedXMLTest.readFile (xml);
444         
445         if (result.indexOf ("org.openide.keep") == -1) {
446             fail ("org.openide.keep should be there: " + result);
447         }
448         if (result.indexOf ("org.openide.remove") == -1) {
449             fail ("org.openide.remove should be there: " + result);
450         }
451
452         String JavaDoc written = PublicPackagesInProjectizedXMLTest.readFile (out);
453         assertEquals ("The remove dependency is not even asked for"
454                 , "cvcvccvcv", written);
455     }
456     
457   public void testRuntimeDepOnOpenideIsSpecial () throws Exception JavaDoc {
458         java.io.File JavaDoc xml = PublicPackagesInProjectizedXMLTest.extractString (
459             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
460             "<project>" +
461             " <module-dependencies>" +
462             " <dependency>" +
463             " <code-name-base>org.openide</code-name-base>" +
464             " <run-dependency>" +
465             " <specification-version>5.1</specification-version> " +
466             " </run-dependency>" +
467             " </dependency>" +
468             " </module-dependencies>" +
469             "</project>"
470         );
471         
472         java.io.File JavaDoc out = PublicPackagesInProjectizedXMLTest.extractString ("");
473         
474         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
475             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
476             "<project name=\"Replace Openide\" basedir=\".\" default=\"all\" >" +
477             " <taskdef name=\"fix\" classname=\"org.netbeans.nbbuild.FixDependencies\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
478             "<target name=\"all\" >" +
479             "<fix antfile=\"${buildscript}\" buildtarget=\"verify\" cleantarget=\"clean\" >" +
480             " <replace codenamebase=\"org.openide\" addcompiletime='true' >" +
481             " <module codenamebase=\"org.openide.util\" spec=\"6.2\" />" +
482             " <module codenamebase=\"org.openide.awt\" spec=\"6.2\" />" +
483             " </replace>" +
484             " <fileset dir=\"" + xml.getParent () + "\">" +
485             " <include name=\"" + xml.getName () + "\" /> " +
486             " </fileset>" +
487             "</fix>" +
488             "</target>" +
489             "" +
490             "<target name=\"verify\" >" + // always succeed
491
"</target>" +
492             "<target name=\"clean\" >" +
493             " <echo message=\"c\" file=\"" + out.getPath () + "\" append='true' />" +
494             "</target>" +
495             "</project>"
496
497         );
498         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { "-Dbuildscript=" + f.getPath () });
499         
500         String JavaDoc result = PublicPackagesInProjectizedXMLTest.readFile (xml);
501         
502         if (result.indexOf ("org.openide") > -1) {
503             fail ("No org.openide should be there: " + result);
504         }
505         if (result.indexOf ("<dependency>") > -1) {
506             fail ("No dependency should be there: " + result);
507         }
508     }
509
510 }
511
Popular Tags