1 19 20 package org.netbeans.modules.ant.freeform; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.OutputStream ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.Collections ; 28 import java.util.List ; 29 import java.util.jar.JarEntry ; 30 import java.util.jar.JarOutputStream ; 31 import org.netbeans.api.project.Project; 32 import org.netbeans.api.project.ProjectInformation; 33 import org.netbeans.api.project.ProjectManager; 34 import org.netbeans.api.project.ProjectUtils; 35 import org.netbeans.junit.NbTestCase; 36 import org.netbeans.modules.ant.freeform.spi.support.Util; 37 import org.netbeans.spi.project.ActionProvider; 38 import org.netbeans.spi.project.support.ant.AntProjectHelper; 39 import org.netbeans.spi.project.support.ant.EditableProperties; 40 import org.openide.filesystems.FileObject; 41 import org.openide.filesystems.FileUtil; 42 import org.openide.util.Lookup; 43 import org.w3c.dom.Element ; 44 import javax.xml.parsers.SAXParser ; 45 import javax.xml.parsers.SAXParserFactory ; 46 import org.netbeans.modules.project.ant.AntBasedProjectFactorySingleton; 47 import org.xml.sax.SAXException ; 48 import org.xml.sax.SAXParseException ; 49 import org.xml.sax.helpers.DefaultHandler ; 50 51 56 public class FreeformProjectGeneratorTest extends NbTestCase { 57 58 private File lib1; 59 private File lib2; 60 private File src; 61 private File test; 62 63 public FreeformProjectGeneratorTest(java.lang.String testName) { 64 super(testName); 65 } 66 67 protected void setUp() throws Exception { 68 clearWorkDir(); 69 } 70 71 public AntProjectHelper createEmptyProject(String projectFolder, String projectName) throws Exception { 72 File base = new File (getWorkDir(), projectFolder); 73 base.mkdir(); 74 File antScript = new File (base, "build.xml"); 75 antScript.createNewFile(); 76 src = new File (base, "src"); 77 src.mkdir(); 78 test = new File (base, "test"); 79 test.mkdir(); 80 File libs = new File (base, "libs"); 81 libs.mkdir(); 82 lib1 = new File (libs, "some.jar"); 83 createRealJarFile(lib1); 84 lib2 = new File (libs, "some2.jar"); 85 createRealJarFile(lib2); 86 87 91 AntProjectHelper helper = FreeformProjectGenerator.createProject(base, base, projectName, null); 92 return helper; 93 } 94 95 public void testCreateProject() throws Exception { 96 AntProjectHelper helper = createEmptyProject("proj1", "proj-1"); 97 FileObject base = helper.getProjectDirectory(); 98 Project p = ProjectManager.getDefault().findProject(base); 99 assertNotNull("Project was not created", p); 100 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 101 102 ProjectInformation pi = ProjectUtils.getInformation(p); 103 assertEquals("Project name was not set", "proj-1", pi.getName()); 104 } 105 106 public void testTargetMappings() throws Exception { 107 AntProjectHelper helper = createEmptyProject("proj2", "proj-2"); 108 FileObject base = helper.getProjectDirectory(); 109 Project p = ProjectManager.getDefault().findProject(base); 110 assertNotNull("Project was not created", p); 111 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 112 113 ActionProvider ap = p.getLookup().lookup(ActionProvider.class); 114 assertNotNull("Project does not have ActionProvider", ap); 115 assertEquals("Project cannot have any action", 0, ap.getSupportedActions().length); 116 117 List <FreeformProjectGenerator.TargetMapping> list = FreeformProjectGenerator.getTargetMappings(helper); 118 assertNotNull("getTargetMappings() cannot return null", list); 119 assertEquals("Project cannot have any action", 0, list.size()); 120 121 list = new ArrayList <FreeformProjectGenerator.TargetMapping>(); 122 FreeformProjectGenerator.TargetMapping tm = new FreeformProjectGenerator.TargetMapping(); 123 tm.name = "clean"; 124 tm.targets = Collections.singletonList("clean-target"); 125 list.add(tm); 126 tm = new FreeformProjectGenerator.TargetMapping(); 127 tm.name = "build"; 128 tm.targets = Collections.singletonList("build-target"); 129 tm.script = "${ant.script.two}"; 130 list.add(tm); 131 tm = new FreeformProjectGenerator.TargetMapping(); 132 tm.name = "rebuild"; 133 tm.targets = Arrays.asList("clean-target", "build-target"); 134 tm.script = "${ant.script.three}"; 135 list.add(tm); 136 tm = new FreeformProjectGenerator.TargetMapping(); 137 FreeformProjectGenerator.putTargetMappings(helper, list); 138 List <FreeformProjectGenerator.TargetMapping> list2 = FreeformProjectGenerator.getTargetMappings(helper); 139 FreeformProjectGenerator.putTargetMappings(helper, list2); 141 list2 = FreeformProjectGenerator.getTargetMappings(helper); 142 assertNotNull("getTargetMappings() cannot return null", list2); 143 assertEquals("Project must have 3 actions", 3, list2.size()); 144 assertEquals("Script was not correctly saved", null, list2.get(0).script); 145 assertEquals("Script was not correctly saved", "${ant.script.two}", list2.get(1).script); 146 assertEquals("Script was not correctly saved", "${ant.script.three}", list2.get(2).script); 147 assertEquals("Project must have 3 actions plus 4 extras (run, javadoc, test, redeploy) plus 4 project operations (copy, rename, move, delete): " + 148 Arrays.asList(ap.getSupportedActions()), 11, ap.getSupportedActions().length); 149 assertTrue("Action clean must be enabled", ap.isActionEnabled("clean", Lookup.EMPTY)); 150 assertTrue("Action build must be enabled", ap.isActionEnabled("build", Lookup.EMPTY)); 151 assertTrue("Action rebuild must be enabled", ap.isActionEnabled("rebuild", Lookup.EMPTY)); 152 boolean ok = false; 153 try { 154 assertFalse("Action twiddle must be disabled", ap.isActionEnabled("twiddle", Lookup.EMPTY)); 155 } catch (IllegalArgumentException ex) { 156 ok = true; 157 } 158 assertTrue("Exception must be thrown for non-existing actions", ok); 159 ProjectManager.getDefault().saveAllProjects(); 160 } 161 162 public void testRawTargetMappings() throws Exception { 163 AntProjectHelper helper = createEmptyProject("proj", "proj"); 164 FileObject base = helper.getProjectDirectory(); 165 Project p = ProjectManager.getDefault().findProject(base); 166 assertNotNull("Project was not created", p); 167 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 168 169 171 List <FreeformProjectGenerator.TargetMapping> mappings = new ArrayList <FreeformProjectGenerator.TargetMapping>(); 172 FreeformProjectGenerator.TargetMapping tm = new FreeformProjectGenerator.TargetMapping(); 173 tm.name = "first-targetName"; 174 tm.script = "antScript"; 175 tm.targets = new ArrayList <String >(); 176 tm.targets.add("target-1"); 177 tm.targets.add("target-2"); 178 tm.targets.add("target-3"); 179 tm.properties = new EditableProperties(false); 180 tm.properties.setProperty("k1", "v1"); 181 tm.properties.setProperty("k2", "v2"); 182 FreeformProjectGenerator.TargetMapping.Context ctx = new FreeformProjectGenerator.TargetMapping.Context(); 183 ctx.folder = "someFolder1"; 184 ctx.format = "relative-path"; 185 ctx.property = "someProperty1"; 186 ctx.pattern = "somePattern1"; 187 tm.context = ctx; 188 mappings.add(tm); 189 tm = new FreeformProjectGenerator.TargetMapping(); 190 tm.name = "second-targetName"; 191 tm.script = "second-antScript"; 192 tm.targets = new ArrayList <String >(); 193 tm.targets.add("second-target-1"); 194 tm.targets.add("second-target-2"); 195 tm.targets.add("second-target-3"); 196 tm.properties = new EditableProperties(false); 197 tm.properties.setProperty("second-k1", "second-v1"); 198 tm.properties.setProperty("second-k2", "second-v2"); 199 ctx = new FreeformProjectGenerator.TargetMapping.Context(); 200 ctx.folder = "second-someFolder1"; 201 ctx.format = "java-name"; 202 ctx.property = "second-someProperty1"; 203 ctx.separator = "someSeparator1"; 204 tm.context = ctx; 205 mappings.add(tm); 206 FreeformProjectGenerator.putTargetMappings(helper, mappings); 207 mappings = FreeformProjectGenerator.getTargetMappings(helper); 209 FreeformProjectGenerator.putTargetMappings(helper, mappings); 210 Element el = Util.getPrimaryConfigurationData(helper); 212 el = Util.findElement(el, "ide-actions", FreeformProjectType.NS_GENERAL); 213 assertNotNull("Target mapping were not saved correctly", el); 214 List <Element > subElements = Util.findSubElements(el); 215 assertEquals(2, subElements.size()); 216 Element el2 = subElements.get(0); 218 assertElement(el2, "action", null, "name", "first-targetName"); 219 List <Element > l1 = Util.findSubElements(el2); 220 assertEquals(7, l1.size()); 221 assertElementArray(l1, 222 new String []{"script", "target", "target", "target", "property", "property", "context"}, 223 new String []{"antScript", "target-1", "target-2", "target-3", "v1", "v2", null}); 224 el2 = l1.get(4); 225 assertElement(el2, "property", "v1", "name", "k1"); 226 el2 = l1.get(5); 227 assertElement(el2, "property", "v2", "name", "k2"); 228 el2 = l1.get(6); 229 List <Element > l2 = Util.findSubElements(el2); 230 assertEquals(5, l2.size()); 231 assertElementArray(l2, 232 new String []{"property", "folder", "pattern", "format", "arity"}, 233 new String []{"someProperty1", "someFolder1", "somePattern1", "relative-path", null}); 234 assertNotNull("have <one-file-only>", Util.findElement(l2.get(4), "one-file-only", FreeformProjectType.NS_GENERAL)); 235 el2 = subElements.get(1); 237 assertElement(el2, "action", null, "name", "second-targetName"); 238 l1 = Util.findSubElements(el2); 239 assertEquals(7, l1.size()); 240 assertElementArray(l1, 241 new String []{"script", "target", "target", "target", "property", "property", "context"}, 242 new String []{"second-antScript", "second-target-1", "second-target-2", "second-target-3", "second-v1", "second-v2", null}); 243 el2 = l1.get(4); 244 assertElement(el2, "property", "second-v1", "name", "second-k1"); 245 el2 = l1.get(5); 246 assertElement(el2, "property", "second-v2", "name", "second-k2"); 247 el2 = l1.get(6); 248 l2 = Util.findSubElements(el2); 249 assertEquals(4, l2.size()); 250 assertElementArray(l2, 251 new String []{"property", "folder", "format", "arity"}, 252 new String []{"second-someProperty1", "second-someFolder1", "java-name", null}); 253 Element sepFilesEl = Util.findElement(l2.get(3), "separated-files", FreeformProjectType.NS_GENERAL); 254 assertNotNull("have <separated-files>", sepFilesEl); 255 assertEquals("right separator", "someSeparator1", Util.findText(sepFilesEl)); 256 ProjectManager.getDefault().saveAllProjects(); 258 validate(p); 259 260 262 mappings = new ArrayList <FreeformProjectGenerator.TargetMapping>(); 263 tm = new FreeformProjectGenerator.TargetMapping(); 264 tm.name = "foo"; 265 tm.script = "antScript"; 266 tm.targets = new ArrayList <String >(); 267 tm.targets.add("target-1"); 268 tm.targets.add("target-2"); 269 mappings.add(tm); 270 tm.properties = new EditableProperties(false); 271 tm.properties.setProperty("key1", "value1"); 272 tm.properties.setProperty("key2", "value2"); 273 FreeformProjectGenerator.putTargetMappings(helper, mappings); 274 el = Util.getPrimaryConfigurationData(helper); 276 el = Util.findElement(el, "ide-actions", FreeformProjectType.NS_GENERAL); 277 assertNotNull("Target mapping were not saved correctly", el); 278 subElements = Util.findSubElements(el); 279 assertEquals(1, subElements.size()); 280 el2 = subElements.get(0); 282 assertElement(el2, "action", null, "name", "foo"); 283 l1 = Util.findSubElements(el2); 284 assertEquals(5, l1.size()); 285 assertElementArray(l1, 286 new String []{"script", "target", "target", "property", "property"}, 287 new String []{"antScript", "target-1", "target-2", "value1", "value2"}); 288 el2 = l1.get(3); 289 assertElement(el2, "property", "value1", "name", "key1"); 290 el2 = l1.get(4); 291 assertElement(el2, "property", "value2", "name", "key2"); 292 mappings = new ArrayList <FreeformProjectGenerator.TargetMapping>(); 293 tm = new FreeformProjectGenerator.TargetMapping(); 294 tm.name = "foo"; 295 tm.script = "diff-script"; 296 tm.targets = new ArrayList <String >(); 297 tm.targets.add("target-1"); 298 tm.targets.add("target-B"); 299 tm.properties = new EditableProperties(false); 300 tm.properties.setProperty("key-1", "value-1"); 301 tm.properties.setProperty("key-2", "value-2"); 302 mappings.add(tm); 303 FreeformProjectGenerator.putTargetMappings(helper, mappings); 304 el = Util.getPrimaryConfigurationData(helper); 306 el = Util.findElement(el, "ide-actions", FreeformProjectType.NS_GENERAL); 307 assertNotNull("Target mapping were not saved correctly", el); 308 subElements = Util.findSubElements(el); 309 assertEquals(1, subElements.size()); 310 el2 = subElements.get(0); 312 assertElement(el2, "action", null, "name", "foo"); 313 l1 = Util.findSubElements(el2); 314 assertEquals(5, l1.size()); 315 assertElementArray(l1, 316 new String []{"script", "target", "target", "property", "property"}, 317 new String []{"diff-script", "target-1", "target-B", "value-1", "value-2"}); 318 el2 = l1.get(3); 319 assertElement(el2, "property", "value-1", "name", "key-1"); 320 el2 = l1.get(4); 321 assertElement(el2, "property", "value-2", "name", "key-2"); 322 ProjectManager.getDefault().saveAllProjects(); 324 validate(p); 325 } 326 327 public void testRawContextMenuActions() throws Exception { 328 AntProjectHelper helper = createEmptyProject("proj", "proj"); 329 FileObject base = helper.getProjectDirectory(); 330 Project p = ProjectManager.getDefault().findProject(base); 331 assertNotNull("Project was not created", p); 332 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 333 334 336 List <FreeformProjectGenerator.TargetMapping> mappings = new ArrayList <FreeformProjectGenerator.TargetMapping>(); 337 FreeformProjectGenerator.TargetMapping tm = new FreeformProjectGenerator.TargetMapping(); 338 tm.name = "first-targetName"; 339 mappings.add(tm); 340 tm = new FreeformProjectGenerator.TargetMapping(); 341 tm.name = "second-targetName"; 342 mappings.add(tm); 343 tm = new FreeformProjectGenerator.TargetMapping(); 344 tm.name = "context-sensitive"; 345 tm.context = new FreeformProjectGenerator.TargetMapping.Context(); 346 mappings.add(tm); 347 FreeformProjectGenerator.putContextMenuAction(helper, mappings); 348 Element el = Util.getPrimaryConfigurationData(helper); 350 el = Util.findElement(el, "view", FreeformProjectType.NS_GENERAL); 351 assertNotNull("Target mapping were not saved correctly", el); 352 el = Util.findElement(el, "context-menu", FreeformProjectType.NS_GENERAL); 353 assertNotNull("Target mapping were not saved correctly", el); 354 List <Element > subElements = Util.findSubElements(el); 355 assertEquals(2, subElements.size()); 356 assertElementArray(subElements, 357 new String []{"ide-action", "ide-action"}, 358 new String []{null, null}, 359 new String []{"name", "name"}, 360 new String []{"first-targetName", "second-targetName"} 361 ); 362 ProjectManager.getDefault().saveAllProjects(); 364 validate(p); 365 366 368 tm = new FreeformProjectGenerator.TargetMapping(); 369 tm.name = "foo"; 370 mappings.add(tm); 371 tm = new FreeformProjectGenerator.TargetMapping(); 372 tm.name = "bar"; 373 mappings.add(tm); 374 FreeformProjectGenerator.putContextMenuAction(helper, mappings); 375 el = Util.getPrimaryConfigurationData(helper); 377 el = Util.findElement(el, "view", FreeformProjectType.NS_GENERAL); 378 assertNotNull("Target mapping were not saved correctly", el); 379 el = Util.findElement(el, "context-menu", FreeformProjectType.NS_GENERAL); 380 assertNotNull("Target mapping were not saved correctly", el); 381 subElements = Util.findSubElements(el); 382 assertEquals(4, subElements.size()); 383 assertElementArray(subElements, 384 new String []{"ide-action", "ide-action", "ide-action", "ide-action"}, 385 new String []{null, null, null, null}, 386 new String []{"name", "name", "name", "name"}, 387 new String []{"first-targetName", "second-targetName", "foo", "bar"} 388 ); 389 ProjectManager.getDefault().saveAllProjects(); 391 validate(p); 392 } 393 394 public void testRawCustomContextMenuActions() throws Exception { 395 AntProjectHelper helper = createEmptyProject("proj", "proj"); 396 FileObject base = helper.getProjectDirectory(); 397 Project p = ProjectManager.getDefault().findProject(base); 398 assertNotNull("Project was not created", p); 399 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 400 401 403 List <FreeformProjectGenerator.CustomTarget> customActions = new ArrayList <FreeformProjectGenerator.CustomTarget>(); 404 FreeformProjectGenerator.CustomTarget ct = new FreeformProjectGenerator.CustomTarget(); 405 ct.label = "customAction1"; 406 ct.script = "customScript1"; 407 ct.targets = new ArrayList <String >(); 408 ct.targets.add("customTarget1"); 409 ct.targets.add("customTarget2"); 410 ct.properties = new EditableProperties(false); 411 ct.properties.setProperty("k1", "v1"); 412 ct.properties.setProperty("k2", "v2"); 413 customActions.add(ct); 414 ct = new FreeformProjectGenerator.CustomTarget(); 415 ct.label = "customAction2"; 416 ct.script = "customScript2"; 417 ct.targets = new ArrayList <String >(); 418 ct.targets.add("second-customTarget1"); 419 ct.targets.add("second-customTarget2"); 420 ct.properties = new EditableProperties(false); 421 ct.properties.setProperty("kk1", "vv1"); 422 ct.properties.setProperty("kk2", "vv2"); 423 customActions.add(ct); 424 FreeformProjectGenerator.putCustomContextMenuActions(helper, customActions); 425 customActions = FreeformProjectGenerator.getCustomContextMenuActions(helper); 427 FreeformProjectGenerator.putCustomContextMenuActions(helper, customActions); 428 Element el = Util.getPrimaryConfigurationData(helper); 430 el = Util.findElement(el, "view", FreeformProjectType.NS_GENERAL); 431 assertNotNull("Target mapping were not saved correctly", el); 432 el = Util.findElement(el, "context-menu", FreeformProjectType.NS_GENERAL); 433 assertNotNull("Target mapping were not saved correctly", el); 434 List <Element > subElements = Util.findSubElements(el); 435 assertEquals(2, subElements.size()); 436 assertElementArray(subElements, 437 new String []{"action", "action"}, 438 new String []{null, null}); 439 Element el2 = subElements.get(0); 441 List <Element > l1 = Util.findSubElements(el2); 442 assertEquals(6, l1.size()); 443 assertElementArray(l1, 444 new String []{"script", "label", "target", "target", "property", "property"}, 445 new String []{"customScript1", "customAction1", "customTarget1", "customTarget2", "v1", "v2"}); 446 el2 = l1.get(4); 447 assertElement(el2, "property", "v1", "name", "k1"); 448 el2 = l1.get(5); 449 assertElement(el2, "property", "v2", "name", "k2"); 450 el2 = subElements.get(1); 452 l1 = Util.findSubElements(el2); 453 assertEquals(6, l1.size()); 454 assertElementArray(l1, 455 new String []{"script", "label", "target", "target", "property", "property"}, 456 new String []{"customScript2", "customAction2", "second-customTarget1", "second-customTarget2", "vv1", "vv2"}); 457 el2 = l1.get(4); 458 assertElement(el2, "property", "vv1", "name", "kk1"); 459 el2 = l1.get(5); 460 assertElement(el2, "property", "vv2", "name", "kk2"); 461 ProjectManager.getDefault().saveAllProjects(); 463 validate(p); 464 465 467 customActions = new ArrayList <FreeformProjectGenerator.CustomTarget>(); 468 ct = new FreeformProjectGenerator.CustomTarget(); 469 ct.label = "fooLabel"; 470 customActions.add(ct); 471 ct = new FreeformProjectGenerator.CustomTarget(); 472 ct.label = "barLabel"; 473 customActions.add(ct); 474 FreeformProjectGenerator.putCustomContextMenuActions(helper, customActions); 475 el = Util.getPrimaryConfigurationData(helper); 477 el = Util.findElement(el, "view", FreeformProjectType.NS_GENERAL); 478 assertNotNull("Target mapping were not saved correctly", el); 479 el = Util.findElement(el, "context-menu", FreeformProjectType.NS_GENERAL); 480 assertNotNull("Target mapping were not saved correctly", el); 481 subElements = Util.findSubElements(el); 482 assertEquals(2, subElements.size()); 483 assertElementArray(subElements, 484 new String []{"action", "action"}, 485 new String []{null, null}); 486 el2 = subElements.get(0); 488 l1 = Util.findSubElements(el2); 489 assertEquals(1, l1.size()); 490 assertElementArray(l1, 491 new String []{"label"}, 492 new String []{"fooLabel"}); 493 el2 = subElements.get(1); 495 l1 = Util.findSubElements(el2); 496 assertEquals(1, l1.size()); 497 assertElementArray(l1, 498 new String []{"label"}, 499 new String []{"barLabel"}); 500 ProjectManager.getDefault().saveAllProjects(); 502 validate(p); 503 } 504 505 511 public static void assertElement(Element element, String expectedName, String expectedValue) { 512 String message = "Element "+element+" does not match [name="+expectedName+",value="+expectedValue+"]"; assertEquals(message, expectedName, element.getLocalName()); 514 if (expectedValue != null) { 515 assertEquals(message, expectedValue, Util.findText(element)); 516 } 517 } 518 519 523 public static void assertElementArray(List <Element > elements, String [] expectedNames, String [] expectedValues) { 524 for (int i=0; i<elements.size(); i++) { 525 assertElement(elements.get(i), expectedNames[i], expectedValues[i]); 526 } 527 } 528 529 538 public static void assertElement(Element element, String expectedName, String expectedValue, String expectedAttrName, String expectedAttrValue) { 539 String message = "Element "+element+" does not match [name="+expectedName+",value="+ 540 expectedValue+", attr="+expectedAttrName+", attrvalue="+expectedAttrValue+"]"; assertEquals(message, expectedName, element.getLocalName()); 542 if (expectedValue != null) { 543 assertEquals(message, expectedValue, Util.findText(element)); 544 } 545 String val = element.getAttribute(expectedAttrName); 546 assertEquals(expectedAttrValue, val); 547 } 548 549 554 public static void assertElementArray(List <Element > elements, String [] expectedNames, String [] expectedValues, String [] expectedAttrName, String [] expectedAttrValue) { 555 assertEquals(expectedNames.length, elements.size()); 556 for (int i=0; i<elements.size(); i++) { 557 assertElement(elements.get(i), expectedNames[i], expectedValues[i], expectedAttrName[i], expectedAttrValue[i]); 558 } 559 } 560 561 562 public void createRealJarFile(File f) throws Exception { 564 OutputStream os = new FileOutputStream (f); 565 try { 566 JarOutputStream jos = new JarOutputStream (os); 567 JarEntry entry = new JarEntry ("foo.txt"); 569 jos.putNextEntry(entry); 573 jos.flush(); 574 jos.close(); 575 } finally { 576 os.close(); 577 } 578 } 579 580 private static String [] getSchemas() throws Exception { 581 return new String [] { 582 FreeformProjectGenerator.class.getResource("resources/freeform-project-general.xsd").toExternalForm(), 583 FreeformProjectGenerator.class.getResource("resources/freeform-project-general-2.xsd").toExternalForm(), 584 AntBasedProjectFactorySingleton.class.getResource("project.xsd").toExternalForm(), 585 }; 586 } 587 588 public static void validate(Project proj) throws Exception { 589 File projF = FileUtil.toFile(proj.getProjectDirectory()); 590 File xml = new File (new File (projF, "nbproject"), "project.xml"); 591 SAXParserFactory f = (SAXParserFactory )Class.forName("org.apache.xerces.jaxp.SAXParserFactoryImpl").newInstance(); 592 if (f == null) { 593 System.err.println("Validation skipped because org.apache.xerces.jaxp.SAXParserFactoryImpl was not found on classpath"); 594 return; 595 } 596 f.setNamespaceAware(true); 597 f.setValidating(true); 598 SAXParser p = f.newSAXParser(); 599 p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 600 "http://www.w3.org/2001/XMLSchema"); 601 p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", getSchemas()); 602 try { 603 p.parse(xml.toURI().toString(), new Handler ()); 604 } catch (SAXParseException e) { 605 assertTrue("Validation of XML document "+xml+" against schema failed. Details: "+ 606 e.getSystemId() + ":" + e.getLineNumber() + ": " + e.getLocalizedMessage(), false); 607 } 608 } 609 610 private static final class Handler extends DefaultHandler { 611 public void warning(SAXParseException e) throws SAXException { 612 throw e; 613 } 614 public void error(SAXParseException e) throws SAXException { 615 throw e; 616 } 617 public void fatalError(SAXParseException e) throws SAXException { 618 throw e; 619 } 620 } 621 622 } 623 | Popular Tags |