KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > xml > content > TestCmsXmlContentWithVfs


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/xml/content/TestCmsXmlContentWithVfs.java,v $
3  * Date : $Date: 2006/03/27 14:52:27 $
4  * Version: $Revision: 1.43 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

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 JavaDoc;
60 import java.util.Collections JavaDoc;
61 import java.util.Iterator JavaDoc;
62 import java.util.List JavaDoc;
63 import java.util.Locale JavaDoc;
64
65 import junit.extensions.TestSetup;
66 import junit.framework.Test;
67 import junit.framework.TestSuite;
68
69 /**
70  * Tests the link resolver for XML contents.<p>
71  *
72  * @author Alexander Kandzior
73  * @version $Revision: 1.43 $
74  */

75 public class TestCmsXmlContentWithVfs extends OpenCmsTestCase {
76
77     private static final String JavaDoc SCHEMA_SYSTEM_ID_2 = "http://www.opencms.org/test2.xsd";
78     private static final String JavaDoc SCHEMA_SYSTEM_ID_3 = "http://www.opencms.org/test3.xsd";
79     private static final String JavaDoc SCHEMA_SYSTEM_ID_3B = "http://www.opencms.org/test3b.xsd";
80     private static final String JavaDoc SCHEMA_SYSTEM_ID_4 = "http://www.opencms.org/test4.xsd";
81     private static final String JavaDoc SCHEMA_SYSTEM_ID_4B = "http://www.opencms.org/test4b.xsd";
82     private static final String JavaDoc SCHEMA_SYSTEM_ID_5 = "http://www.opencms.org/test5.xsd";
83     private static final String JavaDoc SCHEMA_SYSTEM_ID_6 = "http://www.opencms.org/test6.xsd";
84     private static final String JavaDoc SCHEMA_SYSTEM_ID_7 = "http://www.opencms.org/test7.xsd";
85     private static final String JavaDoc SCHEMA_SYSTEM_ID_8 = "http://www.opencms.org/test8.xsd";
86     private static final String JavaDoc SCHEMA_SYSTEM_ID_9 = "http://www.opencms.org/test9.xsd";
87
88     /**
89      * Default JUnit constructor.<p>
90      *
91      * @param arg0 JUnit parameters
92      */

93     public TestCmsXmlContentWithVfs(String JavaDoc arg0) {
94
95         super(arg0);
96     }
97
98     /**
99      * Test suite for this test class.<p>
100      *
101      * @return the test suite
102      */

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     /**
150      * Test accessing elements in nested schemas.<p>
151      *
152      * @throws Exception in case something goes wrong
153      */

154     public void testAccessNestedElements() throws Exception JavaDoc {
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 JavaDoc content;
162         CmsXmlContent xmlcontent;
163
164         // unmarshal content definition
165
content = CmsFileUtil.readFile(
166             "org/opencms/xml/content/xmlcontent-definition-7.xsd",
167             CmsEncoder.ENCODING_UTF_8);
168         // store content definition in entitiy resolver
169
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_7, content.getBytes(CmsEncoder.ENCODING_UTF_8));
170
171         // now read the XML content
172
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         // validate the XML structure
176
xmlcontent.validateXmlStructure(resolver);
177
178         // add a deep cascaded node
179
xmlcontent.addValue(cms, "DeepCascade", Locale.ENGLISH, 0);
180         CmsXmlContentValueSequence level0Sequence = xmlcontent.getValueSequence("DeepCascade[1]", Locale.ENGLISH);
181         assertEquals(1, level0Sequence.getElementCount());
182
183         // validate the XML structure
184
xmlcontent.validateXmlStructure(resolver);
185
186         // check nested cascade level 1
187
CmsXmlContentValueSequence level1Sequence = xmlcontent.getValueSequence(
188             "DeepCascade[1]/Cascade",
189             Locale.ENGLISH);
190         assertEquals(1, level1Sequence.getElementCount());
191
192         // check nested cascade level 2
193
CmsXmlContentValueSequence level2Sequence = xmlcontent.getValueSequence(
194             "DeepCascade[1]/Cascade[1]/VfsLink",
195             Locale.ENGLISH);
196         assertEquals(1, level2Sequence.getElementCount());
197
198         // now append an element to the nested element
199
level1Sequence.addValue(cms, 1);
200         assertEquals(2, level1Sequence.getElementCount());
201
202         // validate the XML structure
203
xmlcontent.validateXmlStructure(resolver);
204
205         // output the current document
206
System.out.println(xmlcontent.toString());
207         // re-create the document
208
xmlcontent = CmsXmlContentFactory.unmarshal(xmlcontent.toString(), CmsEncoder.ENCODING_UTF_8, resolver);
209         // validate the XML structure
210
xmlcontent.validateXmlStructure(resolver);
211
212         level0Sequence = xmlcontent.getValueSequence("DeepCascade[1]", Locale.ENGLISH);
213         assertEquals(1, level0Sequence.getElementCount());
214
215         // check nested cascade level 1
216
level1Sequence = xmlcontent.getValueSequence("DeepCascade[1]/Cascade", Locale.ENGLISH);
217         assertEquals(2, level1Sequence.getElementCount());
218
219         // check nested cascade level 2 (for the NEW element)
220
level2Sequence = xmlcontent.getValueSequence("DeepCascade[1]/Cascade[2]/VfsLink", Locale.ENGLISH);
221         assertEquals(1, level2Sequence.getElementCount());
222
223         // add some values to the level 2 sequence
224
level2Sequence.addValue(cms, 0);
225         level2Sequence.addValue(cms, 2);
226         level2Sequence.addValue(cms, 1);
227         assertEquals(4, level2Sequence.getElementCount());
228
229         // output the current document
230
System.out.println(xmlcontent.toString());
231         // re-create the document
232
xmlcontent = CmsXmlContentFactory.unmarshal(xmlcontent.toString(), CmsEncoder.ENCODING_UTF_8, resolver);
233
234         // validate the XML structure
235
xmlcontent.validateXmlStructure(resolver);
236
237         // check nested cascade level 2 (for the NEW element)
238
level2Sequence = xmlcontent.getValueSequence("DeepCascade[1]/Cascade[2]/VfsLink", Locale.ENGLISH);
239         assertEquals(4, level2Sequence.getElementCount());
240
241         // now add an optional, deep nested node that has no current value
242
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         // output the current document
249
System.out.println(xmlcontent.toString());
250         // re-create the document
251
xmlcontent = CmsXmlContentFactory.unmarshal(xmlcontent.toString(), CmsEncoder.ENCODING_UTF_8, resolver);
252
253         // validate the XML structure
254
xmlcontent.validateXmlStructure(resolver);
255
256         level2Sequence = xmlcontent.getValueSequence("DeepCascade[1]/Cascade[2]/Option", Locale.ENGLISH);
257         assertEquals(2, level2Sequence.getElementCount());
258
259         // now remove the deep cascaded sequence and create a new one
260
level0Sequence = xmlcontent.getValueSequence("DeepCascade", Locale.ENGLISH);
261         assertEquals(1, level0Sequence.getElementCount());
262         level0Sequence.removeValue(0);
263         assertEquals(0, level0Sequence.getElementCount());
264
265         // output the current document
266
System.out.println(xmlcontent.toString());
267         // re-create the document
268
xmlcontent = CmsXmlContentFactory.unmarshal(xmlcontent.toString(), CmsEncoder.ENCODING_UTF_8, resolver);
269
270         // validate the XML structure
271
xmlcontent.validateXmlStructure(resolver);
272
273         level0Sequence = xmlcontent.getValueSequence("DeepCascade", Locale.ENGLISH);
274         assertEquals(0, level0Sequence.getElementCount());
275
276         // add a new value for the deep cascade
277
level0Sequence.addValue(cms, 0);
278         assertEquals(1, level0Sequence.getElementCount());
279
280         // output the current document
281
System.out.println(xmlcontent.toString());
282         // re-create the document
283
xmlcontent = CmsXmlContentFactory.unmarshal(xmlcontent.toString(), CmsEncoder.ENCODING_UTF_8, resolver);
284
285         // validate the XML structure
286
xmlcontent.validateXmlStructure(resolver);
287
288         level0Sequence = xmlcontent.getValueSequence("DeepCascade", Locale.ENGLISH);
289         assertEquals(1, level0Sequence.getElementCount());
290     }
291
292     /**
293      * Test adding and removing elements from an XML content.<p>
294      *
295      * @throws Exception in case something goes wrong
296      */

297     public void testAddRemoveElements() throws Exception JavaDoc {
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 JavaDoc content;
305         CmsXmlContent xmlcontent;
306
307         // unmarshal content definition
308
content = CmsFileUtil.readFile(
309             "org/opencms/xml/content/xmlcontent-definition-6.xsd",
310             CmsEncoder.ENCODING_UTF_8);
311         // store content definition in entitiy resolver
312
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_6, content.getBytes(CmsEncoder.ENCODING_UTF_8));
313
314         // now read the XML content
315
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         // now re-create the XML content from the XML document
335
content = xmlcontent.toString();
336         System.out.println(content);
337         xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
338
339         // ensure the document structure is as expected
340
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         // add an element at the last position
349
newValue = (CmsXmlStringValue)titleSequence.addValue(cms, 2);
350         newValue.setStringValue(cms, "This is the last value.");
351         assertEquals(newValue, titleSequence.getValue(2));
352         // add another element at the 2nd position
353
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         // now re-create the XML content from the XML document
359
content = xmlcontent.toString();
360         System.out.println(content);
361         xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
362
363         // ensure the document structure is as expected
364
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         // now the optional element
375
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         // add an element for the optional element
384
newValue = (CmsXmlStringValue)optionSequence.addValue(cms, 0);
385         newValue.setStringValue(cms, "Optional value 1");
386         assertEquals(newValue, optionSequence.getValue(0));
387         // add another element
388
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         // now re-create the XML content from the XML document
394
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         // now re-create the XML content from the XML document
415
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         // now re-create the XML content from the XML document
429
content = xmlcontent.toString();
430         System.out.println(content);
431     }
432
433     /**
434      * Test adding and removing elements from an XML content, including nested elements.<p>
435      *
436      * @throws Exception in case something goes wrong
437      */

438     public void testAddRemoveNestedElements() throws Exception JavaDoc {
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 JavaDoc content;
446         CmsXmlContent xmlcontent;
447
448         // unmarshal content definition
449
content = CmsFileUtil.readFile(
450             "org/opencms/xml/content/xmlcontent-definition-7.xsd",
451             CmsEncoder.ENCODING_UTF_8);
452         // store content definition in entitiy resolver
453
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_7, content.getBytes(CmsEncoder.ENCODING_UTF_8));
454
455         // now read the XML content
456
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         // re-create the XML content
470
content = xmlcontent.toString();
471         System.out.println(content);
472         xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
473         // validate the XML structure
474
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         // re-create the XML content
489
content = xmlcontent.toString();
490         System.out.println(content);
491         xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
492         // validate the XML structure
493
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         // re-create the XML content
505
content = xmlcontent.toString();
506         System.out.println(content);
507         xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
508         // validate the XML structure
509
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     /**
519      * Test using a different XML content handler then the default handler.<p>
520      *
521      * @throws Exception in case something goes wrong
522      */

523     public void testContentHandler() throws Exception JavaDoc {
524
525         CmsObject cms = getCmsObject();
526         echo("Testing individual content handler for XML content");
527
528         CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms);
529
530         String JavaDoc content;
531
532         // unmarshal content definition
533
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         // store content definition in entitiy resolver
538
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_3, content.getBytes(CmsEncoder.ENCODING_UTF_8));
539
540         // now create the XML content
541
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     /**
550      * Test default values in the appinfo node using a nested XML content schema.<p>
551      *
552      * @throws Exception in case something goes wrong
553      */

554     public void testDefaultNested() throws Exception JavaDoc {
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 JavaDoc content;
562
563         // unmarshal content definition
564
content = CmsFileUtil.readFile(
565             "org/opencms/xml/content/xmlcontent-definition-4.xsd",
566             CmsEncoder.ENCODING_UTF_8);
567         // store content definition in entitiy resolver
568
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_4, content.getBytes(CmsEncoder.ENCODING_UTF_8));
569
570         // now create the XML content
571
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         // validate the XML structure
576
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         // check exact default mappings for nested content
587
value1 = xmlcontent.addValue(cms, "Cascade[1]/Option", Locale.ENGLISH, 1);
588         assertEquals("Default value from outer content definition", value1.getStringValue(cms));
589         
590         // check generic default mappings for nested content
591
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     /**
600      * Test default values after a new XML content has been created.<p>
601      *
602      * @throws Exception in case the test fails
603      */

604     public void testDefaultOnCreation() throws Exception JavaDoc {
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         // create a new xml content article
612
String JavaDoc xmlContentFile = "/xmlcontent/article_0005.html";
613         cms.createResource(xmlContentFile, 12);
614         
615         CmsFile file = cms.readFile(xmlContentFile);
616         String JavaDoc content = new String JavaDoc(file.getContents(), CmsEncoder.ENCODING_UTF_8);
617         CmsXmlContent xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
618         
619         String JavaDoc 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     /**
627      * Test default values in the appinfo node using a nested XML content schema when creating a new content.<p>
628      *
629      * The nested content definition must be non-optional, and must have non-optional element.<p>
630      *
631      * @throws Exception in case something goes wrong
632      */

633     public void testDefaultOnCreationWithNested() throws Exception JavaDoc {
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 JavaDoc content;
641
642         // unmarshal content definitions
643
content = CmsFileUtil.readFile(
644             "org/opencms/xml/content/xmlcontent-definition-3b.xsd",
645             CmsEncoder.ENCODING_UTF_8);
646         // store content definition in entitiy resolver
647
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         // store content definition in entitiy resolver
652
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_4B, content.getBytes(CmsEncoder.ENCODING_UTF_8));
653         
654         // create the content definition
655
CmsXmlContentDefinition cd = CmsXmlContentDefinition.unmarshal(content, SCHEMA_SYSTEM_ID_4B, resolver);
656         
657         CmsXmlContent xmlcontent = CmsXmlContentFactory.createDocument(cms, Locale.ENGLISH, content, cd);
658         
659         String JavaDoc 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     /**
673      * Tests the Locale settings of XMLContents with only optional elements and no element present.<p>
674      *
675      * @throws Exception if the test fails
676      */

677     public void testEmptyLocale() throws Exception JavaDoc {
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 JavaDoc iso = "ISO-8859-1";
685
686         String JavaDoc content;
687         CmsXmlContent xmlcontent;
688
689         // unmarshal content definition
690
content = CmsFileUtil.readFile(
691             "org/opencms/xml/content/xmlcontent-definition-8.xsd",
692             CmsEncoder.ENCODING_UTF_8);
693         // store content definition in entitiy resolver
694
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_8, content.getBytes(iso));
695         CmsXmlContentDefinition cd1 = CmsXmlContentDefinition.unmarshal(content, SCHEMA_SYSTEM_ID_8, resolver);
696
697         // read an existing (empty) XML content with just one locale node
698
content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-8.xml", iso);
699         xmlcontent = CmsXmlContentFactory.unmarshal(content, iso, resolver);
700         // validate the XML structure
701
xmlcontent.validateXmlStructure(resolver);
702         List JavaDoc locales = xmlcontent.getLocales();
703         assertEquals(1, locales.size());
704         assertEquals(Locale.ENGLISH, locales.get(0));
705
706         // create a fresh XML content based on the schema and try again
707
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     /**
716      * Tests locale copy, move and remove operation on an XML content.<p>
717      *
718      * @throws Exception if the test fails
719      */

720     public void testCopyMoveRemoveLocale() throws Exception JavaDoc {
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 JavaDoc iso = "ISO-8859-1";
728
729         String JavaDoc content;
730         CmsXmlContent xmlcontent;
731
732         // unmarshal content definition
733
content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-definition-8.xsd", CmsEncoder.ENCODING_UTF_8);
734         // store content definition in entitiy resolver
735
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_8, content.getBytes(iso));
736
737         // read an existing (empty) XML content with just one locale node
738
content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-8.xml", iso);
739         xmlcontent = CmsXmlContentFactory.unmarshal(content, iso, resolver);
740         // validate the XML structure
741
xmlcontent.validateXmlStructure(resolver);
742         List JavaDoc 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         // validate the XML structure
752
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         // validate the XML structure
761
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         // validate the XML structure
771
xmlcontent.validateXmlStructure(resolver);
772     }
773
774     /**
775      * Test using the GUI widget mapping appinfo nodes.<p>
776      *
777      * @throws Exception in case something goes wrong
778      */

779     public void testLayoutWidgetMapping() throws Exception JavaDoc {
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 JavaDoc content;
787
788         // unmarshal content definition
789
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         // store content definition in entitiy resolver
794
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_5, content.getBytes(CmsEncoder.ENCODING_UTF_8));
795
796         // now read the XML content
797
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         // validate the XML structure
801
xmlcontent.validateXmlStructure(resolver);
802
803         I_CmsWidget widget;
804         I_CmsXmlContentHandler handler = definition.getContentHandler();
805
806         // make sure the selected widgets are of the configured "non-standard" type
807
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         // make sure the alias name works
813
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         // make sure the custom class name works
819
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         // custom widget configuration has extended the handler String
824
assertEquals("Configuration for Toast[some addition here]", widget.getConfiguration());
825     }
826
827     /**
828      * Test resolving the links from an XML content.<p>
829      *
830      * @throws Exception in case something goes wrong
831      */

832     public void testLinkResolver() throws Exception JavaDoc {
833
834         CmsObject cms = getCmsObject();
835         echo("Testing link resolver for XML content");
836
837         CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms);
838
839         String JavaDoc content;
840
841         // unmarshal content definition
842
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         // store content definition in entitiy resolver
847
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_2, content.getBytes(CmsEncoder.ENCODING_UTF_8));
848
849         // now create the XML content
850
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         // must set the value again to ensure link table is properly initialized
861
htmlValue.setStringValue(cms, htmlValue.getStringValue(cms));
862         vfsValue.setStringValue(cms, vfsValue.getStringValue(cms));
863
864         Iterator JavaDoc i;
865         CmsLinkTable table;
866
867         String JavaDoc 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&amp;c=d#anchor\">Index page</a>\n"
871             + "<a HREF=\"/data/opencms/folder1/index.html?a2=b2&amp;c2=d2\">Index page with unescaped ampersand</a>",
872         // note that the & in the links appear correctly escaped here
873
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             // iterate all links and check if the required values are found
883
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") // at this point the & in the link should be unescaped
890
&& 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") // at this point the & in the link should be unescaped
895
&& 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     /**
911      * Tests the macros in messages and default values.<p>
912      *
913      * @throws Exception in case something goes wrong
914      */

915     public void testMacros() throws Exception JavaDoc {
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 JavaDoc content;
930         CmsXmlContent xmlcontent;
931
932         // unmarshal content definition
933
content = CmsFileUtil.readFile(
934             "org/opencms/xml/content/xmlcontent-definition-9.xsd",
935             CmsEncoder.ENCODING_UTF_8);
936         // store content definition in entitiy resolver
937
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_9, content.getBytes(CmsEncoder.ENCODING_UTF_8));
938
939         // now read the XML content
940
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         // output the current document
982
System.out.println(xmlcontent.toString());
983         // re-create the document
984
xmlcontent = CmsXmlContentFactory.unmarshal(xmlcontent.toString(), CmsEncoder.ENCODING_UTF_8, resolver);
985         // validate the XML structure
986
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     /**
996      * Tests the element mappings from the appinfo node.<p>
997      *
998      * @throws Exception in case something goes wrong
999      */

1000    public void testMappings() throws Exception JavaDoc {
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 JavaDoc content;
1008        CmsXmlContent xmlcontent;
1009
1010        // unmarshal content definition
1011
content = CmsFileUtil.readFile(
1012            "org/opencms/xml/content/xmlcontent-definition-8.xsd",
1013            CmsEncoder.ENCODING_UTF_8);
1014        // store content definition in entitiy resolver
1015
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_8, content.getBytes(CmsEncoder.ENCODING_ISO_8859_1));
1016
1017        // now read the XML content
1018
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        // validate the XML structure
1021
xmlcontent.validateXmlStructure(resolver);
1022
1023        String JavaDoc resourcename = "/mappingtext.html";
1024        // create a file in the VFS with this content (required for mappings to work)
1025
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 JavaDoc titleStr = "This must be the Title";
1047        String JavaDoc navImageStr = "This is the String with xpath String[2]";
1048        String JavaDoc 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        // set values for Title[2] and Title[3]
1055
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 JavaDoc 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    /**
1091     * Tests element mappings fom XML content to a property list.<p>
1092     *
1093     * @throws Exception if the test fails
1094     */

1095    public void testMappingsAsList() throws Exception JavaDoc {
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 JavaDoc content;
1103        CmsXmlContent xmlcontent;
1104
1105        // please note: XML schema 8 already in the cache from previous tests
1106

1107        // now read the XML content
1108
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        // validate the XML structure
1111
xmlcontent.validateXmlStructure(resolver);
1112
1113        String JavaDoc resourcename = "/mappinglist.html";
1114        // create a file in the VFS with this content (required for mappings to work)
1115
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 JavaDoc res1 = "/index.html";
1133        String JavaDoc res2 = "/xmlcontent/";
1134        String JavaDoc res3 = "/xmlcontent/article_0001.html";
1135        String JavaDoc res4 = "/folder1/index.html";
1136        
1137        String JavaDoc sr = cms.getRequestContext().getSiteRoot();
1138        String JavaDoc 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        // validate the XML structure
1151
xmlcontent.validateXmlStructure(resolver);
1152        
1153        file.setContents(xmlcontent.toString().getBytes(CmsEncoder.ENCODING_ISO_8859_1));
1154        cms.writeFile(file);
1155
1156        // check for written property values as list
1157
prop = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_DESCRIPTION, false);
1158        List JavaDoc 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        // validate the XML structure
1188
xmlcontent.validateXmlStructure(resolver);
1189        
1190        file.setContents(xmlcontent.toString().getBytes(CmsEncoder.ENCODING_ISO_8859_1));
1191        cms.writeFile(file);
1192
1193        // check for written property values as list
1194
prop2 = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_KEYWORDS, false);
1195        List JavaDoc 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        // validate the XML structure
1225
xmlcontent.validateXmlStructure(resolver);
1226        
1227        file.setContents(xmlcontent.toString().getBytes(CmsEncoder.ENCODING_ISO_8859_1));
1228        cms.writeFile(file);
1229
1230        // check for written property values as list
1231
prop3 = cms.readPropertyObject(resourcename, CmsPropertyDefinition.PROPERTY_NAVTEXT, false);
1232        List JavaDoc 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    /**
1245     * Tests the element mappings from the appinfo node for nested XML content.<p>
1246     *
1247     * @throws Exception in case something goes wrong
1248     */

1249    public void testMappingsOfNestedContent() throws Exception JavaDoc {
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 JavaDoc content;
1257        CmsXmlContent xmlcontent;
1258
1259        // unmarshal content definition
1260
content = CmsFileUtil.readFile(
1261            "org/opencms/xml/content/xmlcontent-definition-7.xsd",
1262            CmsEncoder.ENCODING_UTF_8);
1263        // store content definition in entitiy resolver
1264
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_7, content.getBytes(CmsEncoder.ENCODING_UTF_8));
1265
1266        // now read the XML content
1267
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 JavaDoc resourcename = "/mappingtest_nested.html";
1271        // create a file in the VFS with this content (required for mappings to work)
1272
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 JavaDoc 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 JavaDoc 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    /**
1306     * Tests the element mappings from the appinfo node if there is more then one locale.<p>
1307     *
1308     * @throws Exception in case something goes wrong
1309     */

1310    public void testMappingsWithManyLocales() throws Exception JavaDoc {
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 JavaDoc content;
1318        CmsXmlContent xmlcontent;
1319
1320        // unmarshal content definition
1321
content = CmsFileUtil.readFile(
1322            "org/opencms/xml/content/xmlcontent-definition-8.xsd",
1323            CmsEncoder.ENCODING_UTF_8);
1324        // store content definition in entitiy resolver
1325
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_8, content.getBytes(CmsEncoder.ENCODING_ISO_8859_1));
1326
1327        // now read the XML content
1328
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        // validate the XML structure
1331
xmlcontent.validateXmlStructure(resolver);
1332
1333        // create "en" property
1334
List JavaDoc properties = new ArrayList JavaDoc();
1335        properties.add(new CmsProperty(CmsPropertyDefinition.PROPERTY_LOCALE, Locale.ENGLISH.toString(), null));
1336
1337        String JavaDoc resourcenameEn = "/mappingtext_en.html";
1338        String JavaDoc resourcenameDe = "/mappingtext_de.html";
1339        // create a file in the VFS with this content (required for mappings to work)
1340
cms.createResource(
1341            resourcenameEn,
1342            OpenCms.getResourceManager().getResourceType("xmlcontent").getTypeId(),
1343            content.getBytes(CmsEncoder.ENCODING_ISO_8859_1),
1344            properties);
1345
1346        // copy the resource as a sibling to "de"
1347
cms.copyResource(resourcenameEn, resourcenameDe, org.opencms.file.CmsResource.COPY_AS_SIBLING);
1348        // now lock the "DE" sibling
1349
cms.changeLock(resourcenameDe);
1350        // add the "DE" locale property to the german version
1351
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 JavaDoc 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 JavaDoc 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        // finally unlock the resource
1376
cms.unlockResource(resourcenameDe);
1377
1378        // now check if the properties have been assigned as required to the locales
1379
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    /**
1393     * Test using a nested XML content schema.<p>
1394     *
1395     * @throws Exception in case something goes wrong
1396     */

1397    public void testNestedSchema() throws Exception JavaDoc {
1398
1399        CmsObject cms = getCmsObject();
1400        echo("Testing for nested XML content schemas");
1401
1402        CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms);
1403
1404        String JavaDoc content;
1405
1406        // unmarshal content definition
1407
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        // store content definition in entitiy resolver
1412
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_4, content.getBytes(CmsEncoder.ENCODING_UTF_8));
1413
1414        // now create the XML content
1415
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        // validate the XML structure
1420
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        // ensure Xpath index is based on 1, not 0
1443
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 JavaDoc 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    /**
1476     * Test if the resource bundle in the schema definition is properly initialized.<p>
1477     *
1478     * @throws Exception in case something goes wrong
1479     */

1480    public void testResourceBundle() throws Exception JavaDoc {
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 JavaDoc content;
1488        CmsXmlContentDefinition definition;
1489
1490        // unmarshal content definition
1491
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        // unmarshal content definition
1503
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    /**
1519     * Test the validation of the value elements.<p>
1520     *
1521     * @throws Exception in case something goes wrong
1522     */

1523    public void testValidation() throws Exception JavaDoc {
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 JavaDoc content;
1531        CmsXmlContent xmlcontent;
1532
1533        // unmarshal content definition
1534
content = CmsFileUtil.readFile(
1535            "org/opencms/xml/content/xmlcontent-definition-7.xsd",
1536            CmsEncoder.ENCODING_UTF_8);
1537        // store content definition in entitiy resolver
1538
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_7, content.getBytes(CmsEncoder.ENCODING_UTF_8));
1539
1540        // now read the XML content
1541
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        // add 2 deep cascaded nodes
1545
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        // output the current document
1550
System.out.println(xmlcontent.toString());
1551
1552        CmsXmlContentErrorHandler errorHandler;
1553
1554        // perform a first validation - the must be no errors or warnings reported
1555
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        // output the current document
1589
System.out.println(xmlcontent.toString());
1590        // re-create the document
1591
xmlcontent = CmsXmlContentFactory.unmarshal(xmlcontent.toString(), CmsEncoder.ENCODING_UTF_8, resolver);
1592        // validate the XML structure
1593
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        // output the current document
1605
System.out.println(xmlcontent.toString());
1606        // re-create the document
1607
xmlcontent = CmsXmlContentFactory.unmarshal(xmlcontent.toString(), CmsEncoder.ENCODING_UTF_8, resolver);
1608        // validate the XML structure
1609
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    /**
1619     * Extended test for the validation of the value elements.<p>
1620     *
1621     * @throws Exception in case something goes wrong
1622     */

1623    public void testValidationExtended() throws Exception JavaDoc {
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 JavaDoc content;
1631        CmsXmlContent xmlcontent;
1632
1633        // unmarshal content definition
1634
content = CmsFileUtil.readFile(
1635            "org/opencms/xml/content/xmlcontent-definition-8.xsd",
1636            CmsEncoder.ENCODING_UTF_8);
1637        // store content definition in entitiy resolver
1638
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_8, content.getBytes(CmsEncoder.ENCODING_UTF_8));
1639
1640        // now read the XML content
1641
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        // validate the XML structure
1644
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        // validate the XML structure (no error caused here)
1654
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 JavaDoc 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 JavaDoc 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        // test custom error message
1725
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 JavaDoc 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        // output the current document
1767
System.out.println(xmlcontent.toString());
1768    }
1769
1770    /**
1771     * Test for the validation with different locales.<p>
1772     *
1773     * @throws Exception in case something goes wrong
1774     */

1775    public void testValidationLocale() throws Exception JavaDoc {
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 JavaDoc content;
1783        CmsXmlContent xmlcontent;
1784
1785        // unmarshal content definition
1786
content = CmsFileUtil.readFile(
1787            "org/opencms/xml/content/xmlcontent-definition-8.xsd",
1788            CmsEncoder.ENCODING_UTF_8);
1789        // store content definition in entitiy resolver
1790
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_8, content.getBytes(CmsEncoder.ENCODING_UTF_8));
1791
1792        // now read the XML content
1793
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        // validate the XML structure
1796
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        // validate the XML structure (no error caused here)
1812
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        // output the current document
1829
System.out.println(xmlcontent.toString());
1830    }
1831
1832    /**
1833     * Test the index of the value elements.<p>
1834     *
1835     * @throws Exception in case something goes wrong
1836     */

1837    public void testValueIndex() throws Exception JavaDoc {
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 JavaDoc content;
1845        CmsXmlContent xmlcontent;
1846
1847        // unmarshal content definition
1848
content = CmsFileUtil.readFile(
1849            "org/opencms/xml/content/xmlcontent-definition-7.xsd",
1850            CmsEncoder.ENCODING_UTF_8);
1851        // store content definition in entitiy resolver
1852
CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_7, content.getBytes(CmsEncoder.ENCODING_UTF_8));
1853
1854        // now read the XML content
1855
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        // "fill up" the XML content with some values
1859
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        // output the current document
1867
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        // test min / max occurs values for value instances
1876
I_CmsXmlContentValue toastValue = toastSequence.getValue(1);
1877        assertEquals(1, toastValue.getMinOccurs());
1878        assertEquals(3, toastValue.getMaxOccurs());
1879
1880        // check content handlers for nested elements
1881
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