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