KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileWriter JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.jar.JarEntry JavaDoc;
30 import java.util.jar.JarFile JavaDoc;
31 import java.util.jar.JarOutputStream JavaDoc;
32 import java.util.jar.Manifest JavaDoc;
33 import java.util.regex.Matcher JavaDoc;
34 import java.util.regex.Pattern JavaDoc;
35 import org.netbeans.junit.*;
36 import org.netbeans.nbbuild.PublicPackagesInProjectizedXMLTest.ExecutionError;
37
38
39 /** Is generation of Jnlp files correct?
40  *
41  * @author Jaroslav Tulach
42  */

43 public class MakeNBMTest extends NbTestCase {
44     public MakeNBMTest (String JavaDoc name) {
45         super (name);
46     }
47     
48     protected void setUp() throws Exception JavaDoc {
49         clearWorkDir();
50     }
51     
52     
53     public void testGenerateNBMForSimpleModule() throws Exception JavaDoc {
54         Manifest JavaDoc m;
55         
56         m = ModuleDependenciesTest.createManifest ();
57         m.getMainAttributes ().putValue ("OpenIDE-Module", "org.my.module/3");
58         File JavaDoc simpleJar = generateJar (new String JavaDoc[0], m);
59
60         File JavaDoc parent = simpleJar.getParentFile ();
61         File JavaDoc output = new File JavaDoc(parent, "output");
62         File JavaDoc ks = genereteKeystore("nbm", "netbeans-test");
63         if (ks == null) {
64             return;
65         }
66         
67         File JavaDoc ut = new File JavaDoc (new File JavaDoc(getWorkDir(), "update_tracking"), "org-my-module.xml");
68         ut.getParentFile().mkdirs();
69         FileWriter JavaDoc w = new FileWriter JavaDoc(ut);
70         String JavaDoc UTfile =
71             "<?xml version='1.0' encoding='UTF-8'?>" +
72             "<module codename='org.netbeans.modules.autoupdate/1'>" +
73             " <module_version install_time='1136503038669' last='true' origin='installer' specification_version='2.16.1'>" +
74             " <file crc='3405032071' name='modules/" + simpleJar.getName() + "'/>" +
75             " </module_version>" +
76             "</module>";
77         w.write(UTfile);
78         w.close();
79         
80         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
81             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
82             "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
83             " <taskdef name=\"makenbm\" classname=\"org.netbeans.nbbuild.MakeNBM\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
84             "<target name=\"all\" >" +
85             " <mkdir dir='" + output + "' />" +
86             " <makenbm file='" + output + "/x.nbm'" +
87             " productdir='" + getWorkDir() + "'" +
88             " module='modules/" + simpleJar.getName() + "'" +
89             " homepage='http://www.homepage.org'" +
90             " distribution='distro'" +
91             " needsrestart='false'" +
92             " global='false'" +
93             " releasedate='today'" +
94             " moduleauthor='test'>" +
95             " <license file='" + simpleJar + "'/>" +
96             " <signature keystore='" + ks + "' storepass='netbeans-test' alias='nbm'/>" +
97             " </makenbm>" +
98       // " <fail if='do.fail'/>" +
99
"</target>" +
100             "</project>"
101         );
102         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { "-verbose" });
103         
104         assertTrue ("Output exists", output.exists ());
105         assertTrue ("Output directory created", output.isDirectory());
106         
107         String JavaDoc[] files = output.list();
108         assertEquals("It has the nbm file", 1, files.length);
109         
110         if (!files[0].endsWith("x.nbm")) {
111             fail("Not the right one: " + files[0]);
112         }
113
114         long time = output.listFiles()[0].lastModified();
115         
116         // wait a while so the NBM file has different timestamp
117
// if recreated
118
Thread.sleep(1300);
119
120         // execute once again
121
PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { "-debug", "-Ddo.fail=true"});
122         
123         long newTime = output.listFiles()[0].lastModified();
124         
125         assertEquals("The file has not been modified:\n" + PublicPackagesInProjectizedXMLTest.getStdOut(), time, newTime);
126         
127         
128         CHECK_SIGNED: {
129             File JavaDoc jar = output.listFiles()[0];
130             JarFile JavaDoc signed = new JarFile JavaDoc(jar);
131             Enumeration JavaDoc it = signed.entries();
132             while (it.hasMoreElements()) {
133                 JarEntry JavaDoc entry = (JarEntry JavaDoc)it.nextElement();
134                 if (entry.getName().endsWith(".SF")) {
135                     break CHECK_SIGNED;
136                 }
137             }
138             fail ("File does not seem to be signed: " + jar);
139         }
140         
141     }
142     
143     private final File JavaDoc createNewJarFile (String JavaDoc prefix) throws IOException JavaDoc {
144         if (prefix == null) {
145             prefix = "modules";
146         }
147         String JavaDoc ss = prefix.substring(prefix.length()-1, prefix.length());
148                 
149         File JavaDoc dir = new File JavaDoc(this.getWorkDir(), prefix);
150         dir.mkdirs();
151         
152         int i = 0;
153         for (;;) {
154             File JavaDoc f = new File JavaDoc (dir, ss + i++ + ".jar");
155             if (!f.exists ()) return f;
156         }
157     }
158     
159     protected final File JavaDoc generateJar (String JavaDoc[] content, Manifest JavaDoc manifest) throws IOException JavaDoc {
160         return generateJar(null, content, manifest, null);
161     }
162     
163     protected final File JavaDoc generateJar (String JavaDoc prefix, String JavaDoc[] content, Manifest JavaDoc manifest, Properties JavaDoc props) throws IOException JavaDoc {
164         File JavaDoc f = createNewJarFile (prefix);
165         
166         if (props != null) {
167             manifest.getMainAttributes().putValue("OpenIDE-Module-Localizing-Bundle", "some/fake/prop/name/Bundle.properties");
168         }
169         
170         JarOutputStream JavaDoc os = new JarOutputStream JavaDoc (new FileOutputStream JavaDoc (f), manifest);
171         
172         if (props != null) {
173             os.putNextEntry(new JarEntry JavaDoc("some/fake/prop/name/Bundle.properties"));
174             props.store(os, "# properties for the module");
175             os.closeEntry();
176         }
177         
178         
179         for (int i = 0; i < content.length; i++) {
180             os.putNextEntry(new JarEntry JavaDoc (content[i]));
181             os.closeEntry();
182         }
183         os.closeEntry ();
184         os.close();
185         
186         return f;
187     }
188     
189     private final File JavaDoc genereteKeystore(String JavaDoc alias, String JavaDoc password) throws Exception JavaDoc {
190         File JavaDoc where = new File JavaDoc(getWorkDir(), "key.ks");
191         
192         String JavaDoc script =
193             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
194             "<project name=\"Generate Keystore\" basedir=\".\" default=\"all\" >" +
195             "<target name=\"all\" >" +
196             "<genkey \n" +
197               "alias='" + alias + "' \n" +
198               "keystore='" + where + "' \n" +
199               "storepass='" + password + "' \n" +
200               "dname='CN=A NetBeans Friend, OU=NetBeans, O=netbeans.org, C=US' \n" +
201             "/>\n" +
202             "</target></project>\n";
203         
204         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (script);
205         try {
206             PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { });
207         } catch (ExecutionError err) {
208             if (err.getMessage().indexOf("java.security.ProviderException") != -1) {
209                 // common error on Sun OS:
210
// org.netbeans.nbbuild.PublicPackagesInProjectizedXMLTest$ExecutionError: Execution has to finish without problems was: 1
211
// Output: Buildfile: /space/test4u2/testrun/work/tmpdir/res310.xml
212
//
213
// all:
214
// [genkey] Generating Key for nbm
215
// [genkey] keytool error: java.security.ProviderException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_KEY_SIZE_RANGE
216
return null;
217             }
218         }
219         
220         return where;
221     }
222 }
223
Popular Tags