1 19 20 package org.netbeans.modules.j2ee.clientproject.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.clientproject.TestPlatformProvider; 43 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry; 44 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 45 import org.netbeans.spi.project.ProjectFactory; 46 import org.netbeans.spi.project.ProjectState; 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.util.Lookup; 57 import org.openide.util.lookup.Lookups; 58 import org.openide.util.lookup.ProxyLookup; 59 import org.xml.sax.SAXException ; 60 61 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 public static void initLookup(NbTestCase test) throws Exception { 106 TestUtil.setLookup(new Object [] {new Repo(test)}); 107 } 108 109 private static boolean warned = false; 110 115 public static FileObject makeScratchDir(NbTestCase test) throws IOException { 116 test.clearWorkDir(); 117 File root = test.getWorkDir(); 118 assert root.isDirectory() && root.list().length == 0; 119 FileObject fo = FileUtil.toFileObject(root); 120 if (fo != null) { 121 return fo; 122 } else { 123 if (!warned) { 124 warned = true; 125 System.err.println("No FileObject for " + root + " found.\n" + 126 "Maybe you need ${openide/masterfs.dir}/modules/org-netbeans-modules-masterfs.jar\n" + 127 "in test.unit.run.cp.extra, or make sure Lookups.metaInfServices is included in Lookup.default, so that\n" + 128 "Lookup.default<URLMapper>=" + Lookup.getDefault().lookup(new Lookup.Template(URLMapper.class)).allInstances() + " includes MasterURLMapper\n" + 129 "e.g. by using TestUtil.setLookup(Object[]) rather than TestUtil.setLookup(Lookup)."); 130 } 131 LocalFileSystem lfs = new LocalFileSystem(); 133 try { 134 lfs.setRootDirectory(root); 135 } catch (PropertyVetoException e) { 136 assert false : e; 137 } 138 Repository.getDefault().addFileSystem(lfs); 139 return lfs.getRoot(); 140 } 141 } 142 143 146 public static void deleteRec(File f) throws IOException { 147 if (f.isDirectory()) { 148 File [] kids = f.listFiles(); 149 if (kids == null) { 150 throw new IOException ("List " + f); 151 } 152 for (int i = 0; i < kids.length; i++) { 153 deleteRec(kids[i]); 154 } 155 } 156 if (!f.delete()) { 157 throw new IOException ("Delete " + f); 158 } 159 } 160 161 167 public static ProjectFactory testProjectFactory() { 168 return new TestProjectFactory(); 169 } 170 171 174 public static void gc() { 175 System.gc(); 176 System.runFinalization(); 177 System.gc(); 178 } 179 180 private static final Map <FileObject,Integer > loadCount = new WeakHashMap <FileObject,Integer >(); 181 182 186 public static int projectLoadCount(FileObject dir) { 187 Integer i = loadCount.get(dir); 188 if (i != null) { 189 return i.intValue(); 190 } else { 191 return 0; 192 } 193 } 194 195 202 public static void setProjectSaveWillFail(Project p, Throwable error) { 203 ((TestProject)p).error = error; 204 } 205 206 211 public static int projectSaveCount(Project p) { 212 return ((TestProject)p).saveCount; 213 } 214 215 219 public static void modify(Project p) { 220 ((TestProject)p).state.markModified(); 221 } 222 223 227 public static void notifyDeleted(Project p) { 228 ((TestProject)p).state.notifyDeleted(); 229 } 230 231 241 public static String registerSunAppServer(NbTestCase test) throws Exception { 242 String oldNbHome = System.getProperty("netbeans.home"); String oldNbUser = System.getProperty("netbeans.user"); File workDir = test.getWorkDir(); 245 File systemDir = new File (workDir, "ud/system"); new File (systemDir, "J2EE/InstalledServers").mkdirs(); new File (systemDir, "J2EE/DeploymentPlugins").mkdirs(); new File (workDir, "nb").mkdirs(); System.setProperty("netbeans.home", new File (workDir, "nb").getAbsolutePath()); System.setProperty("netbeans.user", new File (workDir, "ud").getAbsolutePath()); TestUtil.setLookup(new Object [] {new Repo(test), new TestPlatformProvider()}); 252 File asRoot = extractAppSrv(workDir, new File (test.getDataDir(), "SunAppServer.zip")); FileObject dir = Repository.getDefault().getDefaultFileSystem().findResource("/J2EE/InstalledServers"); String name = FileUtil.findFreeFileName(dir, "instance", null); FileObject instanceFO = dir.createData(name); 256 String serverID = "[" + asRoot.getAbsolutePath() + "]deployer:Sun:AppServer::localhost:4848"; instanceFO.setAttribute(InstanceProperties.URL_ATTR, serverID); 258 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(); 265 sr.addInstance(instanceFO); 266 if (oldNbHome != null) { 267 System.setProperty("netbeans.home", oldNbHome); } 269 if (oldNbUser != null) { 270 System.setProperty("netbeans.user", oldNbUser); } 272 return serverID; 273 } 274 275 private static File extractAppSrv(File destDir, File archiveFile) throws IOException { 276 ZipInputStream zis = null; 277 BufferedOutputStream dest = null; 278 try { 279 FileInputStream fis = new FileInputStream (archiveFile); 280 zis = new ZipInputStream (new BufferedInputStream (fis)); 281 ZipEntry entry; 282 while((entry = zis.getNextEntry()) != null) { 283 byte data[] = new byte[BUFFER]; 284 File entryFile = new File (destDir, entry.getName()); 285 if (entry.isDirectory()) { 286 entryFile.mkdirs(); 287 } else { 288 entryFile.getParentFile().mkdirs(); 289 FileOutputStream fos = new FileOutputStream (entryFile); 290 dest = new BufferedOutputStream (fos, BUFFER); 291 int count; 292 while ((count = zis.read(data, 0, BUFFER)) != -1) { 293 dest.write(data, 0, count); 294 } 295 dest.flush(); 296 } 297 } 298 } finally { 299 if (zis != null) { zis.close(); } 300 if (dest != null) { dest.close(); } 301 } 302 return new File (destDir, archiveFile.getName().substring(0, archiveFile.getName().length() - 4)); 303 } 304 305 310 public static Object BROKEN_PROJECT_LOAD_LOCK = null; 311 312 private static final class TestProjectFactory implements ProjectFactory { 313 314 TestProjectFactory() {} 315 316 public Project loadProject(FileObject projectDirectory, ProjectState state) throws IOException { 317 Integer i = loadCount.get(projectDirectory); 318 if (i == null) { 319 i = 1; 320 } else { 321 i++; 322 } 323 loadCount.put(projectDirectory, i); 324 FileObject testproject = projectDirectory.getFileObject("testproject"); 325 if (testproject != null && testproject.isFolder()) { 326 if (testproject.getFileObject("broken") != null) { 327 if (BROKEN_PROJECT_LOAD_LOCK != null) { 328 synchronized (BROKEN_PROJECT_LOAD_LOCK) { 329 try { 330 BROKEN_PROJECT_LOAD_LOCK.wait(); 331 } catch (InterruptedException e) { 332 assert false : e; 333 } 334 } 335 } 336 throw new IOException ("Load failed of " + projectDirectory); 337 } else { 338 return new TestProject(projectDirectory, state); 339 } 340 } else { 341 return null; 342 } 343 } 344 345 public void saveProject(Project project) throws IOException , ClassCastException { 346 TestProject p = (TestProject)project; 347 Throwable t = p.error; 348 if (t != null) { 349 p.error = null; 350 if (t instanceof IOException ) { 351 throw (IOException )t; 352 } else if (t instanceof Error ) { 353 throw (Error )t; 354 } else { 355 throw (RuntimeException )t; 356 } 357 } 358 p.saveCount++; 359 } 360 361 public boolean isProject(FileObject dir) { 362 FileObject testproject = dir.getFileObject("testproject"); 363 return testproject != null && testproject.isFolder(); 364 } 365 366 } 367 368 private static final class TestProject implements Project { 369 370 private final FileObject dir; 371 final ProjectState state; 372 Throwable error; 373 int saveCount = 0; 374 375 public TestProject(FileObject dir, ProjectState state) { 376 this.dir = dir; 377 this.state = state; 378 } 379 380 public Lookup getLookup() { 381 return Lookup.EMPTY; 382 } 383 384 public FileObject getProjectDirectory() { 385 return dir; 386 } 387 388 public String toString() { 389 return "testproject:" + getProjectDirectory().getNameExt(); 390 } 391 392 408 409 } 410 411 419 public static FileObject createFileFromContent(URL content, FileObject parent, String path) throws IOException { 420 if (parent == null) { 421 throw new IllegalArgumentException ("null parent"); 422 } 423 Assert.assertTrue("folder", parent.isFolder()); 424 FileObject fo = parent; 425 StringTokenizer tok = new StringTokenizer (path, "/"); 426 boolean touch = false; 427 while (tok.hasMoreTokens()) { 428 Assert.assertNotNull("fo is null (parent=" + parent + " path=" + path + ")", fo); 429 String name = tok.nextToken(); 430 if (tok.hasMoreTokens()) { 431 FileObject sub = fo.getFileObject(name); 432 if (sub == null) { 433 FileObject fo2 = fo.createFolder(name); 434 Assert.assertNotNull("createFolder(" + fo + ", " + name + ") -> null", fo2); 435 fo = fo2; 436 } else { 437 Assert.assertTrue("folder", sub.isFolder()); 438 fo = sub; 439 } 440 } else { 441 FileObject sub = fo.getFileObject(name); 442 if (sub == null) { 443 FileObject fo2 = fo.createData(name); 444 Assert.assertNotNull("createData(" + fo + ", " + name + ") -> null", fo2); 445 fo = fo2; 446 } else { 447 fo = sub; 448 touch = true; 449 } 450 } 451 } 452 assert fo.isData(); 453 if (content != null || touch) { 454 FileLock lock = fo.lock(); 455 try { 456 OutputStream os = fo.getOutputStream(lock); 457 try { 458 if (content != null) { 459 InputStream is = content.openStream(); 460 try { 461 FileUtil.copy(is, os); 462 } finally { 463 is.close(); 464 } 465 } 466 } finally { 467 os.close(); 468 } 469 } finally { 470 lock.releaseLock(); 471 } 472 } 473 return fo; 474 } 475 476 private static final class Repo extends Repository { 477 478 public Repo(NbTestCase t) throws Exception { 479 super(mksystem(t)); 480 } 481 482 private static FileSystem mksystem(NbTestCase t) throws Exception { 483 LocalFileSystem lfs = new LocalFileSystem(); 484 File systemDir = new File (t.getWorkDir(), "ud/system"); 485 systemDir.mkdirs(); 486 lfs.setRootDirectory(systemDir); 487 lfs.setReadOnly(false); 488 List <FileSystem> layers = new ArrayList <FileSystem>(); 489 layers.add(lfs); 490 addLayer(layers, "org/netbeans/modules/j2ee/sun/ide/j2ee/layer.xml"); 492 addLayer(layers, "org/netbeans/modules/j2ee/clientproject/ui/resources/layer.xml"); 494 addLayer(layers, "org/netbeans/modules/websvc/core/resources/mf-layer.xml"); 496 addLayer(layers, "org/netbeans/modules/java/resources/mf-layer.xml"); 498 MultiFileSystem mfs = new MultiFileSystem((FileSystem[]) layers.toArray(new FileSystem[layers.size()])); 499 return mfs; 500 } 501 502 private static void addLayer(List <FileSystem> layers, String layerRes) throws SAXException { 503 URL layerFile = Repo.class.getClassLoader().getResource(layerRes); 504 assert layerFile != null; 505 layers.add(new XMLFileSystem(layerFile)); 506 } 507 508 } 509 510 514 public static File copyFolder(final File targetDir, final File d) throws IOException { 515 assert d.isDirectory(); 516 File workdir = targetDir; 517 String name = d.getName(); 518 while (name.length() < 3) { 519 name = name + "x"; 520 } 521 File todir = workdir.createTempFile(name, null, workdir); 522 todir.delete(); 523 doCopy(d, todir); 524 return todir; 525 } 526 527 private static void doCopy(File from, File to) throws IOException { 528 if (from.isDirectory()) { 529 if (from.getName().equals("CVS")) { 530 return; 531 } 532 to.mkdir(); 533 String [] kids = from.list(); 534 for (int i = 0; i < kids.length; i++) { 535 doCopy(new File (from, kids[i]), new File (to, kids[i])); 536 } 537 } else { 538 assert from.isFile(); 539 InputStream is = new FileInputStream (from); 540 try { 541 OutputStream os = new FileOutputStream (to); 542 try { 543 FileUtil.copy(is, os); 544 } finally { 545 os.close(); 546 } 547 } finally { 548 is.close(); 549 } 550 } 551 } 552 553 } 554 | Popular Tags |