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.Enumeration;
048: import java.util.jar.JarEntry;
049: import java.util.jar.JarFile;
050: import java.util.jar.JarOutputStream;
051: import java.util.jar.Manifest;
052: import java.util.regex.Matcher;
053: import java.util.regex.Pattern;
054: import junit.framework.AssertionFailedError;
055:
056: import org.netbeans.junit.*;
057:
058: /** Checks that javac.target gets reflected in the manifest.
059: *
060: * @author Jaroslav Tulach
061: */
062: public class JarWithModuleAttributesTest extends NbTestCase {
063: public JarWithModuleAttributesTest(String name) {
064: super (name);
065: }
066:
067: protected void setUp() throws Exception {
068: clearWorkDir();
069: }
070:
071: public void testAddThereVersionFromJavacTarget() throws Exception {
072: File output = new File(getWorkDir(), "output");
073: java.io.File manifest = PublicPackagesInProjectizedXMLTest
074: .extractString("OpenIDE-Module: org.netbeans.modules.sendopts\n"
075: + "OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/sendopts/Bundle.properties\n"
076: + "OpenIDE-Module-Specification-Version: 1.9\n"
077: + "OpenIDE-Module-Layer: org/netbeans/modules/sendopts/layer.xml\n");
078: File jar = new File(getWorkDir(), "x.jar");
079:
080: java.io.File f = PublicPackagesInProjectizedXMLTest
081: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
082: + "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >"
083: + " <taskdef name=\"njar\" classname=\"org.netbeans.nbbuild.JarWithModuleAttributes\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>"
084: + "<target name=\"all\" >"
085: + " <mkdir dir='"
086: + output
087: + "' />"
088: + " <property name='javac.target' value='2.87'/>"
089: + " <property name='public.packages' value=''/>"
090: + " <property name='buildnumber' value='BLDprivateTESTBuild'/>"
091: + " <property name='code.name.base.slashes' value='org/netbeans/modules/sendopts'/>"
092: + " <njar manifest='"
093: + manifest
094: + "' destfile='"
095: + jar
096: + "'>"
097: + " </njar>"
098: + " <unzip src='"
099: + jar
100: + "' dest='"
101: + output
102: + "'/>" + "</target>" + "</project>");
103:
104: PublicPackagesInProjectizedXMLTest.execute(f,
105: new String[] { "-verbose" });
106:
107: assertTrue("JAR created", jar.isFile());
108:
109: File extracted = new File(new File(output, "META-INF"),
110: "MANIFEST.MF");
111: assertTrue("Manifest extracted", extracted.isFile());
112:
113: JarFile file = new JarFile(jar);
114: String value = file.getManifest().getMainAttributes().getValue(
115: "OpenIDE-Module-Java-Dependencies");
116: assertNotNull("Attribute created:\n"
117: + PublicPackagesInProjectizedXMLTest
118: .readFile(extracted), value);
119:
120: String[] arr = value.split(">");
121: assertEquals("Two parts", 2, arr.length);
122: assertEquals("Java", arr[0].trim());
123:
124: assertVersionAtLeast("2.87", arr[1]);
125: }
126:
127: public void testKeepOldVersion() throws Exception {
128: File output = new File(getWorkDir(), "output");
129: java.io.File manifest = PublicPackagesInProjectizedXMLTest
130: .extractString("OpenIDE-Module: org.netbeans.modules.sendopts\n"
131: + "OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/sendopts/Bundle.properties\n"
132: + "OpenIDE-Module-Specification-Version: 1.9\n"
133: + "OpenIDE-Module-Java-Dependencies: Java > 1.3\n"
134: + "OpenIDE-Module-Layer: org/netbeans/modules/sendopts/layer.xml\n");
135: File jar = new File(getWorkDir(), "x.jar");
136:
137: java.io.File f = PublicPackagesInProjectizedXMLTest
138: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
139: + "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >"
140: + " <taskdef name=\"njar\" classname=\"org.netbeans.nbbuild.JarWithModuleAttributes\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>"
141: + "<target name=\"all\" >"
142: + " <mkdir dir='"
143: + output
144: + "' />"
145: + " <property name='javac.target' value='2.87'/>"
146: + " <property name='public.packages' value=''/>"
147: + " <property name='buildnumber' value='BLDprivateTESTBuild'/>"
148: + " <property name='code.name.base.slashes' value='org/netbeans/modules/sendopts'/>"
149: + " <njar manifest='"
150: + manifest
151: + "' destfile='"
152: + jar
153: + "'>"
154: + " </njar>"
155: + " <unzip src='"
156: + jar
157: + "' dest='"
158: + output
159: + "'/>" + "</target>" + "</project>");
160:
161: PublicPackagesInProjectizedXMLTest.execute(f,
162: new String[] { "-verbose" });
163:
164: assertTrue("JAR created", jar.isFile());
165:
166: File extracted = new File(new File(output, "META-INF"),
167: "MANIFEST.MF");
168: assertTrue("Manifest extracted", extracted.isFile());
169:
170: JarFile file = new JarFile(jar);
171: String value = file.getManifest().getMainAttributes().getValue(
172: "OpenIDE-Module-Java-Dependencies");
173: assertNotNull("Attribute created:\n"
174: + PublicPackagesInProjectizedXMLTest
175: .readFile(extracted), value);
176:
177: String[] arr = value.split(">");
178: assertEquals("Two parts", 2, arr.length);
179: assertEquals("Java", arr[0].trim());
180:
181: assertVersionAtLeast("1.3", arr[1]);
182: try {
183: assertVersionAtLeast("1.4", arr[1]);
184: } catch (AssertionFailedError ex) {
185: // ok
186: return;
187: }
188: fail("Version shall not be 1.4 or higher, as it is specified in manifest to be 1.3: "
189: + arr[1]);
190: }
191:
192: public void testIgnoreWeirdJavacTarget() throws Exception {
193: File output = new File(getWorkDir(), "output");
194: java.io.File manifest = PublicPackagesInProjectizedXMLTest
195: .extractString("OpenIDE-Module: org.netbeans.modules.sendopts\n"
196: + "OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/sendopts/Bundle.properties\n"
197: + "OpenIDE-Module-Specification-Version: 1.9\n"
198: + "OpenIDE-Module-Layer: org/netbeans/modules/sendopts/layer.xml\n");
199: File jar = new File(getWorkDir(), "x.jar");
200:
201: java.io.File f = PublicPackagesInProjectizedXMLTest
202: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
203: + "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >"
204: + " <taskdef name=\"njar\" classname=\"org.netbeans.nbbuild.JarWithModuleAttributes\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>"
205: + "<target name=\"all\" >"
206: + " <mkdir dir='"
207: + output
208: + "' />"
209: + " <property name='javac.target' value='jsr99'/>"
210: + " <property name='public.packages' value=''/>"
211: + " <property name='buildnumber' value='BLDprivateTESTBuild'/>"
212: + " <property name='code.name.base.slashes' value='org/netbeans/modules/sendopts'/>"
213: + " <njar manifest='"
214: + manifest
215: + "' destfile='"
216: + jar
217: + "'>"
218: + " </njar>"
219: + " <unzip src='"
220: + jar
221: + "' dest='"
222: + output
223: + "'/>" + "</target>" + "</project>");
224:
225: PublicPackagesInProjectizedXMLTest.execute(f,
226: new String[] { "-verbose" });
227:
228: assertTrue("JAR created", jar.isFile());
229:
230: File extracted = new File(new File(output, "META-INF"),
231: "MANIFEST.MF");
232: assertTrue("Manifest extracted", extracted.isFile());
233:
234: JarFile file = new JarFile(jar);
235: String value = file.getManifest().getMainAttributes().getValue(
236: "OpenIDE-Module-Java-Dependencies");
237: assertNull("Attribute not created:\n"
238: + PublicPackagesInProjectizedXMLTest
239: .readFile(extracted), value);
240: }
241:
242: private final File createNewJarFile() throws IOException {
243: int i = 0;
244: for (;;) {
245: File f = new File(this .getWorkDir(), i++ + ".jar");
246: if (!f.exists())
247: return f;
248: }
249: }
250:
251: protected final File generateJar(String[] content, Manifest manifest)
252: throws IOException {
253: File f = createNewJarFile();
254:
255: JarOutputStream os = new JarOutputStream(
256: new FileOutputStream(f), manifest);
257:
258: for (int i = 0; i < content.length; i++) {
259: os.putNextEntry(new JarEntry(content[i]));
260: os.closeEntry();
261: }
262: os.closeEntry();
263: os.close();
264:
265: return f;
266: }
267:
268: private static void assertVersionAtLeast(String limit, String value) {
269: int[] segLimit = segments(limit);
270: int[] segValue = segments(value);
271:
272: for (int i = 0; i < segLimit.length && i < segValue.length; i++) {
273: if (segValue[i] < segLimit[i]) {
274: fail("Version is younger than it should be. Expected: "
275: + limit + " was: " + value);
276: }
277: if (segValue[i] > segLimit[i]) {
278: return;
279: }
280: }
281: }
282:
283: private static int[] segments(String version) {
284: String[] arr = version.split("\\.");
285: int[] ret = new int[arr.length];
286: for (int i = 0; i < arr.length; i++) {
287: ret[i] = Integer.parseInt(arr[i].trim());
288: }
289: return ret;
290: }
291: }
|