Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2008 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* If you wish your version of this file to be governed by only the CDDL |
25 |
* or only the GPL Version 2, indicate your decision by adding |
26 |
* "[Contributor] elects to include this software in this distribution |
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
28 |
* single choice of license, a recipient has the option to distribute |
29 |
* your version of this file under either the CDDL, the GPL Version 2 or |
30 |
* to extend the choice of license to its licensees as provided above. |
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
32 |
* Version 2 license, then the option applies only if the new code is |
33 |
* made subject to such option by the copyright holder. |
34 |
* |
35 |
* Contributor(s): |
36 |
* |
37 |
* Portions Copyrighted 2008 Sun Microsystems, Inc. |
38 |
*/ |
39 |
package org.netbeans.modules.ide.ergonomics; |
40 |
|
41 |
import java.io.IOException; |
42 |
import java.net.URL; |
43 |
import java.util.ArrayList; |
44 |
import java.util.Collection; |
45 |
import java.util.Collections; |
46 |
import java.util.Enumeration; |
47 |
import java.util.Iterator; |
48 |
import java.util.List; |
49 |
import java.util.Map; |
50 |
import java.util.TreeMap; |
51 |
import org.netbeans.junit.NbTestCase; |
52 |
import org.openide.filesystems.FileObject; |
53 |
import org.openide.filesystems.FileStateInvalidException; |
54 |
import org.openide.filesystems.FileUtil; |
55 |
import org.openide.util.Enumerations; |
56 |
|
57 |
/** |
58 |
* |
59 |
* @author Jaroslav Tulach <jtulach@netbeans.org> |
60 |
*/ |
61 |
public class ServersNodeActionsCheck extends NbTestCase { |
62 |
|
63 |
public ServersNodeActionsCheck(String name) { |
64 |
super(name); |
65 |
} |
66 |
|
67 |
public void testGetAll() throws Exception { |
68 |
clearWorkDir(); |
69 |
|
70 |
FileObject orig = FileUtil.getConfigFile("Servers/Actions"); |
71 |
assertNotNull("actions folder is there", orig); |
72 |
|
73 |
Enumeration<? extends FileObject> nodes = orig.getChildren(true); |
74 |
class VisitOriginalFile implements Enumerations.Processor<FileObject,FileObject> { |
75 |
public FileObject process(FileObject fo, Collection<FileObject> toAdd) { |
76 |
Object attr = fo.getAttribute("originalFile"); |
77 |
if (attr instanceof String) { |
78 |
FileObject originalFile = FileUtil.getConfigFile((String)attr); |
79 |
assertNotNull("Original file for " + attr + " found", originalFile); |
80 |
toAdd.add(originalFile); |
81 |
} |
82 |
return fo; |
83 |
} |
84 |
} |
85 |
|
86 |
|
87 |
StringBuilder errors = new StringBuilder(); |
88 |
Enumeration<? extends FileObject> en = Enumerations.queue(nodes, new VisitOriginalFile()); |
89 |
while (en.hasMoreElements()) { |
90 |
FileObject fo = en.nextElement(); |
91 |
|
92 |
int cnt = 0; |
93 |
Enumeration<String> allAttributes = fo.getAttributes(); |
94 |
while (allAttributes.hasMoreElements()) { |
95 |
String name = allAttributes.nextElement(); |
96 |
Object attr = fo.getAttribute(name); |
97 |
if (attr == null) { |
98 |
fail("fo: " + fo + " has null " + name + " attribute"); |
99 |
} |
100 |
System.setProperty(dynAttr + fo.getPath() + "@" + name, attr.toString()); |
101 |
cnt++; |
102 |
|
103 |
if (attr instanceof URL) { |
104 |
URL u = (URL) attr; |
105 |
int read = -1; |
106 |
try { |
107 |
read = u.openStream().read(new byte[4096]); |
108 |
} catch (IOException ex) { |
109 |
errors.append(ex.getMessage()).append('\n'); |
110 |
} |
111 |
if (read <= 0) { |
112 |
errors.append("Resource shall exist: " + fo + " attr: " + name + " value: " + attr + "\n"); |
113 |
} |
114 |
} |
115 |
} |
116 |
System.setProperty(dynVery + fo.getPath(), String.valueOf(cnt)); |
117 |
String locName = getDisplayName(fo); |
118 |
System.setProperty(dynName + fo.getPath(), locName); |
119 |
} |
120 |
|
121 |
if (errors.length() > 0) { |
122 |
fail(errors.toString()); |
123 |
} |
124 |
} |
125 |
private static final String dynName = "serversnodedynamic/name/"; |
126 |
private static final String dynVery = "serversnodedynamic/verify/"; |
127 |
private static final String dynAttr = "serversnodedynamic/attr/"; |
128 |
|
129 |
public void testCheckAllPretest() throws Exception { |
130 |
testCheckAllReal(); |
131 |
} |
132 |
public void testCheckAllReal() throws Exception { |
133 |
Map<String, List<String>> filesAndAttribs = new TreeMap<String, List<String>>(); |
134 |
for (Object o : System.getProperties().keySet()) { |
135 |
String f = (String)o; |
136 |
if (f.startsWith(dynVery)) { |
137 |
int cnt = Integer.parseInt(System.getProperties().getProperty(f)); |
138 |
f = f.substring(dynVery.length()); |
139 |
ArrayList<String> arr = new ArrayList<String>(); |
140 |
String pref = dynAttr + f + "@"; |
141 |
for (Map.Entry<Object, Object> entry : System.getProperties().entrySet()) { |
142 |
if (entry.getKey() instanceof String) { |
143 |
String a = (String) entry.getKey(); |
144 |
if (a.startsWith(pref)) { |
145 |
a = a.substring(pref.length()); |
146 |
arr.add(a); |
147 |
} |
148 |
} |
149 |
} |
150 |
if (arr.size() != cnt) { |
151 |
fail("There shall be " + cnt + " attributes for " + f); |
152 |
} |
153 |
filesAndAttribs.put(f, arr); |
154 |
} |
155 |
} |
156 |
if (filesAndAttribs.size() == 0) { |
157 |
fail("Some properties shall be set: " + System.getProperties()); |
158 |
} |
159 |
|
160 |
Iterator<? extends String> allTemplates = filesAndAttribs.keySet().iterator(); |
161 |
StringBuilder errors = new StringBuilder(); |
162 |
while (allTemplates.hasNext()) { |
163 |
String fo = allTemplates.next(); |
164 |
FileObject clone = FileUtil.getConfigFile(fo); |
165 |
|
166 |
if (clone == null) { |
167 |
errors.append("Both files exist: " + fo + "\n"); |
168 |
continue; |
169 |
} |
170 |
Enumeration<String> allAttributes = Collections.enumeration(filesAndAttribs.get(fo)); |
171 |
while (allAttributes.hasMoreElements()) { |
172 |
String name = allAttributes.nextElement(); |
173 |
Object attr = clone.getAttribute(name); |
174 |
if (attr == null && "templateWizardIterator".equals(name)) { |
175 |
attr = clone.getAttribute("instantiatingIterator"); |
176 |
} |
177 |
|
178 |
if (attr == null) { |
179 |
errors.append("Attribute " + name + " present in orig on " + fo + " but null in clone\n"); |
180 |
} |
181 |
|
182 |
if (name.equals("iconResource")) { |
183 |
attr = Thread.currentThread().getContextClassLoader().getResource((String)attr); |
184 |
if (!(attr instanceof URL)) { |
185 |
errors.append("iconResource attr for " + fo + " shall exist " + attr); |
186 |
} |
187 |
} |
188 |
|
189 |
if (attr instanceof URL) { |
190 |
URL u = (URL) attr; |
191 |
int read = -1; |
192 |
try { |
193 |
read = u.openStream().read(new byte[4096]); |
194 |
} catch (IOException ex) { |
195 |
errors.append(ex.getMessage()).append('\n'); |
196 |
} |
197 |
if (read <= 0) { |
198 |
errors.append("Resource shall exist: " + fo + " attr: " + name + " value: " + attr + "\n"); |
199 |
} |
200 |
} |
201 |
} |
202 |
allTemplates.remove(); |
203 |
} |
204 |
|
205 |
if (errors.length() > 0) { |
206 |
fail(errors.toString()); |
207 |
} |
208 |
|
209 |
if (!filesAndAttribs.isEmpty()) { |
210 |
fail("All should be empty: " + filesAndAttribs); |
211 |
} |
212 |
} |
213 |
|
214 |
private static String getDisplayName(FileObject f) throws FileStateInvalidException { |
215 |
return f.getFileSystem().getStatus().annotateName( |
216 |
f.getNameExt(), Collections.<FileObject>singleton(f) |
217 |
); |
218 |
} |
219 |
} |