1 31 32 package org.opencms.xml.content; 33 34 import org.opencms.file.CmsFile; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsProperty; 37 import org.opencms.file.CmsPropertyDefinition; 38 import org.opencms.file.CmsUser; 39 import org.opencms.i18n.CmsEncoder; 40 import org.opencms.i18n.CmsMessages; 41 import org.opencms.main.OpenCms; 42 import org.opencms.staticexport.CmsLink; 43 import org.opencms.staticexport.CmsLinkTable; 44 import org.opencms.test.OpenCmsTestCase; 45 import org.opencms.test.OpenCmsTestProperties; 46 import org.opencms.util.CmsFileUtil; 47 import org.opencms.widgets.CmsCheckboxWidget; 48 import org.opencms.widgets.CmsHtmlWidget; 49 import org.opencms.widgets.I_CmsWidget; 50 import org.opencms.xml.CmsXmlContentDefinition; 51 import org.opencms.xml.CmsXmlEntityResolver; 52 import org.opencms.xml.CmsXmlUtils; 53 import org.opencms.xml.types.CmsXmlHtmlValue; 54 import org.opencms.xml.types.CmsXmlNestedContentDefinition; 55 import org.opencms.xml.types.CmsXmlStringValue; 56 import org.opencms.xml.types.CmsXmlVfsFileValue; 57 import org.opencms.xml.types.I_CmsXmlContentValue; 58 59 import java.util.ArrayList ; 60 import java.util.Collections ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 import java.util.Locale ; 64 65 import junit.extensions.TestSetup; 66 import junit.framework.Test; 67 import junit.framework.TestSuite; 68 69 75 public class TestCmsXmlContentWithVfs extends OpenCmsTestCase { 76 77 private static final String SCHEMA_SYSTEM_ID_2 = "http://www.opencms.org/test2.xsd"; 78 private static final String SCHEMA_SYSTEM_ID_3 = "http://www.opencms.org/test3.xsd"; 79 private static final String SCHEMA_SYSTEM_ID_3B = "http://www.opencms.org/test3b.xsd"; 80 private static final String SCHEMA_SYSTEM_ID_4 = "http://www.opencms.org/test4.xsd"; 81 private static final String SCHEMA_SYSTEM_ID_4B = "http://www.opencms.org/test4b.xsd"; 82 private static final String SCHEMA_SYSTEM_ID_5 = "http://www.opencms.org/test5.xsd"; 83 private static final String SCHEMA_SYSTEM_ID_6 = "http://www.opencms.org/test6.xsd"; 84 private static final String SCHEMA_SYSTEM_ID_7 = "http://www.opencms.org/test7.xsd"; 85 private static final String SCHEMA_SYSTEM_ID_8 = "http://www.opencms.org/test8.xsd"; 86 private static final String SCHEMA_SYSTEM_ID_9 = "http://www.opencms.org/test9.xsd"; 87 88 93 public TestCmsXmlContentWithVfs(String arg0) { 94 95 super(arg0); 96 } 97 98 103 public static Test suite() { 104 105 OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH); 106 107 TestSuite suite = new TestSuite(); 108 suite.setName(TestCmsXmlContentWithVfs.class.getName()); 109 110 suite.addTest(new TestCmsXmlContentWithVfs("testAddRemoveElements")); 111 suite.addTest(new TestCmsXmlContentWithVfs("testContentHandler")); 112 suite.addTest(new TestCmsXmlContentWithVfs("testDefaultOnCreation")); 113 suite.addTest(new TestCmsXmlContentWithVfs("testDefaultOnCreationWithNested")); 114 suite.addTest(new TestCmsXmlContentWithVfs("testDefaultNested")); 115 suite.addTest(new TestCmsXmlContentWithVfs("testNestedSchema")); 116 suite.addTest(new TestCmsXmlContentWithVfs("testAddRemoveNestedElements")); 117 suite.addTest(new TestCmsXmlContentWithVfs("testAccessNestedElements")); 118 suite.addTest(new TestCmsXmlContentWithVfs("testValueIndex")); 119 suite.addTest(new TestCmsXmlContentWithVfs("testLayoutWidgetMapping")); 120 suite.addTest(new TestCmsXmlContentWithVfs("testLinkResolver")); 121 suite.addTest(new TestCmsXmlContentWithVfs("testEmptyLocale")); 122 suite.addTest(new TestCmsXmlContentWithVfs("testCopyMoveRemoveLocale")); 123 suite.addTest(new TestCmsXmlContentWithVfs("testValidation")); 124 suite.addTest(new TestCmsXmlContentWithVfs("testValidationExtended")); 125 suite.addTest(new TestCmsXmlContentWithVfs("testValidationLocale")); 126 suite.addTest(new TestCmsXmlContentWithVfs("testMappings")); 127 suite.addTest(new TestCmsXmlContentWithVfs("testMappingsWithManyLocales")); 128 suite.addTest(new TestCmsXmlContentWithVfs("testMappingsOfNestedContent")); 129 suite.addTest(new TestCmsXmlContentWithVfs("testMappingsAsList")); 130 suite.addTest(new TestCmsXmlContentWithVfs("testResourceBundle")); 131 suite.addTest(new TestCmsXmlContentWithVfs("testMacros")); 132 133 TestSetup wrapper = new TestSetup(suite) { 134 135 protected void setUp() { 136 137 setupOpenCms("simpletest", "/sites/default/"); 138 } 139 140 protected void tearDown() { 141 142 removeOpenCms(); 143 } 144 }; 145 146 return wrapper; 147 } 148 149 154 public void testAccessNestedElements() throws Exception { 155 156 CmsObject cms = getCmsObject(); 157 echo("Testing access to nested schema values in XML content"); 158 159 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 160 161 String content; 162 CmsXmlContent xmlcontent; 163 164 content = CmsFileUtil.readFile( 166 "org/opencms/xml/content/xmlcontent-definition-7.xsd", 167 CmsEncoder.ENCODING_UTF_8); 168 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_7, content.getBytes(CmsEncoder.ENCODING_UTF_8)); 170 171 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-7.xml", CmsEncoder.ENCODING_UTF_8); 173 xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 174 175 xmlcontent.validateXmlStructure(resolver); 177 178 xmlcontent.addValue(cms, "DeepCascade", Locale.ENGLISH, 0); 180 CmsXmlContentValueSequence level0Sequence = xmlcontent.getValueSequence("DeepCascade[1]", Locale.ENGLISH); 181 assertEquals(1, level0Sequence.getElementCount()); 182 183 xmlcontent.validateXmlStructure(resolver); 185 186 CmsXmlContentValueSequence level1Sequence = xmlcontent.getValueSequence( 188 "DeepCascade[1]/Cascade", 189 Locale.ENGLISH); 190 assertEquals(1, level1Sequence.getElementCount()); 191 192 CmsXmlContentValueSequence level2Sequence = xmlcontent.getValueSequence( 194 "DeepCascade[1]/Cascade[1]/VfsLink", 195 Locale.ENGLISH); 196 assertEquals(1, level2Sequence.getElementCount()); 197 198 level1Sequence.addValue(cms, 1); 200 assertEquals(2, level1Sequence.getElementCount()); 201 202 xmlcontent.validateXmlStructure(resolver); 204 205 System.out.println(xmlcontent.toString()); 207 xmlcontent = CmsXmlContentFactory.unmarshal(xmlcontent.toString(), CmsEncoder.ENCODING_UTF_8, resolver); 209 xmlcontent.validateXmlStructure(resolver); 211 212 level0Sequence = xmlcontent.getValueSequence("DeepCascade[1]", Locale.ENGLISH); 213 assertEquals(1, level0Sequence.getElementCount()); 214 215 level1Sequence = xmlcontent.getValueSequence("DeepCascade[1]/Cascade", Locale.ENGLISH); 217 assertEquals(2, level1Sequence.getElementCount()); 218 219 level2Sequence = xmlcontent.getValueSequence("DeepCascade[1]/Cascade[2]/VfsLink", Locale.ENGLISH); 221 assertEquals(1, level2Sequence.getElementCount()); 222 223 level2Sequence.addValue(cms, 0); 225 level2Sequence.addValue(cms, 2); 226 level2Sequence.addValue(cms, 1); 227 assertEquals(4, level2Sequence.getElementCount()); 228 229 System.out.println(xmlcontent.toString()); 231 xmlcontent = CmsXmlContentFactory.unmarshal(xmlcontent.toString(), CmsEncoder.ENCODING_UTF_8, resolver); 233 234 xmlcontent.validateXmlStructure(resolver); 236 237 level2Sequence = xmlcontent.getValueSequence("DeepCascade[1]/Cascade[2]/VfsLink", Locale.ENGLISH); 239 assertEquals(4, level2Sequence.getElementCount()); 240 241 level2Sequence = xmlcontent.getValueSequence("DeepCascade[1]/Cascade[2]/Option", Locale.ENGLISH); 243 assertEquals(0, level2Sequence.getElementCount()); 244 level2Sequence.addValue(cms, 0); 245 level2Sequence.addValue(cms, 1); 246 assertEquals(2, level2Sequence.getElementCount()); 247 248 System.out.println(xmlcontent.toString()); 250 xmlcontent = CmsXmlContentFactory.unmarshal(xmlcontent.toString(), CmsEncoder.ENCODING_UTF_8, resolver); 252 253 xmlcontent.validateXmlStructure(resolver); 255 256 level2Sequence = xmlcontent.getValueSequence("DeepCascade[1]/Cascade[2]/Option", Locale.ENGLISH); 257 assertEquals(2, level2Sequence.getElementCount()); 258 259 level0Sequence = xmlcontent.getValueSequence("DeepCascade", Locale.ENGLISH); 261 assertEquals(1, level0Sequence.getElementCount()); 262 level0Sequence.removeValue(0); 263 assertEquals(0, level0Sequence.getElementCount()); 264 265 System.out.println(xmlcontent.toString()); 267 xmlcontent = CmsXmlContentFactory.unmarshal(xmlcontent.toString(), CmsEncoder.ENCODING_UTF_8, resolver); 269 270 xmlcontent.validateXmlStructure(resolver); 272 273 level0Sequence = xmlcontent.getValueSequence("DeepCascade", Locale.ENGLISH); 274 assertEquals(0, level0Sequence.getElementCount()); 275 276 level0Sequence.addValue(cms, 0); 278 assertEquals(1, level0Sequence.getElementCount()); 279 280 System.out.println(xmlcontent.toString()); 282 xmlcontent = CmsXmlContentFactory.unmarshal(xmlcontent.toString(), CmsEncoder.ENCODING_UTF_8, resolver); 284 285 xmlcontent.validateXmlStructure(resolver); 287 288 level0Sequence = xmlcontent.getValueSequence("DeepCascade", Locale.ENGLISH); 289 assertEquals(1, level0Sequence.getElementCount()); 290 } 291 292 297 public void testAddRemoveElements() throws Exception { 298 299 CmsObject cms = getCmsObject(); 300 echo("Testing adding and removing elements from an XML content"); 301 302 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 303 304 String content; 305 CmsXmlContent xmlcontent; 306 307 content = CmsFileUtil.readFile( 309 "org/opencms/xml/content/xmlcontent-definition-6.xsd", 310 CmsEncoder.ENCODING_UTF_8); 311 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_6, content.getBytes(CmsEncoder.ENCODING_UTF_8)); 313 314 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-6.xml", CmsEncoder.ENCODING_UTF_8); 316 xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 317 318 CmsXmlContentValueSequence titleSequence; 319 320 titleSequence = xmlcontent.getValueSequence("Title", Locale.ENGLISH); 321 assertEquals("Title", titleSequence.getElementName()); 322 assertEquals(1, titleSequence.getElementCount()); 323 assertEquals(1, titleSequence.getMinOccurs()); 324 assertEquals(5, titleSequence.getMaxOccurs()); 325 assertEquals("This is just a modification test", titleSequence.getValue(0).getStringValue(cms)); 326 327 CmsXmlStringValue newValue; 328 329 newValue = (CmsXmlStringValue)titleSequence.addValue(cms, 0); 330 assertEquals(2, titleSequence.getElementCount()); 331 assertEquals(newValue, titleSequence.getValue(0)); 332 newValue.setStringValue(cms, "This is another Value!"); 333 334 content = xmlcontent.toString(); 336 System.out.println(content); 337 xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 338 339 titleSequence = xmlcontent.getValueSequence("Title", Locale.ENGLISH); 341 assertEquals("Title", titleSequence.getElementName()); 342 assertEquals(2, titleSequence.getElementCount()); 343 assertEquals(1, titleSequence.getMinOccurs()); 344 assertEquals(5, titleSequence.getMaxOccurs()); 345 assertEquals("This is another Value!", titleSequence.getValue(0).getStringValue(cms)); 346 assertEquals("This is just a modification test", titleSequence.getValue(1).getStringValue(cms)); 347 348 newValue = (CmsXmlStringValue)titleSequence.addValue(cms, 2); 350 newValue.setStringValue(cms, "This is the last value."); 351 assertEquals(newValue, titleSequence.getValue(2)); 352 newValue = (CmsXmlStringValue)titleSequence.addValue(cms, 1); 354 newValue.setStringValue(cms, "This is the 2nd value."); 355 assertEquals(newValue, titleSequence.getValue(1)); 356 assertEquals(4, titleSequence.getElementCount()); 357 358 content = xmlcontent.toString(); 360 System.out.println(content); 361 xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 362 363 titleSequence = xmlcontent.getValueSequence("Title", Locale.ENGLISH); 365 assertEquals("Title", titleSequence.getElementName()); 366 assertEquals(4, titleSequence.getElementCount()); 367 assertEquals(1, titleSequence.getMinOccurs()); 368 assertEquals(5, titleSequence.getMaxOccurs()); 369 assertEquals("This is another Value!", titleSequence.getValue(0).getStringValue(cms)); 370 assertEquals("This is the 2nd value.", titleSequence.getValue(1).getStringValue(cms)); 371 assertEquals("This is just a modification test", titleSequence.getValue(2).getStringValue(cms)); 372 assertEquals("This is the last value.", titleSequence.getValue(3).getStringValue(cms)); 373 374 CmsXmlContentValueSequence optionSequence; 376 377 optionSequence = xmlcontent.getValueSequence("Option", Locale.ENGLISH); 378 assertEquals("Option", optionSequence.getElementName()); 379 assertEquals(0, optionSequence.getElementCount()); 380 assertEquals(0, optionSequence.getMinOccurs()); 381 assertEquals(2, optionSequence.getMaxOccurs()); 382 383 newValue = (CmsXmlStringValue)optionSequence.addValue(cms, 0); 385 newValue.setStringValue(cms, "Optional value 1"); 386 assertEquals(newValue, optionSequence.getValue(0)); 387 newValue = (CmsXmlStringValue)optionSequence.addValue(cms, 0); 389 newValue.setStringValue(cms, "Optional value 0"); 390 assertEquals(newValue, optionSequence.getValue(0)); 391 assertEquals(2, optionSequence.getElementCount()); 392 393 content = xmlcontent.toString(); 395 System.out.println(content); 396 xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 397 398 optionSequence = xmlcontent.getValueSequence("Option", Locale.ENGLISH); 399 assertEquals("Option", optionSequence.getElementName()); 400 assertEquals(2, optionSequence.getElementCount()); 401 assertEquals(0, optionSequence.getMinOccurs()); 402 assertEquals(2, optionSequence.getMaxOccurs()); 403 404 assertEquals("Optional value 0", optionSequence.getValue(0).getStringValue(cms)); 405 assertEquals("Optional value 1", optionSequence.getValue(1).getStringValue(cms)); 406 407 optionSequence.removeValue(1); 408 assertEquals(1, optionSequence.getElementCount()); 409 assertEquals("Optional value 0", optionSequence.getValue(0).getStringValue(cms)); 410 411 optionSequence.removeValue(0); 412 assertEquals(0, optionSequence.getElementCount()); 413 414 content = xmlcontent.toString(); 416 System.out.println(content); 417 xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 418 419 titleSequence = xmlcontent.getValueSequence("Title", Locale.ENGLISH); 420 assertEquals(4, titleSequence.getElementCount()); 421 422 titleSequence.removeValue(0); 423 titleSequence.removeValue(2); 424 assertEquals(2, titleSequence.getElementCount()); 425 assertEquals("This is the 2nd value.", titleSequence.getValue(0).getStringValue(cms)); 426 assertEquals("This is just a modification test", titleSequence.getValue(1).getStringValue(cms)); 427 428 content = xmlcontent.toString(); 430 System.out.println(content); 431 } 432 433 438 public void testAddRemoveNestedElements() throws Exception { 439 440 CmsObject cms = getCmsObject(); 441 echo("Testing adding and removing nested elements from an XML content"); 442 443 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 444 445 String content; 446 CmsXmlContent xmlcontent; 447 448 content = CmsFileUtil.readFile( 450 "org/opencms/xml/content/xmlcontent-definition-7.xsd", 451 CmsEncoder.ENCODING_UTF_8); 452 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_7, content.getBytes(CmsEncoder.ENCODING_UTF_8)); 454 455 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-7.xml", CmsEncoder.ENCODING_UTF_8); 457 xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 458 459 CmsXmlContentValueSequence nestedSequence; 460 461 nestedSequence = xmlcontent.getValueSequence("Cascade", Locale.ENGLISH); 462 assertEquals(1, nestedSequence.getElementCount()); 463 I_CmsXmlContentValue newValue; 464 newValue = nestedSequence.addValue(cms, 0); 465 assertNotNull(newValue); 466 assertFalse(newValue.isSimpleType()); 467 assertEquals(CmsXmlNestedContentDefinition.class.getName(), newValue.getClass().getName()); 468 469 content = xmlcontent.toString(); 471 System.out.println(content); 472 xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 473 xmlcontent.validateXmlStructure(resolver); 475 476 nestedSequence = xmlcontent.getValueSequence("Cascade", Locale.ENGLISH); 477 assertEquals(2, nestedSequence.getElementCount()); 478 479 CmsXmlContentValueSequence deepNestedSequence; 480 deepNestedSequence = xmlcontent.getValueSequence("DeepCascade", Locale.ENGLISH); 481 assertEquals(0, deepNestedSequence.getElementCount()); 482 483 newValue = deepNestedSequence.addValue(cms, 0); 484 assertNotNull(newValue); 485 assertFalse(newValue.isSimpleType()); 486 assertEquals(CmsXmlNestedContentDefinition.class.getName(), newValue.getClass().getName()); 487 488 content = xmlcontent.toString(); 490 System.out.println(content); 491 xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 492 xmlcontent.validateXmlStructure(resolver); 494 495 deepNestedSequence = xmlcontent.getValueSequence("DeepCascade", Locale.ENGLISH); 496 assertEquals(1, deepNestedSequence.getElementCount()); 497 498 nestedSequence = xmlcontent.getValueSequence("Cascade", Locale.ENGLISH); 499 assertEquals(2, nestedSequence.getElementCount()); 500 501 nestedSequence.removeValue(1); 502 deepNestedSequence.removeValue(0); 503 504 content = xmlcontent.toString(); 506 System.out.println(content); 507 xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 508 xmlcontent.validateXmlStructure(resolver); 510 511 deepNestedSequence = xmlcontent.getValueSequence("DeepCascade", Locale.ENGLISH); 512 assertEquals(0, deepNestedSequence.getElementCount()); 513 514 nestedSequence = xmlcontent.getValueSequence("Cascade", Locale.ENGLISH); 515 assertEquals(1, nestedSequence.getElementCount()); 516 } 517 518 523 public void testContentHandler() throws Exception { 524 525 CmsObject cms = getCmsObject(); 526 echo("Testing individual content handler for XML content"); 527 528 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 529 530 String content; 531 532 content = CmsFileUtil.readFile( 534 "org/opencms/xml/content/xmlcontent-definition-3.xsd", 535 CmsEncoder.ENCODING_UTF_8); 536 CmsXmlContentDefinition definition = CmsXmlContentDefinition.unmarshal(content, SCHEMA_SYSTEM_ID_3, resolver); 537 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_3, content.getBytes(CmsEncoder.ENCODING_UTF_8)); 539 540 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-3.xml", CmsEncoder.ENCODING_UTF_8); 542 CmsXmlContent xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 543 544 assertTrue(xmlcontent.hasValue("Html", Locale.ENGLISH)); 545 assertTrue(xmlcontent.hasValue("VfsLink", Locale.ENGLISH)); 546 assertSame(definition.getContentHandler().getClass().getName(), TestXmlContentHandler.class.getName()); 547 } 548 549 554 public void testDefaultNested() throws Exception { 555 556 CmsObject cms = getCmsObject(); 557 echo("Testing for default values in nested XML content schemas"); 558 559 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 560 561 String content; 562 563 content = CmsFileUtil.readFile( 565 "org/opencms/xml/content/xmlcontent-definition-4.xsd", 566 CmsEncoder.ENCODING_UTF_8); 567 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_4, content.getBytes(CmsEncoder.ENCODING_UTF_8)); 569 570 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-4.xml", CmsEncoder.ENCODING_UTF_8); 572 CmsXmlContent xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 573 System.out.println(xmlcontent.toString()); 574 575 xmlcontent.validateXmlStructure(resolver); 577 578 I_CmsXmlContentValue value1; 579 580 value1 = xmlcontent.addValue(cms, "Title", Locale.ENGLISH, 0); 581 assertEquals("Test", value1.getStringValue(cms)); 582 583 value1 = xmlcontent.addValue(cms, "Cascade[1]/Option", Locale.ENGLISH, 0); 584 assertEquals("Default value from the XML", value1.getStringValue(cms)); 585 586 value1 = xmlcontent.addValue(cms, "Cascade[1]/Option", Locale.ENGLISH, 1); 588 assertEquals("Default value from outer content definition", value1.getStringValue(cms)); 589 590 value1 = xmlcontent.addValue(cms, "Cascade[1]/VfsLink", Locale.ENGLISH, 1); 592 assertEquals("/default/for/all/from/outer.txt", value1.getStringValue(cms)); 593 594 value1 = xmlcontent.addValue(cms, "Cascade[1]/VfsLink", Locale.ENGLISH, 2); 595 assertEquals("/default/for/all/from/outer.txt", value1.getStringValue(cms)); 596 597 } 598 599 604 public void testDefaultOnCreation() throws Exception { 605 606 CmsObject cms = getCmsObject(); 607 echo("Testing default values when creating an XML content resource"); 608 609 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 610 611 String xmlContentFile = "/xmlcontent/article_0005.html"; 613 cms.createResource(xmlContentFile, 12); 614 615 CmsFile file = cms.readFile(xmlContentFile); 616 String content = new String (file.getContents(), CmsEncoder.ENCODING_UTF_8); 617 CmsXmlContent xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 618 619 String value = xmlcontent.getStringValue(cms, "Title", Locale.ENGLISH); 620 assertEquals("Default title value", value); 621 622 value = xmlcontent.getStringValue(cms, "Release", Locale.ENGLISH); 623 assertEquals("1114525380000", value); 624 } 625 626 633 public void testDefaultOnCreationWithNested() throws Exception { 634 635 CmsObject cms = getCmsObject(); 636 echo("Testing for default values in nested XML content schemas when creating a new content"); 637 638 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 639 640 String content; 641 642 content = CmsFileUtil.readFile( 644 "org/opencms/xml/content/xmlcontent-definition-3b.xsd", 645 CmsEncoder.ENCODING_UTF_8); 646 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_3B, content.getBytes(CmsEncoder.ENCODING_UTF_8)); 648 content = CmsFileUtil.readFile( 649 "org/opencms/xml/content/xmlcontent-definition-4b.xsd", 650 CmsEncoder.ENCODING_UTF_8); 651 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_4B, content.getBytes(CmsEncoder.ENCODING_UTF_8)); 653 654 CmsXmlContentDefinition cd = CmsXmlContentDefinition.unmarshal(content, SCHEMA_SYSTEM_ID_4B, resolver); 656 657 CmsXmlContent xmlcontent = CmsXmlContentFactory.createDocument(cms, Locale.ENGLISH, content, cd); 658 659 String value = xmlcontent.getStringValue(cms, "Title", Locale.ENGLISH); 660 assertEquals("Test", value); 661 662 value = xmlcontent.getStringValue(cms, "Cascade/Option", Locale.ENGLISH); 663 assertEquals("Default value from outer content definition", value); 664 665 value = xmlcontent.getStringValue(cms, "Cascade/Option[2]", Locale.ENGLISH); 666 assertEquals("Default value from outer content definition (for option node 2)", value); 667 668 value = xmlcontent.getStringValue(cms, "Cascade/VfsLink", Locale.ENGLISH); 669 assertEquals("/default/for/all/from/outer.txt", value); 670 } 671 672 677 public void testEmptyLocale() throws Exception { 678 679 CmsObject cms = getCmsObject(); 680 echo("Testing handling of empty locale nodes in XML content"); 681 682 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 683 684 String iso = "ISO-8859-1"; 685 686 String content; 687 CmsXmlContent xmlcontent; 688 689 content = CmsFileUtil.readFile( 691 "org/opencms/xml/content/xmlcontent-definition-8.xsd", 692 CmsEncoder.ENCODING_UTF_8); 693 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_8, content.getBytes(iso)); 695 CmsXmlContentDefinition cd1 = CmsXmlContentDefinition.unmarshal(content, SCHEMA_SYSTEM_ID_8, resolver); 696 697 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-8.xml", iso); 699 xmlcontent = CmsXmlContentFactory.unmarshal(content, iso, resolver); 700 xmlcontent.validateXmlStructure(resolver); 702 List locales = xmlcontent.getLocales(); 703 assertEquals(1, locales.size()); 704 assertEquals(Locale.ENGLISH, locales.get(0)); 705 706 xmlcontent = CmsXmlContentFactory.createDocument(null, Locale.ENGLISH, CmsEncoder.ENCODING_UTF_8, cd1); 708 xmlcontent.validateXmlStructure(resolver); 709 710 locales = xmlcontent.getLocales(); 711 assertEquals(1, locales.size()); 712 assertEquals(Locale.ENGLISH, locales.get(0)); 713 } 714 715 720 public void testCopyMoveRemoveLocale() throws Exception { 721 722 CmsObject cms = getCmsObject(); 723 echo("Testing locale copy, move and remove operation on an XML content"); 724 725 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 726 727 String iso = "ISO-8859-1"; 728 729 String content; 730 CmsXmlContent xmlcontent; 731 732 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-definition-8.xsd", CmsEncoder.ENCODING_UTF_8); 734 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_8, content.getBytes(iso)); 736 737 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-8.xml", iso); 739 xmlcontent = CmsXmlContentFactory.unmarshal(content, iso, resolver); 740 xmlcontent.validateXmlStructure(resolver); 742 List locales = xmlcontent.getLocales(); 743 assertEquals(1, locales.size()); 744 assertEquals(Locale.ENGLISH, locales.get(0)); 745 746 xmlcontent.copyLocale(Locale.ENGLISH, Locale.GERMANY); 747 assertEquals(2, xmlcontent.getLocales().size()); 748 assertTrue(xmlcontent.hasLocale(Locale.ENGLISH)); 749 assertTrue(xmlcontent.hasLocale(Locale.GERMANY)); 750 751 xmlcontent.validateXmlStructure(resolver); 753 754 xmlcontent.moveLocale(Locale.GERMANY, Locale.FRENCH); 755 assertEquals(2, xmlcontent.getLocales().size()); 756 assertTrue(xmlcontent.hasLocale(Locale.ENGLISH)); 757 assertTrue(xmlcontent.hasLocale(Locale.FRENCH)); 758 assertFalse(xmlcontent.hasLocale(Locale.GERMANY)); 759 760 xmlcontent.validateXmlStructure(resolver); 762 763 xmlcontent.removeLocale(Locale.ENGLISH); 764 assertEquals(1, xmlcontent.getLocales().size()); 765 assertTrue(xmlcontent.hasLocale(Locale.FRENCH)); 766 assertFalse(xmlcontent.hasLocale(Locale.ENGLISH)); 767 assertFalse(xmlcontent.hasLocale(Locale.GERMANY)); 768 assertEquals(Locale.FRENCH, xmlcontent.getLocales().get(0)); 769 770 xmlcontent.validateXmlStructure(resolver); 772 } 773 774 779 public void testLayoutWidgetMapping() throws Exception { 780 781 CmsObject cms = getCmsObject(); 782 echo("Testing mapping of the XML content GUI to different widgets"); 783 784 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 785 786 String content; 787 788 content = CmsFileUtil.readFile( 790 "org/opencms/xml/content/xmlcontent-definition-5.xsd", 791 CmsEncoder.ENCODING_UTF_8); 792 CmsXmlContentDefinition definition = CmsXmlContentDefinition.unmarshal(content, SCHEMA_SYSTEM_ID_5, resolver); 793 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_5, content.getBytes(CmsEncoder.ENCODING_UTF_8)); 795 796 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-5.xml", CmsEncoder.ENCODING_UTF_8); 798 CmsXmlContent xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 799 800 xmlcontent.validateXmlStructure(resolver); 802 803 I_CmsWidget widget; 804 I_CmsXmlContentHandler handler = definition.getContentHandler(); 805 806 widget = handler.getWidget(xmlcontent.getValue("Title", Locale.ENGLISH)); 808 assertNotNull(widget); 809 assertEquals(CmsCheckboxWidget.class.getName(), widget.getClass().getName()); 810 assertEquals("Configuration for Title", handler.getConfiguration(xmlcontent.getValue("Title", Locale.ENGLISH))); 811 812 widget = handler.getWidget(xmlcontent.getValue("Test", Locale.ENGLISH)); 814 assertNotNull(widget); 815 assertEquals(CmsHtmlWidget.class.getName(), widget.getClass().getName()); 816 assertEquals("Configuration for Test", handler.getConfiguration(xmlcontent.getValue("Test", Locale.ENGLISH))); 817 818 widget = handler.getWidget(xmlcontent.getValue("Toast", Locale.ENGLISH)); 820 assertNotNull(widget); 821 assertEquals(TestCustomInputWidgetImpl.class.getName(), widget.getClass().getName()); 822 assertEquals("Configuration for Toast", handler.getConfiguration(xmlcontent.getValue("Toast", Locale.ENGLISH))); 823 assertEquals("Configuration for Toast[some addition here]", widget.getConfiguration()); 825 } 826 827 832 public void testLinkResolver() throws Exception { 833 834 CmsObject cms = getCmsObject(); 835 echo("Testing link resolver for XML content"); 836 837 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 838 839 String content; 840 841 content = CmsFileUtil.readFile( 843 "org/opencms/xml/content/xmlcontent-definition-2.xsd", 844 CmsEncoder.ENCODING_UTF_8); 845 CmsXmlContentDefinition definition = CmsXmlContentDefinition.unmarshal(content, SCHEMA_SYSTEM_ID_2, resolver); 846 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_2, content.getBytes(CmsEncoder.ENCODING_UTF_8)); 848 849 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-2.xml", CmsEncoder.ENCODING_UTF_8); 851 CmsXmlContent xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 852 853 assertTrue(xmlcontent.hasValue("Html", Locale.ENGLISH)); 854 assertTrue(xmlcontent.hasValue("VfsLink", Locale.ENGLISH)); 855 assertSame(definition.getContentHandler().getClass().getName(), CmsDefaultXmlContentHandler.class.getName()); 856 857 CmsXmlHtmlValue htmlValue = (CmsXmlHtmlValue)xmlcontent.getValue("Html", Locale.ENGLISH); 858 CmsXmlVfsFileValue vfsValue = (CmsXmlVfsFileValue)xmlcontent.getValue("VfsLink", Locale.ENGLISH); 859 860 htmlValue.setStringValue(cms, htmlValue.getStringValue(cms)); 862 vfsValue.setStringValue(cms, vfsValue.getStringValue(cms)); 863 864 Iterator i; 865 CmsLinkTable table; 866 867 String retranslatedOutput = htmlValue.getStringValue(cms); 868 assertEquals("Incorrect links in resulting output", "<a HREF=\"http://www.alkacon.com\">Alkacon</a>\n" 869 + "<a HREF=\"/data/opencms/index.html\">Index page</a>\n" 870 + "<a HREF=\"/data/opencms/folder1/index.html?a=b&c=d#anchor\">Index page</a>\n" 871 + "<a HREF=\"/data/opencms/folder1/index.html?a2=b2&c2=d2\">Index page with unescaped ampersand</a>", 872 retranslatedOutput.trim()); 874 875 table = htmlValue.getLinkTable(); 876 assertEquals(4, table.size()); 877 878 879 i = table.iterator(); 880 int result = 0; 881 while (i.hasNext()) { 882 CmsLink link = (CmsLink)i.next(); 884 if (link.getTarget().equals("/sites/default/index.html") && link.isInternal()) { 885 result++; 886 } else if (link.getTarget().equals("http://www.alkacon.com") && !link.isInternal()) { 887 result++; 888 } else if (link.getTarget().equals("/sites/default/folder1/index.html") 889 && link.getQuery().equals("a=b&c=d") && link.getAnchor().equals("anchor") 891 && link.isInternal()) { 892 result++; 893 } else if (link.getTarget().equals("/sites/default/folder1/index.html") 894 && link.getQuery().equals("a2=b2&c2=d2") && link.isInternal()) { 896 result++; 897 } 898 } 899 900 assertEquals(4, result); 901 902 table = vfsValue.getLinkTable(); 903 assertEquals(1, table.size()); 904 CmsLink link = (CmsLink)table.iterator().next(); 905 assertEquals("/sites/default/index.html", link.getTarget()); 906 assertTrue(link.isInternal()); 907 assertEquals("/index.html", vfsValue.getStringValue(cms)); 908 } 909 910 915 public void testMacros() throws Exception { 916 917 CmsObject cms = getCmsObject(); 918 echo("Testing macros in the XML content"); 919 920 CmsUser admin = cms.getRequestContext().currentUser(); 921 admin.setFirstname("Hans"); 922 admin.setLastname("Mustermann"); 923 admin.setEmail("hans.mustermann@germany.de"); 924 admin.setAddress("Heidestraße 17, München"); 925 cms.writeUser(admin); 926 927 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 928 929 String content; 930 CmsXmlContent xmlcontent; 931 932 content = CmsFileUtil.readFile( 934 "org/opencms/xml/content/xmlcontent-definition-9.xsd", 935 CmsEncoder.ENCODING_UTF_8); 936 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_9, content.getBytes(CmsEncoder.ENCODING_UTF_8)); 938 939 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-9.xml", CmsEncoder.ENCODING_UTF_8); 941 xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 942 943 CmsXmlContentErrorHandler errorHandler; 944 945 I_CmsXmlContentValue value1; 946 947 value1 = xmlcontent.getValue("Test", Locale.ENGLISH); 948 value1.setStringValue(cms, "This produces a warning!"); 949 950 errorHandler = xmlcontent.validate(cms); 951 assertFalse(errorHandler.hasErrors()); 952 assertTrue(errorHandler.hasWarnings()); 953 954 value1.setStringValue(cms, "This produces a warning and an error!"); 955 956 errorHandler = xmlcontent.validate(cms); 957 assertTrue(errorHandler.hasErrors()); 958 assertTrue(errorHandler.hasWarnings()); 959 assertEquals(1, errorHandler.getErrors().size()); 960 assertEquals(1, errorHandler.getWarnings().size()); 961 962 value1 = xmlcontent.getValue("Toast", Locale.ENGLISH); 963 value1.setStringValue(cms, "This produces a warning but no error!"); 964 965 errorHandler = xmlcontent.validate(cms); 966 assertTrue(errorHandler.hasErrors()); 967 assertTrue(errorHandler.hasWarnings()); 968 assertEquals(1, errorHandler.getErrors(Locale.ENGLISH).size()); 969 assertEquals(2, errorHandler.getWarnings(Locale.ENGLISH).size()); 970 971 value1 = xmlcontent.addValue(cms, "Option", Locale.ENGLISH, 0); 972 assertEquals( 973 "The author is: Hans Mustermann (Admin), Heidestraße 17, München - hans.mustermann@germany.de", 974 value1.getStringValue(cms)); 975 976 value1 = xmlcontent.addValue(cms, "Option", Locale.GERMAN, 0); 977 assertEquals( 978 "Der Autor ist: Hans Mustermann (Admin), Heidestraße 17, München - hans.mustermann@germany.de", 979 value1.getStringValue(cms)); 980 981 System.out.println(xmlcontent.toString()); 983 xmlcontent = CmsXmlContentFactory.unmarshal(xmlcontent.toString(), CmsEncoder.ENCODING_UTF_8, resolver); 985 xmlcontent.validateXmlStructure(resolver); 987 988 errorHandler = xmlcontent.validate(cms); 989 assertTrue(errorHandler.hasErrors()); 990 assertTrue(errorHandler.hasWarnings()); 991 assertEquals(1, errorHandler.getErrors(Locale.ENGLISH).size()); 992 assertEquals(2, errorHandler.getWarnings(Locale.ENGLISH).size()); 993 } 994 995 1000 public void testMappings() throws Exception { 1001 1002 CmsObject cms = getCmsObject(); 1003 echo("Testing mapping of values in the XML content"); 1004 1005 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 1006 1007 String content; 1008 CmsXmlContent xmlcontent; 1009 1010 content = CmsFileUtil.readFile( 1012 "org/opencms/xml/content/xmlcontent-definition-8.xsd", 1013 CmsEncoder.ENCODING_UTF_8); 1014 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_8, content.getBytes(CmsEncoder.ENCODING_ISO_8859_1)); 1016 1017 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-8.xml", CmsEncoder.ENCODING_ISO_8859_1); 1019 xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_ISO_8859_1, resolver); 1020 xmlcontent.validateXmlStructure(resolver); 1022 1023 String resourcename = "/mappingtext.html"; 1024 cms.createResource( 1026 resourcename, 1027 OpenCms.getResourceManager().getResourceType("xmlcontent").getTypeId(), 1028 content.getBytes(CmsEncoder.ENCODING_ISO_8859_1), 1029 Collections.EMPTY_LIST); 1030 1031 CmsFile file = cms.readFile(resourcename); 1032 xmlcontent = CmsXmlContentFactory.unmarshal(cms, file); 1033 1034 CmsProperty titleProperty = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_TITLE, false); 1035 assertSame(titleProperty, CmsProperty.getNullProperty()); 1036 1037 CmsProperty localeProperty = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_LOCALE, false); 1038 assertSame(localeProperty, CmsProperty.getNullProperty()); 1039 1040 CmsProperty navImageProperty = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_NAVIMAGE, false); 1041 assertSame(navImageProperty, CmsProperty.getNullProperty()); 1042 1043 CmsProperty navInfoProperty = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_NAVINFO, false); 1044 assertSame(navInfoProperty, CmsProperty.getNullProperty()); 1045 1046 String titleStr = "This must be the Title"; 1047 String navImageStr = "This is the String with xpath String[2]"; 1048 String navInfoStr = "Here we have the String with xpath String[3]"; 1049 1050 I_CmsXmlContentValue value; 1051 value = xmlcontent.addValue(cms, "String", Locale.ENGLISH, 0); 1052 value.setStringValue(cms, titleStr); 1053 1054 CmsXmlContentValueSequence seq = xmlcontent.getValueSequence("String", Locale.ENGLISH); 1056 assertEquals(1, seq.getElementCount()); 1057 value = seq.addValue(cms, 1); 1058 value.setStringValue(cms, navImageStr); 1059 value = seq.addValue(cms, 2); 1060 value.setStringValue(cms, navInfoStr); 1061 1062 String localeStr = "en"; 1063 value = xmlcontent.addValue(cms, "Locale", Locale.ENGLISH, 0); 1064 value.setStringValue(cms, localeStr); 1065 1066 file.setContents(xmlcontent.toString().getBytes(CmsEncoder.ENCODING_ISO_8859_1)); 1067 cms.writeFile(file); 1068 1069 titleProperty = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_TITLE, false); 1070 assertEquals(titleStr, titleProperty.getValue()); 1071 assertEquals(titleStr, titleProperty.getStructureValue()); 1072 assertNull(titleProperty.getResourceValue()); 1073 1074 navImageProperty = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_NAVIMAGE, false); 1075 assertEquals(navImageStr, navImageProperty.getValue()); 1076 assertEquals(navImageStr, navImageProperty.getResourceValue()); 1077 assertNull(navImageProperty.getStructureValue()); 1078 1079 navInfoProperty = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_NAVINFO, false); 1080 assertEquals(navInfoStr, navInfoProperty.getValue()); 1081 assertEquals(navInfoStr, navInfoProperty.getStructureValue()); 1082 assertNull(navInfoProperty.getResourceValue()); 1083 1084 localeProperty = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_LOCALE, false); 1085 assertEquals(localeStr, localeProperty.getValue()); 1086 assertEquals(localeStr, localeProperty.getResourceValue()); 1087 assertNull(localeProperty.getStructureValue()); 1088 } 1089 1090 1095 public void testMappingsAsList() throws Exception { 1096 1097 CmsObject cms = getCmsObject(); 1098 echo("Testing element mappings fom XML content to a property list"); 1099 1100 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 1101 1102 String content; 1103 CmsXmlContent xmlcontent; 1104 1105 1107 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-8.xml", CmsEncoder.ENCODING_ISO_8859_1); 1109 xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_ISO_8859_1, resolver); 1110 xmlcontent.validateXmlStructure(resolver); 1112 1113 String resourcename = "/mappinglist.html"; 1114 cms.createResource( 1116 resourcename, 1117 OpenCms.getResourceManager().getResourceType("xmlcontent").getTypeId(), 1118 content.getBytes(CmsEncoder.ENCODING_ISO_8859_1), 1119 Collections.EMPTY_LIST); 1120 1121 CmsFile file = cms.readFile(resourcename); 1122 xmlcontent = CmsXmlContentFactory.unmarshal(cms, file); 1123 1124 CmsProperty prop; 1125 prop = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_DESCRIPTION, false); 1126 assertSame(prop, CmsProperty.getNullProperty()); 1127 1128 I_CmsXmlContentValue value; 1129 CmsXmlContentValueSequence seq = xmlcontent.getValueSequence("VfsFile", Locale.ENGLISH); 1130 assertEquals(0, seq.getElementCount()); 1131 1132 String res1 = "/index.html"; 1133 String res2 = "/xmlcontent/"; 1134 String res3 = "/xmlcontent/article_0001.html"; 1135 String res4 = "/folder1/index.html"; 1136 1137 String sr = cms.getRequestContext().getSiteRoot(); 1138 String propValue = sr + res1 + "|" + sr + res2 + "|" + sr + res3 + "|" + sr + res4; 1139 1140 value = seq.addValue(cms, 0); 1141 value.setStringValue(cms, res1); 1142 value = seq.addValue(cms, 1); 1143 value.setStringValue(cms, res2); 1144 value = seq.addValue(cms, 2); 1145 value.setStringValue(cms, res3); 1146 value = seq.addValue(cms, 3); 1147 value.setStringValue(cms, res4); 1148 1149 assertEquals(4, seq.getElementCount()); 1150 xmlcontent.validateXmlStructure(resolver); 1152 1153 file.setContents(xmlcontent.toString().getBytes(CmsEncoder.ENCODING_ISO_8859_1)); 1154 cms.writeFile(file); 1155 1156 prop = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_DESCRIPTION, false); 1158 List list = prop.getValueList(); 1159 assertNotNull(list); 1160 assertEquals(4, list.size()); 1161 assertEquals(sr + res1, list.get(0)); 1162 assertEquals(sr + res2, list.get(1)); 1163 assertEquals(sr + res3, list.get(2)); 1164 assertEquals(sr + res4, list.get(3)); 1165 assertEquals(propValue, prop.getValue()); 1166 assertEquals(propValue, prop.getStructureValue()); 1167 assertNull(prop.getResourceValue()); 1168 1169 CmsProperty prop2; 1170 prop2 = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_KEYWORDS, false); 1171 assertSame(prop2, CmsProperty.getNullProperty()); 1172 1173 I_CmsXmlContentValue value2; 1174 CmsXmlContentValueSequence seq2 = xmlcontent.getValueSequence("VfsFile2", Locale.ENGLISH); 1175 assertEquals(0, seq2.getElementCount()); 1176 1177 value2 = seq2.addValue(cms, 0); 1178 value2.setStringValue(cms, res1); 1179 value2 = seq2.addValue(cms, 1); 1180 value2.setStringValue(cms, res2); 1181 value2 = seq2.addValue(cms, 2); 1182 value2.setStringValue(cms, res3); 1183 value2 = seq2.addValue(cms, 3); 1184 value2.setStringValue(cms, res4); 1185 1186 assertEquals(4, seq2.getElementCount()); 1187 xmlcontent.validateXmlStructure(resolver); 1189 1190 file.setContents(xmlcontent.toString().getBytes(CmsEncoder.ENCODING_ISO_8859_1)); 1191 cms.writeFile(file); 1192 1193 prop2 = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_KEYWORDS, false); 1195 List list2 = prop.getValueList(); 1196 assertNotNull(list2); 1197 assertEquals(4, list2.size()); 1198 assertEquals(sr + res1, list2.get(0)); 1199 assertEquals(sr + res2, list2.get(1)); 1200 assertEquals(sr + res3, list2.get(2)); 1201 assertEquals(sr + res4, list2.get(3)); 1202 assertEquals(propValue, prop2.getValue()); 1203 assertEquals(propValue, prop2.getResourceValue()); 1204 assertNull(prop2.getStructureValue()); 1205 1206 CmsProperty prop3; 1207 prop3 = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_NAVTEXT, false); 1208 assertSame(prop3, CmsProperty.getNullProperty()); 1209 1210 I_CmsXmlContentValue value3; 1211 CmsXmlContentValueSequence seq3 = xmlcontent.getValueSequence("VfsFile3", Locale.ENGLISH); 1212 assertEquals(0, seq3.getElementCount()); 1213 1214 value3 = seq3.addValue(cms, 0); 1215 value3.setStringValue(cms, res1); 1216 value3 = seq3.addValue(cms, 1); 1217 value3.setStringValue(cms, res2); 1218 value3 = seq3.addValue(cms, 2); 1219 value3.setStringValue(cms, res3); 1220 value3 = seq3.addValue(cms, 3); 1221 value3.setStringValue(cms, res4); 1222 1223 assertEquals(4, seq3.getElementCount()); 1224 xmlcontent.validateXmlStructure(resolver); 1226 1227 file.setContents(xmlcontent.toString().getBytes(CmsEncoder.ENCODING_ISO_8859_1)); 1228 cms.writeFile(file); 1229 1230 prop3 = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_NAVTEXT, false); 1232 List list3 = prop.getValueList(); 1233 assertNotNull(list3); 1234 assertEquals(4, list3.size()); 1235 assertEquals(sr + res1, list3.get(0)); 1236 assertEquals(sr + res2, list3.get(1)); 1237 assertEquals(sr + res3, list3.get(2)); 1238 assertEquals(sr + res4, list3.get(3)); 1239 assertEquals(propValue, prop3.getValue()); 1240 assertEquals(propValue, prop3.getStructureValue()); 1241 assertNull(prop3.getResourceValue()); 1242 } 1243 1244 1249 public void testMappingsOfNestedContent() throws Exception { 1250 1251 CmsObject cms = getCmsObject(); 1252 echo("Testing mapping of values in the XML content with nested elements"); 1253 1254 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 1255 1256 String content; 1257 CmsXmlContent xmlcontent; 1258 1259 content = CmsFileUtil.readFile( 1261 "org/opencms/xml/content/xmlcontent-definition-7.xsd", 1262 CmsEncoder.ENCODING_UTF_8); 1263 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_7, content.getBytes(CmsEncoder.ENCODING_UTF_8)); 1265 1266 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-7.xml", CmsEncoder.ENCODING_UTF_8); 1268 xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 1269 1270 String resourcename = "/mappingtest_nested.html"; 1271 cms.createResource( 1273 resourcename, 1274 OpenCms.getResourceManager().getResourceType("xmlcontent").getTypeId(), 1275 content.getBytes(CmsEncoder.ENCODING_ISO_8859_1), 1276 Collections.EMPTY_LIST); 1277 1278 CmsFile file = cms.readFile(resourcename); 1279 xmlcontent = CmsXmlContentFactory.unmarshal(cms, file); 1280 1281 CmsProperty titleProperty; 1282 titleProperty = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_TITLE, false); 1283 assertSame(titleProperty, CmsProperty.getNullProperty()); 1284 1285 String titleStr = "This must be the Title (not nested)"; 1286 I_CmsXmlContentValue value; 1287 value = xmlcontent.getValue("Test", Locale.ENGLISH); 1288 assertEquals(value.getStringValue(cms), "Another Test"); 1289 value.setStringValue(cms, titleStr); 1290 1291 String descStr = "This must be the Description (which IS nested)"; 1292 value = xmlcontent.getValue("Cascade/Toast", Locale.ENGLISH); 1293 assertEquals(value.getStringValue(cms), "Toast"); 1294 value.setStringValue(cms, descStr); 1295 1296 file.setContents(xmlcontent.toString().getBytes(CmsEncoder.ENCODING_ISO_8859_1)); 1297 cms.writeFile(file); 1298 1299 titleProperty = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_TITLE, false); 1300 assertEquals(titleStr, titleProperty.getValue()); 1301 titleProperty = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_DESCRIPTION, false); 1302 assertEquals(descStr, titleProperty.getValue()); 1303 } 1304 1305 1310 public void testMappingsWithManyLocales() throws Exception { 1311 1312 CmsObject cms = getCmsObject(); 1313 echo("Testing mapping of values in the XML content with locales"); 1314 1315 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 1316 1317 String content; 1318 CmsXmlContent xmlcontent; 1319 1320 content = CmsFileUtil.readFile( 1322 "org/opencms/xml/content/xmlcontent-definition-8.xsd", 1323 CmsEncoder.ENCODING_UTF_8); 1324 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_8, content.getBytes(CmsEncoder.ENCODING_ISO_8859_1)); 1326 1327 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-8.xml", CmsEncoder.ENCODING_ISO_8859_1); 1329 xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_ISO_8859_1, resolver); 1330 xmlcontent.validateXmlStructure(resolver); 1332 1333 List properties = new ArrayList (); 1335 properties.add(new CmsProperty(CmsPropertyDefinition.PROPERTY_LOCALE, Locale.ENGLISH.toString(), null)); 1336 1337 String resourcenameEn = "/mappingtext_en.html"; 1338 String resourcenameDe = "/mappingtext_de.html"; 1339 cms.createResource( 1341 resourcenameEn, 1342 OpenCms.getResourceManager().getResourceType("xmlcontent").getTypeId(), 1343 content.getBytes(CmsEncoder.ENCODING_ISO_8859_1), 1344 properties); 1345 1346 cms.copyResource(resourcenameEn, resourcenameDe, org.opencms.file.CmsResource.COPY_AS_SIBLING); 1348 cms.changeLock(resourcenameDe); 1350 cms.writePropertyObject(resourcenameDe, new CmsProperty( 1352 CmsPropertyDefinition.PROPERTY_LOCALE, 1353 Locale.GERMAN.toString(), 1354 null)); 1355 1356 CmsFile file = cms.readFile(resourcenameDe); 1357 xmlcontent = CmsXmlContentFactory.unmarshal(cms, file); 1358 1359 xmlcontent.addLocale(cms, Locale.GERMAN); 1360 if (!xmlcontent.hasLocale(Locale.ENGLISH)) { 1361 xmlcontent.addLocale(cms, Locale.ENGLISH); 1362 } 1363 1364 String titleStrEn = "This must be the Title in EN"; 1365 I_CmsXmlContentValue value; 1366 value = xmlcontent.addValue(cms, "String", Locale.ENGLISH, 0); 1367 value.setStringValue(cms, titleStrEn); 1368 1369 String titleStrDe = "Das ist der Title in DE"; 1370 value = xmlcontent.addValue(cms, "String", Locale.GERMAN, 0); 1371 value.setStringValue(cms, titleStrDe); 1372 1373 file.setContents(xmlcontent.toString().getBytes(CmsEncoder.ENCODING_ISO_8859_1)); 1374 cms.writeFile(file); 1375 cms.unlockResource(resourcenameDe); 1377 1378 CmsProperty titlePropertyEn = cms.readPropertyObject( 1380 resourcenameEn, 1381 CmsPropertyDefinition.PROPERTY_TITLE, 1382 false); 1383 assertEquals(titleStrEn, titlePropertyEn.getValue()); 1384 1385 CmsProperty titlePropertyDe = cms.readPropertyObject( 1386 resourcenameDe, 1387 CmsPropertyDefinition.PROPERTY_TITLE, 1388 false); 1389 assertEquals(titleStrDe, titlePropertyDe.getValue()); 1390 } 1391 1392 1397 public void testNestedSchema() throws Exception { 1398 1399 CmsObject cms = getCmsObject(); 1400 echo("Testing for nested XML content schemas"); 1401 1402 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 1403 1404 String content; 1405 1406 content = CmsFileUtil.readFile( 1408 "org/opencms/xml/content/xmlcontent-definition-4.xsd", 1409 CmsEncoder.ENCODING_UTF_8); 1410 CmsXmlContentDefinition definition = CmsXmlContentDefinition.unmarshal(content, SCHEMA_SYSTEM_ID_4, resolver); 1411 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_4, content.getBytes(CmsEncoder.ENCODING_UTF_8)); 1413 1414 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-4.xml", CmsEncoder.ENCODING_UTF_8); 1416 CmsXmlContent xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 1417 System.out.println(xmlcontent.toString()); 1418 1419 xmlcontent.validateXmlStructure(resolver); 1421 1422 assertTrue(xmlcontent.hasValue("Title", Locale.ENGLISH)); 1423 assertTrue(xmlcontent.hasValue("Title[1]", Locale.ENGLISH)); 1424 assertTrue(xmlcontent.hasValue("Cascade[1]/Html[1]", Locale.ENGLISH)); 1425 assertTrue(xmlcontent.hasValue("Cascade[1]/VfsLink[1]", Locale.ENGLISH)); 1426 assertTrue(xmlcontent.hasValue("Cascade[1]/VfsLink[2]", Locale.ENGLISH)); 1427 assertTrue(xmlcontent.hasValue("Cascade[2]/Html[1]", Locale.ENGLISH)); 1428 assertTrue(xmlcontent.hasValue("Cascade[2]/VfsLink[1]", Locale.ENGLISH)); 1429 assertTrue(xmlcontent.hasValue("Cascade[2]/Html", Locale.ENGLISH)); 1430 assertTrue(xmlcontent.hasValue("Cascade[2]/VfsLink", Locale.ENGLISH)); 1431 1432 assertTrue(xmlcontent.hasValue("Cascade", Locale.ENGLISH)); 1433 assertTrue(xmlcontent.hasValue("Cascade[1]", Locale.ENGLISH)); 1434 assertTrue(xmlcontent.hasValue("Cascade[2]", Locale.ENGLISH)); 1435 1436 assertTrue(xmlcontent.hasValue("Cascade/Html", Locale.ENGLISH)); 1437 assertTrue(xmlcontent.hasValue("Cascade/Html[1]", Locale.ENGLISH)); 1438 assertTrue(xmlcontent.hasValue("Cascade/VfsLink", Locale.ENGLISH)); 1439 assertTrue(xmlcontent.hasValue("Cascade/VfsLink[1]", Locale.ENGLISH)); 1440 assertTrue(xmlcontent.hasValue("Cascade/VfsLink[2]", Locale.ENGLISH)); 1441 1442 assertFalse(xmlcontent.hasValue("Title[0]", Locale.ENGLISH)); 1444 assertFalse(xmlcontent.hasValue("Cascade[0]", Locale.ENGLISH)); 1445 1446 I_CmsXmlContentValue value1; 1447 I_CmsXmlContentValue value2; 1448 1449 value1 = xmlcontent.getValue("Title", Locale.ENGLISH); 1450 value2 = xmlcontent.getValue("Title[1]", Locale.ENGLISH); 1451 assertSame(value1, value2); 1452 1453 value2 = xmlcontent.getValue("Title", Locale.ENGLISH, 0); 1454 assertSame(value1, value2); 1455 1456 String xpath = "Cascade[1]/VfsLink[2]"; 1457 value1 = xmlcontent.getValue(xpath, Locale.ENGLISH); 1458 assertEquals(xpath, value1.getPath()); 1459 1460 xpath = "Title[1]"; 1461 value1 = xmlcontent.getValue(xpath, Locale.ENGLISH); 1462 assertEquals(xpath, value1.getPath()); 1463 1464 xpath = "Cascade/Html"; 1465 value1 = xmlcontent.getValue(xpath, Locale.ENGLISH); 1466 assertEquals(CmsXmlUtils.createXpath(xpath, 1), value1.getPath()); 1467 1468 xpath = "Cascade"; 1469 value1 = xmlcontent.getValue(xpath, Locale.ENGLISH); 1470 assertEquals(CmsXmlUtils.createXpath(xpath, 1), value1.getPath()); 1471 1472 assertSame(definition.getContentHandler().getClass().getName(), TestXmlContentHandler.class.getName()); 1473 } 1474 1475 1480 public void testResourceBundle() throws Exception { 1481 1482 CmsObject cms = getCmsObject(); 1483 echo("Testing using different resource bundles in content handler for XML content"); 1484 1485 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 1486 1487 String content; 1488 CmsXmlContentDefinition definition; 1489 1490 content = CmsFileUtil.readFile( 1492 "org/opencms/xml/content/xmlcontent-definition-3.xsd", 1493 CmsEncoder.ENCODING_UTF_8); 1494 definition = CmsXmlContentDefinition.unmarshal(content, SCHEMA_SYSTEM_ID_3, resolver); 1495 1496 I_CmsXmlContentHandler contentHandler; 1497 1498 contentHandler = definition.getContentHandler(); 1499 assertSame(definition.getContentHandler().getClass().getName(), TestXmlContentHandler.class.getName()); 1500 assertNull(contentHandler.getMessages(Locale.ENGLISH)); 1501 1502 content = CmsFileUtil.readFile( 1504 "org/opencms/xml/content/xmlcontent-definition-8.xsd", 1505 CmsEncoder.ENCODING_UTF_8); 1506 definition = CmsXmlContentDefinition.unmarshal(content, SCHEMA_SYSTEM_ID_8, resolver); 1507 1508 contentHandler = definition.getContentHandler(); 1509 assertSame(definition.getContentHandler().getClass().getName(), CmsDefaultXmlContentHandler.class.getName()); 1510 1511 CmsMessages messages = contentHandler.getMessages(Locale.ENGLISH); 1512 assertNotNull(messages); 1513 assertEquals( 1514 "The following errors occurred when validating the form:", 1515 messages.key(org.opencms.xml.content.Messages.GUI_EDITOR_XMLCONTENT_VALIDATION_ERROR_TITLE_0)); 1516 } 1517 1518 1523 public void testValidation() throws Exception { 1524 1525 CmsObject cms = getCmsObject(); 1526 echo("Testing the validation for values in the XML content"); 1527 1528 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 1529 1530 String content; 1531 CmsXmlContent xmlcontent; 1532 1533 content = CmsFileUtil.readFile( 1535 "org/opencms/xml/content/xmlcontent-definition-7.xsd", 1536 CmsEncoder.ENCODING_UTF_8); 1537 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_7, content.getBytes(CmsEncoder.ENCODING_UTF_8)); 1539 1540 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-7.xml", CmsEncoder.ENCODING_UTF_8); 1542 xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 1543 1544 xmlcontent.addValue(cms, "DeepCascade", Locale.ENGLISH, 0); 1546 xmlcontent.addValue(cms, "DeepCascade", Locale.ENGLISH, 1); 1547 xmlcontent.addLocale(cms, Locale.GERMAN); 1548 xmlcontent.addValue(cms, "DeepCascade", Locale.GERMAN, 0); 1549 System.out.println(xmlcontent.toString()); 1551 1552 CmsXmlContentErrorHandler errorHandler; 1553 1554 errorHandler = xmlcontent.validate(cms); 1556 assertFalse(errorHandler.hasErrors()); 1557 assertFalse(errorHandler.hasWarnings()); 1558 1559 I_CmsXmlContentValue value1; 1560 1561 value1 = xmlcontent.getValue("Test", Locale.ENGLISH); 1562 value1.setStringValue(cms, "This produces a warning!"); 1563 1564 errorHandler = xmlcontent.validate(cms); 1565 assertFalse(errorHandler.hasErrors()); 1566 assertTrue(errorHandler.hasWarnings()); 1567 1568 value1.setStringValue(cms, "This produces a warning and an error!"); 1569 1570 errorHandler = xmlcontent.validate(cms); 1571 assertTrue(errorHandler.hasErrors()); 1572 assertTrue(errorHandler.hasWarnings()); 1573 assertEquals(1, errorHandler.getErrors().size()); 1574 assertEquals(1, errorHandler.getWarnings().size()); 1575 1576 value1 = xmlcontent.getValue("Toast", Locale.ENGLISH); 1577 value1.setStringValue(cms, "This produces a warning but no error!"); 1578 1579 errorHandler = xmlcontent.validate(cms); 1580 assertTrue(errorHandler.hasErrors()); 1581 assertTrue(errorHandler.hasWarnings()); 1582 assertEquals(1, errorHandler.getErrors(Locale.ENGLISH).size()); 1583 assertEquals(2, errorHandler.getWarnings(Locale.ENGLISH).size()); 1584 1585 value1 = xmlcontent.addValue(cms, "Option", Locale.ENGLISH, 0); 1586 assertEquals("Default value from the appinfos", value1.getStringValue(cms)); 1587 1588 System.out.println(xmlcontent.toString()); 1590 xmlcontent = CmsXmlContentFactory.unmarshal(xmlcontent.toString(), CmsEncoder.ENCODING_UTF_8, resolver); 1592 xmlcontent.validateXmlStructure(resolver); 1594 1595 value1 = xmlcontent.getValue("DeepCascade[1]/Cascade[1]/VfsLink", Locale.ENGLISH); 1596 value1.setStringValue(cms, "/system/workplace/warning"); 1597 1598 value1 = xmlcontent.getValue("DeepCascade[1]/Cascade[1]/Html", Locale.ENGLISH); 1599 value1.setStringValue(cms, "This HTML contains an error!"); 1600 1601 value1 = xmlcontent.addValue(cms, "DeepCascade[1]/Cascade[1]/Option", Locale.ENGLISH, 0); 1602 assertEquals("Default value from the XML", value1.getStringValue(cms)); 1603 1604 System.out.println(xmlcontent.toString()); 1606 xmlcontent = CmsXmlContentFactory.unmarshal(xmlcontent.toString(), CmsEncoder.ENCODING_UTF_8, resolver); 1608 xmlcontent.validateXmlStructure(resolver); 1610 1611 errorHandler = xmlcontent.validate(cms); 1612 assertTrue(errorHandler.hasErrors()); 1613 assertTrue(errorHandler.hasWarnings()); 1614 assertEquals(2, errorHandler.getErrors(Locale.ENGLISH).size()); 1615 assertEquals(3, errorHandler.getWarnings(Locale.ENGLISH).size()); 1616 } 1617 1618 1623 public void testValidationExtended() throws Exception { 1624 1625 CmsObject cms = getCmsObject(); 1626 echo("Extended test for the validation of values in the XML content"); 1627 1628 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 1629 1630 String content; 1631 CmsXmlContent xmlcontent; 1632 1633 content = CmsFileUtil.readFile( 1635 "org/opencms/xml/content/xmlcontent-definition-8.xsd", 1636 CmsEncoder.ENCODING_UTF_8); 1637 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_8, content.getBytes(CmsEncoder.ENCODING_UTF_8)); 1639 1640 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-8.xml", CmsEncoder.ENCODING_UTF_8); 1642 xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 1643 xmlcontent.validateXmlStructure(resolver); 1645 1646 CmsXmlContentValueSequence sequence; 1647 I_CmsXmlContentValue value; 1648 1649 sequence = xmlcontent.getValueSequence("String", Locale.ENGLISH); 1650 value = sequence.addValue(cms, 0); 1651 value.setStringValue(cms, "This is a String that contains an error and a warning!"); 1652 1653 xmlcontent.validateXmlStructure(resolver); 1655 1656 CmsXmlContentErrorHandler errorHandler; 1657 1658 errorHandler = xmlcontent.validate(cms); 1659 assertTrue(errorHandler.hasErrors()); 1660 assertTrue(errorHandler.hasWarnings()); 1661 assertTrue(errorHandler.hasErrors(Locale.ENGLISH)); 1662 assertFalse(errorHandler.hasErrors(Locale.GERMAN)); 1663 assertFalse(errorHandler.hasErrors(Locale.FRENCH)); 1664 assertEquals(1, errorHandler.getErrors().size()); 1665 assertEquals(1, errorHandler.getErrors(Locale.ENGLISH).size()); 1666 assertEquals(1, errorHandler.getWarnings(Locale.ENGLISH).size()); 1667 1668 value.setStringValue(cms, "This is a nice String"); 1669 errorHandler = xmlcontent.validate(cms); 1670 assertFalse(errorHandler.hasErrors()); 1671 assertFalse(errorHandler.hasWarnings()); 1672 1673 sequence = xmlcontent.getValueSequence("DateTime", Locale.ENGLISH); 1674 value = sequence.addValue(cms, 0); 1675 value.setStringValue(cms, "invalid!"); 1676 1677 boolean error = true; 1678 try { 1679 xmlcontent.validateXmlStructure(resolver); 1680 } catch (Exception e) { 1681 error = false; 1682 } 1683 if (error) { 1684 fail("Invalid value was possible for DateTime"); 1685 } 1686 1687 errorHandler = xmlcontent.validate(cms); 1688 assertTrue(errorHandler.hasErrors()); 1689 assertFalse(errorHandler.hasWarnings()); 1690 assertEquals(1, errorHandler.getErrors(Locale.ENGLISH).size()); 1691 1692 value.setStringValue(cms, String.valueOf(System.currentTimeMillis())); 1693 xmlcontent.validateXmlStructure(resolver); 1694 errorHandler = xmlcontent.validate(cms); 1695 assertFalse(errorHandler.hasErrors()); 1696 assertFalse(errorHandler.hasWarnings()); 1697 1698 sequence = xmlcontent.getValueSequence("Color", Locale.ENGLISH); 1699 value = sequence.addValue(cms, 0); 1700 value.setStringValue(cms, "invalid!"); 1701 1702 error = true; 1703 try { 1704 xmlcontent.validateXmlStructure(resolver); 1705 } catch (Exception e) { 1706 error = false; 1707 } 1708 if (error) { 1709 fail("Invalid value was possible for Color"); 1710 } 1711 1712 errorHandler = xmlcontent.validate(cms); 1713 assertTrue(errorHandler.hasErrors()); 1714 assertFalse(errorHandler.hasWarnings()); 1715 assertEquals(1, errorHandler.getErrors(Locale.ENGLISH).size()); 1716 1717 value.setStringValue(cms, "#fff"); 1718 xmlcontent.validateXmlStructure(resolver); 1719 errorHandler = xmlcontent.validate(cms); 1720 assertTrue(errorHandler.hasErrors()); 1721 assertFalse(errorHandler.hasWarnings()); 1722 assertEquals(1, errorHandler.getErrors(Locale.ENGLISH).size()); 1723 1724 assertEquals("A valid HTML color value (e.g. #ffffff) is required", errorHandler.getErrors(Locale.ENGLISH).get( 1726 value.getPath())); 1727 1728 value.setStringValue(cms, "#ffffff"); 1729 xmlcontent.validateXmlStructure(resolver); 1730 errorHandler = xmlcontent.validate(cms); 1731 assertFalse(errorHandler.hasErrors()); 1732 assertFalse(errorHandler.hasWarnings()); 1733 1734 sequence = xmlcontent.getValueSequence("Locale", Locale.ENGLISH); 1735 value = sequence.addValue(cms, 0); 1736 value.setStringValue(cms, "invalid!"); 1737 1738 error = true; 1739 try { 1740 xmlcontent.validateXmlStructure(resolver); 1741 } catch (Exception e) { 1742 error = false; 1743 } 1744 if (error) { 1745 fail("Invalid value was possible for Locale"); 1746 } 1747 1748 errorHandler = xmlcontent.validate(cms); 1749 assertTrue(errorHandler.hasErrors()); 1750 assertFalse(errorHandler.hasWarnings()); 1751 assertEquals(1, errorHandler.getErrors(Locale.ENGLISH).size()); 1752 1753 value.setStringValue(cms, Locale.GERMANY.toString()); 1754 xmlcontent.validateXmlStructure(resolver); 1755 errorHandler = xmlcontent.validate(cms); 1756 assertTrue(errorHandler.hasErrors()); 1757 assertFalse(errorHandler.hasWarnings()); 1758 assertEquals(1, errorHandler.getErrors(Locale.ENGLISH).size()); 1759 1760 value.setStringValue(cms, Locale.GERMAN.toString()); 1761 xmlcontent.validateXmlStructure(resolver); 1762 errorHandler = xmlcontent.validate(cms); 1763 assertFalse(errorHandler.hasErrors()); 1764 assertFalse(errorHandler.hasWarnings()); 1765 1766 System.out.println(xmlcontent.toString()); 1768 } 1769 1770 1775 public void testValidationLocale() throws Exception { 1776 1777 CmsObject cms = getCmsObject(); 1778 echo("Test for the validation of multiple locale values in the XML content"); 1779 1780 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 1781 1782 String content; 1783 CmsXmlContent xmlcontent; 1784 1785 content = CmsFileUtil.readFile( 1787 "org/opencms/xml/content/xmlcontent-definition-8.xsd", 1788 CmsEncoder.ENCODING_UTF_8); 1789 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_8, content.getBytes(CmsEncoder.ENCODING_UTF_8)); 1791 1792 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-8.xml", CmsEncoder.ENCODING_UTF_8); 1794 xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 1795 xmlcontent.validateXmlStructure(resolver); 1797 1798 CmsXmlContentValueSequence sequence; 1799 I_CmsXmlContentValue value; 1800 1801 xmlcontent.addLocale(cms, Locale.GERMAN); 1802 1803 sequence = xmlcontent.getValueSequence("String", Locale.ENGLISH); 1804 value = sequence.addValue(cms, 0); 1805 value.setStringValue(cms, "This is a String that contains an error and a warning!"); 1806 1807 sequence = xmlcontent.getValueSequence("String", Locale.GERMAN); 1808 value = sequence.addValue(cms, 0); 1809 value.setStringValue(cms, "Dieser String enthällt einen Fehler (English: 'error') und eine Warnung!"); 1810 1811 xmlcontent.validateXmlStructure(resolver); 1813 1814 CmsXmlContentErrorHandler errorHandler; 1815 1816 errorHandler = xmlcontent.validate(cms); 1817 assertTrue(errorHandler.hasErrors()); 1818 assertTrue(errorHandler.hasWarnings()); 1819 assertTrue(errorHandler.hasErrors(Locale.ENGLISH)); 1820 assertTrue(errorHandler.hasErrors(Locale.GERMAN)); 1821 assertFalse(errorHandler.hasErrors(Locale.FRENCH)); 1822 assertEquals(2, errorHandler.getErrors().size()); 1823 assertEquals(1, errorHandler.getErrors(Locale.ENGLISH).size()); 1824 assertEquals(1, errorHandler.getWarnings(Locale.ENGLISH).size()); 1825 assertEquals(1, errorHandler.getErrors(Locale.GERMAN).size()); 1826 assertEquals(1, errorHandler.getWarnings(Locale.GERMAN).size()); 1827 1828 System.out.println(xmlcontent.toString()); 1830 } 1831 1832 1837 public void testValueIndex() throws Exception { 1838 1839 CmsObject cms = getCmsObject(); 1840 echo("Testing the value index for nodes in the XML content"); 1841 1842 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms); 1843 1844 String content; 1845 CmsXmlContent xmlcontent; 1846 1847 content = CmsFileUtil.readFile( 1849 "org/opencms/xml/content/xmlcontent-definition-7.xsd", 1850 CmsEncoder.ENCODING_UTF_8); 1851 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_7, content.getBytes(CmsEncoder.ENCODING_UTF_8)); 1853 1854 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-7.xml", CmsEncoder.ENCODING_UTF_8); 1856 xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 1857 1858 CmsXmlContentValueSequence toastSequence = xmlcontent.getValueSequence("Toast", Locale.ENGLISH); 1860 1861 for (int i = 0; i < 2; i++) { 1862 I_CmsXmlContentValue value = toastSequence.addValue(cms, 0); 1863 value.setStringValue(cms, "Added toast value " + i); 1864 } 1865 1866 System.out.println(xmlcontent.toString()); 1868 1869 assertEquals(toastSequence.getElementCount(), 3); 1870 for (int i = 0; i < 3; i++) { 1871 I_CmsXmlContentValue value = toastSequence.getValue(i); 1872 assertEquals(i, value.getIndex()); 1873 } 1874 1875 I_CmsXmlContentValue toastValue = toastSequence.getValue(1); 1877 assertEquals(1, toastValue.getMinOccurs()); 1878 assertEquals(3, toastValue.getMaxOccurs()); 1879 1880 I_CmsXmlContentValue value1 = xmlcontent.getValue("Test", Locale.ENGLISH); 1882 assertSame( 1883 TestXmlContentHandler.class.getName(), 1884 value1.getContentDefinition().getContentHandler().getClass().getName()); 1885 1886 value1 = xmlcontent.getValue("Cascade", Locale.ENGLISH); 1887 assertSame( 1888 TestXmlContentHandler.class.getName(), 1889 value1.getContentDefinition().getContentHandler().getClass().getName()); 1890 1891 value1 = xmlcontent.getValue("Cascade/Title", Locale.ENGLISH); 1892 assertSame( 1893 CmsDefaultXmlContentHandler.class.getName(), 1894 value1.getContentDefinition().getContentHandler().getClass().getName()); 1895 } 1896} | Popular Tags |