1 19 20 package org.netbeans.modules.autoupdate; 21 22 import java.util.*; 23 24 import org.netbeans.junit.*; 26 27 import java.beans.*; 28 import java.io.File ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.util.jar.*; 32 import java.util.zip.ZipEntry ; 33 import java.util.zip.ZipOutputStream ; 34 import org.openide.util.NbBundle; 35 import org.openide.util.lookup.AbstractLookup; 36 import org.openide.util.lookup.InstanceContent; 37 38 39 public abstract class AbstractTestHid extends NbTestCase { 40 protected File userDir, platformDir, clusterDir, nextDir, otherDir; 41 42 public AbstractTestHid (String name) { 43 super (name); 44 } 45 46 protected void setUp () throws Exception { 47 System.setProperty("org.openide.util.Lookup", Lkp.class.getName()); 48 49 super.setUp (); 50 51 this.clearWorkDir(); 53 54 userDir = new File (getWorkDir(), "user"); 55 assertTrue (userDir.mkdirs ()); 56 platformDir = new File (getWorkDir(), "platform"); 57 assertTrue (platformDir.mkdirs ()); 58 clusterDir = new File (getWorkDir (), "clstr"); 59 assertTrue (clusterDir.mkdirs ()); 60 nextDir = new File (getWorkDir (), "nxtclstr"); 61 assertTrue (nextDir.mkdirs ()); 62 otherDir = new File (getWorkDir (), "other1"); 63 64 System.setProperty("netbeans.dirs", ""); 65 System.setProperty("netbeans.home", platformDir.toString ()); 66 assertEquals (platformDir.toString (), System.getProperty ("netbeans.home")); 67 System.setProperty("netbeans.user", userDir.toString ()); 68 assertEquals (userDir.toString (), System.getProperty ("netbeans.user")); 69 } 70 71 protected final void initClusters () { 72 System.setProperty( 73 "netbeans.dirs", 74 clusterDir.toString() + File.pathSeparatorChar + nextDir.toString () + File.pathSeparatorChar + otherDir.toString () 75 ); 76 } 77 78 82 private final File createNewNBMFile () throws IOException { 83 int i = 0; 84 for (;;) { 85 File f = new File (this.getWorkDir(), this.getName() + i++ + ".nbm"); 86 if (!f.exists ()) return f; 87 } 88 } 89 90 protected final File generateNBM(String [] content, String info) throws IOException { 91 File f = createNewNBMFile (); 92 93 ZipOutputStream os = new ZipOutputStream (new FileOutputStream (f)); 94 95 for (int i = 0; i < content.length; i++) { 96 os.putNextEntry(new ZipEntry (content[i])); 97 os.closeEntry(); 98 } 99 os.putNextEntry (new ZipEntry ("Info/info.xml")); 100 os.write (info.getBytes()); 101 os.closeEntry (); 102 os.close(); 103 104 return f; 105 } 106 107 protected final File generateNBM(java.util.jar.Manifest [] content, String info) throws IOException { 108 File f = createNewNBMFile (); 109 110 ZipOutputStream os = new ZipOutputStream (new FileOutputStream (f)); 111 112 for (int i = 0; i < content.length; i++) { 113 ZipEntry entry = new ZipEntry (content[i].getMainAttributes ().getValue ("name")); 114 os.putNextEntry(entry); 115 java.io.ByteArrayOutputStream array = new java.io.ByteArrayOutputStream (); 116 java.util.jar.JarOutputStream jar = new java.util.jar.JarOutputStream (array, content[i]); 117 jar.close (); 118 os.write (array.toByteArray ()); 119 os.closeEntry(); 120 } 121 os.putNextEntry (new ZipEntry ("Info/info.xml")); 122 os.write (info.getBytes()); 123 os.closeEntry (); 124 os.close(); 125 126 return f; 127 } 128 129 protected final static void installNBM (File nbm) { 130 installNBMs (new File [] { nbm }); 131 } 132 133 protected final static void installNBMs (File [] nbms) { 134 class L implements PropertyChangeListener { 135 private PropertyChangeEvent ev; 136 public synchronized void propertyChange (PropertyChangeEvent ev) { 137 if ("FINISHED".equals (ev.getPropertyName())) { 138 this.ev = ev; 139 notify (); 140 } 141 } 142 143 public PropertyChangeEvent waitForEvent () { 144 while (ev == null) { 145 try { 146 wait (); 147 } catch (InterruptedException ex) { 148 fail ("No exceptions expected"); 149 } 150 } 151 return ev; 152 } 153 } 154 155 L listener = new L (); 156 synchronized (listener) { 157 org.netbeans.updater.UpdaterFrame.runFromIDE(nbms, listener, NbBundle.getBranding (), true); 158 listener.waitForEvent (); 159 } 160 } 161 162 165 protected final static Map findFiles (File dir) { 166 HashMap m = new HashMap (); 167 files (m, dir, null); 168 return m; 169 } 170 171 private static void files (HashMap m, File dir, String prefix) { 172 File [] files = dir.listFiles(); 173 for (int i = 0; i < files.length; i++) { 174 String name = prefix == null ? files[i].getName () : prefix + "/" + files[i].getName(); 175 if (files[i].isDirectory()) { 176 files (m, files[i], name); 177 } else { 178 m.put (name, files[i]); 179 } 180 } 181 } 182 183 185 protected final static void download (ModuleUpdate nbm) throws IOException { 186 boolean res = Downloader.downloadFromLocal (nbm); 187 JarFile jf = new JarFile (Downloader.getNBM (nbm)); 188 Enumeration en = jf.entries (); 189 while (en.hasMoreElements ()) { 190 JarEntry entry = (JarEntry) en.nextElement (); 191 SignVerifier.processJarEntry (entry, nbm); 192 } 193 jf.close (); 194 assertTrue ("Download ok for " + nbm.getDistributionFile (), res); 195 } 196 197 198 public static final class Lkp extends AbstractLookup { 199 static InstanceContent ic = new InstanceContent(); 200 201 public Lkp() { 202 super(ic); 203 } 204 } 205 } 206 | Popular Tags |