KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.File JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.jar.JarEntry JavaDoc;
26 import java.util.jar.JarFile JavaDoc;
27 import java.util.jar.JarOutputStream JavaDoc;
28 import java.util.jar.Manifest JavaDoc;
29 import java.util.regex.Matcher JavaDoc;
30 import java.util.regex.Pattern JavaDoc;
31
32 import org.netbeans.junit.*;
33
34
35 /** Is generation of Jnlp files correct?
36  *
37  * @author Jaroslav Tulach
38  */

39 public class MakeMasterJNLPTest extends NbTestCase {
40     public MakeMasterJNLPTest (String JavaDoc name) {
41         super (name);
42     }
43     
44     protected void setUp() throws Exception JavaDoc {
45         clearWorkDir();
46     }
47     
48     
49     public void testGenerateRefenrenceFilesOnce() throws Exception JavaDoc {
50         doGenerateRefenrenceFiles(1);
51     }
52     public void testGenerateRefenrenceFilesThree() throws Exception JavaDoc {
53         doGenerateRefenrenceFiles(3);
54     }
55     
56     private void doGenerateRefenrenceFiles(int cnt) throws Exception JavaDoc {
57         Manifest JavaDoc m;
58         
59         m = ModuleDependenciesTest.createManifest ();
60         m.getMainAttributes ().putValue ("OpenIDE-Module", "org.my.module/3");
61         File JavaDoc simpleJar = generateJar (new String JavaDoc[0], m);
62
63         m = ModuleDependenciesTest.createManifest ();
64         m.getMainAttributes ().putValue ("OpenIDE-Module", "org.second.module/3");
65         File JavaDoc secondJar = generateJar (new String JavaDoc[0], m);
66         
67         File JavaDoc parent = simpleJar.getParentFile ();
68         assertEquals("They are in the same folder", parent, secondJar.getParentFile());
69         
70         File JavaDoc output = new File JavaDoc(parent, "output");
71         
72         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
73             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
74             "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
75             " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeMasterJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
76             "<target name=\"all\" >" +
77             " <mkdir dir='" + output + "' />" +
78             " <jnlp dir='" + output + "' >" +
79             " <modules dir='" + parent + "' >" +
80             " <include name='" + simpleJar.getName() + "' />" +
81             " <include name='" + secondJar.getName() + "' />" +
82             " </modules>" +
83             " </jnlp>" +
84             "</target>" +
85             "</project>"
86         );
87         while (cnt-- > 0) {
88             PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { "-verbose" });
89         }
90         
91         assertTrue ("Output exists", output.exists ());
92         assertTrue ("Output directory created", output.isDirectory());
93         
94         String JavaDoc[] files = output.list();
95         assertEquals("It has two files", 2, files.length);
96
97         java.util.Arrays.sort(files);
98         
99         assertEquals("The res1 file: " + files[0], "org-my-module.ref", files[0]);
100         assertEquals("The res2 file: "+ files[1], "org-second-module.ref", files[1]);
101         
102         File JavaDoc r1 = new File JavaDoc(output, "org-my-module.ref");
103         String JavaDoc res1 = ModuleDependenciesTest.readFile (r1);
104
105         File JavaDoc r2 = new File JavaDoc(output, "org-second-module.ref");
106         String JavaDoc res2 = ModuleDependenciesTest.readFile (r2);
107         
108         assertExt(res1, "org.my.module");
109         assertExt(res2, "org.second.module");
110     }
111     
112     private static void assertExt(String JavaDoc res, String JavaDoc module) {
113         int ext = res.indexOf("<extension");
114         if (ext == -1) {
115             fail ("<extension tag shall start there: " + res);
116         }
117         
118         assertEquals("Just one extension tag", -1, res.indexOf("<extension", ext + 1));
119
120         int cnb = res.indexOf(module);
121         if (cnb == -1) {
122             fail("Cnb has to be there: " + module + " but is " + res);
123         }
124         assertEquals("Just one cnb", -1, res.indexOf(module, cnb + 1));
125         
126         String JavaDoc dashcnb = module.replace('.', '-');
127         
128         int dcnb = res.indexOf(dashcnb);
129         if (dcnb == -1) {
130             fail("Dash Cnb has to be there: " + dashcnb + " but is " + res);
131         }
132         assertEquals("Just one dash cnb", -1, res.indexOf(dashcnb, dcnb + 1));
133     }
134
135     private final File JavaDoc createNewJarFile () throws IOException JavaDoc {
136         int i = 0;
137         for (;;) {
138             File JavaDoc f = new File JavaDoc (this.getWorkDir(), i++ + ".jar");
139             if (!f.exists ()) return f;
140         }
141     }
142     
143     protected final File JavaDoc generateJar (String JavaDoc[] content, Manifest JavaDoc manifest) throws IOException JavaDoc {
144         File JavaDoc f = createNewJarFile ();
145         
146         JarOutputStream JavaDoc os = new JarOutputStream JavaDoc (new FileOutputStream JavaDoc (f), manifest);
147         
148         for (int i = 0; i < content.length; i++) {
149             os.putNextEntry(new JarEntry JavaDoc (content[i]));
150             os.closeEntry();
151         }
152         os.closeEntry ();
153         os.close();
154         
155         return f;
156     }
157
158 }
159
Popular Tags