1 31 32 package org.opencms.file; 33 34 import org.opencms.file.types.CmsResourceTypePlain; 35 import org.opencms.main.CmsRuntimeException; 36 import org.opencms.report.CmsShellReport; 37 import org.opencms.test.OpenCmsTestCase; 38 import org.opencms.test.OpenCmsTestProperties; 39 import org.opencms.test.OpenCmsTestResourceFilter; 40 41 import java.util.ArrayList ; 42 import java.util.Collections ; 43 import java.util.List ; 44 import java.util.Locale ; 45 46 import junit.extensions.TestSetup; 47 import junit.framework.Test; 48 import junit.framework.TestSuite; 49 50 56 public class TestProperty extends OpenCmsTestCase { 57 58 63 public TestProperty(String arg0) { 64 super(arg0); 65 } 66 67 72 public static Test suite() { 73 74 OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH); 75 76 TestSuite suite = new TestSuite(); 77 suite.setName(TestProperty.class.getName()); 78 79 suite.addTest(new TestProperty("testFrozenProperty")); 80 suite.addTest(new TestProperty("testNullProperty")); 81 suite.addTest(new TestProperty("testSharedPropertyIssue1")); 82 suite.addTest(new TestProperty("testPropertyLists")); 83 suite.addTest(new TestProperty("testWriteProperty")); 84 suite.addTest(new TestProperty("testWriteProperties")); 85 suite.addTest(new TestProperty("testRemoveProperty")); 86 suite.addTest(new TestProperty("testRemoveProperties")); 87 suite.addTest(new TestProperty("testCreateProperty")); 88 suite.addTest(new TestProperty("testCreateProperties")); 89 suite.addTest(new TestProperty("testWritePropertyOnFolder")); 90 suite.addTest(new TestProperty("testDefaultPropertyCreation")); 91 suite.addTest(new TestProperty("testCaseSensitiveProperties")); 92 suite.addTest(new TestProperty("testReadResourcesWithProperty")); 93 94 TestSetup wrapper = new TestSetup(suite) { 95 96 protected void setUp() { 97 98 setupOpenCms("simpletest", "/sites/default/"); 99 } 100 101 protected void tearDown() { 102 103 removeOpenCms(); 104 } 105 }; 106 107 return wrapper; 108 } 109 110 115 public void testPropertyLists() throws Exception { 116 117 CmsObject cms = getCmsObject(); 118 echo("Testing reading and writing property lists"); 119 120 String source = "/xmlcontent/article_0001.html"; 121 cms.lockResource(source); 122 123 CmsProperty prop; 124 prop = cms.readPropertyObject(source, CmsPropertyDefinition.PROPERTY_TITLE, false); 125 126 assertEquals("Sample Article 1", prop.getValue()); 128 assertEquals("Sample Article 1", prop.getStructureValue()); 129 assertNull(prop.getResourceValue()); 130 131 assertEquals(1, prop.getValueList().size()); 133 assertEquals(1, prop.getStructureValueList().size()); 134 assertNull(prop.getResourceValueList()); 135 136 List list = new ArrayList (); 138 String value = ""; 139 for (int i=1; i<=10; i++) { 140 String s = "Title " + i; 141 list.add(s); 142 value += s; 143 if (i<10) { 144 value += CmsProperty.VALUE_LIST_DELIMITER; 145 } 146 } 147 prop.setStructureValueList(list); 148 149 assertEquals(value, prop.getValue()); 151 assertEquals(value, prop.getStructureValue()); 152 assertNull(prop.getResourceValue()); 153 assertEquals(10, prop.getValueList().size()); 154 assertEquals(10, prop.getStructureValueList().size()); 155 assertNull(prop.getResourceValueList()); 156 list = prop.getValueList(); 157 for (int i=1; i<=10; i++) { 158 String s = "Title " + i; 159 assertEquals(s, list.get(i-1).toString()); 160 } 161 162 cms.writePropertyObject(source, prop); 164 165 CmsProperty prop2 = cms.readPropertyObject(source, CmsPropertyDefinition.PROPERTY_TITLE, false); 167 168 assertEquals(value, prop2.getValue()); 170 assertEquals(value, prop2.getStructureValue()); 171 assertNull(prop2.getResourceValue()); 172 assertEquals(10, prop2.getValueList().size()); 173 assertEquals(10, prop2.getStructureValueList().size()); 174 assertNull(prop2.getResourceValueList()); 175 list = prop2.getValueList(); 176 for (int i=1; i<=10; i++) { 177 String s = "Title " + i; 178 assertEquals(s, list.get(i-1).toString()); 179 } 180 181 prop.setStructureValue(null); 183 value = "Test|Toast|Hi|Ho"; 184 prop.setValue(value, CmsProperty.TYPE_SHARED); 185 186 assertEquals(value, prop.getValue()); 187 assertEquals(value, prop.getResourceValue()); 188 assertNull(prop.getStructureValue()); 189 assertEquals(4, prop.getValueList().size()); 190 assertEquals(4, prop.getResourceValueList().size()); 191 assertNull(prop.getStructureValueList()); 192 assertEquals("Test", prop.getResourceValueList().get(0)); 193 assertEquals("Toast", prop.getResourceValueList().get(1)); 194 assertEquals("Hi", prop.getResourceValueList().get(2)); 195 assertEquals("Ho", prop.getResourceValueList().get(3)); 196 } 197 198 206 public static void createProperties(OpenCmsTestCase tc, CmsObject cms, String resource1, List propertyList1) throws Throwable { 207 208 tc.storeResources(cms, resource1); 209 210 long timestamp = System.currentTimeMillis(); 211 212 cms.lockResource(resource1); 213 cms.writePropertyObjects(resource1, propertyList1); 214 cms.unlockResource(resource1); 215 216 tc.assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_WRITEPROPERTY); 218 tc.assertProject(cms, resource1, cms.getRequestContext().currentProject()); 220 tc.assertState(cms, resource1, tc.getPreCalculatedState(resource1)); 222 tc.assertDateLastModifiedAfter(cms, resource1, timestamp); 224 tc.assertUserLastModified(cms, resource1, cms.getRequestContext().currentUser()); 226 tc.assertPropertyNew(cms, resource1, propertyList1); 228 } 229 230 238 public static void createProperty(OpenCmsTestCase tc, CmsObject cms, String resource1, CmsProperty property1) throws Throwable { 239 240 tc.storeResources(cms, resource1); 241 242 long timestamp = System.currentTimeMillis(); 243 244 cms.lockResource(resource1); 245 cms.writePropertyObject(resource1, property1); 246 cms.unlockResource(resource1); 247 248 tc.assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_WRITEPROPERTY); 250 tc.assertProject(cms, resource1, cms.getRequestContext().currentProject()); 252 tc.assertState(cms, resource1, tc.getPreCalculatedState(resource1)); 254 tc.assertDateLastModifiedAfter(cms, resource1, timestamp); 256 tc.assertUserLastModified(cms, resource1, cms.getRequestContext().currentUser()); 258 tc.assertPropertyNew(cms, resource1, property1); 260 } 261 262 270 public static void removeProperties(OpenCmsTestCase tc, CmsObject cms, String resource1, List propertyList1) throws Throwable { 271 272 tc.storeResources(cms, resource1); 273 274 long timestamp = System.currentTimeMillis(); 275 276 cms.lockResource(resource1); 277 cms.writePropertyObjects(resource1, propertyList1); 278 cms.unlockResource(resource1); 279 280 tc.assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_WRITEPROPERTY); 282 tc.assertProject(cms, resource1, cms.getRequestContext().currentProject()); 284 tc.assertState(cms, resource1, tc.getPreCalculatedState(resource1)); 286 tc.assertDateLastModifiedAfter(cms, resource1, timestamp); 288 tc.assertUserLastModified(cms, resource1, cms.getRequestContext().currentUser()); 290 tc.assertPropertyRemoved(cms, resource1, propertyList1); 292 } 293 294 302 public static void removeProperty(OpenCmsTestCase tc, CmsObject cms, String resource1, CmsProperty property1) throws Throwable { 303 304 tc.storeResources(cms, resource1); 305 306 long timestamp = System.currentTimeMillis(); 307 308 cms.lockResource(resource1); 309 cms.writePropertyObject(resource1, property1); 310 cms.unlockResource(resource1); 311 312 tc.assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_WRITEPROPERTY); 314 tc.assertProject(cms, resource1, cms.getRequestContext().currentProject()); 316 tc.assertState(cms, resource1, tc.getPreCalculatedState(resource1)); 318 tc.assertDateLastModifiedAfter(cms, resource1, timestamp); 320 tc.assertUserLastModified(cms, resource1, cms.getRequestContext().currentUser()); 322 tc.assertPropertyRemoved(cms, resource1, property1); 324 } 325 326 334 public static void writeProperties(OpenCmsTestCase tc, CmsObject cms, String resource1, List propertyList1) throws Throwable { 335 336 tc.storeResources(cms, resource1); 337 338 long timestamp = System.currentTimeMillis(); 339 340 cms.lockResource(resource1); 341 cms.writePropertyObjects(resource1, propertyList1); 342 cms.unlockResource(resource1); 343 344 tc.assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_WRITEPROPERTY); 346 tc.assertProject(cms, resource1, cms.getRequestContext().currentProject()); 348 tc.assertState(cms, resource1, CmsResource.STATE_CHANGED); 350 tc.assertDateLastModifiedAfter(cms, resource1, timestamp); 352 tc.assertUserLastModified(cms, resource1, cms.getRequestContext().currentUser()); 354 tc.assertPropertyChanged(cms, resource1, propertyList1); 356 } 357 358 366 public static void writeProperty(OpenCmsTestCase tc, CmsObject cms, String resource1, CmsProperty property1) throws Throwable { 367 368 tc.storeResources(cms, resource1); 369 370 long timestamp = System.currentTimeMillis(); 371 372 cms.lockResource(resource1); 373 cms.writePropertyObject(resource1, property1); 374 cms.unlockResource(resource1); 375 376 tc.assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_WRITEPROPERTY); 378 tc.assertProject(cms, resource1, cms.getRequestContext().currentProject()); 380 tc.assertState(cms, resource1, tc.getPreCalculatedState(resource1)); 382 tc.assertDateLastModifiedAfter(cms, resource1, timestamp); 384 tc.assertUserLastModified(cms, resource1, cms.getRequestContext().currentUser()); 386 tc.assertPropertyChanged(cms, resource1, property1); 388 } 389 390 395 public void testCreateProperties() throws Throwable { 396 397 CmsObject cms = getCmsObject(); 398 echo("Testing creating multiple properties on a resource"); 399 CmsProperty property8 = new CmsProperty("Newproperty", "testvalue1", "testvalue2"); 400 CmsProperty property9 = new CmsProperty("AnotherNewproperty", "anothervalue", null); 401 List propertyList3 = new ArrayList (); 402 propertyList3.add(property8); 403 propertyList3.add(property9); 404 createProperties(this, cms, "/index.html", propertyList3); 405 } 406 407 412 public void testCaseSensitiveProperties() throws Throwable { 413 414 CmsObject cms = getCmsObject(); 415 echo("Testing proper behaviour for case sensitiveness in property definition names"); 416 CmsProperty myProperty = new CmsProperty("myProperty", "myValue", "myValue"); 417 CmsProperty myproperty = new CmsProperty("myproperty", "myvalue", "myvalue"); 418 cms.lockResource("/index.html"); 419 cms.writePropertyObject("/index.html", myProperty); 420 cms.writePropertyObject("/index.html", myproperty); 421 cms.unlockResource("/index.html"); 422 assertEquals("myValue", cms.readPropertyObject("/index.html", "myProperty", false).getResourceValue()); 423 assertEquals("myvalue", cms.readPropertyObject("/index.html", "myproperty", false).getResourceValue()); 424 } 425 426 431 public void testCreateProperty() throws Throwable { 432 433 CmsObject cms = getCmsObject(); 434 echo("Testing creating one property on a resource"); 435 CmsProperty property7 = new CmsProperty("Newproperty", "testvalue1", "testvalue2"); 436 createProperty(this, cms, "/folder1/index.html", property7); 437 } 438 439 444 public void testRemoveProperties() throws Throwable { 445 446 CmsObject cms = getCmsObject(); 447 echo("Testing removing multiple properties on a resource"); 448 CmsProperty property5 = new CmsProperty("Title", CmsProperty.DELETE_VALUE, CmsProperty.DELETE_VALUE); 449 CmsProperty property6 = new CmsProperty("NavPos", CmsProperty.DELETE_VALUE, CmsProperty.DELETE_VALUE); 450 List propertyList2 = new ArrayList (); 451 propertyList2.add(property5); 452 propertyList2.add(property6); 453 removeProperties(this, cms, "/folder1/page1.html", propertyList2); 454 } 455 456 461 public void testRemoveProperty() throws Throwable { 462 463 CmsObject cms = getCmsObject(); 464 echo("Testing removing one property on a resource"); 465 CmsProperty property4 = new CmsProperty("Title", CmsProperty.DELETE_VALUE, CmsProperty.DELETE_VALUE); 466 removeProperty(this, cms, "/folder1/page2.html", property4); 467 } 468 469 474 public void testWriteProperties() throws Throwable { 475 476 CmsObject cms = getCmsObject(); 477 echo("Testing writing multiple properties on a resource"); 478 CmsProperty property2 = new CmsProperty("Title", "OpenCms", null); 479 CmsProperty property3 = new CmsProperty("NavPos", "1", null); 480 List propertyList1 = new ArrayList (); 481 propertyList1.add(property2); 482 propertyList1.add(property3); 483 writeProperties(this, cms, "/folder1/page3.html", propertyList1); 484 } 485 486 491 public void testWriteProperty() throws Throwable { 492 493 CmsObject cms = getCmsObject(); 494 echo("Testing writing one property on a resource"); 495 CmsProperty property1 = new CmsProperty("Title", "OpenCms", null); 496 writeProperty(this, cms, "/folder1/image1.gif", property1); 497 } 498 499 504 public void testWritePropertyOnFolder() throws Throwable { 505 506 CmsObject cms = getCmsObject(); 507 echo("Testing writing one property on a folder"); 508 CmsProperty property10 = new CmsProperty("Title", "OpenCms", null); 509 writeProperty(this, cms, "/folder2/", property10); 510 } 511 512 517 public void testReadResourcesWithProperty() throws Throwable { 518 519 CmsObject cms = getCmsObject(); 520 echo("Testing reading resources with property"); 521 522 String typesUri = "/types"; 523 CmsResource res = cms.readResource(typesUri); 524 cms.lockResource(typesUri); 526 cms.writePropertyObject(typesUri, new CmsProperty(CmsPropertyDefinition.PROPERTY_EXPORTNAME, "myfolder", null)); 527 cms.publishProject(); 529 530 List result = cms.readResourcesWithProperty(CmsPropertyDefinition.PROPERTY_EXPORTNAME); 531 assertTrue(result.contains(res)); 532 } 533 534 539 public void testDefaultPropertyCreation() throws Throwable { 540 541 CmsObject cms = getCmsObject(); 542 echo("Testing default property creation"); 543 544 String resourcename = "/folder1/article_test.html"; 545 byte[] content = new byte[0]; 546 547 cms.createResource(resourcename, 12, content, null); 549 550 assertResourceType(cms, resourcename, 12); 552 assertProject(cms, resourcename, cms.getRequestContext().currentProject()); 554 assertState(cms, resourcename, CmsResource.STATE_NEW); 556 assertUserLastModified(cms, resourcename, cms.getRequestContext().currentUser()); 558 559 CmsProperty property1, property2; 560 property1 = new CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, "Test title", null); 561 property2 = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_TITLE, false); 562 assertTrue(property1.isIdentical(property2)); 563 564 property1 = new CmsProperty("template-elements", "/system/modules/org.opencms.frontend.templateone.form/pages/form.html", null); 565 property2 = cms.readPropertyObject(resourcename, "template-elements", false); 566 assertTrue(property1.isIdentical(property2)); 567 568 property1 = new CmsProperty(CmsPropertyDefinition.PROPERTY_DESCRIPTION, null, "Admin_/folder1/article_test.html_/sites/default/folder1/article_test.html"); 569 property2 = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_DESCRIPTION, false); 570 assertTrue(property1.isIdentical(property2)); 571 572 cms.unlockProject(cms.getRequestContext().currentProject().getId()); 574 cms.publishProject(); 575 576 assertState(cms, resourcename, CmsResource.STATE_UNCHANGED); 577 } 578 579 589 public void testSharedPropertyIssue1() throws Throwable { 590 591 CmsObject cms = getCmsObject(); 592 echo("Testing issue with shared properties after deletion of the original sibling"); 593 594 CmsProject offline = cms.readProject("Offline"); 596 cms.getRequestContext().setCurrentProject(offline); 597 598 String source = "/folder1/testprop.txt"; 600 String dest = "/folder1/testprop2.txt"; 601 602 cms.createResource(source, CmsResourceTypePlain.getStaticTypeId()); 603 cms.unlockProject(offline.getId()); 604 cms.publishResource(source); 605 606 cms.lockResource(source); 608 CmsProperty descProperty = new CmsProperty(CmsPropertyDefinition.PROPERTY_DESCRIPTION, null, "A shared value"); 609 cms.writePropertyObject(source, descProperty); 610 cms.unlockProject(offline.getId()); 611 cms.publishResource(source); 612 613 cms.lockResource(source); 615 cms.moveResource(source, dest); 616 cms.unlockProject(offline.getId()); 617 cms.publishResource(dest, true, new CmsShellReport(Locale.ENGLISH)); 618 619 CmsProperty resultProperty = cms.readPropertyObject(dest, CmsPropertyDefinition.PROPERTY_DESCRIPTION, false); 620 assertEquals("A shared value", descProperty.getResourceValue()); 621 assertTrue( 622 "Property '" + CmsPropertyDefinition.PROPERTY_DESCRIPTION + "' must be identical", 623 descProperty.isIdentical(resultProperty)); 624 } 625 626 631 public void testNullProperty() throws Exception { 632 633 CmsProperty nullProperty = CmsProperty.getNullProperty(); 635 CmsProperty p = new CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, "Some title", null); 637 assertFalse("Created property must not be equal to NULL_PROPERTY", p.equals(nullProperty)); 639 assertFalse("NULL_PROPERTY must not be equal to created Property", nullProperty.equals(p)); 640 assertTrue("NULL_PROPERTY must be equal to itself", nullProperty.equals(nullProperty)); 641 assertTrue("NULL_PROPERTY must be identical to itself", nullProperty == CmsProperty.getNullProperty()); 642 } 643 644 649 public void testFrozenProperty() throws Throwable { 650 651 CmsProperty property = CmsProperty.getNullProperty(); 652 if (! property.isFrozen()) { 653 fail("NULL_PROPERTY is not frozen!"); 654 } 655 boolean gotException = false; 656 try { 657 property.setAutoCreatePropertyDefinition(true); 658 } catch (CmsRuntimeException e) { 659 assertEquals(org.opencms.file.Messages.ERR_PROPERTY_FROZEN_1, e.getMessageContainer().getKey()); 660 gotException = true; 661 } 662 assertTrue("Operation did not throw the required Exception", gotException); 663 gotException = false; 664 try { 665 property.setFrozen(false); 666 } catch (CmsRuntimeException e) { 667 assertEquals(org.opencms.file.Messages.ERR_PROPERTY_FROZEN_1, e.getMessageContainer().getKey()); 668 gotException = true; 669 } 670 assertTrue("Operation did not throw the required Exception", gotException); 671 gotException = false; 672 try { 673 property.setName("SomeString"); 674 } catch (CmsRuntimeException e) { 675 assertEquals(org.opencms.file.Messages.ERR_PROPERTY_FROZEN_1, e.getMessageContainer().getKey()); 676 gotException = true; 677 } 678 assertTrue("Operation did not throw the required Exception", gotException); 679 gotException = false; 680 try { 681 property.setValue("SomeString", CmsProperty.TYPE_INDIVIDUAL); 682 } catch (CmsRuntimeException e) { 683 assertEquals(org.opencms.file.Messages.ERR_PROPERTY_FROZEN_1, e.getMessageContainer().getKey()); 684 gotException = true; 685 } 686 assertTrue("Operation did not throw the required Exception", gotException); 687 gotException = false; 688 try { 689 property.setResourceValue("SomeString"); 690 } catch (CmsRuntimeException e) { 691 assertEquals(org.opencms.file.Messages.ERR_PROPERTY_FROZEN_1, e.getMessageContainer().getKey()); 692 gotException = true; 693 } 694 assertTrue("Operation did not throw the required Exception", gotException); 695 gotException = false; 696 try { 697 property.setStructureValue("SomeString"); 698 } catch (CmsRuntimeException e) { 699 assertEquals(org.opencms.file.Messages.ERR_PROPERTY_FROZEN_1, e.getMessageContainer().getKey()); 700 gotException = true; 701 } 702 assertTrue("Operation did not throw the required Exception", gotException); 703 gotException = false; 704 try { 705 property.setResourceValueList(Collections.singletonList("SomeString")); 706 } catch (CmsRuntimeException e) { 707 assertEquals(org.opencms.file.Messages.ERR_PROPERTY_FROZEN_1, e.getMessageContainer().getKey()); 708 gotException = true; 709 } 710 assertTrue("Operation did not throw the required Exception", gotException); 711 gotException = false; 712 try { 713 property.setStructureValueList(Collections.singletonList("SomeString")); 714 } catch (CmsRuntimeException e) { 715 assertEquals(org.opencms.file.Messages.ERR_PROPERTY_FROZEN_1, e.getMessageContainer().getKey()); 716 gotException = true; 717 } 718 assertTrue("Operation did not throw the required Exception", gotException); 719 } 720 } | Popular Tags |