1 19 20 package org.netbeans.nbbuild; 21 import java.io.File ; 22 import java.io.FileOutputStream ; 23 import java.io.FileWriter ; 24 import java.io.IOException ; 25 import java.util.Arrays ; 26 import java.util.Enumeration ; 27 import java.util.HashSet ; 28 import java.util.Properties ; 29 import java.util.jar.JarEntry ; 30 import java.util.jar.JarFile ; 31 import java.util.jar.JarOutputStream ; 32 import java.util.jar.Manifest ; 33 import java.util.regex.Matcher ; 34 import java.util.regex.Pattern ; 35 import org.netbeans.junit.*; 36 import org.netbeans.nbbuild.PublicPackagesInProjectizedXMLTest.ExecutionError; 37 38 39 43 public class MakeNBMTest extends NbTestCase { 44 public MakeNBMTest (String name) { 45 super (name); 46 } 47 48 protected void setUp() throws Exception { 49 clearWorkDir(); 50 } 51 52 53 public void testGenerateNBMForSimpleModule() throws Exception { 54 Manifest m; 55 56 m = ModuleDependenciesTest.createManifest (); 57 m.getMainAttributes ().putValue ("OpenIDE-Module", "org.my.module/3"); 58 File simpleJar = generateJar (new String [0], m); 59 60 File parent = simpleJar.getParentFile (); 61 File output = new File (parent, "output"); 62 File ks = genereteKeystore("nbm", "netbeans-test"); 63 if (ks == null) { 64 return; 65 } 66 67 File ut = new File (new File (getWorkDir(), "update_tracking"), "org-my-module.xml"); 68 ut.getParentFile().mkdirs(); 69 FileWriter w = new FileWriter (ut); 70 String UTfile = 71 "<?xml version='1.0' encoding='UTF-8'?>" + 72 "<module codename='org.netbeans.modules.autoupdate/1'>" + 73 " <module_version install_time='1136503038669' last='true' origin='installer' specification_version='2.16.1'>" + 74 " <file crc='3405032071' name='modules/" + simpleJar.getName() + "'/>" + 75 " </module_version>" + 76 "</module>"; 77 w.write(UTfile); 78 w.close(); 79 80 java.io.File f = PublicPackagesInProjectizedXMLTest.extractString ( 81 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 82 "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" + 83 " <taskdef name=\"makenbm\" classname=\"org.netbeans.nbbuild.MakeNBM\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" + 84 "<target name=\"all\" >" + 85 " <mkdir dir='" + output + "' />" + 86 " <makenbm file='" + output + "/x.nbm'" + 87 " productdir='" + getWorkDir() + "'" + 88 " module='modules/" + simpleJar.getName() + "'" + 89 " homepage='http://www.homepage.org'" + 90 " distribution='distro'" + 91 " needsrestart='false'" + 92 " global='false'" + 93 " releasedate='today'" + 94 " moduleauthor='test'>" + 95 " <license file='" + simpleJar + "'/>" + 96 " <signature keystore='" + ks + "' storepass='netbeans-test' alias='nbm'/>" + 97 " </makenbm>" + 98 "</target>" + 100 "</project>" 101 ); 102 PublicPackagesInProjectizedXMLTest.execute (f, new String [] { "-verbose" }); 103 104 assertTrue ("Output exists", output.exists ()); 105 assertTrue ("Output directory created", output.isDirectory()); 106 107 String [] files = output.list(); 108 assertEquals("It has the nbm file", 1, files.length); 109 110 if (!files[0].endsWith("x.nbm")) { 111 fail("Not the right one: " + files[0]); 112 } 113 114 long time = output.listFiles()[0].lastModified(); 115 116 Thread.sleep(1300); 119 120 PublicPackagesInProjectizedXMLTest.execute (f, new String [] { "-debug", "-Ddo.fail=true"}); 122 123 long newTime = output.listFiles()[0].lastModified(); 124 125 assertEquals("The file has not been modified:\n" + PublicPackagesInProjectizedXMLTest.getStdOut(), time, newTime); 126 127 128 CHECK_SIGNED: { 129 File jar = output.listFiles()[0]; 130 JarFile signed = new JarFile (jar); 131 Enumeration it = signed.entries(); 132 while (it.hasMoreElements()) { 133 JarEntry entry = (JarEntry )it.nextElement(); 134 if (entry.getName().endsWith(".SF")) { 135 break CHECK_SIGNED; 136 } 137 } 138 fail ("File does not seem to be signed: " + jar); 139 } 140 141 } 142 143 private final File createNewJarFile (String prefix) throws IOException { 144 if (prefix == null) { 145 prefix = "modules"; 146 } 147 String ss = prefix.substring(prefix.length()-1, prefix.length()); 148 149 File dir = new File (this.getWorkDir(), prefix); 150 dir.mkdirs(); 151 152 int i = 0; 153 for (;;) { 154 File f = new File (dir, ss + i++ + ".jar"); 155 if (!f.exists ()) return f; 156 } 157 } 158 159 protected final File generateJar (String [] content, Manifest manifest) throws IOException { 160 return generateJar(null, content, manifest, null); 161 } 162 163 protected final File generateJar (String prefix, String [] content, Manifest manifest, Properties props) throws IOException { 164 File f = createNewJarFile (prefix); 165 166 if (props != null) { 167 manifest.getMainAttributes().putValue("OpenIDE-Module-Localizing-Bundle", "some/fake/prop/name/Bundle.properties"); 168 } 169 170 JarOutputStream os = new JarOutputStream (new FileOutputStream (f), manifest); 171 172 if (props != null) { 173 os.putNextEntry(new JarEntry ("some/fake/prop/name/Bundle.properties")); 174 props.store(os, "# properties for the module"); 175 os.closeEntry(); 176 } 177 178 179 for (int i = 0; i < content.length; i++) { 180 os.putNextEntry(new JarEntry (content[i])); 181 os.closeEntry(); 182 } 183 os.closeEntry (); 184 os.close(); 185 186 return f; 187 } 188 189 private final File genereteKeystore(String alias, String password) throws Exception { 190 File where = new File (getWorkDir(), "key.ks"); 191 192 String script = 193 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 194 "<project name=\"Generate Keystore\" basedir=\".\" default=\"all\" >" + 195 "<target name=\"all\" >" + 196 "<genkey \n" + 197 "alias='" + alias + "' \n" + 198 "keystore='" + where + "' \n" + 199 "storepass='" + password + "' \n" + 200 "dname='CN=A NetBeans Friend, OU=NetBeans, O=netbeans.org, C=US' \n" + 201 "/>\n" + 202 "</target></project>\n"; 203 204 java.io.File f = PublicPackagesInProjectizedXMLTest.extractString (script); 205 try { 206 PublicPackagesInProjectizedXMLTest.execute (f, new String [] { }); 207 } catch (ExecutionError err) { 208 if (err.getMessage().indexOf("java.security.ProviderException") != -1) { 209 return null; 217 } 218 } 219 220 return where; 221 } 222 } 223 | Popular Tags |