1 19 package org.netbeans.core.startup; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.util.Collections ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.List ; 27 import java.util.Locale ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import java.util.jar.Manifest ; 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 42 public class NbProblemDisplayerTest extends NbTestCase { 43 44 public NbProblemDisplayerTest(String testName) { 45 super(testName); 46 } 47 48 protected void setUp() throws Exception { 49 } 50 51 protected void tearDown() throws Exception { 52 } 53 54 public void testSimpleDepOnJava() throws Exception { 55 StringBuilder writeTo = new StringBuilder (); 56 Set <ProblemModule> modules = new HashSet <ProblemModule>(); 57 58 { 59 Manifest mf = new Manifest (); 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 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 { 75 StringBuilder writeTo = new StringBuilder (); 76 Set <ProblemModule> modules = new HashSet <ProblemModule>(); 77 78 { 79 Manifest mf = new Manifest (); 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 mf = new Manifest (); 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 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 { 110 StringBuilder writeTo = new StringBuilder (); 111 Set <ProblemModule> modules = new HashSet <ProblemModule>(); 112 113 { 114 Manifest mf = new Manifest (); 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 mf = new Manifest (); 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 mf = new Manifest (); 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 mf = new Manifest (); 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 mf = new Manifest (); 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 mf = new Manifest (); 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 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 attrs = new HashMap (); 194 private Set <Dependency> problems = new HashSet <Dependency>(); 195 196 public ProblemModule(Manifest m) throws IOException { 197 super(MGR, null, m, null, ProblemModule.class.getClassLoader()); 198 parseManifest(); 199 } 200 201 public void addProblem(Set <Dependency> d) { 202 problems.addAll(d); 203 } 204 205 public void addAttr(String key, String value) { 206 attrs.put(key, value); 207 } 208 209 210 public List <File > getAllJars() { 211 return Collections.emptyList(); 212 } 213 214 public void setReloadable(boolean r) { 215 } 216 217 public void reload() throws IOException { 218 } 219 220 protected void classLoaderUp(Set <Module> parents) throws IOException { 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 getLocalizedAttribute(String attr) { 241 return attrs.get(attr); 242 } 243 244 public Set getProblems() { 245 return problems; 246 } 247 248 } 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 <Module> modules) { 258 } 259 260 public void unload(List <Module> modules) { 261 } 262 263 public boolean closing(List <Module> modules) { 264 return true; 265 } 266 267 public void close(List <Module> modules) { 268 } 269 } } 271 | Popular Tags |