KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > settings > convertors > LayersTest


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 2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.settings.convertors;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.jar.JarEntry JavaDoc;
32 import java.util.jar.JarFile JavaDoc;
33 import java.util.jar.Manifest JavaDoc;
34 import java.util.logging.Level JavaDoc;
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 JavaDoc;
51 import org.xml.sax.InputSource JavaDoc;
52
53 /**
54  *
55  * @author radim
56  */

57 public class LayersTest extends NbTestCase {
58     
59     public LayersTest(String JavaDoc 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 JavaDoc {
70         CharSequence JavaDoc 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 JavaDoc<? 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             // check only settings files without convertors
83
String JavaDoc loc = fo.getURL().toExternalForm();
84             Document JavaDoc doc = XMLUtil.parse(new InputSource JavaDoc(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 JavaDoc 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 JavaDoc {
107         ClassLoader JavaDoc l = Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
108         assertNotNull ("In the IDE mode, there always should be a classloader", l);
109         
110         List JavaDoc<Module> urls = new ArrayList JavaDoc<Module>();
111         boolean atLeastOne = false;
112         Enumeration JavaDoc<URL JavaDoc> en = l.getResources("META-INF/MANIFEST.MF");
113         while (en.hasMoreElements ()) {
114             URL JavaDoc u = en.nextElement();
115             InputStream JavaDoc is = u.openStream();
116             Manifest JavaDoc mf;
117             try {
118                 mf = new Manifest JavaDoc(is);
119             } finally {
120                 is.close();
121             }
122             String JavaDoc module = mf.getMainAttributes ().getValue ("OpenIDE-Module");
123             if (module == null) continue;
124             String JavaDoc layer = mf.getMainAttributes ().getValue ("OpenIDE-Module-Layer");
125             if (layer == null) continue;
126             
127             atLeastOne = true;
128             URL JavaDoc layerURL = new URL JavaDoc(u, "../" + layer);
129             Module m = new Module();
130             m.module = module;
131             m.layer = layerURL;
132             urls.add(m);
133         }
134
135 // CharSequence chars = Log.enable(XMLSettingsSupport.class.getName(), Level.FINE);
136
StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
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 JavaDoc<? 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                     // for some reason defined in layer of core/ui although belongs to core
156
continue;
157                 }
158                 // check only settings files without convertors
159
String JavaDoc loc = fo.getURL().toExternalForm();
160                 Document JavaDoc doc = XMLUtil.parse(new InputSource JavaDoc(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;
169
String JavaDoc cnb = (m.module.indexOf('/') == -1)? m.module: m.module.substring(0, m.module.indexOf('/'));
170                     String JavaDoc 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                     // TODO check instance... attrs
180
}
181                 catch (IOException JavaDoc ioe) {
182                     fail("IOException was thrown: "+ioe.getMessage());
183                 }
184 // if (chars.length() > len) {
185
// log("quickParse fails");
186
// len = chars.length();
187
// }
188
}
189         }
190         if (sb.length() > 0) {
191             fail(sb.toString());
192         }
193     }
194
195     private static class Module {
196         String JavaDoc module;
197         URL JavaDoc layer;
198     }
199 }
200
Popular Tags