KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > NbProblemDisplayerTest


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 package org.netbeans.core.startup;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.jar.Manifest JavaDoc;
31 import org.netbeans.InvalidException;
32 import org.netbeans.Module;
33 import org.netbeans.ModuleInstaller;
34 import org.netbeans.ModuleManager;
35 import org.netbeans.junit.NbTestCase;
36 import org.openide.modules.Dependency;
37
38 /**
39  *
40  * @author Jaroslav Tulach
41  */

42 public class NbProblemDisplayerTest extends NbTestCase {
43     
44     public NbProblemDisplayerTest(String JavaDoc testName) {
45         super(testName);
46     }
47
48     protected void setUp() throws Exception JavaDoc {
49     }
50
51     protected void tearDown() throws Exception JavaDoc {
52     }
53
54     public void testSimpleDepOnJava() throws Exception JavaDoc {
55         StringBuilder JavaDoc writeTo = new StringBuilder JavaDoc();
56         Set JavaDoc<ProblemModule> modules = new HashSet JavaDoc<ProblemModule>();
57
58         {
59             Manifest JavaDoc mf = new Manifest JavaDoc();
60             mf.getMainAttributes().putValue("OpenIDE-Module", "root.module");
61             ProblemModule pm = new ProblemModule(mf);
62             pm.addProblem(Dependency.create(Dependency.TYPE_JAVA, "Java > 1.30"));
63             pm.addAttr("OpenIDE-Module-Name", "RootModule");
64             modules.add(pm);
65         }
66         
67         NbProblemDisplayer.problemMessagesForModules(writeTo, modules, true);
68
69         String JavaDoc msg = writeTo.toString();
70         if (msg.indexOf("RootModule") == -1) {
71             fail("There should be noted the root module: " + msg);
72         }
73     }
74     public void testFindTheRootCause() throws Exception JavaDoc {
75         StringBuilder JavaDoc writeTo = new StringBuilder JavaDoc();
76         Set JavaDoc<ProblemModule> modules = new HashSet JavaDoc<ProblemModule>();
77
78         {
79             Manifest JavaDoc mf = new Manifest JavaDoc();
80             mf.getMainAttributes().putValue("OpenIDE-Module", "root.module");
81             ProblemModule pm = new ProblemModule(mf);
82             pm.addProblem(Dependency.create(Dependency.TYPE_JAVA, "Java > 1.30"));
83             pm.addAttr("OpenIDE-Module-Name", "RootModule");
84             modules.add(pm);
85         }
86
87         {
88             Manifest JavaDoc mf = new Manifest JavaDoc();
89             mf.getMainAttributes().putValue("OpenIDE-Module", "dep.module");
90             ProblemModule pm = new ProblemModule(mf);
91             pm.addProblem(Dependency.create(Dependency.TYPE_MODULE, "root.module"));
92             pm.addAttr("OpenIDE-Module-Name", "DepModule");
93             modules.add(pm);
94         }
95         
96         NbProblemDisplayer.problemMessagesForModules(writeTo, modules, true);
97
98         String JavaDoc msg = writeTo.toString();
99         if (msg.indexOf("DepModule") >= 0) {
100             fail("There should not be be name of dep.module: " + msg);
101         }
102
103         Locale.setDefault(Locale.US);
104
105         if (msg.toUpperCase().indexOf("ANOTHER MODULE") == -1) {
106             fail("There should be note about one missing module: " + msg);
107         }
108     }
109     public void testFindTheRootCauseForMoreCausesAtOnce() throws Exception JavaDoc {
110         StringBuilder JavaDoc writeTo = new StringBuilder JavaDoc();
111         Set JavaDoc<ProblemModule> modules = new HashSet JavaDoc<ProblemModule>();
112
113         {
114             Manifest JavaDoc mf = new Manifest JavaDoc();
115             mf.getMainAttributes().putValue("OpenIDE-Module", "root.module");
116             ProblemModule pm = new ProblemModule(mf);
117             pm.addProblem(Dependency.create(Dependency.TYPE_JAVA, "Java > 1.30"));
118             pm.addAttr("OpenIDE-Module-Name", "RootModule");
119             modules.add(pm);
120         }
121         {
122             Manifest JavaDoc mf = new Manifest JavaDoc();
123             mf.getMainAttributes().putValue("OpenIDE-Module", "root2.module");
124             ProblemModule pm = new ProblemModule(mf);
125             pm.addProblem(Dependency.create(Dependency.TYPE_JAVA, "Java > 1.35"));
126             pm.addAttr("OpenIDE-Module-Name", "Root2Module");
127             modules.add(pm);
128         }
129         {
130             Manifest JavaDoc mf = new Manifest JavaDoc();
131             mf.getMainAttributes().putValue("OpenIDE-Module", "root3.module");
132             ProblemModule pm = new ProblemModule(mf);
133             pm.addProblem(Dependency.create(Dependency.TYPE_JAVA, "Java > 1.40"));
134             pm.addAttr("OpenIDE-Module-Name", "Root3Module");
135             modules.add(pm);
136         }
137
138         {
139             Manifest JavaDoc mf = new Manifest JavaDoc();
140             mf.getMainAttributes().putValue("OpenIDE-Module", "dep.module");
141             ProblemModule pm = new ProblemModule(mf);
142             pm.addProblem(Dependency.create(Dependency.TYPE_MODULE, "root.module"));
143             pm.addAttr("OpenIDE-Module-Name", "DepModule");
144             modules.add(pm);
145         }
146         {
147             Manifest JavaDoc mf = new Manifest JavaDoc();
148             mf.getMainAttributes().putValue("OpenIDE-Module", "dep2.module");
149             ProblemModule pm = new ProblemModule(mf);
150             pm.addProblem(Dependency.create(Dependency.TYPE_MODULE, "root2.module"));
151             pm.addAttr("OpenIDE-Module-Name", "Dep2Module");
152             modules.add(pm);
153         }
154         {
155             Manifest JavaDoc mf = new Manifest JavaDoc();
156             mf.getMainAttributes().putValue("OpenIDE-Module", "dep3.module");
157             ProblemModule pm = new ProblemModule(mf);
158             pm.addProblem(Dependency.create(Dependency.TYPE_MODULE, "root3.module"));
159             pm.addAttr("OpenIDE-Module-Name", "Dep3Module");
160             modules.add(pm);
161         }
162         
163         NbProblemDisplayer.problemMessagesForModules(writeTo, modules, true);
164
165         String JavaDoc msg = writeTo.toString();
166         if (msg.indexOf("DepModule") >= 0) {
167             fail("There should not be be name of dep.module: " + msg);
168         }
169
170         Locale.setDefault(Locale.US);
171
172         if (msg.toUpperCase().indexOf("3 FURTHER") == -1) {
173             fail("There should be note about 3 missing modules: " + msg);
174         }
175         if (msg.toUpperCase().indexOf("DEP3") >= 0) {
176             fail("Nothing about Dep3: " + msg);
177         }
178         if (msg.toUpperCase().indexOf("DEP2") >= 0) {
179             fail("Nothing about Dep2: " + msg);
180         }
181         if (msg.toUpperCase().indexOf("1.35") == -1) {
182             fail("Something about Root2: " + msg);
183         }
184         if (msg.toUpperCase().indexOf("1.40") == -1) {
185             fail("Something about Root3: " + msg);
186         }
187     }
188
189     private static class ProblemModule extends Module {
190         private static final Inst INST = new Inst();
191         private static final ModuleManager MGR = new ModuleManager(INST, new NbEvents());
192
193         private Map JavaDoc attrs = new HashMap JavaDoc();
194         private Set JavaDoc<Dependency> problems = new HashSet JavaDoc<Dependency>();
195
196         public ProblemModule(Manifest JavaDoc m) throws IOException JavaDoc {
197             super(MGR, null, m, null, ProblemModule.class.getClassLoader());
198             parseManifest();
199         }
200
201         public void addProblem(Set JavaDoc<Dependency> d) {
202             problems.addAll(d);
203         }
204
205         public void addAttr(String JavaDoc key, String JavaDoc value) {
206             attrs.put(key, value);
207         }
208
209
210         public List JavaDoc<File JavaDoc> getAllJars() {
211             return Collections.emptyList();
212         }
213
214         public void setReloadable(boolean r) {
215         }
216
217         public void reload() throws IOException JavaDoc {
218         }
219
220         protected void classLoaderUp(Set JavaDoc<Module> parents) throws IOException JavaDoc {
221         }
222
223         protected void classLoaderDown() {
224         }
225
226         protected void cleanup() {
227         }
228
229         protected void destroy() {
230         }
231
232         public boolean isValid() {
233             return true;
234         }
235
236         public boolean isFixed() {
237             return false;
238         }
239
240         public Object JavaDoc getLocalizedAttribute(String JavaDoc attr) {
241             return attrs.get(attr);
242         }
243
244         public Set JavaDoc getProblems() {
245             return problems;
246         }
247
248     } // end of ProblemModule
249

250     private static final class Inst extends ModuleInstaller {
251         public void prepare(Module m) throws InvalidException {
252         }
253
254         public void dispose(Module m) {
255         }
256
257         public void load(List JavaDoc<Module> modules) {
258         }
259
260         public void unload(List JavaDoc<Module> modules) {
261         }
262
263         public boolean closing(List JavaDoc<Module> modules) {
264             return true;
265         }
266
267         public void close(List JavaDoc<Module> modules) {
268         }
269     } // end of ModuleInstaller
270
}
271
Popular Tags