1 19 20 package org.netbeans.modules.apisupport.project; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileNotFoundException ; 26 import java.io.FileOutputStream ; 27 import java.io.FileReader ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.InputStreamReader ; 31 import java.io.OutputStream ; 32 import java.io.PrintWriter ; 33 import java.net.URL ; 34 import java.util.ArrayList ; 35 import java.util.Arrays ; 36 import java.util.HashMap ; 37 import java.util.List ; 38 import java.util.Map ; 39 import java.util.Set ; 40 import java.util.SortedSet ; 41 import java.util.zip.ZipEntry ; 42 import java.util.zip.ZipOutputStream ; 43 import junit.framework.TestCase; 44 import org.netbeans.api.project.ProjectInformation; 45 import org.netbeans.api.project.ProjectUtils; 46 import org.netbeans.modules.apisupport.project.CreatedModifiedFiles.Operation; 47 import org.netbeans.modules.apisupport.project.layers.LayerTestBase; 48 import org.netbeans.modules.apisupport.project.ui.customizer.ModuleDependency; 49 import org.netbeans.modules.apisupport.project.universe.LocalizedBundleInfo; 50 import org.netbeans.spi.project.support.ant.EditableProperties; 51 import org.openide.filesystems.FileLock; 52 import org.openide.filesystems.FileObject; 53 import org.openide.filesystems.FileUtil; 54 import org.openide.modules.SpecificationVersion; 55 56 60 public class CreatedModifiedFilesTest extends LayerTestBase { 61 62 private static final String [] HTML_CONTENT = new String [] { 63 "<html>", 64 "<em>i am some template</em>", 65 "</html>" 66 }; 67 68 private static final Map TOKENS_MAP = new HashMap (2); 69 70 static { 71 TOKENS_MAP.put("some", "a\\ nonsense"); 72 TOKENS_MAP.put("\\<(\\/{0,1})em\\>", "<$1strong>"); 73 } 74 75 private static final String [] HTML_CONTENT_TOKENIZED = new String [] { 76 "<html>", 77 "<strong>i am a nonsense template</strong>", 78 "</html>" 79 }; 80 81 public CreatedModifiedFilesTest(String name) { 82 super(name); 83 } 84 85 protected void setUp() throws Exception { 86 super.setUp(); 87 TestBase.initializeBuildProperties(getWorkDir(), getDataDir()); 88 } 89 90 public void testCreatedModifiedFiles() throws Exception { 91 NbModuleProject project = TestBase.generateStandaloneModule(getWorkDir(), "module1"); 92 CreatedModifiedFiles cmf = new CreatedModifiedFiles(project); 93 cmf.add(cmf.bundleKeyDefaultBundle(LocalizedBundleInfo.NAME, "Much Better Name")); 94 cmf.add(cmf.bundleKey("src/custom.properties", "some.property", "some value")); 95 cmf.add(cmf.addLoaderSection("org/example/module1/MyExtLoader", null)); 96 cmf.add(cmf.createFile("src/org/example/module1/resources/template.html", createFile(HTML_CONTENT))); 97 cmf.add(cmf.addLookupRegistration( 98 "org.example.spi.somemodule.ProvideMe", 99 "org.example.module1.ProvideMeImpl", false)); 100 101 assertRelativePaths( 102 new String [] {"src/META-INF/services/org.example.spi.somemodule.ProvideMe", "src/custom.properties", "src/org/example/module1/resources/template.html"}, 103 cmf.getCreatedPaths()); 104 assertRelativePaths( 105 new String [] {"manifest.mf", "src/org/example/module1/resources/Bundle.properties"}, 106 cmf.getModifiedPaths()); 107 108 cmf.run(); 109 } 110 111 public void testBundleKeyDefaultBundle() throws Exception { 112 NbModuleProject project = TestBase.generateStandaloneModule(getWorkDir(), "module1"); 113 ProjectInformation pi = ProjectUtils.getInformation(project); 114 assertEquals("display name before from bundle", "Testing Module", pi.getDisplayName()); 115 assertEquals("display name before from project", "Testing Module", project.getBundleInfo().getDisplayName()); 116 117 CreatedModifiedFiles cmf = new CreatedModifiedFiles(project); 118 Operation op = cmf.bundleKeyDefaultBundle(LocalizedBundleInfo.NAME, "Much Better Name"); 119 assertRelativePath("src/org/example/module1/resources/Bundle.properties", 120 op.getModifiedPaths()); 121 op.run(); 122 123 pi = ProjectUtils.getInformation(project); 124 assertEquals("display name after from bundle", "Much Better Name", pi.getDisplayName()); 125 assertEquals("display name after from project", "Much Better Name", project.getBundleInfo().getDisplayName()); 126 } 127 128 public void testBundleKey() throws Exception { 129 NbModuleProject project = TestBase.generateStandaloneModule(getWorkDir(), "module1"); 130 131 CreatedModifiedFiles cmf = new CreatedModifiedFiles(project); 132 Operation op = cmf.bundleKey("src/custom.properties", "some.property", "some value"); 133 134 assertRelativePath("src/custom.properties", op.getCreatedPaths()); 135 136 cmf.add(op); 137 cmf.run(); 138 139 EditableProperties ep = Util.loadProperties(FileUtil.toFileObject(TestBase.file(getWorkDir(), "module1/src/custom.properties"))); 140 assertEquals("property created", "some value", ep.getProperty("some.property")); 141 } 142 143 public void testAddLoaderSection() throws Exception { 144 NbModuleProject project = TestBase.generateStandaloneModule(getWorkDir(), "module1"); 145 146 CreatedModifiedFiles cmf = new CreatedModifiedFiles(project); 147 Operation op = cmf.addLoaderSection("org/example/module1/MyExtLoader", null); 148 149 assertRelativePath("manifest.mf", op.getModifiedPaths()); 150 151 op.run(); 152 153 EditableManifest em = Util.loadManifest(FileUtil.toFileObject(TestBase.file(getWorkDir(), "module1/manifest.mf"))); 154 assertEquals("loader section was added", "Loader", em.getAttribute("OpenIDE-Module-Class", "org/example/module1/MyExtLoader.class")); 155 } 156 157 public void testAddLookupRegistration() throws Exception { 158 NbModuleProject project = TestBase.generateStandaloneModule(getWorkDir(), "module1"); 159 160 CreatedModifiedFiles cmf = new CreatedModifiedFiles(project); 161 cmf.add(cmf.addLookupRegistration( 162 "org.example.spi.somemodule.ProvideMe", 163 "org.example.module1.ProvideMeImpl1", false)); 164 cmf.add(cmf.addLookupRegistration( 165 "org.example.spi.somemodule.ProvideMe", 166 "org.example.module1.ProvideMeImpl2", false)); 167 cmf.add(cmf.addLookupRegistration( 168 "org.example.spi.somemodule.ProvideMe", 169 "org.example.module1.ProvideMeImpl1", true)); 170 171 String [] paths = { 172 "src/META-INF/services/org.example.spi.somemodule.ProvideMe", 173 "test/unit/src/META-INF/services/org.example.spi.somemodule.ProvideMe", 174 }; 175 assertRelativePaths(paths, cmf.getCreatedPaths()); 176 177 cmf.run(); 178 179 FileObject registry = project.getProjectDirectory().getFileObject(paths[0]); 180 assertNotNull(registry); 181 InputStream is = registry.getInputStream(); 182 try { 183 BufferedReader r = new BufferedReader (new InputStreamReader (is, "UTF-8")); 184 assertEquals("org.example.module1.ProvideMeImpl1", r.readLine()); 185 assertEquals("org.example.module1.ProvideMeImpl2", r.readLine()); 186 assertEquals(null, r.readLine()); 187 } finally { 188 is.close(); 189 } 190 registry = project.getProjectDirectory().getFileObject(paths[1]); 191 assertNotNull(registry); 192 is = registry.getInputStream(); 193 try { 194 BufferedReader r = new BufferedReader (new InputStreamReader (is, "UTF-8")); 195 assertEquals("org.example.module1.ProvideMeImpl1", r.readLine()); 196 assertEquals(null, r.readLine()); 197 } finally { 198 is.close(); 199 } 200 } 201 202 public void testCreateFile() throws Exception { 203 NbModuleProject project = TestBase.generateStandaloneModule(getWorkDir(), "module1"); 204 205 CreatedModifiedFiles cmf = new CreatedModifiedFiles(project); 206 207 String templatePath = "src/org/example/module1/resources/template.html"; 208 Operation op = cmf.createFile(templatePath, createFile(HTML_CONTENT)); 209 210 assertRelativePath(templatePath, op.getCreatedPaths()); 211 212 cmf.add(op); 213 cmf.run(); 214 215 assertFileContent(HTML_CONTENT, new File (getWorkDir(), "module1/" + templatePath)); 216 } 217 218 public void testCreateBinaryFile() throws Exception { 219 NbModuleProject project = TestBase.generateStandaloneModule(getWorkDir(), "module1"); 220 221 CreatedModifiedFiles cmf = new CreatedModifiedFiles(project); 222 223 String templatePath = "src/org/example/module1/resources/binarytemplate.zip"; 224 225 File binaryFile = createBinaryFile(HTML_CONTENT); 226 227 Operation op = cmf.createFile(templatePath, binaryFile.toURI().toURL()); 228 229 assertRelativePath(templatePath, op.getCreatedPaths()); 230 231 cmf.add(op); 232 cmf.run(); 233 234 assertFileContent(binaryFile, new File (getWorkDir(), "module1/" + templatePath)); 235 } 236 237 public void testCreateFileWithSubstitutions() throws Exception { 238 NbModuleProject project = TestBase.generateStandaloneModule(getWorkDir(), "module1"); 239 240 CreatedModifiedFiles cmf = new CreatedModifiedFiles(project); 241 242 String templatePath = "src/org/example/module1/resources/template.html"; 243 Operation op = cmf.createFileWithSubstitutions(templatePath, createFile(HTML_CONTENT), TOKENS_MAP); 244 245 assertRelativePath(templatePath, op.getCreatedPaths()); 246 247 cmf.add(op); 248 cmf.run(); 249 250 assertFileContent(HTML_CONTENT_TOKENIZED, new File (getWorkDir(), "module1/" + templatePath)); 251 } 252 253 public void testAddModuleDependency() throws Exception { 254 NbModuleProject project = TestBase.generateStandaloneModule(getWorkDir(), "module1"); 255 256 CreatedModifiedFiles cmf = new CreatedModifiedFiles(project); 257 258 Operation op = cmf.addModuleDependency("org.apache.tools.ant.module", "3", 259 new SpecificationVersion("3.9"), true); 260 261 assertRelativePath("nbproject/project.xml", op.getModifiedPaths()); 262 263 cmf.add(op); 264 cmf.run(); 265 266 ProjectXMLManager pxm = new ProjectXMLManager(project); 267 Set deps = pxm.getDirectDependencies(); 268 assertEquals("one dependency", 1, deps.size()); 269 ModuleDependency antDep = (ModuleDependency) deps.toArray()[0]; 270 assertEquals("cnb", "org.apache.tools.ant.module", antDep.getModuleEntry().getCodeNameBase()); 271 assertEquals("release version", "3", antDep.getReleaseVersion()); 272 assertEquals("specification version", "3.9", antDep.getSpecificationVersion()); 273 assertTrue("compile dependeny", antDep.hasCompileDependency()); 274 assertFalse("implementation dependeny", antDep.hasImplementationDepedendency()); 275 } 276 277 public void testTheSameModuleDependencyTwice() throws Exception { 278 NbModuleProject project = TestBase.generateStandaloneModule(getWorkDir(), "module1"); 279 280 CreatedModifiedFiles cmf = new CreatedModifiedFiles(project); 281 282 Operation op = cmf.addModuleDependency("org.apache.tools.ant.module",null,null,false); 283 284 assertRelativePath("nbproject/project.xml", op.getModifiedPaths()); 285 286 cmf.add(op); 287 cmf.add(op); 288 cmf.run(); 289 290 ProjectXMLManager pxm = new ProjectXMLManager(project); 291 Set deps = pxm.getDirectDependencies(); 292 assertEquals("one dependency", 1, deps.size()); 293 ModuleDependency antDep = (ModuleDependency) deps.toArray()[0]; 294 assertEquals("cnb", "org.apache.tools.ant.module", antDep.getModuleEntry().getCodeNameBase()); 295 } 296 297 public void testOrderLayerEntry() throws Exception { 298 NbModuleProject project = TestBase.generateStandaloneModule(getWorkDir(), "module1"); 300 301 CreatedModifiedFiles cmf = new CreatedModifiedFiles(project); 302 303 Operation op = cmf.orderLayerEntry("Loaders/text/x-java/Actions", 304 "IAmSecond.instance", "IAmThird.instance"); 305 306 assertRelativePath("src/org/example/module1/resources/layer.xml", op.getModifiedPaths()); 307 308 cmf.add(op); 309 cmf.run(); 310 } 311 312 public void testCreateLayerEntry() throws Exception { 313 NbModuleProject project = TestBase.generateStandaloneModule(getWorkDir(), "module1"); 314 315 CreatedModifiedFiles cmf = new CreatedModifiedFiles(project); 316 Operation layerOp = cmf.createLayerEntry( 317 "Menu/Tools/org-example-module1-BeepAction.instance", 318 null, 319 null, 320 null, 321 null); 322 layerOp.run(); 323 324 layerOp = cmf.createLayerEntry( 325 "Services/org-example-module1-Module1UI.settings", 326 null, 327 null, 328 null, 329 null); 330 cmf.add(layerOp); 331 assertRelativePath("src/org/example/module1/resources/layer.xml", layerOp.getModifiedPaths()); 332 333 layerOp = cmf.orderLayerEntry("Menu/Tools", 334 "org-example-module1-BeepAction.instance", 335 "org-example-module1-BlareAction.instance"); 336 cmf.add(layerOp); 337 338 layerOp = cmf.createLayerEntry( 339 "Menu/Tools/org-example-module1-BlareAction.instance", 340 null, 341 null, 342 null, 343 null); 344 cmf.add(layerOp); 345 346 layerOp = cmf.createLayerEntry( 347 "Services/org-example-module1-Other.settings", 348 createFile(HTML_CONTENT), 349 null, 350 null, 351 null); 352 cmf.add(layerOp); 353 354 layerOp = cmf.createLayerEntry( 355 "Services/org-example-module1-Tokenized.settings", 356 createFile(HTML_CONTENT), 357 TOKENS_MAP, 358 null, 359 null); 360 cmf.add(layerOp); 361 362 layerOp = cmf.createLayerEntry( 363 "Services/org-example-module1-LocalizedAndTokened.settings", 364 createFile(HTML_CONTENT), 365 TOKENS_MAP, 366 "Some Settings", 367 null); 368 cmf.add(layerOp); 369 370 assertRelativePaths( 371 new String [] {"src/org/example/module1/resources/Bundle.properties", "src/org/example/module1/resources/layer.xml"}, 372 cmf.getModifiedPaths()); 373 assertRelativePaths( 374 new String [] { 375 "src/org/example/module1/resources/org-example-module1-LocalizedAndTokenedSettings.xml", 376 "src/org/example/module1/resources/org-example-module1-OtherSettings.xml", 377 "src/org/example/module1/resources/org-example-module1-TokenizedSettings.xml" 378 }, 379 cmf.getCreatedPaths()); 380 cmf.run(); 381 382 assertFileContent(HTML_CONTENT_TOKENIZED, new File (getWorkDir(), "module1/src/org/example/module1/resources/org-example-module1-TokenizedSettings.xml")); 383 384 String [] supposedContent = new String [] { 386 "<filesystem>", 387 "<folder name=\"Menu\">", 388 "<folder name=\"Tools\">", 389 "<file name=\"org-example-module1-BeepAction.instance\"/>", 390 "<attr name=\"org-example-module1-BeepAction.instance/org-example-module1-BlareAction.instance\" boolvalue=\"true\"/>", 391 "<file name=\"org-example-module1-BlareAction.instance\"/>", 392 "</folder>", 393 "</folder>", 394 "<folder name=\"Services\">", 395 "<file name=\"org-example-module1-LocalizedAndTokened.settings\" url=\"org-example-module1-LocalizedAndTokenedSettings.xml\">", 396 "<attr name=\"SystemFileSystem.localizingBundle\" stringvalue=\"org.example.module1.resources.Bundle\"/>", 397 "</file>", 398 "<file name=\"org-example-module1-Module1UI.settings\"/>", 399 "<file name=\"org-example-module1-Other.settings\" url=\"org-example-module1-OtherSettings.xml\"/>", 400 "<file name=\"org-example-module1-Tokenized.settings\" url=\"org-example-module1-TokenizedSettings.xml\"/>", 401 "</folder>", 402 "</filesystem>" 403 }; 404 assertLayerContent(supposedContent, 405 new File (getWorkDir(), "module1/src/org/example/module1/resources/layer.xml")); 406 407 EditableProperties ep = Util.loadProperties(FileUtil.toFileObject( 409 TestBase.file(getWorkDir(), "module1/src/org/example/module1/resources/Bundle.properties"))); 410 assertEquals("localized name property", "Some Settings", 411 ep.getProperty("Services/org-example-module1-LocalizedAndTokened.settings")); 412 assertEquals("module name", "Testing Module", ep.getProperty("OpenIDE-Module-Name")); 413 } 414 415 public void testCreateLayerAttribute() throws Exception { 416 NbModuleProject project = TestBase.generateStandaloneModule(getWorkDir(), "module1"); 417 CreatedModifiedFiles cmf = new CreatedModifiedFiles(project); 418 String fqClassName = "org.example.module1.BeepAction"; 419 String dashedFqClassName = fqClassName.replace('.', '-'); 420 String layerPath = "Actions/Tools/" + dashedFqClassName + ".instance"; 421 422 Operation op = cmf.createLayerEntry(layerPath, null, null, null, null); 423 cmf.add(op); 424 425 op = cmf.createLayerAttribute( 426 layerPath, "instanceClass", fqClassName); 427 assertRelativePath("src/org/example/module1/resources/layer.xml", op.getModifiedPaths()); 428 429 cmf.add(op); 430 cmf.run(); 431 432 String [] supposedContent = new String [] { 433 "<filesystem>", 434 "<folder name=\"Actions\">", 435 "<folder name=\"Tools\">", 436 "<file name=\"org-example-module1-BeepAction.instance\">", 437 "<attr name=\"instanceClass\" stringvalue=\"org.example.module1.BeepAction\"/>", 438 "</file>", 439 "</folder>", 440 "</folder>", 441 "</filesystem>" 442 }; 443 assertLayerContent(supposedContent, 444 new File (getWorkDir(), "module1/src/org/example/module1/resources/layer.xml")); 445 } 446 447 448 public void testCreateLayerEntryWithoutLocalizingBundle() throws Exception { 449 NbModuleProject project = TestBase.generateStandaloneModule(getWorkDir(), "module"); 450 project.getProjectDirectory().getFileObject("src/org/example/module/resources/Bundle.properties").delete(); 451 FileObject mf = project.getProjectDirectory().getFileObject("manifest.mf"); 452 EditableManifest m; 453 InputStream is = mf.getInputStream(); 454 try { 455 m = new EditableManifest(is); 456 } finally { 457 is.close(); 458 } 459 m.removeAttribute("OpenIDE-Module-Localizing-Bundle", null); 460 FileLock lock = mf.lock(); 461 try { 462 OutputStream os = mf.getOutputStream(lock); 463 try { 464 m.write(os); 465 } finally { 466 os.close(); 467 } 468 } finally { 469 lock.releaseLock(); 470 } 471 CreatedModifiedFiles cmf = new CreatedModifiedFiles(project); 472 Operation op = cmf.createLayerEntry("f", null, null, "F!", null); 473 cmf.add(op); 474 cmf.run(); 475 String [] supposedContent = new String [] { 476 "<filesystem>", 477 "<file name=\"f\"/>", 478 "</filesystem>" 479 }; 480 assertLayerContent(supposedContent, 481 new File (getWorkDir(), "module/src/org/example/module/resources/layer.xml")); 482 } 483 484 public static void assertRelativePath(String expectedPath, String [] paths) { 485 TestCase.assertEquals("one path", 1, paths.length); 486 TestCase.assertEquals("created, modified paths", expectedPath, paths[0]); 487 } 488 489 public static void assertRelativePath(String expectedPath, SortedSet paths) { 490 String [] s = new String [paths.size()]; 491 assertRelativePath(expectedPath, (String []) paths.toArray(s)); 492 } 493 494 public static void assertRelativePaths(String [] expectedPaths, String [] paths) { 495 TestCase.assertEquals("created, modified paths", Arrays.asList(expectedPaths), Arrays.asList(paths)); 496 } 497 498 private URL createFile(String [] content) throws IOException { 499 File myTemplate = new File (getWorkDir(), "myTemplate.html"); 500 OutputStream myTemplateOS = new FileOutputStream (myTemplate); 501 PrintWriter pw = new PrintWriter (myTemplateOS); 502 try { 503 for (int i = 0; i < content.length; i++) { 504 pw.println(content[i]); 505 } 506 } finally { 507 pw.close(); 508 } 509 return myTemplate.toURI().toURL(); 510 } 511 512 private File createBinaryFile(String [] content) throws IOException { 513 File myTemplate = new File (getWorkDir(), "myTemplate.zip"); 514 ZipOutputStream zos = new ZipOutputStream (new FileOutputStream (myTemplate)); 515 ZipEntry entry = new ZipEntry ("a/b/c/d.txt"); 516 zos.putNextEntry(entry); 517 518 try { 519 for (int i = 0; i < content.length; i++) { 520 zos.write(content[i].getBytes()); 521 } 522 523 } finally { 524 zos.close(); 525 } 526 return myTemplate; 527 } 528 529 private void assertFileContent(String [] content, File file) throws IOException { 530 assertTrue("file exist and is a regular file", file.isFile()); 531 BufferedReader br = new BufferedReader (new FileReader (file)); 532 try { 533 for (int i = 0; i < content.length; i++) { 534 assertEquals("file content", content[i], br.readLine()); 535 } 536 assertNull(br.readLine()); 537 } finally { 538 br.close(); 539 } 540 } 541 542 private void assertFileContent(File f1, File f2) throws IOException { 543 InputStream is = new FileInputStream (f1); 544 InputStream is2 = new FileInputStream (f2); 545 546 try { 547 byte[] content = new byte[is.available()]; 548 is.read(content); 549 550 byte[] content2 = new byte[is2.available()]; 551 is2.read(content2); 552 553 for (int i = 0; i < content.length; i++) { 554 assertEquals("file content", content[i], content2[i]); 555 } 556 } finally { 557 is.close(); 558 is2.close(); 559 } 560 } 561 562 public static void assertLayerContent(final String [] supposedContent, 563 final File layerF) throws IOException , FileNotFoundException { 564 BufferedReader reader = new BufferedReader (new FileReader (layerF)); 565 List actualContent = new ArrayList (); 566 boolean fsElementReached = false; 567 String line; 568 569 try { 570 while ((line = reader.readLine()) != null) { 571 line = line.trim(); 572 if (!fsElementReached && line.equals(supposedContent[0])) { 573 fsElementReached = true; 574 actualContent.add(line); 575 continue; 576 } 577 if (fsElementReached) { 578 actualContent.add(line); 579 } 580 } 581 } finally { 582 reader.close(); 583 } 584 585 assertEquals("content of layer", Arrays.asList(supposedContent).toString(), actualContent.toString()); 586 } 587 588 } 589 590 | Popular Tags |