KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
23 import org.netbeans.junit.*;
24 import java.io.File JavaDoc;
25 import java.io.FileReader JavaDoc;
26 import java.io.IOException JavaDoc;
27
28
29 /** Tests the module name with any alphabet order.
30  * @author Jiri Rechtacek
31  * @See issue 53316
32  */

33 public class ModuleNameTest extends AbstractTestHid
34         implements org.xml.sax.EntityResolver JavaDoc {
35     public ModuleNameTest (String JavaDoc name) {
36         super (name);
37     }
38
39     public static void main (java.lang.String JavaDoc[] args) {
40         if (args.length == 1) {
41             junit.textui.TestRunner.run (new ModuleNameTest (args[0]));
42         } else {
43             junit.textui.TestRunner.run (new NbTestSuite (ModuleNameTest.class));
44         }
45         System.exit (0);
46     }
47     
48     public void testFindModuleByCodenamebase () throws Exception JavaDoc {
49         String JavaDoc moduleName = "modules/org-netbeans-modules-aaa.jar";
50         String JavaDoc[] jars = new String JavaDoc[] {"modules/dummy.jar", moduleName, "modules/subdir/aaa.jar", "modules/util.jar"};
51         testFindModuleByModuleName (moduleName, jars, false, false);
52     }
53     
54     public void testModuleJarControlsAutoload () throws Exception JavaDoc {
55         String JavaDoc moduleName = "modules/org-netbeans-modules-aaa.jar";
56         String JavaDoc[] jars = new String JavaDoc[] {"modules/autoload/dummy.jar", moduleName, "modules/subdir/aaa.jar", "modules/autoload/util.jar"};
57         testFindModuleByModuleName (moduleName, jars, false, false);
58         
59         // now you are autoload
60
jars = new String JavaDoc[] {"modules/autoload/org-netbeans-modules-aaa.jar"};
61         testFindModuleByModuleName (moduleName, jars, true, false);
62     }
63     
64     public void testModuleJarControlsEager () throws Exception JavaDoc {
65         String JavaDoc moduleName = "modules/org-netbeans-modules-aaa.jar";
66         String JavaDoc[] jars = new String JavaDoc[] {"modules/eager/dummy.jar", moduleName, "modules/subdir/aaa.jar", "modules/eager/util.jar"};
67         testFindModuleByModuleName (moduleName, jars, false, false);
68         
69         // now you are eager
70
jars = new String JavaDoc[] {"modules/eager/org-netbeans-modules-aaa.jar"};
71         testFindModuleByModuleName (moduleName, jars, false, true);
72     }
73     
74     public void testFindModuleByOtherName () throws Exception JavaDoc {
75         String JavaDoc moduleName = "modules/dummy.jar";
76         String JavaDoc[] jars = new String JavaDoc[] {moduleName};
77         testFindModuleByModuleName (moduleName, jars, false, false);
78     }
79     
80     private void testFindModuleByModuleName (String JavaDoc moduleName, String JavaDoc[] jars, boolean autoload, boolean eager) throws Exception JavaDoc {
81         File JavaDoc f = generateNbmWithoutModulesXML (jars);
82         
83         ModuleUpdate mu = ModuleUpdate.getModuleUpdate (f);
84         download (mu);
85         installNBM (Downloader.getNBM (mu));
86         
87         Map JavaDoc userFiles = findFiles (userDir);
88         for (int i = 0; i < jars.length; i++) {
89             assertNotNull ("Contains " + jars[i], userFiles.get (jars[i]));
90         }
91         
92         File JavaDoc config = (File JavaDoc)userFiles.get ("config/Modules/org-netbeans-modules-aaa.xml");
93         
94         assertNotNull ("The config file was generated", config);
95
96         assertModule ("Should be enabled.", "org-netbeans-modules-aaa", moduleName, true, false, false);
97     }
98     
99     private void assertModule (String JavaDoc txt, String JavaDoc dashBase, String JavaDoc moduleName, boolean enabled, boolean autoload, boolean eager)
100     throws Exception JavaDoc {
101         boolean f = assertModule (userDir, txt, dashBase, moduleName, enabled, autoload, eager);
102         f |= assertModule (platformDir, txt, dashBase, moduleName, enabled, autoload, eager);
103         f |= assertModule (clusterDir, txt, dashBase, moduleName, enabled, autoload, eager);
104         f |= assertModule (nextDir, txt, dashBase, moduleName, enabled, autoload, eager);
105         
106         assertTrue ("At least once cluster has to contain the module", f);
107     }
108     
109     private boolean assertModule (File JavaDoc dir, String JavaDoc txt, String JavaDoc dashBase, String JavaDoc moduleName, boolean enabled, boolean autoload, boolean eager)
110     throws Exception JavaDoc {
111         if (dir == null || !dir.isDirectory ()) {
112             return false;
113         }
114         
115         File JavaDoc config = new File JavaDoc (dir, "config/Modules/" + dashBase + ".xml");
116         if (!config.isFile ()) {
117             return false;
118         }
119         
120         javax.xml.parsers.DocumentBuilderFactory JavaDoc f;
121         f = javax.xml.parsers.DocumentBuilderFactory.newInstance ();
122         javax.xml.parsers.DocumentBuilder JavaDoc b = f.newDocumentBuilder ();
123         b.setEntityResolver (this);
124         
125         org.w3c.dom.Document JavaDoc document = b.parse (config);
126         org.w3c.dom.Element JavaDoc e;
127         
128         org.w3c.dom.NodeList JavaDoc list = document.getElementsByTagName ("module");
129         assertEquals ("One module element", 1, list.getLength ());
130         e = (org.w3c.dom.Element JavaDoc)list.item (0);
131         assertEquals ("The right name", dashBase.replace ('-', '.'), e.getAttribute ("name"));
132         
133         boolean jarFound = false;
134         list = document.getElementsByTagName ("param");
135         for (int i = 0; i < list.getLength (); i++) {
136             e = (org.w3c.dom.Element JavaDoc)list.item (i);
137             
138             String JavaDoc name = e.getAttribute ("name");
139             
140             boolean toTest;
141             if ("autoload".equals (name)) {
142                 toTest = autoload;
143             } else if ("enabled".equals (name)) {
144                 toTest = enabled;
145             } else if ("eager".equals (name)) {
146                 toTest = eager;
147             } else if ("jar".equals (name)) {
148                 jarFound = true;
149                 org.w3c.dom.Text JavaDoc t = (org.w3c.dom.Text JavaDoc)e.getChildNodes ().item (0);
150                 assertNotNull ("Jar file found in config file.", t);
151                 assertEquals ("The correct jar file.", moduleName, t.getNodeValue ());
152                 continue;
153             } else {
154                 continue;
155             }
156             
157             org.w3c.dom.Text JavaDoc t = (org.w3c.dom.Text JavaDoc)e.getChildNodes ().item (0);
158
159             assertEquals ("Attribute " + name, toTest, Boolean.valueOf (t.getNodeValue ()).booleanValue ());
160         }
161         
162         assertTrue ("jar attribute must be specified", jarFound);
163         return true;
164     }
165     
166     
167     private File JavaDoc generateNbmWithoutModulesXML (String JavaDoc[] files) throws Exception JavaDoc {
168         String JavaDoc manifest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
169                 "<!DOCTYPE module PUBLIC \"-//NetBeans//DTD Autoupdate Module Info 2.0//EN\" \"http://www.netbeans.org/dtds/autoupdate-info-2_0.dtd\">" +
170                 "<module codenamebase=\"org.netbeans.netbeans.aaa\"" +
171                 " homepage=\"http://contrib.netbeans.org/\"" +
172                 " distribution=\"http://www.netbeans.org/aaa.nbm\"" +
173                 " downloadsize=\"0\"" +
174                 ">" +
175                 "<manifest " +
176                 " OpenIDE-Module=\"org.netbeans.modules.aaa/1\"" +
177                 " OpenIDE-Module-Display-Category=\"Infrastructure\"" +
178                 " OpenIDE-Module-IDE-Dependencies=\"IDE/1 &gt; 3.17\"" +
179                 " OpenIDE-Module-Implementation-Version=\"200404191800\"" +
180                 " OpenIDE-Module-Long-Description=\"Testing only.\"" +
181                 " OpenIDE-Module-Name=\"Aaa\"" +
182                 " OpenIDE-Module-Short-Description=\"Testing only..\"" +
183                 " OpenIDE-Module-Specification-Version=\"0.1\"" +
184                 "/>" +
185                 "</module>";
186
187         String JavaDoc[] fileList = new String JavaDoc [files.length];
188         for (int i = 0; i < files.length; i++) {
189             fileList[i] = "netbeans/" + files[i];
190         }
191         
192         File JavaDoc f = generateNBM (fileList, manifest);
193         
194         return f;
195         
196     }
197     
198     public org.xml.sax.InputSource JavaDoc resolveEntity (String JavaDoc publicId, String JavaDoc systemId) throws org.xml.sax.SAXException JavaDoc, IOException JavaDoc {
199         java.io.InputStream JavaDoc is = new java.io.ByteArrayInputStream JavaDoc (new byte[0]);
200         return new org.xml.sax.InputSource JavaDoc (is);
201     }
202     
203 }
204
Popular Tags