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.File;
045: import java.io.FileOutputStream;
046: import java.io.FileWriter;
047: import java.io.IOException;
048: import java.util.Arrays;
049: import java.util.Enumeration;
050: import java.util.HashSet;
051: import java.util.Properties;
052: import java.util.jar.JarEntry;
053: import java.util.jar.JarFile;
054: import java.util.jar.JarOutputStream;
055: import java.util.jar.Manifest;
056: import java.util.regex.Matcher;
057: import java.util.regex.Pattern;
058: import org.netbeans.junit.*;
059: import org.netbeans.nbbuild.PublicPackagesInProjectizedXMLTest.ExecutionError;
060:
061: /** Is generation of Jnlp files correct?
062: *
063: * @author Jaroslav Tulach
064: */
065: public class MakeNBMTest extends NbTestCase {
066: public MakeNBMTest(String name) {
067: super (name);
068: }
069:
070: protected void setUp() throws Exception {
071: clearWorkDir();
072: }
073:
074: public void testGenerateNBMForSimpleModule() throws Exception {
075: Manifest m;
076:
077: m = ModuleDependenciesTest.createManifest();
078: m.getMainAttributes().putValue("OpenIDE-Module",
079: "org.my.module/3");
080: File simpleJar = generateJar(new String[0], m);
081:
082: File parent = simpleJar.getParentFile();
083: File output = new File(parent, "output");
084: File ks = genereteKeystore("nbm", "netbeans-test");
085: if (ks == null) {
086: return;
087: }
088:
089: File ut = new File(new File(getWorkDir(), "update_tracking"),
090: "org-my-module.xml");
091: ut.getParentFile().mkdirs();
092: FileWriter w = new FileWriter(ut);
093: String UTfile = "<?xml version='1.0' encoding='UTF-8'?>"
094: + "<module codename='org.netbeans.modules.autoupdate/1'>"
095: + " <module_version install_time='1136503038669' last='true' origin='installer' specification_version='2.16.1'>"
096: + " <file crc='3405032071' name='modules/"
097: + simpleJar.getName() + "'/>" + " </module_version>"
098: + "</module>";
099: w.write(UTfile);
100: w.close();
101:
102: java.io.File f = PublicPackagesInProjectizedXMLTest
103: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
104: + "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >"
105: + " <taskdef name=\"makenbm\" classname=\"org.netbeans.nbbuild.MakeNBM\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>"
106: + "<target name=\"all\" >" + " <mkdir dir='"
107: + output
108: + "' />"
109: + " <makenbm file='"
110: + output
111: + "/x.nbm'"
112: + " productdir='"
113: + getWorkDir()
114: + "'"
115: + " module='modules/"
116: + simpleJar.getName()
117: + "'"
118: + " homepage='http://www.homepage.org'"
119: + " distribution='distro'"
120: + " needsrestart='false'"
121: + " global='false'"
122: + " releasedate='today'"
123: + " moduleauthor='test'>"
124: + " <license file='"
125: + simpleJar
126: + "'/>"
127: + " <signature keystore='"
128: + ks
129: + "' storepass='netbeans-test' alias='nbm'/>"
130: + " </makenbm>" +
131: // " <fail if='do.fail'/>" +
132: "</target>" + "</project>");
133: PublicPackagesInProjectizedXMLTest.execute(f,
134: new String[] { "-verbose" });
135:
136: assertTrue("Output exists", output.exists());
137: assertTrue("Output directory created", output.isDirectory());
138:
139: String[] files = output.list();
140: assertEquals("It has the nbm file", 1, files.length);
141:
142: if (!files[0].endsWith("x.nbm")) {
143: fail("Not the right one: " + files[0]);
144: }
145:
146: long time = output.listFiles()[0].lastModified();
147:
148: // wait a while so the NBM file has different timestamp
149: // if recreated
150: Thread.sleep(1300);
151:
152: // execute once again
153: PublicPackagesInProjectizedXMLTest.execute(f, new String[] {
154: "-debug", "-Ddo.fail=true" });
155:
156: long newTime = output.listFiles()[0].lastModified();
157:
158: assertEquals("The file has not been modified:\n"
159: + PublicPackagesInProjectizedXMLTest.getStdOut(), time,
160: newTime);
161:
162: CHECK_SIGNED: {
163: File jar = output.listFiles()[0];
164: JarFile signed = new JarFile(jar);
165: Enumeration it = signed.entries();
166: while (it.hasMoreElements()) {
167: JarEntry entry = (JarEntry) it.nextElement();
168: if (entry.getName().endsWith(".SF")) {
169: break CHECK_SIGNED;
170: }
171: }
172: fail("File does not seem to be signed: " + jar);
173: }
174:
175: }
176:
177: private final File createNewJarFile(String prefix)
178: throws IOException {
179: if (prefix == null) {
180: prefix = "modules";
181: }
182: String ss = prefix.substring(prefix.length() - 1, prefix
183: .length());
184:
185: File dir = new File(this .getWorkDir(), prefix);
186: dir.mkdirs();
187:
188: int i = 0;
189: for (;;) {
190: File f = new File(dir, ss + i++ + ".jar");
191: if (!f.exists())
192: return f;
193: }
194: }
195:
196: protected final File generateJar(String[] content, Manifest manifest)
197: throws IOException {
198: return generateJar(null, content, manifest, null);
199: }
200:
201: protected final File generateJar(String prefix, String[] content,
202: Manifest manifest, Properties props) throws IOException {
203: File f = createNewJarFile(prefix);
204:
205: if (props != null) {
206: manifest.getMainAttributes().putValue(
207: "OpenIDE-Module-Localizing-Bundle",
208: "some/fake/prop/name/Bundle.properties");
209: }
210:
211: JarOutputStream os = new JarOutputStream(
212: new FileOutputStream(f), manifest);
213:
214: if (props != null) {
215: os.putNextEntry(new JarEntry(
216: "some/fake/prop/name/Bundle.properties"));
217: props.store(os, "# properties for the module");
218: os.closeEntry();
219: }
220:
221: for (int i = 0; i < content.length; i++) {
222: os.putNextEntry(new JarEntry(content[i]));
223: os.closeEntry();
224: }
225: os.closeEntry();
226: os.close();
227:
228: return f;
229: }
230:
231: private final File genereteKeystore(String alias, String password)
232: throws Exception {
233: File where = new File(getWorkDir(), "key.ks");
234:
235: String script = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
236: + "<project name=\"Generate Keystore\" basedir=\".\" default=\"all\" >"
237: + "<target name=\"all\" >"
238: + "<genkey \n"
239: + "alias='"
240: + alias
241: + "' \n"
242: + "keystore='"
243: + where
244: + "' \n"
245: + "storepass='"
246: + password
247: + "' \n"
248: + "dname='CN=A NetBeans Friend, OU=NetBeans, O=netbeans.org, C=US' \n"
249: + "/>\n" + "</target></project>\n";
250:
251: java.io.File f = PublicPackagesInProjectizedXMLTest
252: .extractString(script);
253: try {
254: PublicPackagesInProjectizedXMLTest.execute(f,
255: new String[] {});
256: } catch (ExecutionError err) {
257: if (err.getMessage().indexOf(
258: "java.security.ProviderException") != -1) {
259: // common error on Sun OS:
260: // org.netbeans.nbbuild.PublicPackagesInProjectizedXMLTest$ExecutionError: Execution has to finish without problems was: 1
261: // Output: Buildfile: /space/test4u2/testrun/work/tmpdir/res310.xml
262: //
263: // all:
264: // [genkey] Generating Key for nbm
265: // [genkey] keytool error: java.security.ProviderException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_KEY_SIZE_RANGE
266: return null;
267: }
268: }
269:
270: return where;
271: }
272: }
|