KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.ref.*;
23 import java.util.*;
24
25 //import junit.framework.*;
26
import org.netbeans.junit.*;
27
28 import java.util.List JavaDoc;
29 import java.awt.Image JavaDoc;
30 import java.awt.datatransfer.Transferable JavaDoc;
31 import java.beans.*;
32 import java.io.File JavaDoc;
33 import java.io.FileOutputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.util.jar.*;
36 import java.util.jar.Manifest JavaDoc;
37 import java.util.regex.*;
38 import java.util.zip.ZipEntry JavaDoc;
39 import java.util.zip.ZipOutputStream JavaDoc;
40
41 //import org.openide.ErrorManager;
42

43
44
45 public class MissingModuleXMLFileGeneratedTest extends AbstractTestHid
46 implements org.xml.sax.EntityResolver JavaDoc {
47     public MissingModuleXMLFileGeneratedTest (String JavaDoc name) {
48         super (name);
49     }
50     
51     public static void main(java.lang.String JavaDoc[] args) {
52         if (args.length == 1) {
53             junit.textui.TestRunner.run(new MissingModuleXMLFileGeneratedTest (args[0]));
54         } else {
55             junit.textui.TestRunner.run(new NbTestSuite(MissingModuleXMLFileGeneratedTest.class));
56         }
57         System.exit (0);
58     }
59     
60     
61     
62     public void testNbmWithoutModuleConfigGetsCorrectlyListed () throws Exception JavaDoc {
63         File JavaDoc f = generateNbmWithoutModulesXML ("modules/autoload/naming.jar");
64
65         ModuleUpdate mu = ModuleUpdate.getModuleUpdate (f);
66         download (mu);
67         installNBM (Downloader.getNBM (mu));
68         
69         
70         Map userFiles = findFiles (userDir);
71         assertNotNull ("Contains naming.jar", userFiles.get ("modules/autoload/naming.jar"));
72         assertNotNull ("Contains update tracking", userFiles.get ("update_tracking/org-netbeans-core-naming.xml"));
73         File JavaDoc config = (File JavaDoc)userFiles.get ("config/Modules/org-netbeans-core-naming.xml");
74         assertNotNull ("The config file was generated", config);
75         
76         assertEquals ("Empty platform dir", 0, findFiles (platformDir).size ());
77         
78         
79         assertModule ("Should be autoload", "org-netbeans-core-naming", false, true, false);
80     }
81
82     public void testEagerNbmWithoutModuleConfigGetsCorrectlyListed () throws Exception JavaDoc {
83         File JavaDoc f = generateNbmWithoutModulesXML ("modules/eager/naming.jar");
84
85         ModuleUpdate mu = ModuleUpdate.getModuleUpdate (f);
86         download (mu);
87         installNBM (Downloader.getNBM (mu));
88         
89         
90         Map userFiles = findFiles (userDir);
91         assertNotNull ("Contains naming.jar", userFiles.get ("modules/eager/naming.jar"));
92         assertNotNull ("Contains update tracking", userFiles.get ("update_tracking/org-netbeans-core-naming.xml"));
93         File JavaDoc config = (File JavaDoc)userFiles.get ("config/Modules/org-netbeans-core-naming.xml");
94         assertNotNull ("The config file was generated", config);
95         
96         assertEquals ("Empty platform dir", 0, findFiles (platformDir).size ());
97         
98         
99         assertModule ("Should be autoload", "org-netbeans-core-naming", false, false, true);
100     }
101
102     public void testRegularNbmWithoutModuleConfigGetsCorrectlyListed () throws Exception JavaDoc {
103         File JavaDoc f = generateNbmWithoutModulesXML ("modules/naming.jar");
104
105         ModuleUpdate mu = ModuleUpdate.getModuleUpdate (f);
106         download (mu);
107         installNBM (Downloader.getNBM (mu));
108         
109         
110         Map userFiles = findFiles (userDir);
111         assertNotNull ("Contains naming.jar", userFiles.get ("modules/naming.jar"));
112         assertNotNull ("Contains update tracking", userFiles.get ("update_tracking/org-netbeans-core-naming.xml"));
113         File JavaDoc config = (File JavaDoc)userFiles.get ("config/Modules/org-netbeans-core-naming.xml");
114         assertNotNull ("The config file was generated", config);
115         
116         assertEquals ("Empty platform dir", 0, findFiles (platformDir).size ());
117         
118         
119         assertModule ("Should be autoload", "org-netbeans-core-naming", true, false, false);
120     }
121     
122     private void assertModule (String JavaDoc txt, String JavaDoc dashBase, boolean enabled, boolean autoload, boolean eager)
123     throws Exception JavaDoc {
124         boolean f = assertModule (userDir, txt, dashBase, enabled, autoload, eager);
125         f |= assertModule (platformDir, txt, dashBase, enabled, autoload, eager);
126         f |= assertModule (clusterDir, txt, dashBase, enabled, autoload, eager);
127         f |= assertModule (nextDir, txt, dashBase, enabled, autoload, eager);
128         
129         assertTrue ("At least once cluster has to contain the module", f);
130     }
131     
132     private boolean assertModule (File JavaDoc dir, String JavaDoc txt, String JavaDoc dashBase, boolean enabled, boolean autoload, boolean eager)
133     throws Exception JavaDoc {
134         if (dir == null || !dir.isDirectory ()) {
135             return false;
136         }
137         
138         File JavaDoc config = new File JavaDoc (dir, "config/Modules/" + dashBase + ".xml");
139         if (!config.isFile ()) {
140             return false;
141         }
142
143         javax.xml.parsers.DocumentBuilderFactory JavaDoc f;
144         f = javax.xml.parsers.DocumentBuilderFactory.newInstance ();
145         javax.xml.parsers.DocumentBuilder JavaDoc b = f.newDocumentBuilder ();
146         b.setEntityResolver (this);
147         
148         org.w3c.dom.Document JavaDoc document = b.parse (config);
149         org.w3c.dom.Element JavaDoc e;
150
151         org.w3c.dom.NodeList JavaDoc list = document.getElementsByTagName ("module");
152         assertEquals ("One module element", 1, list.getLength ());
153         e = (org.w3c.dom.Element JavaDoc)list.item (0);
154         assertEquals ("The right name", dashBase.replace ('-', '.'), e.getAttribute ("name"));
155         
156         boolean jarFound = false;
157         list = document.getElementsByTagName ("param");
158         for (int i = 0; i < list.getLength (); i++) {
159             e = (org.w3c.dom.Element JavaDoc)list.item (i);
160     
161             String JavaDoc name = e.getAttribute ("name");
162             
163             boolean toTest;
164             if ("autoload".equals (name)) {
165                 toTest = autoload;
166             } else if ("enabled".equals (name)) {
167                 toTest = enabled;
168             } else if ("eager".equals (name)) {
169                 toTest = eager;
170             } else if ("jar".equals (name)) {
171                 jarFound = true;
172                 continue;
173             } else {
174                 continue;
175             }
176             
177             org.w3c.dom.Text JavaDoc t = (org.w3c.dom.Text JavaDoc)e.getChildNodes ().item (0);
178             
179             // XXX assertion for debugging on different platforms
180
String JavaDoc boolValue = t.getNodeValue ();
181             if (!"true".equalsIgnoreCase (boolValue)) {
182                 if (!"false".equalsIgnoreCase (boolValue)) {
183                     fail ("Value of attribute 'name' must be 'true' or 'false' but was: " + boolValue);
184                 }
185             }
186             // end of debugging
187

188             assertEquals ("Attribute " + name, toTest, Boolean.valueOf (t.getNodeValue ()).booleanValue ());
189         }
190         
191         assertTrue ("jar attribute must be specified", jarFound);
192         return true;
193     }
194     
195     
196     private File JavaDoc generateNbmWithoutModulesXML (String JavaDoc file) throws Exception JavaDoc {
197         String JavaDoc manifest =
198             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
199             "<!DOCTYPE module PUBLIC \"-//NetBeans//DTD Autoupdate Module Info 2.0//EN\" \"http://www.netbeans.org/dtds/autoupdate-info-2_0.dtd\">" +
200             "<module codenamebase=\"org.netbeans.core.naming\"" +
201             " homepage=\"http://contrib.netbeans.org/\"" +
202             " distribution=\"http://www.netbeans.org/download/nbms/alpha/dev/naming.nbm\"" +
203             " downloadsize=\"0\"" +
204             ">" +
205             "<manifest " +
206             " OpenIDE-Module=\"org.netbeans.core.naming/1\"" +
207             " OpenIDE-Module-Display-Category=\"Infrastructure\"" +
208             " OpenIDE-Module-IDE-Dependencies=\"IDE/1 &gt; 3.17\"" +
209             " OpenIDE-Module-Implementation-Version=\"200404191800\"" +
210             " OpenIDE-Module-Long-Description=\"Provides implementation of JNDI over content of system file system.\"" +
211             " OpenIDE-Module-Name=\"Naming\"" +
212             " OpenIDE-Module-Short-Description=\"Implementation of JNDI.\"" +
213             " OpenIDE-Module-Specification-Version=\"1.4\"" +
214             "/>" +
215             "</module>";
216
217         String JavaDoc[] fileList = {
218             "netbeans/" + file
219         };
220         
221         File JavaDoc f = generateNBM (fileList, manifest);
222         
223         return f;
224         
225     }
226     
227     public org.xml.sax.InputSource JavaDoc resolveEntity (String JavaDoc publicId, String JavaDoc systemId) throws org.xml.sax.SAXException JavaDoc, IOException JavaDoc {
228         java.io.InputStream JavaDoc is = new java.io.ByteArrayInputStream JavaDoc (new byte[0]);
229         return new org.xml.sax.InputSource JavaDoc (is);
230     }
231     
232 }
233
Popular Tags