1 package org.netbeans.modules.web.core.test; 2 20 21 import java.beans.PropertyVetoException ; 22 import java.io.BufferedInputStream ; 23 import java.io.BufferedOutputStream ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.OutputStream ; 30 import java.net.URL ; 31 import java.util.ArrayList ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.StringTokenizer ; 35 import java.util.WeakHashMap ; 36 import java.util.zip.ZipEntry ; 37 import java.util.zip.ZipInputStream ; 38 import junit.framework.Assert; 39 import org.netbeans.api.project.Project; 40 import org.netbeans.junit.NbTestCase; 41 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry; 42 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 43 import org.netbeans.spi.project.ProjectFactory; 44 import org.netbeans.spi.project.ProjectState; 45 import org.netbeans.spi.project.support.ant.AntProjectHelper; 46 import org.netbeans.spi.project.support.ant.EditableProperties; 47 import org.openide.filesystems.FileLock; 48 import org.openide.filesystems.FileObject; 49 import org.openide.filesystems.FileSystem; 50 import org.openide.filesystems.FileUtil; 51 import org.openide.filesystems.LocalFileSystem; 52 import org.openide.filesystems.MultiFileSystem; 53 import org.openide.filesystems.Repository; 54 import org.openide.filesystems.URLMapper; 55 import org.openide.filesystems.XMLFileSystem; 56 import org.openide.modules.InstalledFileLocator; 57 import org.openide.util.Lookup; 58 import org.openide.util.lookup.Lookups; 59 import org.openide.util.lookup.ProxyLookup; 60 import org.xml.sax.SAXException ; 61 62 66 public final class TestUtil extends ProxyLookup { 67 68 static { 69 TestUtil.class.getClassLoader().setDefaultAssertionStatus(true); 70 System.setProperty("org.openide.util.Lookup", TestUtil.class.getName()); 71 Assert.assertEquals(TestUtil.class, Lookup.getDefault().getClass()); 72 } 73 74 private static TestUtil DEFAULT; 75 private static final int BUFFER = 2048; 76 77 78 public TestUtil() { 79 Assert.assertNull(DEFAULT); 80 DEFAULT = this; 81 setLookup(new Object [0]); 82 } 83 84 89 public static void setLookup(Lookup l) { 90 DEFAULT.setLookups(new Lookup[] {l}); 91 } 92 93 96 public static void setLookup(Object [] instances) { 97 ClassLoader l = TestUtil.class.getClassLoader(); 98 DEFAULT.setLookups(new Lookup[] { 99 Lookups.fixed(instances), 100 Lookups.metaInfServices(l), 101 Lookups.singleton(l), 102 }); 103 } 104 105 private static boolean warned = false; 106 111 public static FileObject makeScratchDir(NbTestCase test) throws IOException { 112 test.clearWorkDir(); 113 File root = test.getWorkDir(); 114 assert root.isDirectory() && root.list().length == 0; 115 FileObject fo = FileUtil.toFileObject(root); 116 if (fo != null) { 117 return fo; 118 } else { 119 if (!warned) { 120 warned = true; 121 System.err.println("No FileObject for " + root + " found.\n" + 122 "Maybe you need ${openide/masterfs.dir}/modules/org-netbeans-modules-masterfs.jar\n" + 123 "in test.unit.run.cp.extra, or make sure Lookups.metaInfServices is included in Lookup.default, so that\n" + 124 "Lookup.default<URLMapper>=" + Lookup.getDefault().lookup(new Lookup.Template(URLMapper.class)).allInstances() + " includes MasterURLMapper\n" + 125 "e.g. by using TestUtil.setLookup(Object[]) rather than TestUtil.setLookup(Lookup)."); 126 } 127 LocalFileSystem lfs = new LocalFileSystem(); 129 try { 130 lfs.setRootDirectory(root); 131 } catch (PropertyVetoException e) { 132 assert false : e; 133 } 134 Repository.getDefault().addFileSystem(lfs); 135 return lfs.getRoot(); 136 } 137 } 138 139 142 public static void deleteRec(File f) throws IOException { 143 if (f.isDirectory()) { 144 File [] kids = f.listFiles(); 145 if (kids == null) { 146 throw new IOException ("List " + f); 147 } 148 for (int i = 0; i < kids.length; i++) { 149 deleteRec(kids[i]); 150 } 151 } 152 if (!f.delete()) { 153 throw new IOException ("Delete " + f); 154 } 155 } 156 157 163 public static ProjectFactory testProjectFactory() { 164 return new TestProjectFactory(); 165 } 166 167 170 public static void gc() { 171 System.gc(); 172 System.runFinalization(); 173 System.gc(); 174 } 175 176 private static final Map <FileObject,Integer > loadCount = new WeakHashMap (); 177 178 182 public static int projectLoadCount(FileObject dir) { 183 Integer i = (Integer )loadCount.get(dir); 184 if (i != null) { 185 return i.intValue(); 186 } else { 187 return 0; 188 } 189 } 190 191 198 public static void setProjectSaveWillFail(Project p, Throwable error) { 199 ((TestProject)p).error = error; 200 } 201 202 207 public static int projectSaveCount(Project p) { 208 return ((TestProject)p).saveCount; 209 } 210 211 215 public static void modify(Project p) { 216 ((TestProject)p).state.markModified(); 217 } 218 219 223 public static void notifyDeleted(Project p) { 224 ((TestProject)p).state.notifyDeleted(); 225 } 226 227 237 public static String registerSunAppServer(NbTestCase test) throws Exception { 238 return registerSunAppServer(test, new Object [0]); 239 } 240 241 public static String registerSunAppServer(NbTestCase test, Object [] additionalLookupItems) throws Exception { 242 String oldNbHome = System.getProperty("netbeans.home"); String oldNbUser = System.getProperty("netbeans.user"); File root = test.getWorkDir(); 245 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()); 252 Object [] appServerNeed = new Object [] { new Repo(test), new IFL() }; 254 Object [] instances = new Object [additionalLookupItems.length + appServerNeed.length]; 255 System.arraycopy(additionalLookupItems, 0, instances, 0, additionalLookupItems.length); 256 System.arraycopy(appServerNeed, 0, instances, additionalLookupItems.length, appServerNeed.length); 257 TestUtil.setLookup(instances); 258 259 File asRoot = null; 260 if (System.getProperty("appserv.home") != null) { asRoot = new File (System.getProperty("appserv.home")); } else { 263 asRoot = extractAppSrv(test.getWorkDir(), new File (test.getDataDir(), "SunAppServer.zip")); } 265 FileObject dir = Repository.getDefault().getDefaultFileSystem().findResource("/J2EE/InstalledServers"); String name = FileUtil.findFreeFileName(dir, "instance", null); FileObject instanceFO = dir.createData(name); 268 String serverID = "[" + asRoot.getAbsolutePath() + "]deployer:Sun:AppServer::localhost:4848"; instanceFO.setAttribute(InstanceProperties.URL_ATTR, serverID); 270 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(); 277 sr.addInstance(instanceFO); 278 if (oldNbHome != null) { 279 System.setProperty("netbeans.home", oldNbHome); } 281 if (oldNbUser != null) { 282 System.setProperty("netbeans.user", oldNbUser); } 284 return serverID; 285 } 286 287 private static File extractAppSrv(File destDir, File archiveFile) throws IOException { 288 ZipInputStream zis = null; 289 BufferedOutputStream dest = null; 290 try { 291 FileInputStream fis = new FileInputStream (archiveFile); 292 zis = new ZipInputStream (new BufferedInputStream (fis)); 293 ZipEntry entry; 294 while((entry = zis.getNextEntry()) != null) { 295 byte data[] = new byte[BUFFER]; 296 File entryFile = new File (destDir, entry.getName()); 297 if (entry.isDirectory()) { 298 entryFile.mkdirs(); 299 } else { 300 entryFile.getParentFile().mkdirs(); 301 FileOutputStream fos = new FileOutputStream (entryFile); 302 dest = new BufferedOutputStream (fos, BUFFER); 303 int count; 304 while ((count = zis.read(data, 0, BUFFER)) != -1) { 305 dest.write(data, 0, count); 306 } 307 dest.flush(); 308 } 309 } 310 } finally { 311 if (zis != null) { zis.close(); } 312 if (dest != null) { dest.close(); } 313 } 314 return new File (destDir, archiveFile.getName().substring(0, archiveFile.getName().length() - 4)); 315 } 316 317 public static EditableProperties loadProjectProperties( 318 final FileObject projectDir) throws IOException { 319 FileObject propsFO = projectDir.getFileObject(AntProjectHelper.PROJECT_PROPERTIES_PATH); 320 InputStream propsIS = propsFO.getInputStream(); 321 EditableProperties props = new EditableProperties(true); 322 try { 323 props.load(propsIS); 324 } finally { 325 propsIS.close(); 326 } 327 return props; 328 } 329 330 public static void storeProjectProperties(FileObject projectDir, EditableProperties props) throws IOException { 331 FileObject propsFO = projectDir.getFileObject(AntProjectHelper.PROJECT_PROPERTIES_PATH); 332 FileLock lock = propsFO.lock(); 333 try { 334 OutputStream os = propsFO.getOutputStream(lock); 335 try { 336 props.store(os); 337 } finally { 338 os.close(); 339 } 340 } finally { 341 lock.releaseLock(); 342 } 343 } 344 345 350 public static Object BROKEN_PROJECT_LOAD_LOCK = null; 351 352 private static final class TestProjectFactory implements ProjectFactory { 353 354 TestProjectFactory() {} 355 356 public Project loadProject(FileObject projectDirectory, ProjectState state) throws IOException { 357 Integer i = loadCount.get(projectDirectory); 358 if (i == null) { 359 i = 1; 360 } else { 361 i++; 362 } 363 loadCount.put(projectDirectory, i); 364 FileObject testproject = projectDirectory.getFileObject("testproject"); 365 if (testproject != null && testproject.isFolder()) { 366 if (testproject.getFileObject("broken") != null) { 367 if (BROKEN_PROJECT_LOAD_LOCK != null) { 368 synchronized (BROKEN_PROJECT_LOAD_LOCK) { 369 try { 370 BROKEN_PROJECT_LOAD_LOCK.wait(); 371 } catch (InterruptedException e) { 372 assert false : e; 373 } 374 } 375 } 376 throw new IOException ("Load failed of " + projectDirectory); 377 } else { 378 return new TestProject(projectDirectory, state); 379 } 380 } else { 381 return null; 382 } 383 } 384 385 public void saveProject(Project project) throws IOException , ClassCastException { 386 TestProject p = (TestProject)project; 387 Throwable t = p.error; 388 if (t != null) { 389 p.error = null; 390 if (t instanceof IOException ) { 391 throw (IOException )t; 392 } else if (t instanceof Error ) { 393 throw (Error )t; 394 } else { 395 throw (RuntimeException )t; 396 } 397 } 398 p.saveCount++; 399 } 400 401 public boolean isProject(FileObject dir) { 402 FileObject testproject = dir.getFileObject("testproject"); 403 return testproject != null && testproject.isFolder(); 404 } 405 406 } 407 408 private static final class TestProject implements Project { 409 410 private final FileObject dir; 411 final ProjectState state; 412 Throwable error; 413 int saveCount = 0; 414 415 public TestProject(FileObject dir, ProjectState state) { 416 this.dir = dir; 417 this.state = state; 418 } 419 420 public Lookup getLookup() { 421 return Lookup.EMPTY; 422 } 423 424 public FileObject getProjectDirectory() { 425 return dir; 426 } 427 428 public String toString() { 429 return "testproject:" + getProjectDirectory().getNameExt(); 430 } 431 432 448 449 } 450 451 459 public static FileObject createFileFromContent(URL content, FileObject parent, String path) throws IOException { 460 if (parent == null) { 461 throw new IllegalArgumentException ("null parent"); 462 } 463 Assert.assertTrue("folder", parent.isFolder()); 464 FileObject fo = parent; 465 StringTokenizer tok = new StringTokenizer (path, "/"); 466 boolean touch = false; 467 while (tok.hasMoreTokens()) { 468 Assert.assertNotNull("fo is null (parent=" + parent + " path=" + path + ")", fo); 469 String name = tok.nextToken(); 470 if (tok.hasMoreTokens()) { 471 FileObject sub = fo.getFileObject(name); 472 if (sub == null) { 473 FileObject fo2 = fo.createFolder(name); 474 Assert.assertNotNull("createFolder(" + fo + ", " + name + ") -> null", fo2); 475 fo = fo2; 476 } else { 477 Assert.assertTrue("folder", sub.isFolder()); 478 fo = sub; 479 } 480 } else { 481 FileObject sub = fo.getFileObject(name); 482 if (sub == null) { 483 FileObject fo2 = fo.createData(name); 484 Assert.assertNotNull("createData(" + fo + ", " + name + ") -> null", fo2); 485 fo = fo2; 486 } else { 487 fo = sub; 488 touch = true; 489 } 490 } 491 } 492 assert fo.isData(); 493 if (content != null || touch) { 494 FileLock lock = fo.lock(); 495 try { 496 OutputStream os = fo.getOutputStream(lock); 497 try { 498 if (content != null) { 499 InputStream is = content.openStream(); 500 try { 501 FileUtil.copy(is, os); 502 } finally { 503 is.close(); 504 } 505 } 506 } finally { 507 os.close(); 508 } 509 } finally { 510 lock.releaseLock(); 511 } 512 } 513 return fo; 514 } 515 516 private static final class Repo extends Repository { 517 518 public Repo(NbTestCase t) throws Exception { 519 super(mksystem(t)); 520 } 521 522 private static FileSystem mksystem(NbTestCase t) throws Exception { 523 LocalFileSystem lfs = new LocalFileSystem(); 524 File systemDir = new File (t.getWorkDir(), "ud/system"); 525 systemDir.mkdirs(); 526 lfs.setRootDirectory(systemDir); 527 lfs.setReadOnly(false); 528 List <FileSystem> layers = new ArrayList <FileSystem>(); 529 layers.add(lfs); 530 536 537 MultiFileSystem mfs = new MultiFileSystem((FileSystem[]) layers.toArray(new FileSystem[layers.size()])); 541 return mfs; 542 } 543 544 private static void addLayer(List <FileSystem> layers, String layerRes) throws SAXException { 545 URL layerFile = Repo.class.getClassLoader().getResource(layerRes); 546 assert layerFile != null; 547 layers.add(new XMLFileSystem(layerFile)); 548 } 549 550 } 551 552 553 private static final class IFL extends InstalledFileLocator { 554 555 public IFL() {} 556 557 public File locate(String relativePath, String codeNameBase, boolean localized) { 558 if (relativePath.equals("modules/ext/appsrvbridge.jar")) { 559 String path = System.getProperty("test.appsrvbridge.jar"); 560 Assert.assertNotNull("must set test.appsrvbridge.jar", path); 561 return new File (path); 562 } 563 return null; 564 } 565 } 566 567 } 568 | Popular Tags |