1 19 20 package org.netbeans.modules.autoupdate; 21 22 import java.util.Map ; 23 import org.netbeans.junit.*; 24 import java.io.File ; 25 import java.io.FileReader ; 26 import java.io.IOException ; 27 28 29 33 public class ModuleNameTest extends AbstractTestHid 34 implements org.xml.sax.EntityResolver { 35 public ModuleNameTest (String name) { 36 super (name); 37 } 38 39 public static void main (java.lang.String [] 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 { 49 String moduleName = "modules/org-netbeans-modules-aaa.jar"; 50 String [] jars = new String [] {"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 { 55 String moduleName = "modules/org-netbeans-modules-aaa.jar"; 56 String [] jars = new String [] {"modules/autoload/dummy.jar", moduleName, "modules/subdir/aaa.jar", "modules/autoload/util.jar"}; 57 testFindModuleByModuleName (moduleName, jars, false, false); 58 59 jars = new String [] {"modules/autoload/org-netbeans-modules-aaa.jar"}; 61 testFindModuleByModuleName (moduleName, jars, true, false); 62 } 63 64 public void testModuleJarControlsEager () throws Exception { 65 String moduleName = "modules/org-netbeans-modules-aaa.jar"; 66 String [] jars = new String [] {"modules/eager/dummy.jar", moduleName, "modules/subdir/aaa.jar", "modules/eager/util.jar"}; 67 testFindModuleByModuleName (moduleName, jars, false, false); 68 69 jars = new String [] {"modules/eager/org-netbeans-modules-aaa.jar"}; 71 testFindModuleByModuleName (moduleName, jars, false, true); 72 } 73 74 public void testFindModuleByOtherName () throws Exception { 75 String moduleName = "modules/dummy.jar"; 76 String [] jars = new String [] {moduleName}; 77 testFindModuleByModuleName (moduleName, jars, false, false); 78 } 79 80 private void testFindModuleByModuleName (String moduleName, String [] jars, boolean autoload, boolean eager) throws Exception { 81 File f = generateNbmWithoutModulesXML (jars); 82 83 ModuleUpdate mu = ModuleUpdate.getModuleUpdate (f); 84 download (mu); 85 installNBM (Downloader.getNBM (mu)); 86 87 Map 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 config = (File )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 txt, String dashBase, String moduleName, boolean enabled, boolean autoload, boolean eager) 100 throws Exception { 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 dir, String txt, String dashBase, String moduleName, boolean enabled, boolean autoload, boolean eager) 110 throws Exception { 111 if (dir == null || !dir.isDirectory ()) { 112 return false; 113 } 114 115 File config = new File (dir, "config/Modules/" + dashBase + ".xml"); 116 if (!config.isFile ()) { 117 return false; 118 } 119 120 javax.xml.parsers.DocumentBuilderFactory f; 121 f = javax.xml.parsers.DocumentBuilderFactory.newInstance (); 122 javax.xml.parsers.DocumentBuilder b = f.newDocumentBuilder (); 123 b.setEntityResolver (this); 124 125 org.w3c.dom.Document document = b.parse (config); 126 org.w3c.dom.Element e; 127 128 org.w3c.dom.NodeList list = document.getElementsByTagName ("module"); 129 assertEquals ("One module element", 1, list.getLength ()); 130 e = (org.w3c.dom.Element )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 )list.item (i); 137 138 String 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 t = (org.w3c.dom.Text )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 t = (org.w3c.dom.Text )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 generateNbmWithoutModulesXML (String [] files) throws Exception { 168 String 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 > 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 [] fileList = new String [files.length]; 188 for (int i = 0; i < files.length; i++) { 189 fileList[i] = "netbeans/" + files[i]; 190 } 191 192 File f = generateNBM (fileList, manifest); 193 194 return f; 195 196 } 197 198 public org.xml.sax.InputSource resolveEntity (String publicId, String systemId) throws org.xml.sax.SAXException , IOException { 199 java.io.InputStream is = new java.io.ByteArrayInputStream (new byte[0]); 200 return new org.xml.sax.InputSource (is); 201 } 202 203 } 204 | Popular Tags |