1 19 20 package org.netbeans.modules.j2ee.earproject.test; 21 22 import java.beans.PropertyVetoException ; 23 import java.io.BufferedInputStream ; 24 import java.io.BufferedOutputStream ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 import java.net.URL ; 32 import java.util.ArrayList ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.StringTokenizer ; 36 import java.util.WeakHashMap ; 37 import java.util.zip.ZipEntry ; 38 import java.util.zip.ZipInputStream ; 39 import junit.framework.Assert; 40 import org.netbeans.api.project.Project; 41 import org.netbeans.junit.NbTestCase; 42 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry; 43 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 44 import org.netbeans.spi.project.ProjectFactory; 45 import org.netbeans.spi.project.ProjectState; 46 import org.netbeans.spi.project.support.ant.AntProjectHelper; 47 import org.netbeans.spi.project.support.ant.EditableProperties; 48 import org.openide.filesystems.FileLock; 49 import org.openide.filesystems.FileObject; 50 import org.openide.filesystems.FileSystem; 51 import org.openide.filesystems.FileUtil; 52 import org.openide.filesystems.LocalFileSystem; 53 import org.openide.filesystems.MultiFileSystem; 54 import org.openide.filesystems.Repository; 55 import org.openide.filesystems.URLMapper; 56 import org.openide.filesystems.XMLFileSystem; 57 import org.openide.modules.InstalledFileLocator; 58 import org.openide.util.Lookup; 59 import org.openide.util.lookup.Lookups; 60 import org.openide.util.lookup.ProxyLookup; 61 import org.xml.sax.SAXException ; 62 63 68 public final class TestUtil extends ProxyLookup { 69 70 static { 71 TestUtil.class.getClassLoader().setDefaultAssertionStatus(true); 72 System.setProperty("org.openide.util.Lookup", TestUtil.class.getName()); 73 Assert.assertEquals(TestUtil.class, Lookup.getDefault().getClass()); 74 } 75 76 private static TestUtil DEFAULT; 77 private static final int BUFFER = 2048; 78 79 80 public TestUtil() { 81 Assert.assertNull(DEFAULT); 82 DEFAULT = this; 83 setLookup(new Object [0]); 84 } 85 86 91 public static void setLookup(Lookup l) { 92 DEFAULT.setLookups(new Lookup[] {l}); 93 } 94 95 98 public static void setLookup(Object [] instances) { 99 ClassLoader l = TestUtil.class.getClassLoader(); 100 DEFAULT.setLookups(new Lookup[] { 101 Lookups.fixed(instances), 102 Lookups.metaInfServices(l), 103 Lookups.singleton(l), 104 }); 105 } 106 107 public static void initLookup(NbTestCase test) throws Exception { 108 TestUtil.setLookup(new Object [] {new Repo(test), new IFL()}); 109 } 110 111 private static boolean warned = false; 112 117 public static FileObject makeScratchDir(NbTestCase test) throws IOException { 118 test.clearWorkDir(); 119 File root = test.getWorkDir(); 120 assert root.isDirectory() && root.list().length == 0; 121 FileObject fo = FileUtil.toFileObject(root); 122 if (fo != null) { 123 return fo; 124 } else { 125 if (!warned) { 126 warned = true; 127 System.err.println("No FileObject for " + root + " found.\n" + 128 "Maybe you need ${openide/masterfs.dir}/modules/org-netbeans-modules-masterfs.jar\n" + 129 "in test.unit.run.cp.extra, or make sure Lookups.metaInfServices is included in Lookup.default, so that\n" + 130 "Lookup.default<URLMapper>=" + Lookup.getDefault().lookup(new Lookup.Template(URLMapper.class)).allInstances() + " includes MasterURLMapper\n" + 131 "e.g. by using TestUtil.setLookup(Object[]) rather than TestUtil.setLookup(Lookup)."); 132 } 133 LocalFileSystem lfs = new LocalFileSystem(); 135 try { 136 lfs.setRootDirectory(root); 137 } catch (PropertyVetoException e) { 138 assert false : e; 139 } 140 Repository.getDefault().addFileSystem(lfs); 141 return lfs.getRoot(); 142 } 143 } 144 145 148 public static void deleteRec(File f) throws IOException { 149 if (f.isDirectory()) { 150 File [] kids = f.listFiles(); 151 if (kids == null) { 152 throw new IOException ("List " + f); 153 } 154 for (int i = 0; i < kids.length; i++) { 155 deleteRec(kids[i]); 156 } 157 } 158 if (!f.delete()) { 159 throw new IOException ("Delete " + f); 160 } 161 } 162 163 169 public static ProjectFactory testProjectFactory() { 170 return new TestProjectFactory(); 171 } 172 173 176 public static void gc() { 177 System.gc(); 178 System.runFinalization(); 179 System.gc(); 180 } 181 182 private static final Map <FileObject,Integer > loadCount = new WeakHashMap <FileObject,Integer >(); 183 184 188 public static int projectLoadCount(FileObject dir) { 189 Integer i = loadCount.get(dir); 190 if (i != null) { 191 return i.intValue(); 192 } else { 193 return 0; 194 } 195 } 196 197 204 public static void setProjectSaveWillFail(Project p, Throwable error) { 205 ((TestProject)p).error = error; 206 } 207 208 213 public static int projectSaveCount(Project p) { 214 return ((TestProject)p).saveCount; 215 } 216 217 221 public static void modify(Project p) { 222 ((TestProject)p).state.markModified(); 223 } 224 225 229 public static void notifyDeleted(Project p) { 230 ((TestProject)p).state.notifyDeleted(); 231 } 232 233 243 public static String registerSunAppServer(NbTestCase test) throws Exception { 244 return registerSunAppServer(test, new Object [0]); 245 } 246 247 public static String registerSunAppServer(NbTestCase test, Object [] additionalLookupItems) throws Exception { 248 String oldNbHome = System.getProperty("netbeans.home"); String oldNbUser = System.getProperty("netbeans.user"); File root = test.getWorkDir(); 251 File systemDir = new File (root, "ud/system"); new File (systemDir, "J2EE/InstalledServers").mkdirs(); new File (systemDir, "J2EE/DeploymentPlugins").mkdirs(); new File (root, "nb").mkdirs(); System.setProperty("netbeans.home", new File (test.getWorkDir(), "nb").getAbsolutePath()); System.setProperty("netbeans.user", new File (test.getWorkDir(), "ud").getAbsolutePath()); 258 Object [] appServerNeed = new Object [] { new Repo(test), new IFL() }; 260 Object [] instances = new Object [additionalLookupItems.length + appServerNeed.length]; 261 System.arraycopy(additionalLookupItems, 0, instances, 0, additionalLookupItems.length); 262 System.arraycopy(appServerNeed, 0, instances, additionalLookupItems.length, appServerNeed.length); 263 TestUtil.setLookup(instances); 264 265 File asRoot = null; 266 if (System.getProperty("appserv.home") != null) { asRoot = new File (System.getProperty("appserv.home")); } else { 269 asRoot = extractAppSrv(test.getWorkDir(), new File (test.getDataDir(), "SunAppServer.zip")); } 271 FileObject dir = Repository.getDefault().getDefaultFileSystem().findResource("/J2EE/InstalledServers"); String name = FileUtil.findFreeFileName(dir, "instance", null); FileObject instanceFO = dir.createData(name); 274 String serverID = "[" + asRoot.getAbsolutePath() + "]deployer:Sun:AppServer::localhost:4848"; instanceFO.setAttribute(InstanceProperties.URL_ATTR, serverID); 276 instanceFO.setAttribute(InstanceProperties.USERNAME_ATTR, "admin"); instanceFO.setAttribute(InstanceProperties.PASSWORD_ATTR, "adminadmin"); instanceFO.setAttribute(InstanceProperties.DISPLAY_NAME_ATTR, "testdname"); instanceFO.setAttribute(InstanceProperties.HTTP_PORT_NUMBER, "4848"); instanceFO.setAttribute("DOMAIN", "testdomain1"); instanceFO.setAttribute("LOCATION", new File (asRoot, "domains").getAbsolutePath()); ServerRegistry sr = ServerRegistry.getInstance(); 283 sr.addInstance(instanceFO); 284 if (oldNbHome != null) { 285 System.setProperty("netbeans.home", oldNbHome); } 287 if (oldNbUser != null) { 288 System.setProperty("netbeans.user", oldNbUser); } 290 return serverID; 291 } 292 293 private static File extractAppSrv(File destDir, File archiveFile) throws IOException { 294 ZipInputStream zis = null; 295 BufferedOutputStream dest = null; 296 try { 297 FileInputStream fis = new FileInputStream (archiveFile); 298 zis = new ZipInputStream (new BufferedInputStream (fis)); 299 ZipEntry entry; 300 while((entry = zis.getNextEntry()) != null) { 301 byte data[] = new byte[BUFFER]; 302 File entryFile = new File (destDir, entry.getName()); 303 if (entry.isDirectory()) { 304 entryFile.mkdirs(); 305 } else { 306 entryFile.getParentFile().mkdirs(); 307 FileOutputStream fos = new FileOutputStream (entryFile); 308 dest = new BufferedOutputStream (fos, BUFFER); 309 int count; 310 while ((count = zis.read(data, 0, BUFFER)) != -1) { 311 dest.write(data, 0, count); 312 } 313 dest.flush(); 314 } 315 } 316 } finally { 317 if (zis != null) { zis.close(); } 318 if (dest != null) { dest.close(); } 319 } 320 return new File (destDir, archiveFile.getName().substring(0, archiveFile.getName().length() - 4)); 321 } 322 323 public static EditableProperties loadProjectProperties( 324 final FileObject projectDir) throws IOException { 325 FileObject propsFO = projectDir.getFileObject(AntProjectHelper.PROJECT_PROPERTIES_PATH); 326 InputStream propsIS = propsFO.getInputStream(); 327 EditableProperties props = new EditableProperties(true); 328 try { 329 props.load(propsIS); 330 } finally { 331 propsIS.close(); 332 } 333 return props; 334 } 335 336 public static void storeProjectProperties(FileObject projectDir, EditableProperties props) throws IOException { 337 FileObject propsFO = projectDir.getFileObject(AntProjectHelper.PROJECT_PROPERTIES_PATH); 338 FileLock lock = propsFO.lock(); 339 try { 340 OutputStream os = propsFO.getOutputStream(lock); 341 try { 342 props.store(os); 343 } finally { 344 os.close(); 345 } 346 } finally { 347 lock.releaseLock(); 348 } 349 } 350 351 356 public static Object BROKEN_PROJECT_LOAD_LOCK = null; 357 358 private static final class TestProjectFactory implements ProjectFactory { 359 360 TestProjectFactory() {} 361 362 public Project loadProject(FileObject projectDirectory, ProjectState state) throws IOException { 363 Integer i = loadCount.get(projectDirectory); 364 if (i == null) { 365 i = 1; 366 } else { 367 i++; 368 } 369 loadCount.put(projectDirectory, i); 370 FileObject testproject = projectDirectory.getFileObject("testproject"); 371 if (testproject != null && testproject.isFolder()) { 372 if (testproject.getFileObject("broken") != null) { 373 if (BROKEN_PROJECT_LOAD_LOCK != null) { 374 synchronized (BROKEN_PROJECT_LOAD_LOCK) { 375 try { 376 BROKEN_PROJECT_LOAD_LOCK.wait(); 377 } catch (InterruptedException e) { 378 assert false : e; 379 } 380 } 381 } 382 throw new IOException ("Load failed of " + projectDirectory); 383 } else { 384 return new TestProject(projectDirectory, state); 385 } 386 } else { 387 return null; 388 } 389 } 390 391 public void saveProject(Project project) throws IOException , ClassCastException { 392 TestProject p = (TestProject)project; 393 Throwable t = p.error; 394 if (t != null) { 395 p.error = null; 396 if (t instanceof IOException ) { 397 throw (IOException )t; 398 } else if (t instanceof Error ) { 399 throw (Error )t; 400 } else { 401 throw (RuntimeException )t; 402 } 403 } 404 p.saveCount++; 405 } 406 407 public boolean isProject(FileObject dir) { 408 FileObject testproject = dir.getFileObject("testproject"); 409 return testproject != null && testproject.isFolder(); 410 } 411 412 } 413 414 private static final class TestProject implements Project { 415 416 private final FileObject dir; 417 final ProjectState state; 418 Throwable error; 419 int saveCount = 0; 420 421 public TestProject(FileObject dir, ProjectState state) { 422 this.dir = dir; 423 this.state = state; 424 } 425 426 public Lookup getLookup() { 427 return Lookup.EMPTY; 428 } 429 430 public FileObject getProjectDirectory() { 431 return dir; 432 } 433 434 public String toString() { 435 return "testproject:" + getProjectDirectory().getNameExt(); 436 } 437 438 454 455 } 456 457 465 public static FileObject createFileFromContent(URL content, FileObject parent, String path) throws IOException { 466 if (parent == null) { 467 throw new IllegalArgumentException ("null parent"); 468 } 469 Assert.assertTrue("folder", parent.isFolder()); 470 FileObject fo = parent; 471 StringTokenizer tok = new StringTokenizer (path, "/"); 472 boolean touch = false; 473 while (tok.hasMoreTokens()) { 474 Assert.assertNotNull("fo is null (parent=" + parent + " path=" + path + ")", fo); 475 String name = tok.nextToken(); 476 if (tok.hasMoreTokens()) { 477 FileObject sub = fo.getFileObject(name); 478 if (sub == null) { 479 FileObject fo2 = fo.createFolder(name); 480 Assert.assertNotNull("createFolder(" + fo + ", " + name + ") -> null", fo2); 481 fo = fo2; 482 } else { 483 Assert.assertTrue("folder", sub.isFolder()); 484 fo = sub; 485 } 486 } else { 487 FileObject sub = fo.getFileObject(name); 488 if (sub == null) { 489 FileObject fo2 = fo.createData(name); 490 Assert.assertNotNull("createData(" + fo + ", " + name + ") -> null", fo2); 491 fo = fo2; 492 } else { 493 fo = sub; 494 touch = true; 495 } 496 } 497 } 498 assert fo.isData(); 499 if (content != null || touch) { 500 FileLock lock = fo.lock(); 501 try { 502 OutputStream os = fo.getOutputStream(lock); 503 try { 504 if (content != null) { 505 InputStream is = content.openStream(); 506 try { 507 FileUtil.copy(is, os); 508 } finally { 509 is.close(); 510 } 511 } 512 } finally { 513 os.close(); 514 } 515 } finally { 516 lock.releaseLock(); 517 } 518 } 519 return fo; 520 } 521 522 private static final class Repo extends Repository { 523 524 public Repo(NbTestCase t) throws Exception { 525 super(mksystem(t)); 526 } 527 528 private static FileSystem mksystem(NbTestCase t) throws Exception { 529 LocalFileSystem lfs = new LocalFileSystem(); 530 File systemDir = new File (t.getWorkDir(), "ud/system"); 531 systemDir.mkdirs(); 532 lfs.setRootDirectory(systemDir); 533 lfs.setReadOnly(false); 534 List <FileSystem> layers = new ArrayList <FileSystem>(); 535 layers.add(lfs); 536 542 addLayer(layers, "org/netbeans/modules/j2ee/sun/ide/j2ee/layer.xml"); 544 addLayer(layers, "org/netbeans/modules/j2ee/earproject/ui/resources/layer.xml"); 545 addLayer(layers, "org/netbeans/modules/j2ee/ejbjarproject/ui/resources/layer.xml"); 547 addLayer(layers, "org/netbeans/modules/j2ee/clientproject/ui/resources/layer.xml"); 549 addLayer(layers, "org/netbeans/modules/web/project/ui/resources/layer.xml"); 551 MultiFileSystem mfs = new MultiFileSystem((FileSystem[]) layers.toArray(new FileSystem[layers.size()])); 552 return mfs; 553 } 554 555 private static void addLayer(List <FileSystem> layers, String layerRes) throws SAXException { 556 URL layerFile = Repo.class.getClassLoader().getResource(layerRes); 557 assert layerFile != null; 558 layers.add(new XMLFileSystem(layerFile)); 559 } 560 561 } 562 563 564 private static final class IFL extends InstalledFileLocator { 565 566 public IFL() {} 567 568 public File locate(String relativePath, String codeNameBase, boolean localized) { 569 if (relativePath.equals("modules/ext/appsrvbridge.jar")) { 570 String path = System.getProperty("test.appsrvbridge.jar"); 571 Assert.assertNotNull("must set test.appsrvbridge.jar", path); 572 return new File (path); 573 } 574 return null; 575 } 576 } 577 578 } 579 | Popular Tags |