1 19 20 package org.netbeans.modules.apisupport.project; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.ByteArrayOutputStream ; 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.io.OutputStreamWriter ; 32 import java.io.Writer ; 33 import java.util.Collections ; 34 import java.util.HashMap ; 35 import java.util.HashSet ; 36 import java.util.Iterator ; 37 import java.util.Map ; 38 import java.util.Properties ; 39 import java.util.Set ; 40 import java.util.jar.JarEntry ; 41 import java.util.jar.JarOutputStream ; 42 import java.util.jar.Manifest ; 43 import java.util.zip.CRC32 ; 44 import org.netbeans.api.project.ProjectManager; 45 import org.netbeans.junit.NbTestCase; 46 import org.netbeans.modules.apisupport.project.suite.SuiteProject; 47 import org.netbeans.modules.apisupport.project.suite.SuiteProjectGenerator; 48 import org.openide.filesystems.FileObject; 49 import org.openide.filesystems.FileUtil; 50 import org.netbeans.modules.apisupport.project.universe.NbPlatform; 51 import org.openide.filesystems.FileLock; 52 import org.openide.modules.ModuleInfo; 53 import org.openide.util.Lookup; 54 55 60 public abstract class TestBase extends NbTestCase { 61 62 public static final String CLUSTER_IDE = "ide8"; 63 public static final String CLUSTER_PLATFORM = "platform7"; 64 public static final String CLUSTER_ENTERPRISE = "enterprise4"; 65 66 protected TestBase(String name) { 67 super(name); 68 } 69 70 private static String EEP = "example-external-projects"; 71 72 76 private boolean cvsAvailable; 77 78 79 private File nbcvsrootF; 80 81 82 private FileObject nbcvsroot; 83 84 85 protected File destDirF; 86 87 protected File apisZip; 88 89 91 protected static boolean noDataDir = false; 92 93 protected void setUp() throws Exception { 94 super.setUp(); 95 Lookup.getDefault().lookup(ModuleInfo.class); 96 cvsAvailable = isCVSAvailable(); 97 if (cvsAvailable) { 98 nbcvsrootF = FileUtil.normalizeFile(getTestNBRoot()); 99 nbcvsroot = FileUtil.toFileObject(nbcvsrootF); 100 assertNotNull("have a file object for nbcvsroot when using " + System.getProperty("java.class.path"), nbcvsroot); 101 destDirF = file(nbcvsrootF, "nbbuild/netbeans").getAbsoluteFile(); 102 File extexamplesF = file(getDataDir(), EEP); 103 if (!noDataDir) { 104 assertTrue("there is a dir " + extexamplesF, extexamplesF.isDirectory()); 105 assertNotNull("have a file object for extexamples", FileUtil.toFileObject(extexamplesF)); 106 } 107 } else { 108 destDirF = getXTestNBDestDir(); 109 } 110 111 assertTrue("Directory really exists: " + destDirF, destDirF.isDirectory()); 112 113 clearWorkDir(); 115 116 ErrorManagerImpl.registerCase(this); 117 118 apisZip = new File (getWorkDir(), "apis.zip"); 120 File userPropertiesFile = initializeBuildProperties(getWorkDir(), getDataDir(), apisZip,noDataDir); 121 String [] suites = { 122 "suite1", 124 "suite2", 125 "suite4", 126 "suite3/dummy-project", 128 }; 129 if (!noDataDir) { 130 for (int i = 0; i < suites.length; i++) { 131 File platformPrivate = resolveEEPFile(suites[i] + "/nbproject/private/platform-private.properties"); 132 Properties p = new Properties (); 133 p.setProperty("user.properties.file", userPropertiesFile.getAbsolutePath()); 134 platformPrivate.getParentFile().mkdirs(); 135 OutputStream os = new FileOutputStream (platformPrivate); 136 try { 137 p.store(os, null); 138 } finally { 139 os.close(); 140 } 141 } 142 } 143 NbPlatform.reset(); 144 } 145 146 protected void tearDown() throws Exception { 147 super.tearDown(); 148 ErrorManagerImpl.registerCase(null); 149 } 150 151 158 public static File initializeBuildProperties(File workDir, File dataDir) throws Exception { 159 return initializeBuildProperties(workDir, dataDir, null,noDataDir); 160 } 161 162 private static File initializeBuildProperties(File workDir, File dataDir, File apisZip,boolean noDataDir) throws Exception { 163 File nbcvsrootF = getTestNBRoot(); 164 boolean cvsAvailable = isCVSAvailable(); 165 System.setProperty("netbeans.user", workDir.getAbsolutePath()); 166 File userPropertiesFile = new File (workDir, "build.properties"); 167 Properties p = new Properties (); 168 File defaultPlatform = cvsAvailable ? file(nbcvsrootF, "nbbuild/netbeans") : getXTestNBDestDir(); 169 assertTrue("default platform available (" + defaultPlatform + ')', defaultPlatform.isDirectory()); 170 p.setProperty("nbplatform.default.netbeans.dest.dir", defaultPlatform.getAbsolutePath()); 171 p.setProperty("nbplatform.default.harness.dir", "${nbplatform.default.netbeans.dest.dir}/harness"); 172 if (!noDataDir) { 173 File customPlatform = file(file(dataDir, EEP), "/suite3/nbplatform"); 174 assertTrue("custom platform available (" + customPlatform + ')', customPlatform.isDirectory()); 175 p.setProperty("nbplatform.custom.netbeans.dest.dir", customPlatform.getAbsolutePath()); 176 if (apisZip != null) { 177 p.setProperty("nbplatform.default.javadoc", apisZip.getAbsolutePath()); 178 } 179 if (cvsAvailable) { 180 p.setProperty("nbplatform.default.sources", nbcvsrootF.getAbsolutePath() + ":" + file(file(dataDir, EEP), "/suite2").getAbsolutePath()); 182 } 183 } 184 OutputStream os = new FileOutputStream (userPropertiesFile); 185 try { 186 p.store(os, null); 187 } finally { 188 os.close(); 189 } 190 191 return userPropertiesFile; 192 } 193 194 197 protected static File file(File root, String path) { 198 return new File (root, path.replace('/', File.separatorChar)); 199 } 200 201 private static boolean isCVSAvailable() { 202 return new File (getTestNBRoot(), "nbbuild/netbeans/" + CLUSTER_IDE 203 + "/modules/org-netbeans-modules-apisupport-project.jar").isFile(); 204 } 205 206 protected File nbCVSRootFile() { 207 assertTrue("NB CVS tree is available", cvsAvailable); 208 return nbcvsrootF; 209 } 210 211 protected FileObject nbCVSRoot() { 212 assertTrue("NB CVS tree is available", cvsAvailable); 213 return nbcvsroot; 214 } 215 216 protected File resolveEEPFile(final String relativePath) { 217 File eepF = FileUtil.normalizeFile(new File (getDataDir(), EEP)); 218 assertTrue("has EEP directory (" + eepF + ')', eepF.isDirectory()); 219 File eepRelF = new File (eepF, relativePath); 220 return eepRelF; 222 } 223 224 protected String resolveEEPPath(final String relativePath) { 225 return resolveEEPFile(relativePath).getAbsolutePath(); 226 } 227 228 protected FileObject resolveEEP(final String relativePath) { 229 return FileUtil.toFileObject(resolveEEPFile(relativePath)); 230 } 231 232 237 protected File file(String path) { 238 return file(nbcvsrootF, path); 239 } 240 241 245 protected File copyFolder(File d) throws IOException { 246 assert d.isDirectory(); 247 File workdir = getWorkDir(); 248 String name = d.getName(); 249 while (name.length() < 3) { 250 name = name + "x"; 251 } 252 File todir = workdir.createTempFile(name, null, workdir); 253 todir.delete(); 254 doCopy(d, todir); 255 return todir; 256 } 257 258 private static void doCopy(File from, File to) throws IOException { 259 if (from.isDirectory()) { 260 if (from.getName().equals("CVS")) { 261 return; 262 } 263 to.mkdir(); 264 String [] kids = from.list(); 265 for (int i = 0; i < kids.length; i++) { 266 doCopy(new File (from, kids[i]), new File (to, kids[i])); 267 } 268 } else { 269 assert from.isFile(); 270 InputStream is = new FileInputStream (from); 271 try { 272 OutputStream os = new FileOutputStream (to); 273 try { 274 FileUtil.copy(is, os); 275 } finally { 276 os.close(); 277 } 278 } finally { 279 is.close(); 280 } 281 } 282 } 283 284 public static String slurp(FileObject fileObject) throws IOException { 285 InputStream is = fileObject.getInputStream(); 286 try { 287 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 288 FileUtil.copy(is, baos); 289 return baos.toString("UTF-8"); 290 } finally { 291 is.close(); 292 } 293 } 294 public static void dump(FileObject f, String contents) throws IOException { 295 FileLock lock = f.lock(); 296 try { 297 OutputStream os = f.getOutputStream(lock); 298 try { 299 Writer w = new OutputStreamWriter (os, "UTF-8"); 300 w.write(contents); 301 w.flush(); 302 } finally { 303 os.close(); 304 } 305 } finally { 306 lock.releaseLock(); 307 } 308 } 309 public static String slurp(File file) throws IOException { 310 InputStream is = new FileInputStream (file); 311 try { 312 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 313 FileUtil.copy(is, baos); 314 return baos.toString("UTF-8"); 315 } finally { 316 is.close(); 317 } 318 } 319 public static void dump(File f, String contents) throws IOException { 320 f.getParentFile().mkdirs(); 321 OutputStream os = new FileOutputStream (f); 322 try { 323 Writer w = new OutputStreamWriter (os, "UTF-8"); 324 w.write(contents); 325 w.flush(); 326 } finally { 327 os.close(); 328 } 329 } 330 331 public static final class TestPCL implements PropertyChangeListener { 333 334 public final Set changed = new HashSet (); 335 public final Map newvals = new HashMap (); 336 public final Map oldvals = new HashMap (); 337 338 public TestPCL() {} 339 340 public void reset() { 341 changed.clear(); 342 newvals.clear(); 343 oldvals.clear(); 344 } 345 346 public void propertyChange(PropertyChangeEvent evt) { 347 String prop = evt.getPropertyName(); 348 String nue = (String )evt.getNewValue(); 349 String old = (String )evt.getOldValue(); 350 changed.add(prop); 351 if (prop != null) { 352 newvals.put(prop, nue); 353 oldvals.put(prop, old); 354 } else { 355 assert nue == null : "null prop name -> null new value"; 356 assert old == null : "null prop name -> null old value"; 357 } 358 } 359 360 } 361 362 366 public NbModuleProject generateStandaloneModule(String prjDir) throws IOException { 367 return generateStandaloneModule(getWorkDir(), prjDir); 368 } 369 370 388 public static NbModuleProject generateStandaloneModule(File workDir, String prjDir) throws IOException { 389 FileObject prjDirFO = generateStandaloneModuleDirectory(workDir, prjDir); 390 return (NbModuleProject) ProjectManager.getDefault().findProject(prjDirFO); 391 } 392 393 397 public static FileObject generateStandaloneModuleDirectory(File workDir, String prjDir) throws IOException { 398 String prjDirDotted = prjDir.replace('/', '.'); 399 File prjDirF = file(workDir, prjDir); 400 NbModuleProjectGenerator.createStandAloneModule( 401 prjDirF, 402 "org.example." + prjDirDotted, "Testing Module", "org/example/" + prjDir + "/resources/Bundle.properties", 405 "org/example/" + prjDir + "/resources/layer.xml", 406 NbPlatform.PLATFORM_ID_DEFAULT); return FileUtil.toFileObject(prjDirF); 408 } 409 410 414 public SuiteProject generateSuite(String prjDir) throws IOException { 415 return generateSuite(getWorkDir(), prjDir); 416 } 417 418 419 public static SuiteProject generateSuite(File workDir, String prjDir) throws IOException { 420 return generateSuite(workDir, prjDir, NbPlatform.PLATFORM_ID_DEFAULT); 421 } 422 423 424 public static SuiteProject generateSuite(File workDir, String prjDir, String platformID) throws IOException { 425 File prjDirF = file(workDir, prjDir); 426 SuiteProjectGenerator.createSuiteProject(prjDirF, platformID); 427 return (SuiteProject) ProjectManager.getDefault().findProject( 428 FileUtil.toFileObject(prjDirF)); 429 } 430 431 438 public static NbModuleProject generateSuiteComponent(SuiteProject suiteProject, String prjDir) throws Exception { 439 File suiteDir = suiteProject.getProjectDirectoryFile(); 440 return generateSuiteComponent(suiteProject, suiteDir, prjDir); 441 } 442 443 450 public static NbModuleProject generateSuiteComponent(SuiteProject suiteProject, File parentDir, String prjDir) throws Exception { 451 String prjDirDotted = prjDir.replace('/', '.'); 452 File suiteDir = suiteProject.getProjectDirectoryFile(); 453 File prjDirF = file(parentDir, prjDir); 454 NbModuleProjectGenerator.createSuiteComponentModule( 455 prjDirF, 456 "org.example." + prjDirDotted, "Testing Module", "org/example/" + prjDir + "/resources/Bundle.properties", 459 "org/example/" + prjDir + "/resources/layer.xml", 460 suiteDir); return (NbModuleProject) ProjectManager.getDefault().findProject( 462 FileUtil.toFileObject(prjDirF)); 463 } 464 465 471 public static void createJar(File jar, Map contents, Manifest manifest) throws IOException { 472 if (manifest != null) { 473 manifest.getMainAttributes().putValue("Manifest-Version", "1.0"); } 475 jar.getParentFile().mkdirs(); 476 OutputStream os = new FileOutputStream (jar); 477 try { 478 JarOutputStream jos = manifest != null ? new JarOutputStream (os, manifest) : new JarOutputStream (os); 479 Iterator it = contents.entrySet().iterator(); 480 while (it.hasNext()) { 481 Map.Entry entry = (Map.Entry ) it.next(); 482 String path = (String ) entry.getKey(); 483 byte[] data = ((String ) entry.getValue()).getBytes("UTF-8"); 484 JarEntry je = new JarEntry (path); 485 je.setSize(data.length); 486 CRC32 crc = new CRC32 (); 487 crc.update(data); 488 je.setCrc(crc.getValue()); 489 jos.putNextEntry(je); 490 jos.write(data); 491 } 492 jos.close(); 493 } finally { 494 os.close(); 495 } 496 } 497 498 public static void makePlatform(File d) throws IOException { 499 Manifest mani = new Manifest (); 501 mani.getMainAttributes().putValue("OpenIDE-Module", "core"); 502 TestBase.createJar(new File (new File (new File (d, "platform"), "core"), "core.jar"), Collections.EMPTY_MAP, mani); 503 mani = new Manifest (); 504 mani.getMainAttributes().putValue("OpenIDE-Module", "org.netbeans.modules.apisupport.harness"); 505 mani.getMainAttributes().putValue("OpenIDE-Module-Specification-Version", "1.6.1"); TestBase.createJar(new File (new File (new File (d, "harness"), "modules"), "org-netbeans-modules-apisupport-harness.jar"), Collections.EMPTY_MAP, mani); 507 } 508 509 public static void delete(File f) throws IOException { 510 if (f.isDirectory()) { 511 File [] kids = f.listFiles(); 512 for (int i = 0; i < kids.length; i++) { 513 delete(kids[i]); 514 } 515 } 516 if (!f.delete()) { 517 throw new IOException ("Could not delete " + f); 518 } 519 } 520 521 private static File getTestNBRoot() { 522 String nbcvsroot = System.getProperty("test.nbcvsroot"); 523 assertNotNull("test.nbcvsroot property has to be set", nbcvsroot); 524 return new File (nbcvsroot); 525 } 526 527 private static File getXTestNBDestDir() { 528 String destDir = System.getProperty("xtest.netbeans.dest.dir"); 529 assertNotNull("xtest.netbeans.dest.dir property has to be set when running within binary distribution", destDir); 530 return new File (destDir); 531 } 532 533 } 534 | Popular Tags |