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.CmsXmlException; 39 import org.opencms.xml.types.CmsXmlDateTimeValue; 40 import org.opencms.xml.types.CmsXmlStringValue; 41 42 import java.io.StringWriter ; 43 import java.util.Locale ; 44 45 import junit.framework.TestCase; 46 47 import org.dom4j.Document; 48 import org.dom4j.io.OutputFormat; 49 import org.dom4j.io.XMLWriter; 50 51 57 public class TestCmsXmlContentDefinition extends TestCase { 58 59 64 public TestCmsXmlContentDefinition(String arg0) { 65 super(arg0); 66 } 67 68 73 public void testCmsXmlContentDefiniton() throws Exception { 74 CmsXmlContentDefinition cd1 = new CmsXmlContentDefinition("Article", null); 75 76 cd1.addType(new CmsXmlStringValue("Author", "1", "1")); 77 cd1.addType(new CmsXmlStringValue("Teaser", "0", "1")); 78 cd1.addType(new CmsXmlStringValue("Toaster", "1", String.valueOf(Integer.MAX_VALUE))); 79 cd1.addType(new CmsXmlStringValue("Rollercoaster", "3", "10")); 80 81 Document schema; 82 StringWriter out; 83 84 out = new StringWriter (); 85 schema = cd1.getSchema(); 86 87 XMLWriter writer; 88 89 writer = new XMLWriter(out, OutputFormat.createPrettyPrint()); 91 writer.write(schema); 92 writer.flush(); 93 94 System.out.println(out.toString()); 95 96 CmsXmlContentDefinition cd2 = CmsXmlContentDefinition.unmarshal(out.toString().getBytes(), null, null); 97 98 out = new StringWriter (); 99 schema = cd2.getSchema(); 100 101 writer = new XMLWriter(out, OutputFormat.createPrettyPrint()); 103 writer.write(schema); 104 writer.flush(); 105 106 System.out.println(out.toString()); 107 108 assertEquals(cd1, cd2); 109 110 cd1.addType(new CmsXmlStringValue("AddedLater", "1", "1")); 111 assertFalse(cd1.equals(cd2)); 112 } 113 114 119 public void testCreateXmlContent() throws Exception { 120 121 String schemaUri = "http://www.opencms.org/test.xsd"; 122 CmsXmlContentDefinition cd1 = new CmsXmlContentDefinition("Article", schemaUri); 123 124 cd1.addType(new CmsXmlStringValue("Author", "1", "1")); 125 cd1.addType(new CmsXmlStringValue("Teaser", "1", "1")); 126 cd1.addType(new CmsXmlStringValue("Text", "1", "1")); 127 cd1.addType(new CmsXmlDateTimeValue("Date", "1", "1")); 128 cd1.addType(new CmsXmlStringValue("Option", "0", "1")); 129 130 CmsXmlEntityResolver.cacheSystemId(schemaUri, cd1.getSchema().asXML().getBytes(CmsEncoder.ENCODING_UTF_8)); 131 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null); 132 133 Locale locale = Locale.ENGLISH; 134 135 CmsXmlContent content = CmsXmlContentFactory.createDocument(null, locale, CmsEncoder.ENCODING_UTF_8, cd1); 136 content.validateXmlStructure(resolver); 137 138 cd1.addType(new CmsXmlStringValue("Kaputt", "1", "1")); 140 CmsXmlEntityResolver.cacheSystemId(schemaUri, cd1.getSchema().asXML().getBytes(CmsEncoder.ENCODING_UTF_8)); 141 142 try { 143 content.validateXmlStructure(resolver); 144 fail("Validation wrongly works with modified cd"); 145 } catch (CmsXmlException e) { 146 } 148 149 String schemaUri2 = "http://www.opencms.org/test2.xsd"; 150 CmsXmlContentDefinition cd2 = new CmsXmlContentDefinition("ArticleList", "Article", schemaUri2); 151 152 cd2.addType(new CmsXmlStringValue("Author", "1", "1")); 153 cd2.addType(new CmsXmlStringValue("Teaser", "1", "1")); 154 cd2.addType(new CmsXmlStringValue("Text", "1", "1")); 155 cd2.addType(new CmsXmlDateTimeValue("Date", "1", "1")); 156 cd2.addType(new CmsXmlStringValue("Option", "0", "1")); 157 158 CmsXmlEntityResolver.cacheSystemId(schemaUri2, cd2.getSchema().asXML().getBytes(CmsEncoder.ENCODING_UTF_8)); 159 CmsXmlEntityResolver resolver2 = new CmsXmlEntityResolver(null); 160 CmsXmlContent content2 = CmsXmlContentFactory.createDocument(null, Locale.ENGLISH, CmsEncoder.ENCODING_UTF_8, cd2); 161 content2.validateXmlStructure(resolver2); 162 163 System.out.println(content.toString()); 165 } 166 167 private static final String SCHEMA_SYSTEM_ID_1B = "http://www.opencms.org/test1b.xsd"; 168 169 174 public void testDifferentInnerOuterName() throws Exception { 175 176 CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null); 177 String content; 178 179 content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-definition-1b.xsd", CmsEncoder.ENCODING_UTF_8); 181 CmsXmlContentDefinition cd1 = CmsXmlContentDefinition.unmarshal(content, SCHEMA_SYSTEM_ID_1B, resolver); 182 183 Document schema; 184 StringWriter out; 185 186 out = new StringWriter (); 187 schema = cd1.getSchema(); 188 189 XMLWriter writer; 190 191 writer = new XMLWriter(out, OutputFormat.createPrettyPrint()); 193 writer.write(schema); 194 writer.flush(); 195 196 System.out.println(out.toString()); 197 198 CmsXmlContentDefinition cd2 = new CmsXmlContentDefinition("Outer", "Inner", SCHEMA_SYSTEM_ID_1B); 199 200 cd2.addType(new CmsXmlStringValue("E1", "1", "1")); 201 cd2.addType(new CmsXmlStringValue("E2", "1", "1")); 202 203 out = new StringWriter (); 204 schema = cd2.getSchema(); 205 206 writer = new XMLWriter(out, OutputFormat.createPrettyPrint()); 208 writer.write(schema); 209 writer.flush(); 210 211 System.out.println(out.toString()); 212 213 assertEquals(cd1, cd2); 214 215 CmsXmlContentDefinition cd3 = CmsXmlContentDefinition.unmarshal(out.toString().getBytes(), null, null); 216 217 out = new StringWriter (); 218 schema = cd3.getSchema(); 219 220 writer = new XMLWriter(out, OutputFormat.createPrettyPrint()); 222 writer.write(schema); 223 writer.flush(); 224 225 System.out.println(out.toString()); 226 227 assertEquals(cd1, cd3); 228 229 cd2.addType(new CmsXmlStringValue("AddedLater", "1", "1")); 230 assertEquals(cd1, cd3); 231 assertFalse(cd2.equals(cd1)); 232 assertFalse(cd2.equals(cd3)); 233 } 234 } 235 | Popular Tags |