KickJava   Java API By Example, From Geeks To Geeks.

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


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 import junit.framework.AssertionFailedError;
32
33 import org.netbeans.junit.*;
34
35
36 /** Checks that javac.target gets reflected in the manifest.
37  *
38  * @author Jaroslav Tulach
39  */

40 public class JarWithModuleAttributesTest extends NbTestCase {
41     public JarWithModuleAttributesTest (String JavaDoc name) {
42         super (name);
43     }
44     
45     protected void setUp() throws Exception JavaDoc {
46         clearWorkDir();
47     }
48     
49     public void testAddThereVersionFromJavacTarget() throws Exception JavaDoc {
50         File JavaDoc output = new File JavaDoc(getWorkDir(), "output");
51         java.io.File JavaDoc manifest = PublicPackagesInProjectizedXMLTest.extractString (
52 "OpenIDE-Module: org.netbeans.modules.sendopts\n" +
53 "OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/sendopts/Bundle.properties\n" +
54 "OpenIDE-Module-Specification-Version: 1.9\n" +
55 "OpenIDE-Module-Layer: org/netbeans/modules/sendopts/layer.xml\n"
56         );
57         File JavaDoc jar = new File JavaDoc(getWorkDir(), "x.jar");
58         
59         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
60             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
61             "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
62             " <taskdef name=\"njar\" classname=\"org.netbeans.nbbuild.JarWithModuleAttributes\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
63             "<target name=\"all\" >" +
64             " <mkdir dir='" + output + "' />" +
65             " <property name='javac.target' value='2.87'/>" +
66             " <property name='public.packages' value=''/>" +
67             " <property name='buildnumber' value='BLDprivateTESTBuild'/>" +
68             " <property name='code.name.base.slashes' value='org/netbeans/modules/sendopts'/>" +
69             " <njar manifest='" + manifest + "' destfile='" + jar + "'>" +
70             " </njar>" +
71             " <unzip SRC='" + jar + "' dest='" + output + "'/>" +
72             "</target>" +
73             "</project>"
74         );
75
76
77         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { "-verbose" });
78         
79         assertTrue ("JAR created", jar.isFile());
80
81         File JavaDoc extracted = new File JavaDoc(new File JavaDoc(output, "META-INF"), "MANIFEST.MF");
82         assertTrue("Manifest extracted", extracted.isFile());
83
84         JarFile JavaDoc file = new JarFile JavaDoc(jar);
85         String JavaDoc value = file.getManifest().getMainAttributes().getValue("OpenIDE-Module-Java-Dependencies");
86         assertNotNull("Attribute created:\n" + PublicPackagesInProjectizedXMLTest.readFile(extracted), value);
87
88         String JavaDoc[] arr = value.split(">");
89         assertEquals("Two parts", 2, arr.length);
90         assertEquals("Java", arr[0].trim());
91
92         assertVersionAtLeast("2.87", arr[1]);
93     }
94
95     public void testKeepOldVersion() throws Exception JavaDoc {
96         File JavaDoc output = new File JavaDoc(getWorkDir(), "output");
97         java.io.File JavaDoc manifest = PublicPackagesInProjectizedXMLTest.extractString (
98 "OpenIDE-Module: org.netbeans.modules.sendopts\n" +
99 "OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/sendopts/Bundle.properties\n" +
100 "OpenIDE-Module-Specification-Version: 1.9\n" +
101 "OpenIDE-Module-Java-Dependencies: Java > 1.3\n" +
102 "OpenIDE-Module-Layer: org/netbeans/modules/sendopts/layer.xml\n"
103         );
104         File JavaDoc jar = new File JavaDoc(getWorkDir(), "x.jar");
105         
106         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
107             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
108             "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
109             " <taskdef name=\"njar\" classname=\"org.netbeans.nbbuild.JarWithModuleAttributes\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
110             "<target name=\"all\" >" +
111             " <mkdir dir='" + output + "' />" +
112             " <property name='javac.target' value='2.87'/>" +
113             " <property name='public.packages' value=''/>" +
114             " <property name='buildnumber' value='BLDprivateTESTBuild'/>" +
115             " <property name='code.name.base.slashes' value='org/netbeans/modules/sendopts'/>" +
116             " <njar manifest='" + manifest + "' destfile='" + jar + "'>" +
117             " </njar>" +
118             " <unzip SRC='" + jar + "' dest='" + output + "'/>" +
119             "</target>" +
120             "</project>"
121         );
122
123
124         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { "-verbose" });
125         
126         assertTrue ("JAR created", jar.isFile());
127
128         File JavaDoc extracted = new File JavaDoc(new File JavaDoc(output, "META-INF"), "MANIFEST.MF");
129         assertTrue("Manifest extracted", extracted.isFile());
130
131         JarFile JavaDoc file = new JarFile JavaDoc(jar);
132         String JavaDoc value = file.getManifest().getMainAttributes().getValue("OpenIDE-Module-Java-Dependencies");
133         assertNotNull("Attribute created:\n" + PublicPackagesInProjectizedXMLTest.readFile(extracted), value);
134
135         String JavaDoc[] arr = value.split(">");
136         assertEquals("Two parts", 2, arr.length);
137         assertEquals("Java", arr[0].trim());
138
139         assertVersionAtLeast("1.3", arr[1]);
140         try {
141             assertVersionAtLeast("1.4", arr[1]);
142         } catch (AssertionFailedError ex) {
143             // ok
144
return;
145         }
146         fail("Version shall not be 1.4 or higher, as it is specified in manifest to be 1.3: " + arr[1]);
147     }
148
149     public void testIgnoreWeirdJavacTarget() throws Exception JavaDoc {
150         File JavaDoc output = new File JavaDoc(getWorkDir(), "output");
151         java.io.File JavaDoc manifest = PublicPackagesInProjectizedXMLTest.extractString (
152 "OpenIDE-Module: org.netbeans.modules.sendopts\n" +
153 "OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/sendopts/Bundle.properties\n" +
154 "OpenIDE-Module-Specification-Version: 1.9\n" +
155 "OpenIDE-Module-Layer: org/netbeans/modules/sendopts/layer.xml\n"
156         );
157         File JavaDoc jar = new File JavaDoc(getWorkDir(), "x.jar");
158         
159         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
160             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
161             "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
162             " <taskdef name=\"njar\" classname=\"org.netbeans.nbbuild.JarWithModuleAttributes\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
163             "<target name=\"all\" >" +
164             " <mkdir dir='" + output + "' />" +
165             " <property name='javac.target' value='jsr99'/>" +
166             " <property name='public.packages' value=''/>" +
167             " <property name='buildnumber' value='BLDprivateTESTBuild'/>" +
168             " <property name='code.name.base.slashes' value='org/netbeans/modules/sendopts'/>" +
169             " <njar manifest='" + manifest + "' destfile='" + jar + "'>" +
170             " </njar>" +
171             " <unzip SRC='" + jar + "' dest='" + output + "'/>" +
172             "</target>" +
173             "</project>"
174         );
175
176
177         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { "-verbose" });
178         
179         assertTrue ("JAR created", jar.isFile());
180
181         File JavaDoc extracted = new File JavaDoc(new File JavaDoc(output, "META-INF"), "MANIFEST.MF");
182         assertTrue("Manifest extracted", extracted.isFile());
183
184         JarFile JavaDoc file = new JarFile JavaDoc(jar);
185         String JavaDoc value = file.getManifest().getMainAttributes().getValue("OpenIDE-Module-Java-Dependencies");
186         assertNull("Attribute not created:\n" + PublicPackagesInProjectizedXMLTest.readFile(extracted), value);
187     }
188     
189     private final File JavaDoc createNewJarFile () throws IOException JavaDoc {
190         int i = 0;
191         for (;;) {
192             File JavaDoc f = new File JavaDoc (this.getWorkDir(), i++ + ".jar");
193             if (!f.exists ()) return f;
194         }
195     }
196     
197     protected final File JavaDoc generateJar (String JavaDoc[] content, Manifest JavaDoc manifest) throws IOException JavaDoc {
198         File JavaDoc f = createNewJarFile ();
199         
200         JarOutputStream JavaDoc os = new JarOutputStream JavaDoc (new FileOutputStream JavaDoc (f), manifest);
201         
202         for (int i = 0; i < content.length; i++) {
203             os.putNextEntry(new JarEntry JavaDoc (content[i]));
204             os.closeEntry();
205         }
206         os.closeEntry ();
207         os.close();
208         
209         return f;
210     }
211
212     private static void assertVersionAtLeast(String JavaDoc limit, String JavaDoc value) {
213         int[] segLimit = segments(limit);
214         int[] segValue = segments(value);
215
216         for (int i = 0; i < segLimit.length && i < segValue.length; i++) {
217             if (segValue[i] < segLimit[i]) {
218                 fail("Version is younger than it should be. Expected: " + limit + " was: " + value);
219             }
220             if (segValue[i] > segLimit[i]) {
221                 return;
222             }
223         }
224     }
225
226     private static int[] segments(String JavaDoc version) {
227         String JavaDoc[] arr = version.split("\\.");
228         int[] ret = new int[arr.length];
229         for (int i = 0; i < arr.length; i++) {
230             ret[i] = Integer.parseInt(arr[i].trim());
231         }
232         return ret;
233     }
234 }
235
Popular Tags