KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > autoupdate > AbstractTestHid


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.autoupdate;
21
22 import java.util.*;
23
24 //import junit.framework.*;
25
import org.netbeans.junit.*;
26
27 import java.beans.*;
28 import java.io.File JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.util.jar.*;
32 import java.util.zip.ZipEntry JavaDoc;
33 import java.util.zip.ZipOutputStream JavaDoc;
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 JavaDoc userDir, platformDir, clusterDir, nextDir, otherDir;
41     
42     public AbstractTestHid (String JavaDoc name) {
43         super (name);
44     }
45     
46     protected void setUp () throws Exception JavaDoc {
47         System.setProperty("org.openide.util.Lookup", Lkp.class.getName());
48         
49         super.setUp ();
50         
51         // clear directories first
52
this.clearWorkDir();
53         
54         userDir = new File JavaDoc (getWorkDir(), "user");
55         assertTrue (userDir.mkdirs ());
56         platformDir = new File JavaDoc (getWorkDir(), "platform");
57         assertTrue (platformDir.mkdirs ());
58         clusterDir = new File JavaDoc (getWorkDir (), "clstr");
59         assertTrue (clusterDir.mkdirs ());
60         nextDir = new File JavaDoc (getWorkDir (), "nxtclstr");
61         assertTrue (nextDir.mkdirs ());
62         otherDir = new File JavaDoc (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     //
79
// Utilities
80
//
81

82     private final File JavaDoc createNewNBMFile () throws IOException JavaDoc {
83         int i = 0;
84         for (;;) {
85             File JavaDoc f = new File JavaDoc (this.getWorkDir(), this.getName() + i++ + ".nbm");
86             if (!f.exists ()) return f;
87         }
88     }
89     
90     protected final File JavaDoc generateNBM(String JavaDoc[] content, String JavaDoc info) throws IOException JavaDoc {
91         File JavaDoc f = createNewNBMFile ();
92         
93         ZipOutputStream JavaDoc os = new ZipOutputStream JavaDoc (new FileOutputStream JavaDoc (f));
94         
95         for (int i = 0; i < content.length; i++) {
96             os.putNextEntry(new ZipEntry JavaDoc (content[i]));
97             os.closeEntry();
98         }
99         os.putNextEntry (new ZipEntry JavaDoc ("Info/info.xml"));
100         os.write (info.getBytes());
101         os.closeEntry ();
102         os.close();
103         
104         return f;
105     }
106     
107     protected final File JavaDoc generateNBM(java.util.jar.Manifest JavaDoc[] content, String JavaDoc info) throws IOException JavaDoc {
108         File JavaDoc f = createNewNBMFile ();
109         
110         ZipOutputStream JavaDoc os = new ZipOutputStream JavaDoc (new FileOutputStream JavaDoc (f));
111         
112         for (int i = 0; i < content.length; i++) {
113             ZipEntry JavaDoc entry = new ZipEntry JavaDoc (content[i].getMainAttributes ().getValue ("name"));
114             os.putNextEntry(entry);
115             java.io.ByteArrayOutputStream JavaDoc array = new java.io.ByteArrayOutputStream JavaDoc ();
116             java.util.jar.JarOutputStream JavaDoc jar = new java.util.jar.JarOutputStream JavaDoc (array, content[i]);
117             jar.close ();
118             os.write (array.toByteArray ());
119             os.closeEntry();
120         }
121         os.putNextEntry (new ZipEntry JavaDoc ("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 JavaDoc nbm) {
130         installNBMs (new File JavaDoc[] { nbm });
131     }
132     
133     protected final static void installNBMs (File JavaDoc[] 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 JavaDoc 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     /** Scans files in user dir
163      * @return Map<String, File>
164      */

165     protected final static Map findFiles (File JavaDoc dir) {
166         HashMap m = new HashMap ();
167         files (m, dir, null);
168         return m;
169     }
170     
171     private static void files (HashMap m, File JavaDoc dir, String JavaDoc prefix) {
172         File JavaDoc[] files = dir.listFiles();
173         for (int i = 0; i < files.length; i++) {
174             String JavaDoc 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     /** Simulates download of the NBM file.
184      */

185     protected final static void download (ModuleUpdate nbm) throws IOException JavaDoc {
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