KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileOutputStream JavaDoc;
24 import java.io.FileWriter JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.Random JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.SortedSet JavaDoc;
32 import java.util.TreeSet JavaDoc;
33 import java.util.jar.JarEntry JavaDoc;
34 import java.util.jar.JarFile JavaDoc;
35 import java.util.jar.JarOutputStream JavaDoc;
36 import java.util.jar.Manifest JavaDoc;
37 import java.util.logging.Level JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39 import java.util.regex.Matcher JavaDoc;
40 import java.util.regex.Pattern JavaDoc;
41 import org.netbeans.junit.NbTestCase;
42
43 /** Is generation of Jnlp files correct?
44  *
45  * @author Jaroslav Tulach, Jesse Glick
46  */

47 public class MakeJNLPTest extends NbTestCase {
48     public MakeJNLPTest (String JavaDoc name) {
49         super (name);
50     }
51     
52     protected void setUp() throws Exception JavaDoc {
53         clearWorkDir();
54     }
55
56     private static void assertFilenames(File JavaDoc dir, String JavaDoc... contents) {
57         assertTrue(dir + " is a directory", dir.isDirectory());
58         SortedSet JavaDoc<String JavaDoc> expected = new TreeSet JavaDoc<String JavaDoc>(Arrays.asList(contents));
59         SortedSet JavaDoc<String JavaDoc> actual = new TreeSet JavaDoc<String JavaDoc>();
60         findFilenames(dir, "", actual);
61         assertEquals("correct contents of " + dir, expected/*.toString()*/, actual/*.toString()*/);
62     }
63     private static void findFilenames(File JavaDoc dir, String JavaDoc prefix, Set JavaDoc<String JavaDoc> names) {
64         for (File JavaDoc f : dir.listFiles()) {
65             if (f.isFile()) {
66                 names.add(prefix + f.getName());
67             } else if (f.isDirectory()) {
68                 findFilenames(f, prefix + f.getName() + "/", names);
69             }
70         }
71     }
72     
73     public void testGenerateJNLPAndSignedJarForSimpleModule() throws Exception JavaDoc {
74         Manifest JavaDoc m;
75         
76         m = ModuleDependenciesTest.createManifest ();
77         m.getMainAttributes ().putValue ("OpenIDE-Module", "org.my.module/3");
78         File JavaDoc simpleJar = generateJar (new String JavaDoc[0], m);
79
80         File JavaDoc parent = simpleJar.getParentFile ();
81         File JavaDoc output = new File JavaDoc(parent, "output");
82         File JavaDoc ks = generateKeystore("jnlp", "netbeans-test");
83         
84         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
85             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
86             "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
87             " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
88             "<target name=\"all\" >" +
89             " <mkdir dir='" + output + "' />" +
90             " <jnlp dir='" + output + "' alias='jnlp' storepass='netbeans-test' keystore='" + ks + "' >" +
91             " <modules dir='" + parent + "' >" +
92             " <include name='" + simpleJar.getName() + "' />" +
93             " </modules>" +
94             " </jnlp>" +
95             "</target>" +
96             "</project>"
97         );
98         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { "-verbose" });
99         
100         assertFilenames(output, "org-my-module.jnlp", "org-my-module/s0.jar");
101         
102         File JavaDoc jnlp = new File JavaDoc(output, "org-my-module.jnlp");
103         String JavaDoc res = ModuleDependenciesTest.readFile (jnlp);
104         
105         assertTrue ("Component JNLP type: " + res, res.indexOf ("<component-desc/>") >= 0);
106         assertTrue ("We support all permitions by default: " + res, res.indexOf ("<all-permissions/>") >= 0);
107         
108         Matcher JavaDoc match = Pattern.compile(".*codebase=['\\\"]([^'\\\"]*)['\\\"]").matcher(res);
109         assertTrue("codebase is there", match.find());
110         assertEquals("one group found", 1, match.groupCount());
111         String JavaDoc base = match.group(1);
112         
113         assertEquals("By default the dest directory is $$codebase: ", "$$codebase", base);
114
115         CHECK_SIGNED: {
116             File JavaDoc jar = new File JavaDoc(output, "org-my-module/s0.jar");
117             JarFile JavaDoc signed = new JarFile JavaDoc(jar);
118             Enumeration JavaDoc it = signed.entries();
119             while (it.hasMoreElements()) {
120                 JarEntry JavaDoc entry = (JarEntry JavaDoc)it.nextElement();
121                 if (entry.getName().endsWith(".SF")) {
122                     break CHECK_SIGNED;
123                 }
124             }
125             fail ("File does not seem to be signed: " + jar);
126         }
127     }
128     
129     public void testTheLocalizedAutoupdateProblem() throws Exception JavaDoc {
130         String JavaDoc UTfile =
131             "<?xml version='1.0' encoding='UTF-8'?>" +
132             "<module codename='org.netbeans.modules.autoupdate/1'>" +
133             " <module_version install_time='1136503038669' last='true' origin='installer' specification_version='2.16.1'>" +
134             " <file crc='746562502' name='config/Modules/org-netbeans-modules-autoupdate.xml'/>" +
135             " <file crc='3552349255' name='modules/ext/locale/updater_ja.jar'/>" +
136             " <file crc='72601456' name='modules/ext/locale/updater_zh_CN.jar'/>" +
137             " <file crc='3405032071' name='modules/ext/updater.jar'/>" +
138             " <file crc='2409221434' name='modules/locale/org-netbeans-modules-autoupdate_ja.jar'/>" +
139             " <file crc='1180043929' name='modules/locale/org-netbeans-modules-autoupdate_zh_CN.jar'/>" +
140             " <file crc='3477298901' name='modules/org-netbeans-modules-autoupdate.jar'/>" +
141             " </module_version>" +
142             "</module>";
143       
144         Manifest JavaDoc m = ModuleDependenciesTest.createManifest ();
145         m.getMainAttributes ().putValue ("OpenIDE-Module", "org.netbeans.modules.autoupdate/1");
146         m.getMainAttributes ().putValue ("Class-Path", "ext/updater.jar");
147         File JavaDoc simpleJar = generateJar ("modules/", new String JavaDoc[0], m, null);
148         File JavaDoc moduleJar = new File JavaDoc(simpleJar.getParentFile(), "org-netbeans-modules-autoupdate.jar");
149         simpleJar.renameTo(moduleJar);
150         
151         File JavaDoc p = simpleJar.getParentFile();
152         
153         simpleJar = generateJar ("modules/locale/", new String JavaDoc[0], ModuleDependenciesTest.createManifest(), null);
154         simpleJar.renameTo(new File JavaDoc(simpleJar.getParentFile(), "org-netbeans-modules-autoupdate_ja.jar"));
155
156         simpleJar = generateJar ("modules/locale/", new String JavaDoc[0], ModuleDependenciesTest.createManifest(), null);
157         simpleJar.renameTo(new File JavaDoc(simpleJar.getParentFile(), "org-netbeans-modules-autoupdate_zh_CN.jar"));
158         
159         simpleJar = generateJar ("modules/ext/", new String JavaDoc[0], ModuleDependenciesTest.createManifest(), null);
160         simpleJar.renameTo(new File JavaDoc(simpleJar.getParentFile(), "updater.jar"));
161
162         simpleJar = generateJar ("modules/ext/locale/", new String JavaDoc[0], ModuleDependenciesTest.createManifest(), null);
163         simpleJar.renameTo(new File JavaDoc(simpleJar.getParentFile(), "updater_ja.jar"));
164
165         simpleJar = generateJar ("modules/ext/locale/", new String JavaDoc[0], ModuleDependenciesTest.createManifest(), null);
166         simpleJar.renameTo(new File JavaDoc(simpleJar.getParentFile(), "updater_zh_CN.jar"));
167
168         File JavaDoc xml = new File JavaDoc(p, "config/Modules/org-netbeans-modules-autoupdate.xml");
169         xml.getParentFile().mkdirs();
170         xml.createNewFile();
171         
172         File JavaDoc updateTracking = new File JavaDoc(getWorkDir(), "update_tracking");
173         updateTracking.mkdirs();
174         assertTrue("Created", updateTracking.isDirectory());
175         
176         File JavaDoc trackingFile = new File JavaDoc(updateTracking, "org-netbeans-modules-autoupdate.xml");
177         FileWriter JavaDoc w = new FileWriter JavaDoc(trackingFile);
178         w.write(UTfile);
179         w.close();
180
181         File JavaDoc output = new File JavaDoc(getWorkDir(), "output");
182         File JavaDoc ks = generateKeystore("jnlp", "netbeans-test");
183         
184         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
185             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
186             "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
187             " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
188             "<target name=\"all\" >" +
189             " <mkdir dir='" + output + "' />" +
190             " <jnlp dir='" + output + "' alias='jnlp' storepass='netbeans-test' keystore='" + ks + "' verify='true' >" +
191             " <modules dir='" + p + "' >" +
192             " <include name='" + moduleJar.getName() + "' />" +
193             " </modules>" +
194             " </jnlp>" +
195             "</target>" +
196             "</project>"
197         );
198         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { "-verbose" });
199         
200         assertFilenames(output, "org-netbeans-modules-autoupdate.jnlp",
201                 "org-netbeans-modules-autoupdate/org-netbeans-modules-autoupdate.jar",
202                 "org-netbeans-modules-autoupdate/locale-org-netbeans-modules-autoupdate_zh_CN.jar",
203                 "org-netbeans-modules-autoupdate/locale-org-netbeans-modules-autoupdate_ja.jar",
204                 "org-netbeans-modules-autoupdate/ext-locale-updater_zh_CN.jar",
205                 "org-netbeans-modules-autoupdate/ext-locale-updater_ja.jar",
206                 "org-netbeans-modules-autoupdate/ext-updater.jar");
207         
208         File JavaDoc jnlp = new File JavaDoc(output, "org-netbeans-modules-autoupdate.jnlp");
209         String JavaDoc res = ModuleDependenciesTest.readFile (jnlp);
210         
211         
212         assertTrue ("Component JNLP type: " + res, res.indexOf ("<component-desc/>") >= 0);
213         assertTrue ("We support all permitions by default: " + res, res.indexOf ("<all-permissions/>") >= 0);
214         
215         Matcher JavaDoc match = Pattern.compile(".*codebase=['\\\"]([^'\\\"]*)['\\\"]").matcher(res);
216         assertTrue("codebase is there", match.find());
217         assertEquals("one group found", 1, match.groupCount());
218         String JavaDoc base = match.group(1);
219         
220         assertEquals("By default the dest directory is $$codebase: ", "$$codebase", base);
221         
222         assertResource(res, "ja", "org-netbeans-modules-autoupdate/ext-locale-updater_ja.jar");
223         assertResource(res, "zh_CN", "org-netbeans-modules-autoupdate/ext-locale-updater_zh_CN.jar");
224         assertResource(res, "ja", "org-netbeans-modules-autoupdate/locale-org-netbeans-modules-autoupdate_ja.jar");
225         assertResource(res, "zh_CN", "org-netbeans-modules-autoupdate/locale-org-netbeans-modules-autoupdate_zh_CN.jar");
226
227         CHECK_SIGNED: for (File JavaDoc jar : new File JavaDoc(output, "org-netbeans-modules-autoupdate").listFiles()) {
228             if (!jar.getName().endsWith(".jar")) {
229                 continue;
230             }
231             JarFile JavaDoc signed = new JarFile JavaDoc(jar);
232             Enumeration JavaDoc it = signed.entries();
233             while (it.hasMoreElements()) {
234                 JarEntry JavaDoc entry = (JarEntry JavaDoc)it.nextElement();
235                 if (entry.getName().endsWith(".SF")) {
236                     continue CHECK_SIGNED;
237                 }
238             }
239             fail ("File does not seem to be signed: " + jar);
240         }
241         
242     }
243     
244     public void testGenerateJNLPAndSignedJarForSimpleLocalizedModule() throws Exception JavaDoc {
245         Manifest JavaDoc m;
246         
247         m = ModuleDependenciesTest.createManifest ();
248         m.getMainAttributes ().putValue ("OpenIDE-Module", "org.my.module/3");
249         File JavaDoc simpleJar = generateJar ("modules/", new String JavaDoc[0], m, null);
250
251         File JavaDoc parent = simpleJar.getParentFile ();
252         File JavaDoc localizedJarCZ = generateJar("modules/locale/", new String JavaDoc[0], ModuleDependenciesTest.createManifest(), null);
253         assertEquals("There need to have the same name", simpleJar.getName(), localizedJarCZ.getName());
254         assertTrue("Successful rename", localizedJarCZ.renameTo(new File JavaDoc(localizedJarCZ.getParent(), "0_cs.jar")));
255         
256         File JavaDoc localizedJarZH = generateJar("modules/locale/", new String JavaDoc[0], ModuleDependenciesTest.createManifest(), null);
257         assertEquals("There need to have the same name", simpleJar.getName(), localizedJarZH.getName());
258         assertTrue("Successful rename", localizedJarZH.renameTo(new File JavaDoc(localizedJarCZ.getParent(), "0_zh_CN.jar")));
259         
260         File JavaDoc localizedJarJA = generateJar("modules/locale/", new String JavaDoc[0], ModuleDependenciesTest.createManifest(), null);
261         assertEquals("There need to have the same name", simpleJar.getName(), localizedJarJA.getName());
262         assertTrue("Successful rename", localizedJarJA.renameTo(new File JavaDoc(localizedJarCZ.getParent(), "0_ja.jar")));
263
264         File JavaDoc updateTracking = new File JavaDoc(getWorkDir(), "update_tracking");
265         updateTracking.mkdirs();
266         assertTrue("Created", updateTracking.isDirectory());
267         
268         File JavaDoc trackingFile = new File JavaDoc(updateTracking, "org-my-module.xml");
269         FileWriter JavaDoc w = new FileWriter JavaDoc(trackingFile);
270         w.write(
271 "<?xml version='1.0' encoding='UTF-8'?>\n" +
272 "<module codename='org.my.module/3'>\n" +
273     "<module_version specification_version='3.22' origin='installer' last='true' install_time='1124194231878'>\n" +
274         "<file name='modules/" + simpleJar.getName() + "' crc='3245456472'/>\n" +
275         "<file name='config/Modules/org-my-module.xml' crc='43434' />\n" +
276         "<file name='modules/locale/0_cs.jar' crc='454244' />\n" +
277         "<file name='modules/locale/0_ja.jar' crc='779831' />\n" +
278         "<file name='modules/locale/0_zh_CN.jar' crc='475345' />\n" +
279 " </module_version>\n" +
280 "</module>\n"
281         );
282         w.close();
283         
284         
285         File JavaDoc output = new File JavaDoc(parent, "output");
286         File JavaDoc ks = generateKeystore("jnlp", "netbeans-test");
287         
288         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
289             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
290             "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
291             " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
292             "<target name=\"all\" >" +
293             " <mkdir dir='" + output + "' />" +
294             " <jnlp dir='" + output + "' alias='jnlp' storepass='netbeans-test' keystore='" + ks + "' verify='true' >" +
295             " <modules dir='" + parent + "' >" +
296             " <include name='" + simpleJar.getName() + "' />" +
297             " </modules>" +
298             " </jnlp>" +
299             "</target>" +
300             "</project>"
301         );
302         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { "-verbose" });
303
304         assertFilenames(output, "org-my-module.jnlp",
305                 "org-my-module/0.jar",
306                 "org-my-module/locale-0_cs.jar",
307                 "org-my-module/locale-0_zh_CN.jar",
308                 "org-my-module/locale-0_ja.jar");
309
310         File JavaDoc jnlp = new File JavaDoc(output, "org-my-module.jnlp");
311         String JavaDoc res = ModuleDependenciesTest.readFile (jnlp);
312         
313         assertTrue ("Component JNLP type: " + res, res.indexOf ("<component-desc/>") >= 0);
314         assertTrue ("We support all permissions by default: " + res, res.indexOf ("<all-permissions/>") >= 0);
315         
316         Matcher JavaDoc match = Pattern.compile(".*codebase=['\\\"]([^'\\\"]*)['\\\"]").matcher(res);
317         assertTrue("codebase is there", match.find());
318         assertEquals("one group found", 1, match.groupCount());
319         String JavaDoc base = match.group(1);
320         
321         assertEquals("By default the dest directory is $$codebase: ", "$$codebase", base);
322         
323         assertResource(res, "cs", "org-my-module/locale-0_cs.jar");
324         assertResource(res, "ja", "org-my-module/locale-0_ja.jar");
325         assertResource(res, "zh_CN", "org-my-module/locale-0_zh_CN.jar");
326
327         CHECK_SIGNED: for (File JavaDoc jar : new File JavaDoc(output, "org-my-module").listFiles()) {
328             if (!jar.getName().endsWith(".jar")) {
329                 continue;
330             }
331             
332             JarFile JavaDoc signed = new JarFile JavaDoc(jar);
333             Enumeration JavaDoc it = signed.entries();
334             while (it.hasMoreElements()) {
335                 JarEntry JavaDoc entry = (JarEntry JavaDoc)it.nextElement();
336                 if (entry.getName().endsWith(".SF")) {
337                     continue CHECK_SIGNED;
338                 }
339             }
340             fail ("File does not seem to be signed: " + jar);
341         }
342     }
343     
344     private static void assertResource(String JavaDoc where, String JavaDoc locale, String JavaDoc file) {
345         where = where.replace('\n', ' ');
346         Matcher JavaDoc match = Pattern.compile("<resources *locale='" + locale + "' *>.*<jar HREF='" + file + "' */>.*</resources>").matcher(where);
347         assertTrue("File really referenced " + file + " in locale " + locale + "\n" + where, match.find());
348     }
349     
350     public void testOneCanChangeTheCodeBase() throws Exception JavaDoc {
351         Manifest JavaDoc m;
352         
353         m = ModuleDependenciesTest.createManifest ();
354         m.getMainAttributes ().putValue ("OpenIDE-Module", "org.my.module/3");
355         File JavaDoc simpleJar = generateJar (new String JavaDoc[0], m);
356
357         File JavaDoc parent = simpleJar.getParentFile ();
358         File JavaDoc output = new File JavaDoc(parent, "output");
359         File JavaDoc ks = generateKeystore("jnlp", "netbeans-test");
360         
361         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
362             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
363             "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
364             " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
365             "<target name=\"all\" >" +
366             " <mkdir dir='" + output + "' />" +
367             " <jnlp dir='" + output + "' alias='jnlp' storepass='netbeans-test' keystore='" + ks + "' codebase='http://www.my.org/' >" +
368             " <modules dir='" + parent + "' >" +
369             " <include name='" + simpleJar.getName() + "' />" +
370             " </modules>" +
371             " </jnlp>" +
372             "</target>" +
373             "</project>"
374         );
375         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { "-verbose" });
376         
377         assertFilenames(output, "org-my-module.jnlp",
378                 "org-my-module/s0.jar");
379         
380         File JavaDoc jnlp = new File JavaDoc(output, "org-my-module.jnlp");
381         String JavaDoc res = ModuleDependenciesTest.readFile (jnlp);
382         
383         assertTrue ("Component JNLP type: " + res, res.indexOf ("<component-desc/>") >= 0);
384         assertTrue ("We support all permitions by default: " + res, res.indexOf ("<all-permissions/>") >= 0);
385         
386         Matcher JavaDoc match = Pattern.compile(".*codebase=['\\\"]([^'\\\"]*)['\\\"]").matcher(res);
387         assertTrue("codebase is there", match.find());
388         assertEquals("one group found", 1, match.groupCount());
389         String JavaDoc base = match.group(1);
390         
391         assertEquals("By default the codebases can be changed: ", "http://www.my.org/", base);
392     }
393
394     public void testGenerateJNLPAndSignedJarForModuleWithClassPath() throws Exception JavaDoc {
395         File JavaDoc output = doClassPathModuleCheck(
396             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
397             "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
398             " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
399             "<target name=\"all\" >" +
400             " <mkdir dir='${test.output}' />" +
401             " <jnlp dir='${test.output}' alias='jnlp' storepass='netbeans-test' keystore='${test.ks}' >" +
402             " <modules dir='${test.parent}' >" +
403             " <include name='${test.name}' />" +
404             " </modules>" +
405             " </jnlp>" +
406             "</target>" +
407             "</project>"
408         );
409         
410         assertFilenames(output, "aaa-my-module.jnlp", "aaa-my-module/ext-t0.jar", "aaa-my-module/s0.jar");
411         
412         File JavaDoc jnlp = new File JavaDoc(output, "aaa-my-module.jnlp");
413         String JavaDoc res = ModuleDependenciesTest.readFile (jnlp);
414
415         
416         Matcher JavaDoc m = Pattern.compile("<jar HREF='(.*)' */>").matcher(res);
417         for (int x = 0; x < 2; x++) {
418             assertTrue("Matches at least one" + "(" + x + ")", m.find());
419             assertEquals("Found a group" + "(" + x + ")", m.groupCount(), 1);
420             File JavaDoc f = new File JavaDoc (jnlp.getParentFile(), m.group(1));
421             assertTrue("The file " + f + " exists" + "(" + x + ")", f.exists());
422         }
423         
424     }
425
426     public void testGenerateJNLPAndSignedJarForModuleWithClassPathAndSignedJar() throws Exception JavaDoc {
427         File JavaDoc ks = generateKeystore("external", "netbeans-test");
428         
429         File JavaDoc output = doClassPathModuleCheck(
430             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
431             "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
432             " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
433             "<target name=\"all\" >" +
434             " <mkdir dir='${test.output}' />" +
435             " <signjar jar='${test.ext}' alias='external' storepass='netbeans-test' keystore='${test.ks}' />\n" +
436             " <jnlp dir='${test.output}' alias='jnlp' storepass='netbeans-test' keystore='${test.ks}' >" +
437             " <modules dir='${test.parent}' >" +
438             " <include name='${test.name}' />" +
439             " </modules>" +
440             " </jnlp>" +
441             "</target>" +
442             "</project>"
443         );
444         
445         assertFilenames(output, "aaa-my-module.jnlp", "aaa-my-module/s0.jar",
446                 "aaa-my-module/ext-t0.jar",
447                 "aaa-my-module/ext-t0.jnlp");
448         
449         JarFile JavaDoc f = new JarFile JavaDoc(new File JavaDoc(output, "aaa-my-module/ext-t0.jar"));
450         Enumeration JavaDoc en = f.entries();
451         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
452         int cnt = 0;
453         while (en.hasMoreElements()) {
454             JarEntry JavaDoc e = (JarEntry JavaDoc)en.nextElement();
455             if (e.getName().endsWith("SF")) {
456                 cnt++;
457                 if (!e.getName().equals("META-INF/EXTERNAL.SF")) {
458                     fail("Signed with wrong entity: " + e.getName());
459                 }
460             }
461             sb.append(e.getName());
462             sb.append('\n');
463         }
464
465         if (cnt == 0) {
466             fail("Signed with wrong file:\n" + sb);
467         }
468         
469         File JavaDoc jnlp = new File JavaDoc(output, "aaa-my-module.jnlp");
470         
471         String JavaDoc res = ModuleDependenciesTest.readFile (jnlp);
472
473         int first = res.indexOf("jar href");
474         assertEquals("Just one jar href ", -1, res.indexOf("jar href", first + 1));
475         
476         String JavaDoc extRes = ModuleDependenciesTest.readFile(new File JavaDoc(output, "aaa-my-module/ext-t0.jnlp"));
477         
478         Matcher JavaDoc m = Pattern.compile("<title>(.*)</title>").matcher(extRes);
479         assertTrue("title is there: " + extRes, m.find());
480         assertEquals("Name of file is used for title", "t0", m.group(1));
481     }
482     
483     public void testInformationIsTakenFromLocalizedBundle() throws Exception JavaDoc {
484         Manifest JavaDoc m;
485         
486         m = ModuleDependenciesTest.createManifest ();
487         m.getMainAttributes ().putValue ("OpenIDE-Module", "org.my.module/3");
488         
489         Properties JavaDoc props = new Properties JavaDoc();
490         props.put("OpenIDE-Module-Name", "Module Build Harness");
491         props.put("OpenIDE-Module-Display-Category", "Developing NetBeans");
492         props.put("OpenIDE-Module-Short-Description", "Lets you build external plug-in modules from sources.");
493         props.put("OpenIDE-Module-Long-Description", "XXX");
494         
495         File JavaDoc simpleJar = generateJar (null, new String JavaDoc[0], m, props);
496
497         File JavaDoc parent = simpleJar.getParentFile ();
498         File JavaDoc output = new File JavaDoc(parent, "output");
499         File JavaDoc ks = generateKeystore("jnlp", "netbeans-test");
500         
501         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
502             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
503             "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
504             " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
505             "<target name=\"all\" >" +
506             " <mkdir dir='" + output + "' />" +
507             " <jnlp dir='" + output + "' alias='jnlp' storepass='netbeans-test' keystore='" + ks + "' >" +
508             " <modules dir='" + parent + "' >" +
509             " <include name='" + simpleJar.getName() + "' />" +
510             " </modules>" +
511             " </jnlp>" +
512             "</target>" +
513             "</project>"
514         );
515         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { });
516     
517         assertFilenames(output, "org-my-module.jnlp", "org-my-module/s0.jar");
518         
519         File JavaDoc jnlp = new File JavaDoc(output, "org-my-module.jnlp");
520         String JavaDoc res = ModuleDependenciesTest.readFile (jnlp);
521
522         int infoBegin = res.indexOf("<information>");
523         int infoEnd = res.indexOf("</information>");
524         
525         if (infoEnd == -1 || infoBegin == -1) {
526             fail ("Both information tags must be present: " + res);
527         }
528         
529         String JavaDoc info = res.substring(infoBegin, infoEnd);
530         
531         if (info.indexOf("<title>Module Build Harness</title>") == -1) {
532             fail("Title should be there with Module Build Harness inside itself: " + info);
533         }
534         
535         if (info.indexOf("<description kind='one-line'>Lets you build external plug-in modules from sources.</description>") == -1) {
536             fail("one-line should be there with 'lets you...' inside itself: " + info);
537         }
538         
539         if (info.indexOf("<description kind='short'>XXX</description>") == -1) {
540             fail("short should be there with XXX inside itself: " + info);
541         }
542     }
543     
544     public void testGenerateJNLPFailsForModulesWithExtraFiles() throws Exception JavaDoc {
545         doCompareJNLPFileWithUpdateTracking(true, null, "");
546     }
547     public void testGenerateJNLPSucceedsWithExtraFiles() throws Exception JavaDoc {
548         doCompareJNLPFileWithUpdateTracking(false, null, "");
549     }
550     public void testGenerateJNLPSucceedsWhenExtraFileIsExcluded() throws Exception JavaDoc {
551         doCompareJNLPFileWithUpdateTracking(false, "lib/nbexec", " verifyexcludes=' one, lib/nbexec, three ' ");
552     }
553     public void testGenerateJNLPSucceedsWhenModuleAutoDepsArePresent() throws Exception JavaDoc {
554         doCompareJNLPFileWithUpdateTracking(false, "config/ModuleAutoDeps/aaa-my-module.xml", " verifyexcludes=' none ' ");
555     }
556     
557     private void doCompareJNLPFileWithUpdateTracking(boolean useNonModule, String JavaDoc fakeEntry, String JavaDoc extraScript) throws Exception JavaDoc {
558         File JavaDoc nonModule = generateJar (new String JavaDoc[0], ModuleDependenciesTest.createManifest());
559         
560         Manifest JavaDoc m = ModuleDependenciesTest.createManifest ();
561         m.getMainAttributes ().putValue ("OpenIDE-Module", "aaa.my.module/3");
562         File JavaDoc module = generateJar (new String JavaDoc[0], m);
563         
564         File JavaDoc updateTracking = new File JavaDoc(getWorkDir(), "update_tracking");
565         updateTracking.mkdirs();
566         assertTrue("Created", updateTracking.isDirectory());
567
568         File JavaDoc enableXML = new File JavaDoc(new File JavaDoc(getWorkDir(), "config"), "Modules");
569         enableXML.getParentFile().mkdirs();
570         enableXML.createNewFile();
571         
572         File JavaDoc trackingFile = new File JavaDoc(updateTracking, "aaa-my-module.xml");
573         FileWriter JavaDoc w = new FileWriter JavaDoc(trackingFile);
574         w.write(
575 "<?xml version='1.0' encoding='UTF-8'?>\n" +
576 "<module codename='org.apache.tools.ant.module/3'>\n" +
577     "<module_version specification_version='3.22' origin='installer' last='true' install_time='1124194231878'>\n" +
578         (useNonModule ? ("<file name='modules/" + nonModule.getName() + "' crc='1536373800'/>\n") : "") +
579         "<file name='modules/" + module.getName() + "' crc='3245456472'/>\n" +
580         "<file name='config/Modules/aaa-my-module.xml' crc='43434' />\n" +
581         (fakeEntry != null ? "<file name='" + fakeEntry + "' crc='43222' />\n" : "") +
582 " </module_version>\n" +
583 "</module>\n"
584         );
585         w.close();
586         
587         
588         
589         String JavaDoc script =
590             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
591             "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
592             " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
593             "<target name=\"all\" >" +
594             " <mkdir dir='${test.output}' />" +
595             " <jnlp dir='${test.output}' alias='jnlp' storepass='netbeans-test' keystore='${test.ks}' verify='true' " + extraScript + " >" +
596             " <modules dir='${test.parent}' >" +
597             " <include name='${test.name}' />" +
598             " </modules>" +
599             " </jnlp>" +
600             "</target>" +
601             "</project>";
602
603         assertEquals("Both modules in the same dir", module.getParentFile(), nonModule.getParentFile());
604         
605         File JavaDoc output = new File JavaDoc(getWorkDir(), "output");
606         File JavaDoc ks = generateKeystore("jnlp", "netbeans-test");
607         
608         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (script);
609         try {
610             PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] {
611                 "-Dtest.output=" + output,
612                 "-Dtest.parent=" + module.getParent(),
613                 "-Dtest.name=" + module.getName(),
614                 "-Dtest.ks=" + ks,
615             });
616             if (useNonModule) {
617                 fail("The task has to fail");
618             }
619             
620             assertTrue ("Output exists", output.exists ());
621             assertTrue ("Output directory created", output.isDirectory());
622
623             File JavaDoc ext = new File JavaDoc (output, module.getName());
624
625
626             String JavaDoc[] files = ext.getParentFile().list();
627             assertEquals("Two files are there", 2, files.length);
628         } catch (PublicPackagesInProjectizedXMLTest.ExecutionError ex) {
629             if (!useNonModule) {
630                 throw ex;
631             } else {
632                 // ok, this is fine
633
assertTrue ("Output exists", output.exists ());
634                 assertTrue ("Output directory created", output.isDirectory());
635
636                 File JavaDoc ext = new File JavaDoc (output, module.getName());
637
638
639                 String JavaDoc[] files = ext.getParentFile().list();
640                 assertEquals("Output dir is empty as nothing has been generated", 0, files.length);
641             }
642         }
643         
644     }
645     
646     private File JavaDoc doClassPathModuleCheck(String JavaDoc script) throws Exception JavaDoc {
647         Manifest JavaDoc m;
648
649         File JavaDoc extJar = generateJar ("modules/ext", new String JavaDoc[0], ModuleDependenciesTest.createManifest(), null);
650         
651         m = ModuleDependenciesTest.createManifest ();
652         m.getMainAttributes ().putValue ("OpenIDE-Module", "aaa.my.module/3");
653         m.getMainAttributes ().putValue ("Class-Path", "ext/" + extJar.getName());
654         File JavaDoc simpleJar = generateJar ("modules", new String JavaDoc[0], m, null);
655
656         File JavaDoc parent = simpleJar.getParentFile ();
657         
658         File JavaDoc output = new File JavaDoc(parent, "output");
659         File JavaDoc ks = generateKeystore("jnlp", "netbeans-test");
660         
661         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (script);
662         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] {
663             "-Dtest.output=" + output,
664             "-Dtest.parent=" + parent,
665             "-Dtest.name=" + simpleJar.getName(),
666             "-Dtest.ks=" + ks,
667             "-Dtest.ext=" + extJar
668         });
669         
670         return output;
671     }
672     
673     
674     private final File JavaDoc createNewJarFile (String JavaDoc prefix) throws IOException JavaDoc {
675         if (prefix == null) {
676             prefix = "modules";
677         }
678         String JavaDoc ss = prefix.substring(prefix.length()-1, prefix.length());
679                 
680         File JavaDoc dir = new File JavaDoc(this.getWorkDir(), prefix);
681         dir.mkdirs();
682         
683         int i = 0;
684         for (;;) {
685             File JavaDoc f = new File JavaDoc (dir, ss + i++ + ".jar");
686             if (!f.exists ()) return f;
687         }
688     }
689     
690     protected final File JavaDoc generateJar (String JavaDoc[] content, Manifest JavaDoc manifest) throws IOException JavaDoc {
691         return generateJar(null, content, manifest, null);
692     }
693     
694     protected final File JavaDoc generateJar (String JavaDoc prefix, String JavaDoc[] content, Manifest JavaDoc manifest, Properties JavaDoc props) throws IOException JavaDoc {
695         File JavaDoc f = createNewJarFile (prefix);
696         
697         if (props != null) {
698             manifest.getMainAttributes().putValue("OpenIDE-Module-Localizing-Bundle", "some/fake/prop/name/Bundle.properties");
699         }
700         
701         JarOutputStream JavaDoc os = new JarOutputStream JavaDoc (new FileOutputStream JavaDoc (f), manifest);
702         
703         if (props != null) {
704             os.putNextEntry(new JarEntry JavaDoc("some/fake/prop/name/Bundle.properties"));
705             props.store(os, "# properties for the module");
706             os.closeEntry();
707         }
708         
709         
710         for (int i = 0; i < content.length; i++) {
711             os.putNextEntry(new JarEntry JavaDoc (content[i]));
712             os.closeEntry();
713         }
714         os.closeEntry ();
715         os.close();
716         
717         return f;
718     }
719     
720     private final File JavaDoc generateKeystore(String JavaDoc alias, String JavaDoc password) throws Exception JavaDoc {
721         Error JavaDoc lastEx = null;
722         for (int i = 0; i < 10; i++) {
723             File JavaDoc where = new File JavaDoc(getWorkDir(), "key" + i + ".ks");
724
725             String JavaDoc script =
726                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
727                 "<project name=\"Generate Keystore\" basedir=\".\" default=\"all\" >" +
728                 "<target name=\"all\" >" +
729                 "<genkey \n" +
730                   "alias='" + alias + "' \n" +
731                   "keystore='" + where + "' \n" +
732                   "storepass='" + password + "' \n" +
733                   "dname='CN=A NetBeans Friend, OU=NetBeans, O=netbeans.org, C=US' \n" +
734                 "/>\n" +
735                 "</target></project>\n";
736
737             java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (script);
738             try {
739                 PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { });
740             } catch (PublicPackagesInProjectizedXMLTest.ExecutionError ex) {
741                 Logger.getAnonymousLogger().log(Level.WARNING, "Failed for " + i, ex);
742                 lastEx = ex;
743                 if (ex.getMessage().indexOf("CKR_KEY_SIZE_RANGE") >= 0) {
744                     Thread.sleep(new Random JavaDoc().nextInt(1000));
745                     continue;
746                 }
747                 throw ex;
748             }
749             return where;
750         }
751         throw lastEx;
752     }
753
754     public void testIndirectJars() throws Exception JavaDoc {
755         Manifest JavaDoc m = ModuleDependenciesTest.createManifest();
756         m.getMainAttributes().putValue("OpenIDE-Module", "me");
757         generateJar(new String JavaDoc[0], m);
758         generateJar("lib", new String JavaDoc[0], new Manifest JavaDoc(), null);
759         assertTrue(new File JavaDoc(getWorkDir(), "lib/b0.jar").isFile());
760         File JavaDoc output = new File JavaDoc(getWorkDir(), "output");
761         File JavaDoc ks = generateKeystore("jnlp", "netbeans-test");
762         File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString(
763                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
764                 "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
765                 " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
766                 "<target name=\"all\" >" +
767                 " <mkdir dir='" + output + "' />" +
768                 " <jnlp dir='" + output + "' alias='jnlp' storepass='netbeans-test' keystore='" + ks + "' >" +
769                 " <modules dir='" + getWorkDir() + "'>" +
770                 " <include name='modules/s0.jar'/>" +
771                 " </modules>" +
772                 " <indirectjars dir='" + getWorkDir() + "'>" +
773                 " <include name='lib/b0.jar'/>" +
774                 " </indirectjars>" +
775                 " </jnlp>" +
776                 "</target>" +
777                 "</project>"
778                 );
779         PublicPackagesInProjectizedXMLTest.execute(f, new String JavaDoc[] { "-verbose" });
780         assertFilenames(output, "me.jnlp", "me/s0.jar", "me/lib-b0.jar");
781         File JavaDoc jnlp = new File JavaDoc(output, "me.jnlp");
782         String JavaDoc res = ModuleDependenciesTest.readFile(jnlp);
783         assertTrue(res, res.contains("me/lib-b0.jar"));
784         JarFile JavaDoc otherJar = new JarFile JavaDoc(new File JavaDoc(output, "me/lib-b0.jar"));
785         assertNotNull(otherJar.getEntry("META-INF/clusterpath/lib/b0.jar"));
786     }
787
788 }
789
Popular Tags