1 19 20 package org.netbeans.modules.apisupport.project.metainf; 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.io.PrintStream ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import java.util.logging.Handler ; 28 import java.util.logging.Level ; 29 import java.util.logging.LogRecord ; 30 import junit.framework.Test; 31 import junit.framework.TestSuite; 32 import org.netbeans.api.project.ProjectManager; 33 import org.netbeans.api.project.ui.OpenProjects; 34 import org.netbeans.junit.NbTestSuite; 35 import org.netbeans.modules.apisupport.project.NbModuleProject; 36 import org.netbeans.modules.apisupport.project.TestBase; 37 import org.netbeans.modules.apisupport.project.metainf.SUtil; 38 import org.netbeans.modules.apisupport.project.metainf.ServiceNodeHandler.ServiceRootChildren; 39 import org.netbeans.modules.apisupport.project.suite.SuiteProject; 40 import org.netbeans.modules.apisupport.project.ui.customizer.SuiteProperties; 41 import org.netbeans.spi.project.support.ant.EditableProperties; 42 import org.openide.filesystems.FileLock; 43 import org.openide.filesystems.FileObject; 44 import org.openide.filesystems.FileSystem.AtomicAction; 45 import org.openide.filesystems.FileUtil; 46 import org.openide.nodes.Node; 47 51 public class ServiceNodeHandlerTest extends TestBase { 52 NbModuleProject prj; 53 ServiceNodeHandler nodeHandler; 54 ServiceNodeHandler.ServiceRootNode serviceRootNode; 55 LoggingHandler log; 56 57 public ServiceNodeHandlerTest(java.lang.String testName) { 58 super(testName); 59 } 60 61 protected void setUp() throws Exception { 62 super.setUp(); 63 log = new LoggingHandler(); 64 SUtil.getLogger().setLevel(Level.INFO); 65 SUtil.getLogger().addHandler(log); 66 } 67 68 private void setUpSimpleSuite() throws Exception { 69 SuiteProject suite = TestBase.generateSuite(getWorkDir(), "suite"); 70 EditableProperties ep = suite.getHelper().getProperties("nbproject/platform.properties"); 71 ep.setProperty(SuiteProperties.DISABLED_CLUSTERS_PROPERTY, CLUSTER_IDE + "," + CLUSTER_ENTERPRISE); 72 suite.getHelper().putProperties("nbproject/platform.properties", ep); 73 ProjectManager.getDefault().saveProject(suite); 74 75 prj = TestBase.generateSuiteComponent(suite, "prj1"); 76 nodeHandler = (ServiceNodeHandler) prj.getLookup().lookup(ServiceNodeHandler.class); 77 serviceRootNode = (ServiceNodeHandler.ServiceRootNode) nodeHandler.createServiceRootNode(); 78 } 79 80 private void setUpStandaloneModule() throws Exception { 81 prj = TestBase.generateStandaloneModule(getWorkDir(), "prj1"); 82 nodeHandler = (ServiceNodeHandler) prj.getLookup().lookup(ServiceNodeHandler.class); 83 serviceRootNode = (ServiceNodeHandler.ServiceRootNode) nodeHandler.createServiceRootNode(); 84 } 85 public void testSuiteWithManyProjects() throws IOException , Exception { 86 SuiteProject suite = TestBase.generateSuite(getWorkDir(), "suite"); 87 88 EditableProperties ep = suite.getHelper().getProperties("nbproject/platform.properties"); 89 ep.setProperty(SuiteProperties.DISABLED_CLUSTERS_PROPERTY, CLUSTER_IDE + "," + CLUSTER_ENTERPRISE + "," + CLUSTER_PLATFORM); 90 suite.getHelper().putProperties("nbproject/platform.properties", ep); 91 ProjectManager.getDefault().saveProject(suite); 92 93 NbModuleProject prj1 = TestBase.generateSuiteComponent(suite, "prj1"); 94 NbModuleProject prj2 = TestBase.generateSuiteComponent(suite, "prj2"); 95 NbModuleProject prj3 = TestBase.generateSuiteComponent(suite,"prj3"); 96 nodeHandler = (ServiceNodeHandler) prj1.getLookup().lookup(ServiceNodeHandler.class); 97 serviceRootNode = (ServiceNodeHandler.ServiceRootNode) nodeHandler.createServiceRootNode(); 98 exploreNodes(nodeHandler.moduleChild); 100 exploreNodes(nodeHandler.allInContextChild); 101 102 assertNodes("org.myservice","",""); 103 104 writeServices(prj2,"org.myservice","impl"); 105 assertNodes("org.myservice", 106 "", 107 "org.myservice,org.myservice,impl,impl," 108 ); 109 110 nodeHandler = (ServiceNodeHandler) prj3.getLookup().lookup(ServiceNodeHandler.class); 111 serviceRootNode = (ServiceNodeHandler.ServiceRootNode) nodeHandler.createServiceRootNode(); 112 exploreNodes(nodeHandler.moduleChild); 114 exploreNodes(nodeHandler.allInContextChild); 115 116 assertNodes("org.myservice", 117 "", 118 "org.myservice,org.myservice,impl,impl," 119 ); 120 121 nodeHandler = (ServiceNodeHandler) prj2.getLookup().lookup(ServiceNodeHandler.class); 122 serviceRootNode = (ServiceNodeHandler.ServiceRootNode) nodeHandler.createServiceRootNode(); 123 exploreNodes(nodeHandler.moduleChild); 124 exploreNodes(nodeHandler.allInContextChild); 125 Service service = (Service)((List ) nodeHandler.moduleServiceMap.get("org.myservice")).get(0); 126 assertNotNull("prj2 contains org.myservice",service); 127 service.getClasses().clear(); 128 service.write(prj2); 129 assertNull("org.myservice is deleted",prj2.getSourceDirectory().getFileObject("META-INF/services/org.myservice")); 130 131 deleteServiceFolder(prj1); 133 deleteServiceFolder(prj2); 134 deleteServiceFolder(prj3); 135 136 } 137 private static void writeServices(NbModuleProject prj,String serviceType,String classes) throws IOException { 138 FileObject servicesFolder = SUtil.getServicesFolder(prj,true); 139 FileObject fo = FileUtil.createData(servicesFolder,serviceType); 140 System.out.println("writeServices:" + fo.getPath()); 141 FileLock lock = fo.lock(); 142 OutputStream os = fo.getOutputStream(lock); 143 PrintStream ps = new PrintStream (os); 144 ps.println(classes); 145 ps.close(); 146 os.close(); 147 lock.releaseLock(); 148 } 149 159 160 public void testAModuleInSuite() throws Exception { 161 FileLock lock = null; 162 try { 163 setUpSimpleSuite(); 164 lock = doSingleModule(); 165 } finally { 166 if (lock != null) { 167 lock.releaseLock(); 168 } 169 } 170 } 171 public void testStandaloneModule() throws Exception { 172 FileLock lock = null; 173 try { 174 setUpStandaloneModule(); 175 lock = doSingleModule(); 176 } finally { 177 if (lock != null) { 178 lock.releaseLock(); 179 } 180 } 181 } 182 private void deleteService(FileObject prjRoot,String name) throws IOException { 183 FileObject fo = prjRoot.getFileObject("src/META-INF/services/" + name); 184 if (fo != null) { 185 fo.delete(); 186 } 187 } 188 public void testNbOrgModule() throws Exception { 189 FileObject prjFo = nbCVSRoot().getFileObject("openide/fs"); 190 FileObject prj2Fo = nbCVSRoot().getFileObject("openide/masterfs"); 191 deleteService(prjFo,"org.myservice"); 192 deleteService(prj2Fo,"org.myservice"); 193 try { 194 NbModuleProject prj = (NbModuleProject) ProjectManager.getDefault().findProject(prjFo); 195 NbModuleProject prj2 = (NbModuleProject) ProjectManager.getDefault().findProject(prj2Fo); 196 OpenProjects.getDefault().open(new NbModuleProject[]{prj,prj2}, false); 197 nodeHandler = (ServiceNodeHandler) prj.getLookup().lookup(ServiceNodeHandler.class); 198 serviceRootNode = (ServiceNodeHandler.ServiceRootNode) nodeHandler.createServiceRootNode(); 199 exploreNodes(nodeHandler.moduleChild); 200 exploreNodes(nodeHandler.allInContextChild); 201 202 assertNodes("org.myservice","",""); 203 writeServices(prj,"org.myservice","impl"); 204 assertNodes("org.myservice", 205 "org.myservice,org.myservice,impl,impl,", 206 "org.myservice,<b>org.myservice</b>,impl,<b>impl</b>," 207 ); 208 OpenProjects.getDefault().open(new NbModuleProject[]{prj2}, false); 210 writeServices(prj2,"org.myservice","impl2"); 211 assertNodes("org.myservice", 212 "org.myservice,org.myservice,impl,impl,", 213 "org.myservice,<b>org.myservice</b>,impl,<b>impl</b>,impl2,impl2,"); 214 215 } finally { 216 deleteService(prjFo,"org.myservice"); 217 deleteService(prj2Fo,"org.myservice"); 218 } 219 } 220 221 private FileLock doSingleModule() throws IOException , Exception { 222 FileLock lock; 223 224 assertTrue("testing equals of ServiceNodeHandler",nodeHandler.equals(nodeHandler)); 225 226 FileObject servicesFolder = SUtil.getServicesFolder(prj,true); 229 servicesFolder.delete(); 230 servicesFolder = SUtil.getServicesFolder(prj,true); 231 232 exploreNodes(nodeHandler.moduleChild); 233 exploreNodes(nodeHandler.allInContextChild); 234 assertNotNull(nodeHandler.moduleServiceMap); 235 String serviceType = "org.openide.filesystems.Repository"; 236 assertNodes(serviceType,"","org.openide.filesystems.Repository,org.openide.filesystems.Repository,org.netbeans.core.startup.NbRepository,org.netbeans.core.startup.NbRepository,"); 237 FileObject fo = servicesFolder.createData(serviceType); 238 assertNodes(serviceType, 240 "org.openide.filesystems.Repository,org.openide.filesystems.Repository,", 241 "org.openide.filesystems.Repository,<b>org.openide.filesystems.Repository</b>,org.netbeans.core.startup.NbRepository,org.netbeans.core.startup.NbRepository,"); 242 243 lock = fo.lock(); 244 OutputStream os = fo.getOutputStream(lock); 245 PrintStream ps = new PrintStream (os); 246 ps.println("org.nic.Repository"); 247 ps.close(); 248 os.close(); 249 assertNodes(serviceType,"org.openide.filesystems.Repository,org.openide.filesystems.Repository,org.nic.Repository,org.nic.Repository,", 250 "org.openide.filesystems.Repository,<b>org.openide.filesystems.Repository</b>,org.netbeans.core.startup.NbRepository,org.netbeans.core.startup.NbRepository,org.nic.Repository,<b>org.nic.Repository</b>,"); 251 252 os = fo.getOutputStream(lock); 253 ps = new PrintStream (os); 254 ps.println("org.nic.Repository"); 255 ps.println("-org.netbeans.core.startup.NbRepository"); 256 ps.close(); 257 os.close(); 258 assertNodes(serviceType,"org.openide.filesystems.Repository,org.openide.filesystems.Repository,-org.netbeans.core.startup.NbRepository,-org.netbeans.core.startup.NbRepository,org.nic.Repository,org.nic.Repository," 259 ,"org.openide.filesystems.Repository,<b>org.openide.filesystems.Repository</b>,org.netbeans.core.startup.NbRepository,<s>org.netbeans.core.startup.NbRepository</s>,org.nic.Repository,<b>org.nic.Repository</b>,"); 260 261 262 os = fo.getOutputStream(lock); 263 ps = new PrintStream (os); 264 ps.println("org.nic2.ErrorManager"); 265 ps.close(); 266 os.close(); 267 assertNodes(serviceType,"org.openide.filesystems.Repository,org.openide.filesystems.Repository,org.nic2.ErrorManager,org.nic2.ErrorManager,", 268 "org.openide.filesystems.Repository,<b>org.openide.filesystems.Repository</b>,org.netbeans.core.startup.NbRepository,org.netbeans.core.startup.NbRepository,org.nic2.ErrorManager,<b>org.nic2.ErrorManager</b>,"); 269 fo.delete(lock); 270 assertNodes(serviceType,"","org.openide.filesystems.Repository,org.openide.filesystems.Repository,org.netbeans.core.startup.NbRepository,org.netbeans.core.startup.NbRepository,"); 271 return lock; 272 } 273 274 275 281 private void assertNodes(String serviceType,String moduleNodes,String moduleInContextNodes) { 282 ServiceNodeHandler.ServiceRootChildren children = nodeHandler.moduleChild ; 283 log("module nodes"); 284 assertEquals(moduleNodes,printChildren(serviceType,children)); 285 children = nodeHandler.allInContextChild; 286 log("module in context nodes"); 287 assertEquals(moduleInContextNodes,printChildren(serviceType,children)); 288 } 289 290 private String printChildren(final String serviceType,final ServiceRootChildren serviceRootChildren) { 291 StringBuffer buff = new StringBuffer (); 292 Node nodes[] = null; final Node nodesRef[][] = new Node[][]{null}; 294 295 nodesRef[0] = serviceRootChildren.getNodes(true); 296 nodes = nodesRef[0]; 297 for (int i = 0 ; i < nodes.length; i++) { 298 if (nodes[i].getName().equals(serviceType)) { 299 printNode(nodes[i],buff); 300 } 301 } 302 log(buff.toString()); 303 return buff.toString(); 304 } 305 306 private void printNodes(Node[] nodes,StringBuffer buff) { 307 for (int i = 0 ; i < nodes.length; i++) { 308 printNode(nodes[i],buff); 309 } 310 } 311 312 private void printNode(Node node,StringBuffer buff) { 313 buff.append(node.getName() + ","); 314 buff.append(node.getHtmlDisplayName() + ","); 315 Node nodes[] = node.getChildren().getNodes(true); 316 printNodes(nodes,buff); 317 } 318 319 public void log (String string) { 320 System.out.println(string); 321 super.log(string); 322 } 323 324 private Node[] exploreNodes(ServiceNodeHandler.ServiceRootChildren children) throws Exception { 325 List events = new ArrayList (); 326 events.add(SUtil.LOG_COMPUTE_KEYS); 327 events.add(SUtil.LOG_END_COMPUTE_KEYS); 328 log.setEvents(events); 329 children.getNodes(true); 330 log.waitToEvents(); 331 SUtil.log("Test.exploreNodes : node computed"); 332 return children.getNodes(true); 333 } 334 335 private static void deleteServiceFolder(NbModuleProject prj) throws IOException { 336 FileObject srcDir = prj.getSourceDirectory(); 337 FileObject serviceFo = FileUtil.createData(srcDir,"META-INF/services"); 338 final FileObject miFo = FileUtil.createData(srcDir,"META-INF"); 339 miFo.getFileSystem().runAtomicAction(new AtomicAction() { 340 public void run() throws IOException { 341 miFo.delete(); 342 } 343 }); 344 } 345 346 static class LoggingHandler extends Handler { 347 private List events; 348 private int index; 349 private boolean bWait; 350 public void publish(LogRecord rec) { 351 synchronized(this) { 352 if (events != null && index < events.size() && rec.getMessage().equals(events.get(index))) { 353 index++; 354 } 355 if (events != null && index == events.size() && bWait) { 356 bWait = false; 357 System.out.println("notify"); 358 notify(); 359 System.out.println("end not"); 360 } 361 362 } 363 } 364 public synchronized void setEvents(List events) { 365 this.events = events; 366 index = 0; 367 368 } 369 public synchronized void waitToEvents() { 370 if(events != null && index < events.size() ) { 371 try { 372 bWait = true; 373 System.out.println("wait"); 374 wait(); 375 } catch (InterruptedException ex) { 376 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, 377 ex.getMessage(), 378 ex); 379 } 380 } else { 381 System.out.println("no wait"); 382 } 383 } 384 public void flush() { 385 386 } 387 388 public void close() throws SecurityException { 389 } 390 } 391 392 } 414 | Popular Tags |