1 31 32 package org.opencms.xml.content; 33 34 import org.opencms.i18n.CmsEncoder; 35 import org.opencms.util.CmsFileUtil; 36 import org.opencms.xml.CmsXmlContentDefinition; 37 import org.opencms.xml.CmsXmlEntityResolver; 38 import org.opencms.xml.types.CmsXmlDateTimeValue; 39 import org.opencms.xml.types.CmsXmlHtmlValue; 40 import org.opencms.xml.types.CmsXmlLocaleValue; 41 import org.opencms.xml.types.CmsXmlStringValue; 42 import org.opencms.xml.types.I_CmsXmlContentValue; 43 44 import java.util.Locale ; 45 46 import junit.framework.TestCase; 47 48 54 public class TestCmsXmlContent extends TestCase { 55 56 private static final String SCHEMA_SYSTEM_ID_1 = "http://www.opencms.org/test1.xsd"; 57 58 63 public TestCmsXmlContent(String arg0) { 64 super(arg0); 65 } 66 67 72 public void testUnmarshalFromString() throws Exception { 73 74 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null); 75 76 String content; 77 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-definition-1.xsd", CmsEncoder.ENCODING_UTF_8); 79 CmsXmlContentDefinition definition = CmsXmlContentDefinition.unmarshal(content, SCHEMA_SYSTEM_ID_1, resolver); 80 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-1.xml", CmsEncoder.ENCODING_UTF_8); 82 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_1, definition.getSchema().asXML().getBytes(CmsEncoder.ENCODING_UTF_8)); 83 CmsXmlContent xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 85 86 assertTrue(xmlcontent.hasValue("String", Locale.ENGLISH)); 87 assertTrue(xmlcontent.hasValue("DateTime", Locale.ENGLISH)); 88 assertTrue(xmlcontent.hasValue("Html", Locale.ENGLISH)); 89 assertTrue(xmlcontent.hasValue("Locale", Locale.ENGLISH)); 90 91 assertSame(definition.getContentHandler().getClass().getName(), CmsDefaultXmlContentHandler.class.getName()); 92 93 CmsXmlStringValue stringValue = (CmsXmlStringValue)xmlcontent.getValue("String", Locale.ENGLISH); 94 CmsXmlDateTimeValue dateTimeValue = (CmsXmlDateTimeValue)xmlcontent.getValue("DateTime", Locale.ENGLISH); 95 CmsXmlHtmlValue htmlValue = (CmsXmlHtmlValue)xmlcontent.getValue("Html", Locale.ENGLISH); 96 CmsXmlLocaleValue localeValue = (CmsXmlLocaleValue)xmlcontent.getValue("Locale", Locale.ENGLISH); 97 98 assertEquals("Multitest 1", stringValue.getStringValue(null)); 99 assertEquals("-58254180000", dateTimeValue.getStringValue(null)); 100 assertEquals("<p>This is some Html</p>", htmlValue.getStringValue(null)); 101 assertEquals("en_EN", localeValue.getStringValue(null)); 102 } 103 104 109 public void testMoveUpDown() throws Exception { 110 111 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null); 112 113 String content; 114 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-definition-1.xsd", CmsEncoder.ENCODING_UTF_8); 116 CmsXmlContentDefinition definition = CmsXmlContentDefinition.unmarshal(content, SCHEMA_SYSTEM_ID_1, resolver); 117 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-1.xml", CmsEncoder.ENCODING_UTF_8); 119 CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_1, definition.getSchema().asXML().getBytes(CmsEncoder.ENCODING_UTF_8)); 120 CmsXmlContent xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver); 122 123 String nn = "String"; 125 CmsXmlStringValue stringValue = (CmsXmlStringValue)xmlcontent.getValue(nn, Locale.ENGLISH, 0); 126 assertEquals("Multitest 1", stringValue.getStringValue(null)); 127 128 I_CmsXmlContentValue value = xmlcontent.addValue(null, nn, Locale.ENGLISH, 1); 130 value.setStringValue(null, "Node 2"); 131 value = xmlcontent.addValue(null, nn, Locale.ENGLISH, 2); 132 value.setStringValue(null, "Node 3"); 133 value = xmlcontent.addValue(null, nn, Locale.ENGLISH, 3); 134 String node4 = "Node 4"; 135 value.setStringValue(null, node4); 136 137 int maxIndex = xmlcontent.getValue(nn, Locale.ENGLISH).getMaxIndex(); 139 assertEquals(4, maxIndex); 140 141 I_CmsXmlContentValue checkValue = xmlcontent.getValue(nn, Locale.ENGLISH, maxIndex - 1); 143 assertEquals(node4, checkValue.getStringValue(null)); 144 145 value.moveDown(); 147 value.moveDown(); 148 value.moveDown(); 149 System.out.println(xmlcontent.toString()); 150 checkValue = xmlcontent.getValue(nn, Locale.ENGLISH, 0); 151 assertEquals(node4, checkValue.getStringValue(null)); 152 153 value.moveDown(); 155 checkValue = xmlcontent.getValue(nn, Locale.ENGLISH, 0); 156 assertEquals(node4, checkValue.getStringValue(null)); 157 158 value.moveUp(); 160 value.moveUp(); 161 value.moveUp(); 162 System.out.println(xmlcontent.toString()); 163 checkValue = xmlcontent.getValue(nn, Locale.ENGLISH, maxIndex - 1); 164 assertEquals(node4, checkValue.getStringValue(null)); 165 166 value.moveUp(); 168 checkValue = xmlcontent.getValue(nn, Locale.ENGLISH, maxIndex - 1); 169 assertEquals(node4, checkValue.getStringValue(null)); 170 } 171 } | Popular Tags |