1 19 20 package org.netbeans.nbbuild; 21 import java.io.File ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.util.Enumeration ; 25 import java.util.jar.JarEntry ; 26 import java.util.jar.JarFile ; 27 import java.util.jar.JarOutputStream ; 28 import java.util.jar.Manifest ; 29 import java.util.regex.Matcher ; 30 import java.util.regex.Pattern ; 31 import junit.framework.AssertionFailedError; 32 33 import org.netbeans.junit.*; 34 35 36 40 public class JarWithModuleAttributesTest extends NbTestCase { 41 public JarWithModuleAttributesTest (String name) { 42 super (name); 43 } 44 45 protected void setUp() throws Exception { 46 clearWorkDir(); 47 } 48 49 public void testAddThereVersionFromJavacTarget() throws Exception { 50 File output = new File (getWorkDir(), "output"); 51 java.io.File 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 jar = new File (getWorkDir(), "x.jar"); 58 59 java.io.File 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 [] { "-verbose" }); 78 79 assertTrue ("JAR created", jar.isFile()); 80 81 File extracted = new File (new File (output, "META-INF"), "MANIFEST.MF"); 82 assertTrue("Manifest extracted", extracted.isFile()); 83 84 JarFile file = new JarFile (jar); 85 String value = file.getManifest().getMainAttributes().getValue("OpenIDE-Module-Java-Dependencies"); 86 assertNotNull("Attribute created:\n" + PublicPackagesInProjectizedXMLTest.readFile(extracted), value); 87 88 String [] 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 { 96 File output = new File (getWorkDir(), "output"); 97 java.io.File 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 jar = new File (getWorkDir(), "x.jar"); 105 106 java.io.File 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 [] { "-verbose" }); 125 126 assertTrue ("JAR created", jar.isFile()); 127 128 File extracted = new File (new File (output, "META-INF"), "MANIFEST.MF"); 129 assertTrue("Manifest extracted", extracted.isFile()); 130 131 JarFile file = new JarFile (jar); 132 String value = file.getManifest().getMainAttributes().getValue("OpenIDE-Module-Java-Dependencies"); 133 assertNotNull("Attribute created:\n" + PublicPackagesInProjectizedXMLTest.readFile(extracted), value); 134 135 String [] 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 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 { 150 File output = new File (getWorkDir(), "output"); 151 java.io.File 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 jar = new File (getWorkDir(), "x.jar"); 158 159 java.io.File 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 [] { "-verbose" }); 178 179 assertTrue ("JAR created", jar.isFile()); 180 181 File extracted = new File (new File (output, "META-INF"), "MANIFEST.MF"); 182 assertTrue("Manifest extracted", extracted.isFile()); 183 184 JarFile file = new JarFile (jar); 185 String 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 createNewJarFile () throws IOException { 190 int i = 0; 191 for (;;) { 192 File f = new File (this.getWorkDir(), i++ + ".jar"); 193 if (!f.exists ()) return f; 194 } 195 } 196 197 protected final File generateJar (String [] content, Manifest manifest) throws IOException { 198 File f = createNewJarFile (); 199 200 JarOutputStream os = new JarOutputStream (new FileOutputStream (f), manifest); 201 202 for (int i = 0; i < content.length; i++) { 203 os.putNextEntry(new JarEntry (content[i])); 204 os.closeEntry(); 205 } 206 os.closeEntry (); 207 os.close(); 208 209 return f; 210 } 211 212 private static void assertVersionAtLeast(String limit, String 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 version) { 227 String [] 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 |