1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.FileWriter ; 25 import java.io.IOException ; 26 import java.util.Arrays ; 27 import java.util.Enumeration ; 28 import java.util.Properties ; 29 import java.util.Random ; 30 import java.util.Set ; 31 import java.util.SortedSet ; 32 import java.util.TreeSet ; 33 import java.util.jar.JarEntry ; 34 import java.util.jar.JarFile ; 35 import java.util.jar.JarOutputStream ; 36 import java.util.jar.Manifest ; 37 import java.util.logging.Level ; 38 import java.util.logging.Logger ; 39 import java.util.regex.Matcher ; 40 import java.util.regex.Pattern ; 41 import org.netbeans.junit.NbTestCase; 42 43 47 public class MakeJNLPTest extends NbTestCase { 48 public MakeJNLPTest (String name) { 49 super (name); 50 } 51 52 protected void setUp() throws Exception { 53 clearWorkDir(); 54 } 55 56 private static void assertFilenames(File dir, String ... contents) { 57 assertTrue(dir + " is a directory", dir.isDirectory()); 58 SortedSet <String > expected = new TreeSet <String >(Arrays.asList(contents)); 59 SortedSet <String > actual = new TreeSet <String >(); 60 findFilenames(dir, "", actual); 61 assertEquals("correct contents of " + dir, expected, actual); 62 } 63 private static void findFilenames(File dir, String prefix, Set <String > names) { 64 for (File f : dir.listFiles()) { 65 if (f.isFile()) { 66 names.add(prefix + f.getName()); 67 } else if (f.isDirectory()) { 68 findFilenames(f, prefix + f.getName() + "/", names); 69 } 70 } 71 } 72 73 public void testGenerateJNLPAndSignedJarForSimpleModule() throws Exception { 74 Manifest m; 75 76 m = ModuleDependenciesTest.createManifest (); 77 m.getMainAttributes ().putValue ("OpenIDE-Module", "org.my.module/3"); 78 File simpleJar = generateJar (new String [0], m); 79 80 File parent = simpleJar.getParentFile (); 81 File output = new File (parent, "output"); 82 File ks = generateKeystore("jnlp", "netbeans-test"); 83 84 java.io.File f = PublicPackagesInProjectizedXMLTest.extractString ( 85 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 86 "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" + 87 " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" + 88 "<target name=\"all\" >" + 89 " <mkdir dir='" + output + "' />" + 90 " <jnlp dir='" + output + "' alias='jnlp' storepass='netbeans-test' keystore='" + ks + "' >" + 91 " <modules dir='" + parent + "' >" + 92 " <include name='" + simpleJar.getName() + "' />" + 93 " </modules>" + 94 " </jnlp>" + 95 "</target>" + 96 "</project>" 97 ); 98 PublicPackagesInProjectizedXMLTest.execute (f, new String [] { "-verbose" }); 99 100 assertFilenames(output, "org-my-module.jnlp", "org-my-module/s0.jar"); 101 102 File jnlp = new File (output, "org-my-module.jnlp"); 103 String res = ModuleDependenciesTest.readFile (jnlp); 104 105 assertTrue ("Component JNLP type: " + res, res.indexOf ("<component-desc/>") >= 0); 106 assertTrue ("We support all permitions by default: " + res, res.indexOf ("<all-permissions/>") >= 0); 107 108 Matcher match = Pattern.compile(".*codebase=['\\\"]([^'\\\"]*)['\\\"]").matcher(res); 109 assertTrue("codebase is there", match.find()); 110 assertEquals("one group found", 1, match.groupCount()); 111 String base = match.group(1); 112 113 assertEquals("By default the dest directory is $$codebase: ", "$$codebase", base); 114 115 CHECK_SIGNED: { 116 File jar = new File (output, "org-my-module/s0.jar"); 117 JarFile signed = new JarFile (jar); 118 Enumeration it = signed.entries(); 119 while (it.hasMoreElements()) { 120 JarEntry entry = (JarEntry )it.nextElement(); 121 if (entry.getName().endsWith(".SF")) { 122 break CHECK_SIGNED; 123 } 124 } 125 fail ("File does not seem to be signed: " + jar); 126 } 127 } 128 129 public void testTheLocalizedAutoupdateProblem() throws Exception { 130 String UTfile = 131 "<?xml version='1.0' encoding='UTF-8'?>" + 132 "<module codename='org.netbeans.modules.autoupdate/1'>" + 133 " <module_version install_time='1136503038669' last='true' origin='installer' specification_version='2.16.1'>" + 134 " <file crc='746562502' name='config/Modules/org-netbeans-modules-autoupdate.xml'/>" + 135 " <file crc='3552349255' name='modules/ext/locale/updater_ja.jar'/>" + 136 " <file crc='72601456' name='modules/ext/locale/updater_zh_CN.jar'/>" + 137 " <file crc='3405032071' name='modules/ext/updater.jar'/>" + 138 " <file crc='2409221434' name='modules/locale/org-netbeans-modules-autoupdate_ja.jar'/>" + 139 " <file crc='1180043929' name='modules/locale/org-netbeans-modules-autoupdate_zh_CN.jar'/>" + 140 " <file crc='3477298901' name='modules/org-netbeans-modules-autoupdate.jar'/>" + 141 " </module_version>" + 142 "</module>"; 143 144 Manifest m = ModuleDependenciesTest.createManifest (); 145 m.getMainAttributes ().putValue ("OpenIDE-Module", "org.netbeans.modules.autoupdate/1"); 146 m.getMainAttributes ().putValue ("Class-Path", "ext/updater.jar"); 147 File simpleJar = generateJar ("modules/", new String [0], m, null); 148 File moduleJar = new File (simpleJar.getParentFile(), "org-netbeans-modules-autoupdate.jar"); 149 simpleJar.renameTo(moduleJar); 150 151 File p = simpleJar.getParentFile(); 152 153 simpleJar = generateJar ("modules/locale/", new String [0], ModuleDependenciesTest.createManifest(), null); 154 simpleJar.renameTo(new File (simpleJar.getParentFile(), "org-netbeans-modules-autoupdate_ja.jar")); 155 156 simpleJar = generateJar ("modules/locale/", new String [0], ModuleDependenciesTest.createManifest(), null); 157 simpleJar.renameTo(new File (simpleJar.getParentFile(), "org-netbeans-modules-autoupdate_zh_CN.jar")); 158 159 simpleJar = generateJar ("modules/ext/", new String [0], ModuleDependenciesTest.createManifest(), null); 160 simpleJar.renameTo(new File (simpleJar.getParentFile(), "updater.jar")); 161 162 simpleJar = generateJar ("modules/ext/locale/", new String [0], ModuleDependenciesTest.createManifest(), null); 163 simpleJar.renameTo(new File (simpleJar.getParentFile(), "updater_ja.jar")); 164 165 simpleJar = generateJar ("modules/ext/locale/", new String [0], ModuleDependenciesTest.createManifest(), null); 166 simpleJar.renameTo(new File (simpleJar.getParentFile(), "updater_zh_CN.jar")); 167 168 File xml = new File (p, "config/Modules/org-netbeans-modules-autoupdate.xml"); 169 xml.getParentFile().mkdirs(); 170 xml.createNewFile(); 171 172 File updateTracking = new File (getWorkDir(), "update_tracking"); 173 updateTracking.mkdirs(); 174 assertTrue("Created", updateTracking.isDirectory()); 175 176 File trackingFile = new File (updateTracking, "org-netbeans-modules-autoupdate.xml"); 177 FileWriter w = new FileWriter (trackingFile); 178 w.write(UTfile); 179 w.close(); 180 181 File output = new File (getWorkDir(), "output"); 182 File ks = generateKeystore("jnlp", "netbeans-test"); 183 184 java.io.File f = PublicPackagesInProjectizedXMLTest.extractString ( 185 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 186 "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" + 187 " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" + 188 "<target name=\"all\" >" + 189 " <mkdir dir='" + output + "' />" + 190 " <jnlp dir='" + output + "' alias='jnlp' storepass='netbeans-test' keystore='" + ks + "' verify='true' >" + 191 " <modules dir='" + p + "' >" + 192 " <include name='" + moduleJar.getName() + "' />" + 193 " </modules>" + 194 " </jnlp>" + 195 "</target>" + 196 "</project>" 197 ); 198 PublicPackagesInProjectizedXMLTest.execute (f, new String [] { "-verbose" }); 199 200 assertFilenames(output, "org-netbeans-modules-autoupdate.jnlp", 201 "org-netbeans-modules-autoupdate/org-netbeans-modules-autoupdate.jar", 202 "org-netbeans-modules-autoupdate/locale-org-netbeans-modules-autoupdate_zh_CN.jar", 203 "org-netbeans-modules-autoupdate/locale-org-netbeans-modules-autoupdate_ja.jar", 204 "org-netbeans-modules-autoupdate/ext-locale-updater_zh_CN.jar", 205 "org-netbeans-modules-autoupdate/ext-locale-updater_ja.jar", 206 "org-netbeans-modules-autoupdate/ext-updater.jar"); 207 208 File jnlp = new File (output, "org-netbeans-modules-autoupdate.jnlp"); 209 String res = ModuleDependenciesTest.readFile (jnlp); 210 211 212 assertTrue ("Component JNLP type: " + res, res.indexOf ("<component-desc/>") >= 0); 213 assertTrue ("We support all permitions by default: " + res, res.indexOf ("<all-permissions/>") >= 0); 214 215 Matcher match = Pattern.compile(".*codebase=['\\\"]([^'\\\"]*)['\\\"]").matcher(res); 216 assertTrue("codebase is there", match.find()); 217 assertEquals("one group found", 1, match.groupCount()); 218 String base = match.group(1); 219 220 assertEquals("By default the dest directory is $$codebase: ", "$$codebase", base); 221 222 assertResource(res, "ja", "org-netbeans-modules-autoupdate/ext-locale-updater_ja.jar"); 223 assertResource(res, "zh_CN", "org-netbeans-modules-autoupdate/ext-locale-updater_zh_CN.jar"); 224 assertResource(res, "ja", "org-netbeans-modules-autoupdate/locale-org-netbeans-modules-autoupdate_ja.jar"); 225 assertResource(res, "zh_CN", "org-netbeans-modules-autoupdate/locale-org-netbeans-modules-autoupdate_zh_CN.jar"); 226 227 CHECK_SIGNED: for (File jar : new File (output, "org-netbeans-modules-autoupdate").listFiles()) { 228 if (!jar.getName().endsWith(".jar")) { 229 continue; 230 } 231 JarFile signed = new JarFile (jar); 232 Enumeration it = signed.entries(); 233 while (it.hasMoreElements()) { 234 JarEntry entry = (JarEntry )it.nextElement(); 235 if (entry.getName().endsWith(".SF")) { 236 continue CHECK_SIGNED; 237 } 238 } 239 fail ("File does not seem to be signed: " + jar); 240 } 241 242 } 243 244 public void testGenerateJNLPAndSignedJarForSimpleLocalizedModule() throws Exception { 245 Manifest m; 246 247 m = ModuleDependenciesTest.createManifest (); 248 m.getMainAttributes ().putValue ("OpenIDE-Module", "org.my.module/3"); 249 File simpleJar = generateJar ("modules/", new String [0], m, null); 250 251 File parent = simpleJar.getParentFile (); 252 File localizedJarCZ = generateJar("modules/locale/", new String [0], ModuleDependenciesTest.createManifest(), null); 253 assertEquals("There need to have the same name", simpleJar.getName(), localizedJarCZ.getName()); 254 assertTrue("Successful rename", localizedJarCZ.renameTo(new File (localizedJarCZ.getParent(), "0_cs.jar"))); 255 256 File localizedJarZH = generateJar("modules/locale/", new String [0], ModuleDependenciesTest.createManifest(), null); 257 assertEquals("There need to have the same name", simpleJar.getName(), localizedJarZH.getName()); 258 assertTrue("Successful rename", localizedJarZH.renameTo(new File (localizedJarCZ.getParent(), "0_zh_CN.jar"))); 259 260 File localizedJarJA = generateJar("modules/locale/", new String [0], ModuleDependenciesTest.createManifest(), null); 261 assertEquals("There need to have the same name", simpleJar.getName(), localizedJarJA.getName()); 262 assertTrue("Successful rename", localizedJarJA.renameTo(new File (localizedJarCZ.getParent(), "0_ja.jar"))); 263 264 File updateTracking = new File (getWorkDir(), "update_tracking"); 265 updateTracking.mkdirs(); 266 assertTrue("Created", updateTracking.isDirectory()); 267 268 File trackingFile = new File (updateTracking, "org-my-module.xml"); 269 FileWriter w = new FileWriter (trackingFile); 270 w.write( 271 "<?xml version='1.0' encoding='UTF-8'?>\n" + 272 "<module codename='org.my.module/3'>\n" + 273 "<module_version specification_version='3.22' origin='installer' last='true' install_time='1124194231878'>\n" + 274 "<file name='modules/" + simpleJar.getName() + "' crc='3245456472'/>\n" + 275 "<file name='config/Modules/org-my-module.xml' crc='43434' />\n" + 276 "<file name='modules/locale/0_cs.jar' crc='454244' />\n" + 277 "<file name='modules/locale/0_ja.jar' crc='779831' />\n" + 278 "<file name='modules/locale/0_zh_CN.jar' crc='475345' />\n" + 279 " </module_version>\n" + 280 "</module>\n" 281 ); 282 w.close(); 283 284 285 File output = new File (parent, "output"); 286 File ks = generateKeystore("jnlp", "netbeans-test"); 287 288 java.io.File f = PublicPackagesInProjectizedXMLTest.extractString ( 289 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 290 "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" + 291 " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" + 292 "<target name=\"all\" >" + 293 " <mkdir dir='" + output + "' />" + 294 " <jnlp dir='" + output + "' alias='jnlp' storepass='netbeans-test' keystore='" + ks + "' verify='true' >" + 295 " <modules dir='" + parent + "' >" + 296 " <include name='" + simpleJar.getName() + "' />" + 297 " </modules>" + 298 " </jnlp>" + 299 "</target>" + 300 "</project>" 301 ); 302 PublicPackagesInProjectizedXMLTest.execute (f, new String [] { "-verbose" }); 303 304 assertFilenames(output, "org-my-module.jnlp", 305 "org-my-module/0.jar", 306 "org-my-module/locale-0_cs.jar", 307 "org-my-module/locale-0_zh_CN.jar", 308 "org-my-module/locale-0_ja.jar"); 309 310 File jnlp = new File (output, "org-my-module.jnlp"); 311 String res = ModuleDependenciesTest.readFile (jnlp); 312 313 assertTrue ("Component JNLP type: " + res, res.indexOf ("<component-desc/>") >= 0); 314 assertTrue ("We support all permissions by default: " + res, res.indexOf ("<all-permissions/>") >= 0); 315 316 Matcher match = Pattern.compile(".*codebase=['\\\"]([^'\\\"]*)['\\\"]").matcher(res); 317 assertTrue("codebase is there", match.find()); 318 assertEquals("one group found", 1, match.groupCount()); 319 String base = match.group(1); 320 321 assertEquals("By default the dest directory is $$codebase: ", "$$codebase", base); 322 323 assertResource(res, "cs", "org-my-module/locale-0_cs.jar"); 324 assertResource(res, "ja", "org-my-module/locale-0_ja.jar"); 325 assertResource(res, "zh_CN", "org-my-module/locale-0_zh_CN.jar"); 326 327 CHECK_SIGNED: for (File jar : new File (output, "org-my-module").listFiles()) { 328 if (!jar.getName().endsWith(".jar")) { 329 continue; 330 } 331 332 JarFile signed = new JarFile (jar); 333 Enumeration it = signed.entries(); 334 while (it.hasMoreElements()) { 335 JarEntry entry = (JarEntry )it.nextElement(); 336 if (entry.getName().endsWith(".SF")) { 337 continue CHECK_SIGNED; 338 } 339 } 340 fail ("File does not seem to be signed: " + jar); 341 } 342 } 343 344 private static void assertResource(String where, String locale, String file) { 345 where = where.replace('\n', ' '); 346 Matcher match = Pattern.compile("<resources *locale='" + locale + "' *>.*<jar HREF='" + file + "' */>.*</resources>").matcher(where); 347 assertTrue("File really referenced " + file + " in locale " + locale + "\n" + where, match.find()); 348 } 349 350 public void testOneCanChangeTheCodeBase() throws Exception { 351 Manifest m; 352 353 m = ModuleDependenciesTest.createManifest (); 354 m.getMainAttributes ().putValue ("OpenIDE-Module", "org.my.module/3"); 355 File simpleJar = generateJar (new String [0], m); 356 357 File parent = simpleJar.getParentFile (); 358 File output = new File (parent, "output"); 359 File ks = generateKeystore("jnlp", "netbeans-test"); 360 361 java.io.File f = PublicPackagesInProjectizedXMLTest.extractString ( 362 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 363 "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" + 364 " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" + 365 "<target name=\"all\" >" + 366 " <mkdir dir='" + output + "' />" + 367 " <jnlp dir='" + output + "' alias='jnlp' storepass='netbeans-test' keystore='" + ks + "' codebase='http://www.my.org/' >" + 368 " <modules dir='" + parent + "' >" + 369 " <include name='" + simpleJar.getName() + "' />" + 370 " </modules>" + 371 " </jnlp>" + 372 "</target>" + 373 "</project>" 374 ); 375 PublicPackagesInProjectizedXMLTest.execute (f, new String [] { "-verbose" }); 376 377 assertFilenames(output, "org-my-module.jnlp", 378 "org-my-module/s0.jar"); 379 380 File jnlp = new File (output, "org-my-module.jnlp"); 381 String res = ModuleDependenciesTest.readFile (jnlp); 382 383 assertTrue ("Component JNLP type: " + res, res.indexOf ("<component-desc/>") >= 0); 384 assertTrue ("We support all permitions by default: " + res, res.indexOf ("<all-permissions/>") >= 0); 385 386 Matcher match = Pattern.compile(".*codebase=['\\\"]([^'\\\"]*)['\\\"]").matcher(res); 387 assertTrue("codebase is there", match.find()); 388 assertEquals("one group found", 1, match.groupCount()); 389 String base = match.group(1); 390 391 assertEquals("By default the codebases can be changed: ", "http://www.my.org/", base); 392 } 393 394 public void testGenerateJNLPAndSignedJarForModuleWithClassPath() throws Exception { 395 File output = doClassPathModuleCheck( 396 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 397 "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" + 398 " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" + 399 "<target name=\"all\" >" + 400 " <mkdir dir='${test.output}' />" + 401 " <jnlp dir='${test.output}' alias='jnlp' storepass='netbeans-test' keystore='${test.ks}' >" + 402 " <modules dir='${test.parent}' >" + 403 " <include name='${test.name}' />" + 404 " </modules>" + 405 " </jnlp>" + 406 "</target>" + 407 "</project>" 408 ); 409 410 assertFilenames(output, "aaa-my-module.jnlp", "aaa-my-module/ext-t0.jar", "aaa-my-module/s0.jar"); 411 412 File jnlp = new File (output, "aaa-my-module.jnlp"); 413 String res = ModuleDependenciesTest.readFile (jnlp); 414 415 416 Matcher m = Pattern.compile("<jar HREF='(.*)' */>").matcher(res); 417 for (int x = 0; x < 2; x++) { 418 assertTrue("Matches at least one" + "(" + x + ")", m.find()); 419 assertEquals("Found a group" + "(" + x + ")", m.groupCount(), 1); 420 File f = new File (jnlp.getParentFile(), m.group(1)); 421 assertTrue("The file " + f + " exists" + "(" + x + ")", f.exists()); 422 } 423 424 } 425 426 public void testGenerateJNLPAndSignedJarForModuleWithClassPathAndSignedJar() throws Exception { 427 File ks = generateKeystore("external", "netbeans-test"); 428 429 File output = doClassPathModuleCheck( 430 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 431 "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" + 432 " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" + 433 "<target name=\"all\" >" + 434 " <mkdir dir='${test.output}' />" + 435 " <signjar jar='${test.ext}' alias='external' storepass='netbeans-test' keystore='${test.ks}' />\n" + 436 " <jnlp dir='${test.output}' alias='jnlp' storepass='netbeans-test' keystore='${test.ks}' >" + 437 " <modules dir='${test.parent}' >" + 438 " <include name='${test.name}' />" + 439 " </modules>" + 440 " </jnlp>" + 441 "</target>" + 442 "</project>" 443 ); 444 445 assertFilenames(output, "aaa-my-module.jnlp", "aaa-my-module/s0.jar", 446 "aaa-my-module/ext-t0.jar", 447 "aaa-my-module/ext-t0.jnlp"); 448 449 JarFile f = new JarFile (new File (output, "aaa-my-module/ext-t0.jar")); 450 Enumeration en = f.entries(); 451 StringBuffer sb = new StringBuffer (); 452 int cnt = 0; 453 while (en.hasMoreElements()) { 454 JarEntry e = (JarEntry )en.nextElement(); 455 if (e.getName().endsWith("SF")) { 456 cnt++; 457 if (!e.getName().equals("META-INF/EXTERNAL.SF")) { 458 fail("Signed with wrong entity: " + e.getName()); 459 } 460 } 461 sb.append(e.getName()); 462 sb.append('\n'); 463 } 464 465 if (cnt == 0) { 466 fail("Signed with wrong file:\n" + sb); 467 } 468 469 File jnlp = new File (output, "aaa-my-module.jnlp"); 470 471 String res = ModuleDependenciesTest.readFile (jnlp); 472 473 int first = res.indexOf("jar href"); 474 assertEquals("Just one jar href ", -1, res.indexOf("jar href", first + 1)); 475 476 String extRes = ModuleDependenciesTest.readFile(new File (output, "aaa-my-module/ext-t0.jnlp")); 477 478 Matcher m = Pattern.compile("<title>(.*)</title>").matcher(extRes); 479 assertTrue("title is there: " + extRes, m.find()); 480 assertEquals("Name of file is used for title", "t0", m.group(1)); 481 } 482 483 public void testInformationIsTakenFromLocalizedBundle() throws Exception { 484 Manifest m; 485 486 m = ModuleDependenciesTest.createManifest (); 487 m.getMainAttributes ().putValue ("OpenIDE-Module", "org.my.module/3"); 488 489 Properties props = new Properties (); 490 props.put("OpenIDE-Module-Name", "Module Build Harness"); 491 props.put("OpenIDE-Module-Display-Category", "Developing NetBeans"); 492 props.put("OpenIDE-Module-Short-Description", "Lets you build external plug-in modules from sources."); 493 props.put("OpenIDE-Module-Long-Description", "XXX"); 494 495 File simpleJar = generateJar (null, new String [0], m, props); 496 497 File parent = simpleJar.getParentFile (); 498 File output = new File (parent, "output"); 499 File ks = generateKeystore("jnlp", "netbeans-test"); 500 501 java.io.File f = PublicPackagesInProjectizedXMLTest.extractString ( 502 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 503 "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" + 504 " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" + 505 "<target name=\"all\" >" + 506 " <mkdir dir='" + output + "' />" + 507 " <jnlp dir='" + output + "' alias='jnlp' storepass='netbeans-test' keystore='" + ks + "' >" + 508 " <modules dir='" + parent + "' >" + 509 " <include name='" + simpleJar.getName() + "' />" + 510 " </modules>" + 511 " </jnlp>" + 512 "</target>" + 513 "</project>" 514 ); 515 PublicPackagesInProjectizedXMLTest.execute (f, new String [] { }); 516 517 assertFilenames(output, "org-my-module.jnlp", "org-my-module/s0.jar"); 518 519 File jnlp = new File (output, "org-my-module.jnlp"); 520 String res = ModuleDependenciesTest.readFile (jnlp); 521 522 int infoBegin = res.indexOf("<information>"); 523 int infoEnd = res.indexOf("</information>"); 524 525 if (infoEnd == -1 || infoBegin == -1) { 526 fail ("Both information tags must be present: " + res); 527 } 528 529 String info = res.substring(infoBegin, infoEnd); 530 531 if (info.indexOf("<title>Module Build Harness</title>") == -1) { 532 fail("Title should be there with Module Build Harness inside itself: " + info); 533 } 534 535 if (info.indexOf("<description kind='one-line'>Lets you build external plug-in modules from sources.</description>") == -1) { 536 fail("one-line should be there with 'lets you...' inside itself: " + info); 537 } 538 539 if (info.indexOf("<description kind='short'>XXX</description>") == -1) { 540 fail("short should be there with XXX inside itself: " + info); 541 } 542 } 543 544 public void testGenerateJNLPFailsForModulesWithExtraFiles() throws Exception { 545 doCompareJNLPFileWithUpdateTracking(true, null, ""); 546 } 547 public void testGenerateJNLPSucceedsWithExtraFiles() throws Exception { 548 doCompareJNLPFileWithUpdateTracking(false, null, ""); 549 } 550 public void testGenerateJNLPSucceedsWhenExtraFileIsExcluded() throws Exception { 551 doCompareJNLPFileWithUpdateTracking(false, "lib/nbexec", " verifyexcludes=' one, lib/nbexec, three ' "); 552 } 553 public void testGenerateJNLPSucceedsWhenModuleAutoDepsArePresent() throws Exception { 554 doCompareJNLPFileWithUpdateTracking(false, "config/ModuleAutoDeps/aaa-my-module.xml", " verifyexcludes=' none ' "); 555 } 556 557 private void doCompareJNLPFileWithUpdateTracking(boolean useNonModule, String fakeEntry, String extraScript) throws Exception { 558 File nonModule = generateJar (new String [0], ModuleDependenciesTest.createManifest()); 559 560 Manifest m = ModuleDependenciesTest.createManifest (); 561 m.getMainAttributes ().putValue ("OpenIDE-Module", "aaa.my.module/3"); 562 File module = generateJar (new String [0], m); 563 564 File updateTracking = new File (getWorkDir(), "update_tracking"); 565 updateTracking.mkdirs(); 566 assertTrue("Created", updateTracking.isDirectory()); 567 568 File enableXML = new File (new File (getWorkDir(), "config"), "Modules"); 569 enableXML.getParentFile().mkdirs(); 570 enableXML.createNewFile(); 571 572 File trackingFile = new File (updateTracking, "aaa-my-module.xml"); 573 FileWriter w = new FileWriter (trackingFile); 574 w.write( 575 "<?xml version='1.0' encoding='UTF-8'?>\n" + 576 "<module codename='org.apache.tools.ant.module/3'>\n" + 577 "<module_version specification_version='3.22' origin='installer' last='true' install_time='1124194231878'>\n" + 578 (useNonModule ? ("<file name='modules/" + nonModule.getName() + "' crc='1536373800'/>\n") : "") + 579 "<file name='modules/" + module.getName() + "' crc='3245456472'/>\n" + 580 "<file name='config/Modules/aaa-my-module.xml' crc='43434' />\n" + 581 (fakeEntry != null ? "<file name='" + fakeEntry + "' crc='43222' />\n" : "") + 582 " </module_version>\n" + 583 "</module>\n" 584 ); 585 w.close(); 586 587 588 589 String script = 590 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 591 "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" + 592 " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" + 593 "<target name=\"all\" >" + 594 " <mkdir dir='${test.output}' />" + 595 " <jnlp dir='${test.output}' alias='jnlp' storepass='netbeans-test' keystore='${test.ks}' verify='true' " + extraScript + " >" + 596 " <modules dir='${test.parent}' >" + 597 " <include name='${test.name}' />" + 598 " </modules>" + 599 " </jnlp>" + 600 "</target>" + 601 "</project>"; 602 603 assertEquals("Both modules in the same dir", module.getParentFile(), nonModule.getParentFile()); 604 605 File output = new File (getWorkDir(), "output"); 606 File ks = generateKeystore("jnlp", "netbeans-test"); 607 608 java.io.File f = PublicPackagesInProjectizedXMLTest.extractString (script); 609 try { 610 PublicPackagesInProjectizedXMLTest.execute (f, new String [] { 611 "-Dtest.output=" + output, 612 "-Dtest.parent=" + module.getParent(), 613 "-Dtest.name=" + module.getName(), 614 "-Dtest.ks=" + ks, 615 }); 616 if (useNonModule) { 617 fail("The task has to fail"); 618 } 619 620 assertTrue ("Output exists", output.exists ()); 621 assertTrue ("Output directory created", output.isDirectory()); 622 623 File ext = new File (output, module.getName()); 624 625 626 String [] files = ext.getParentFile().list(); 627 assertEquals("Two files are there", 2, files.length); 628 } catch (PublicPackagesInProjectizedXMLTest.ExecutionError ex) { 629 if (!useNonModule) { 630 throw ex; 631 } else { 632 assertTrue ("Output exists", output.exists ()); 634 assertTrue ("Output directory created", output.isDirectory()); 635 636 File ext = new File (output, module.getName()); 637 638 639 String [] files = ext.getParentFile().list(); 640 assertEquals("Output dir is empty as nothing has been generated", 0, files.length); 641 } 642 } 643 644 } 645 646 private File doClassPathModuleCheck(String script) throws Exception { 647 Manifest m; 648 649 File extJar = generateJar ("modules/ext", new String [0], ModuleDependenciesTest.createManifest(), null); 650 651 m = ModuleDependenciesTest.createManifest (); 652 m.getMainAttributes ().putValue ("OpenIDE-Module", "aaa.my.module/3"); 653 m.getMainAttributes ().putValue ("Class-Path", "ext/" + extJar.getName()); 654 File simpleJar = generateJar ("modules", new String [0], m, null); 655 656 File parent = simpleJar.getParentFile (); 657 658 File output = new File (parent, "output"); 659 File ks = generateKeystore("jnlp", "netbeans-test"); 660 661 java.io.File f = PublicPackagesInProjectizedXMLTest.extractString (script); 662 PublicPackagesInProjectizedXMLTest.execute (f, new String [] { 663 "-Dtest.output=" + output, 664 "-Dtest.parent=" + parent, 665 "-Dtest.name=" + simpleJar.getName(), 666 "-Dtest.ks=" + ks, 667 "-Dtest.ext=" + extJar 668 }); 669 670 return output; 671 } 672 673 674 private final File createNewJarFile (String prefix) throws IOException { 675 if (prefix == null) { 676 prefix = "modules"; 677 } 678 String ss = prefix.substring(prefix.length()-1, prefix.length()); 679 680 File dir = new File (this.getWorkDir(), prefix); 681 dir.mkdirs(); 682 683 int i = 0; 684 for (;;) { 685 File f = new File (dir, ss + i++ + ".jar"); 686 if (!f.exists ()) return f; 687 } 688 } 689 690 protected final File generateJar (String [] content, Manifest manifest) throws IOException { 691 return generateJar(null, content, manifest, null); 692 } 693 694 protected final File generateJar (String prefix, String [] content, Manifest manifest, Properties props) throws IOException { 695 File f = createNewJarFile (prefix); 696 697 if (props != null) { 698 manifest.getMainAttributes().putValue("OpenIDE-Module-Localizing-Bundle", "some/fake/prop/name/Bundle.properties"); 699 } 700 701 JarOutputStream os = new JarOutputStream (new FileOutputStream (f), manifest); 702 703 if (props != null) { 704 os.putNextEntry(new JarEntry ("some/fake/prop/name/Bundle.properties")); 705 props.store(os, "# properties for the module"); 706 os.closeEntry(); 707 } 708 709 710 for (int i = 0; i < content.length; i++) { 711 os.putNextEntry(new JarEntry (content[i])); 712 os.closeEntry(); 713 } 714 os.closeEntry (); 715 os.close(); 716 717 return f; 718 } 719 720 private final File generateKeystore(String alias, String password) throws Exception { 721 Error lastEx = null; 722 for (int i = 0; i < 10; i++) { 723 File where = new File (getWorkDir(), "key" + i + ".ks"); 724 725 String script = 726 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 727 "<project name=\"Generate Keystore\" basedir=\".\" default=\"all\" >" + 728 "<target name=\"all\" >" + 729 "<genkey \n" + 730 "alias='" + alias + "' \n" + 731 "keystore='" + where + "' \n" + 732 "storepass='" + password + "' \n" + 733 "dname='CN=A NetBeans Friend, OU=NetBeans, O=netbeans.org, C=US' \n" + 734 "/>\n" + 735 "</target></project>\n"; 736 737 java.io.File f = PublicPackagesInProjectizedXMLTest.extractString (script); 738 try { 739 PublicPackagesInProjectizedXMLTest.execute (f, new String [] { }); 740 } catch (PublicPackagesInProjectizedXMLTest.ExecutionError ex) { 741 Logger.getAnonymousLogger().log(Level.WARNING, "Failed for " + i, ex); 742 lastEx = ex; 743 if (ex.getMessage().indexOf("CKR_KEY_SIZE_RANGE") >= 0) { 744 Thread.sleep(new Random ().nextInt(1000)); 745 continue; 746 } 747 throw ex; 748 } 749 return where; 750 } 751 throw lastEx; 752 } 753 754 public void testIndirectJars() throws Exception { 755 Manifest m = ModuleDependenciesTest.createManifest(); 756 m.getMainAttributes().putValue("OpenIDE-Module", "me"); 757 generateJar(new String [0], m); 758 generateJar("lib", new String [0], new Manifest (), null); 759 assertTrue(new File (getWorkDir(), "lib/b0.jar").isFile()); 760 File output = new File (getWorkDir(), "output"); 761 File ks = generateKeystore("jnlp", "netbeans-test"); 762 File f = PublicPackagesInProjectizedXMLTest.extractString( 763 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 764 "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" + 765 " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" + 766 "<target name=\"all\" >" + 767 " <mkdir dir='" + output + "' />" + 768 " <jnlp dir='" + output + "' alias='jnlp' storepass='netbeans-test' keystore='" + ks + "' >" + 769 " <modules dir='" + getWorkDir() + "'>" + 770 " <include name='modules/s0.jar'/>" + 771 " </modules>" + 772 " <indirectjars dir='" + getWorkDir() + "'>" + 773 " <include name='lib/b0.jar'/>" + 774 " </indirectjars>" + 775 " </jnlp>" + 776 "</target>" + 777 "</project>" 778 ); 779 PublicPackagesInProjectizedXMLTest.execute(f, new String [] { "-verbose" }); 780 assertFilenames(output, "me.jnlp", "me/s0.jar", "me/lib-b0.jar"); 781 File jnlp = new File (output, "me.jnlp"); 782 String res = ModuleDependenciesTest.readFile(jnlp); 783 assertTrue(res, res.contains("me/lib-b0.jar")); 784 JarFile otherJar = new JarFile (new File (output, "me/lib-b0.jar")); 785 assertNotNull(otherJar.getEntry("META-INF/clusterpath/lib/b0.jar")); 786 } 787 788 } 789 | Popular Tags |