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 32 import org.netbeans.junit.*; 33 34 35 39 public class MakeMasterJNLPTest extends NbTestCase { 40 public MakeMasterJNLPTest (String name) { 41 super (name); 42 } 43 44 protected void setUp() throws Exception { 45 clearWorkDir(); 46 } 47 48 49 public void testGenerateRefenrenceFilesOnce() throws Exception { 50 doGenerateRefenrenceFiles(1); 51 } 52 public void testGenerateRefenrenceFilesThree() throws Exception { 53 doGenerateRefenrenceFiles(3); 54 } 55 56 private void doGenerateRefenrenceFiles(int cnt) throws Exception { 57 Manifest m; 58 59 m = ModuleDependenciesTest.createManifest (); 60 m.getMainAttributes ().putValue ("OpenIDE-Module", "org.my.module/3"); 61 File simpleJar = generateJar (new String [0], m); 62 63 m = ModuleDependenciesTest.createManifest (); 64 m.getMainAttributes ().putValue ("OpenIDE-Module", "org.second.module/3"); 65 File secondJar = generateJar (new String [0], m); 66 67 File parent = simpleJar.getParentFile (); 68 assertEquals("They are in the same folder", parent, secondJar.getParentFile()); 69 70 File output = new File (parent, "output"); 71 72 java.io.File f = PublicPackagesInProjectizedXMLTest.extractString ( 73 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 74 "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" + 75 " <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeMasterJNLP\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" + 76 "<target name=\"all\" >" + 77 " <mkdir dir='" + output + "' />" + 78 " <jnlp dir='" + output + "' >" + 79 " <modules dir='" + parent + "' >" + 80 " <include name='" + simpleJar.getName() + "' />" + 81 " <include name='" + secondJar.getName() + "' />" + 82 " </modules>" + 83 " </jnlp>" + 84 "</target>" + 85 "</project>" 86 ); 87 while (cnt-- > 0) { 88 PublicPackagesInProjectizedXMLTest.execute (f, new String [] { "-verbose" }); 89 } 90 91 assertTrue ("Output exists", output.exists ()); 92 assertTrue ("Output directory created", output.isDirectory()); 93 94 String [] files = output.list(); 95 assertEquals("It has two files", 2, files.length); 96 97 java.util.Arrays.sort(files); 98 99 assertEquals("The res1 file: " + files[0], "org-my-module.ref", files[0]); 100 assertEquals("The res2 file: "+ files[1], "org-second-module.ref", files[1]); 101 102 File r1 = new File (output, "org-my-module.ref"); 103 String res1 = ModuleDependenciesTest.readFile (r1); 104 105 File r2 = new File (output, "org-second-module.ref"); 106 String res2 = ModuleDependenciesTest.readFile (r2); 107 108 assertExt(res1, "org.my.module"); 109 assertExt(res2, "org.second.module"); 110 } 111 112 private static void assertExt(String res, String module) { 113 int ext = res.indexOf("<extension"); 114 if (ext == -1) { 115 fail ("<extension tag shall start there: " + res); 116 } 117 118 assertEquals("Just one extension tag", -1, res.indexOf("<extension", ext + 1)); 119 120 int cnb = res.indexOf(module); 121 if (cnb == -1) { 122 fail("Cnb has to be there: " + module + " but is " + res); 123 } 124 assertEquals("Just one cnb", -1, res.indexOf(module, cnb + 1)); 125 126 String dashcnb = module.replace('.', '-'); 127 128 int dcnb = res.indexOf(dashcnb); 129 if (dcnb == -1) { 130 fail("Dash Cnb has to be there: " + dashcnb + " but is " + res); 131 } 132 assertEquals("Just one dash cnb", -1, res.indexOf(dashcnb, dcnb + 1)); 133 } 134 135 private final File createNewJarFile () throws IOException { 136 int i = 0; 137 for (;;) { 138 File f = new File (this.getWorkDir(), i++ + ".jar"); 139 if (!f.exists ()) return f; 140 } 141 } 142 143 protected final File generateJar (String [] content, Manifest manifest) throws IOException { 144 File f = createNewJarFile (); 145 146 JarOutputStream os = new JarOutputStream (new FileOutputStream (f), manifest); 147 148 for (int i = 0; i < content.length; i++) { 149 os.putNextEntry(new JarEntry (content[i])); 150 os.closeEntry(); 151 } 152 os.closeEntry (); 153 os.close(); 154 155 return f; 156 } 157 158 } 159 | Popular Tags |