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.IOException;
047: import java.util.jar.JarEntry;
048: import java.util.jar.JarOutputStream;
049: import java.util.jar.Manifest;
050: import org.netbeans.junit.NbTestCase;
051:
052: /** Is generation of Jnlp files correct?
053: *
054: * @author Jaroslav Tulach
055: */
056: public class MakeMasterJNLPTest extends NbTestCase {
057: public MakeMasterJNLPTest(String name) {
058: super (name);
059: }
060:
061: protected void setUp() throws Exception {
062: clearWorkDir();
063: }
064:
065: public void testGenerateReferenceFilesOnce() throws Exception {
066: doGenerateReferenceFiles(1);
067: }
068:
069: public void testGenerateReferenceFilesThrice() throws Exception {
070: doGenerateReferenceFiles(3);
071: }
072:
073: private void doGenerateReferenceFiles(int cnt) throws Exception {
074: Manifest m;
075:
076: m = ModuleDependenciesTest.createManifest();
077: m.getMainAttributes().putValue("OpenIDE-Module",
078: "org.my.module/3");
079: File simpleJar = generateJar(new String[0], m);
080:
081: m = ModuleDependenciesTest.createManifest();
082: m.getMainAttributes().putValue("OpenIDE-Module",
083: "org.second.module/3");
084: File secondJar = generateJar(new String[0], m);
085:
086: File parent = simpleJar.getParentFile();
087: assertEquals("They are in the same folder", parent, secondJar
088: .getParentFile());
089:
090: File output = new File(parent, "output");
091:
092: java.io.File f = PublicPackagesInProjectizedXMLTest
093: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
094: + "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >"
095: + " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeMasterJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>"
096: + "<target name=\"all\" >"
097: + " <mkdir dir='"
098: + output
099: + "' />"
100: + " <jnlp dir='"
101: + output
102: + "' >"
103: + " <modules dir='"
104: + parent
105: + "' >"
106: + " <include name='"
107: + simpleJar.getName()
108: + "' />"
109: + " <include name='"
110: + secondJar.getName()
111: + "' />"
112: + " </modules>"
113: + " </jnlp>"
114: + "</target>" + "</project>");
115: while (cnt-- > 0) {
116: PublicPackagesInProjectizedXMLTest.execute(f,
117: new String[] { "-verbose" });
118: }
119:
120: assertTrue("Output exists", output.exists());
121: assertTrue("Output directory created", output.isDirectory());
122:
123: String[] files = output.list();
124: assertEquals("It has two files", 2, files.length);
125:
126: java.util.Arrays.sort(files);
127:
128: assertEquals("The res1 file: " + files[0], "org-my-module.ref",
129: files[0]);
130: assertEquals("The res2 file: " + files[1],
131: "org-second-module.ref", files[1]);
132:
133: File r1 = new File(output, "org-my-module.ref");
134: String res1 = ModuleDependenciesTest.readFile(r1);
135:
136: File r2 = new File(output, "org-second-module.ref");
137: String res2 = ModuleDependenciesTest.readFile(r2);
138:
139: assertExt(res1, "org.my.module");
140: assertExt(res2, "org.second.module");
141: }
142:
143: private static void assertExt(String res, String module) {
144: int ext = res.indexOf("<extension");
145: if (ext == -1) {
146: fail("<extension tag shall start there: " + res);
147: }
148:
149: assertEquals("Just one extension tag", -1, res.indexOf(
150: "<extension", ext + 1));
151:
152: int cnb = res.indexOf(module);
153: if (cnb == -1) {
154: fail("Cnb has to be there: " + module + " but is " + res);
155: }
156: assertEquals("Just one cnb", -1, res.indexOf(module, cnb + 1));
157:
158: String dashcnb = module.replace('.', '-');
159:
160: int dcnb = res.indexOf(dashcnb);
161: if (dcnb == -1) {
162: fail("Dash Cnb has to be there: " + dashcnb + " but is "
163: + res);
164: }
165: assertEquals("Just one dash cnb", -1, res.indexOf(dashcnb,
166: dcnb + 1));
167: }
168:
169: private final File createNewJarFile() throws IOException {
170: int i = 0;
171: for (;;) {
172: File f = new File(this .getWorkDir(), i++ + ".jar");
173: if (!f.exists())
174: return f;
175: }
176: }
177:
178: protected final File generateJar(String[] content, Manifest manifest)
179: throws IOException {
180: File f = createNewJarFile();
181:
182: JarOutputStream os = new JarOutputStream(
183: new FileOutputStream(f), manifest);
184:
185: for (int i = 0; i < content.length; i++) {
186: os.putNextEntry(new JarEntry(content[i]));
187: os.closeEntry();
188: }
189: os.closeEntry();
190: os.close();
191:
192: return f;
193: }
194:
195: }
|