1 19 20 package org.netbeans.modules.settings.convertors; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.net.URL ; 26 import java.util.ArrayList ; 27 import java.util.Enumeration ; 28 import java.util.HashSet ; 29 import java.util.List ; 30 import java.util.Set ; 31 import java.util.jar.JarEntry ; 32 import java.util.jar.JarFile ; 33 import java.util.jar.Manifest ; 34 import java.util.logging.Level ; 35 import junit.framework.TestCase; 36 import junit.framework.*; 37 import org.netbeans.*; 38 import org.netbeans.core.startup.ModuleSystem; 39 import org.netbeans.junit.Log; 40 import org.netbeans.junit.NbTestCase; 41 import org.netbeans.junit.NbTestSuite; 42 import org.openide.filesystems.FileObject; 43 import org.openide.filesystems.FileSystem; 44 import org.openide.filesystems.Repository; 45 import org.openide.filesystems.XMLFileSystem; 46 import org.openide.modules.ModuleInfo; 47 import org.openide.util.Lookup; 48 import org.openide.xml.EntityCatalog; 49 import org.openide.xml.XMLUtil; 50 import org.w3c.dom.Document ; 51 import org.xml.sax.InputSource ; 52 53 57 public class LayersTest extends NbTestCase { 58 59 public LayersTest(String testName) { 60 super(testName); 61 } 62 63 public static Test suite() { 64 TestSuite suite = new NbTestSuite(LayersTest.class); 65 66 return suite; 67 } 68 69 public void testFastParsingOfXMLFiles() throws Exception { 70 CharSequence chars = Log.enable(XMLSettingsSupport.class.getName(), Level.FINE); 71 int len = 0; 72 FileSystem sfs = Repository.getDefault().getDefaultFileSystem(); 73 FileObject dir = sfs.getRoot(); 74 Enumeration <? extends FileObject> en = dir.getChildren(true); 75 while (en.hasMoreElements()) { 76 FileObject fo = en.nextElement(); 77 if (fo.isFolder()) 78 continue; 79 if (!"settings".equals(fo.getExt())) { 80 continue; 81 } 82 String loc = fo.getURL().toExternalForm(); 84 Document doc = XMLUtil.parse(new InputSource (loc), false, true, null, EntityCatalog.getDefault()); 85 if (!"-//NetBeans//DTD Session settings 1.0//EN".equals(doc.getDoctype().getPublicId())) 86 continue; 87 88 log("checking "+fo.getPath()); 89 try { 90 XMLSettingsSupport.SettingsRecognizer sr = new XMLSettingsSupport.SettingsRecognizer(true, fo); 91 sr.parse(); 92 } 93 catch (IOException ioe) { 94 fail("IOException was thrown: "+ioe.getMessage()); 95 } 96 if (chars.length() > len) { 97 log("quickParse fails"); 98 len = chars.length(); 99 } 100 } 101 if (chars.length() > 0) { 102 fail("fast parsing of .settings files fails :"+chars.toString()); 103 } 104 } 105 106 public void testCorrectContentOfSettingsFiles() throws Exception { 107 ClassLoader l = Lookup.getDefault().lookup(ClassLoader .class); 108 assertNotNull ("In the IDE mode, there always should be a classloader", l); 109 110 List <Module> urls = new ArrayList <Module>(); 111 boolean atLeastOne = false; 112 Enumeration <URL > en = l.getResources("META-INF/MANIFEST.MF"); 113 while (en.hasMoreElements ()) { 114 URL u = en.nextElement(); 115 InputStream is = u.openStream(); 116 Manifest mf; 117 try { 118 mf = new Manifest (is); 119 } finally { 120 is.close(); 121 } 122 String module = mf.getMainAttributes ().getValue ("OpenIDE-Module"); 123 if (module == null) continue; 124 String layer = mf.getMainAttributes ().getValue ("OpenIDE-Module-Layer"); 125 if (layer == null) continue; 126 127 atLeastOne = true; 128 URL layerURL = new URL (u, "../" + layer); 129 Module m = new Module(); 130 m.module = module; 131 m.layer = layerURL; 132 urls.add(m); 133 } 134 135 StringBuilder sb = new StringBuilder (); 137 int len = 0; 138 for (Module m: urls) { 139 if ("org.netbeans.modules.settings.xtest/1".equals(m.module)) { 140 continue; 141 } 142 log("Checking layer of "+m.module); 143 XMLFileSystem xmlfs = new XMLFileSystem(m.layer); 144 FileObject dir = xmlfs.getRoot(); 145 Enumeration <? extends FileObject> en2 = dir.getChildren(true); 146 while (en2.hasMoreElements()) { 147 FileObject fo = en2.nextElement(); 148 if (fo.isFolder()) 149 continue; 150 if (!"settings".equals(fo.getExt())) { 151 continue; 152 } 153 154 if ("Services/org-netbeans-core-IDESettings.settings".equals(fo.getPath())) { 155 continue; 157 } 158 String loc = fo.getURL().toExternalForm(); 160 Document doc = XMLUtil.parse(new InputSource (loc), false, true, null, EntityCatalog.getDefault()); 161 if (!"-//NetBeans//DTD Session settings 1.0//EN".equals(doc.getDoctype().getPublicId())) 162 continue; 163 164 log("checking "+fo.getPath()); 165 try { 166 XMLSettingsSupport.SettingsRecognizer sr = new XMLSettingsSupport.SettingsRecognizer(true, fo); 167 sr.parse(); 168 String cnb = (m.module.indexOf('/') == -1)? m.module: m.module.substring(0, m.module.indexOf('/')); 170 String cnbFromFile = sr.getCodeNameBase(); 171 if (sr.getCodeNameBase() != null && sr.getCodeNameBase().indexOf('/') != -1) { 172 cnbFromFile = sr.getCodeNameBase().substring(0, sr.getCodeNameBase().indexOf('/')); 173 } 174 if (!cnb.equals(cnbFromFile)) { 175 sb.append("Codenamebase of module in ").append(fo.getPath()). 176 append(" does not refer to module ").append(m.module).append(" it refers to "). 177 append(sr.getCodeNameBase()).append('\n'); 178 } 179 } 181 catch (IOException ioe) { 182 fail("IOException was thrown: "+ioe.getMessage()); 183 } 184 } 189 } 190 if (sb.length() > 0) { 191 fail(sb.toString()); 192 } 193 } 194 195 private static class Module { 196 String module; 197 URL layer; 198 } 199 } 200 | Popular Tags |