KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/xml/content/TestCmsXmlContent.java,v $
3  * Date : $Date: 2006/03/27 14:52:27 $
4  * Version: $Revision: 1.10 $
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.i18n.CmsEncoder;
35 import org.opencms.util.CmsFileUtil;
36 import org.opencms.xml.CmsXmlContentDefinition;
37 import org.opencms.xml.CmsXmlEntityResolver;
38 import org.opencms.xml.types.CmsXmlDateTimeValue;
39 import org.opencms.xml.types.CmsXmlHtmlValue;
40 import org.opencms.xml.types.CmsXmlLocaleValue;
41 import org.opencms.xml.types.CmsXmlStringValue;
42 import org.opencms.xml.types.I_CmsXmlContentValue;
43
44 import java.util.Locale JavaDoc;
45
46 import junit.framework.TestCase;
47
48 /**
49  * Tests for generating an XML content.<p>
50  *
51  * @author Alexander Kandzior
52  * @version $Revision: 1.10 $
53  */

54 public class TestCmsXmlContent extends TestCase {
55
56     private static final String JavaDoc SCHEMA_SYSTEM_ID_1 = "http://www.opencms.org/test1.xsd";
57     
58     /**
59      * Default JUnit constructor.<p>
60      *
61      * @param arg0 JUnit parameters
62      */

63     public TestCmsXmlContent(String JavaDoc arg0) {
64         super(arg0);
65     }
66     
67     /**
68      * Test unmarshalling an XML content from a String.<p>
69      *
70      * @throws Exception in case something goes wrong
71      */

72     public void testUnmarshalFromString() throws Exception JavaDoc {
73         
74         CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);
75         
76         String JavaDoc content;
77         // unmarshal content definition
78
content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-definition-1.xsd", CmsEncoder.ENCODING_UTF_8);
79         CmsXmlContentDefinition definition = CmsXmlContentDefinition.unmarshal(content, SCHEMA_SYSTEM_ID_1, resolver);
80         // store content definition in entitiy resolver
81
content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-1.xml", CmsEncoder.ENCODING_UTF_8);
82         CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_1, definition.getSchema().asXML().getBytes(CmsEncoder.ENCODING_UTF_8));
83         // now create the XML content
84
CmsXmlContent xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
85                         
86         assertTrue(xmlcontent.hasValue("String", Locale.ENGLISH));
87         assertTrue(xmlcontent.hasValue("DateTime", Locale.ENGLISH));
88         assertTrue(xmlcontent.hasValue("Html", Locale.ENGLISH));
89         assertTrue(xmlcontent.hasValue("Locale", Locale.ENGLISH));
90         
91         assertSame(definition.getContentHandler().getClass().getName(), CmsDefaultXmlContentHandler.class.getName());
92         
93         CmsXmlStringValue stringValue = (CmsXmlStringValue)xmlcontent.getValue("String", Locale.ENGLISH);
94         CmsXmlDateTimeValue dateTimeValue = (CmsXmlDateTimeValue)xmlcontent.getValue("DateTime", Locale.ENGLISH);
95         CmsXmlHtmlValue htmlValue = (CmsXmlHtmlValue)xmlcontent.getValue("Html", Locale.ENGLISH);
96         CmsXmlLocaleValue localeValue = (CmsXmlLocaleValue)xmlcontent.getValue("Locale", Locale.ENGLISH);
97         
98         assertEquals("Multitest 1", stringValue.getStringValue(null));
99         assertEquals("-58254180000", dateTimeValue.getStringValue(null));
100         assertEquals("<p>This is some Html</p>", htmlValue.getStringValue(null));
101         assertEquals("en_EN", localeValue.getStringValue(null));
102     }
103     
104     /**
105      * Tests moving elements up and down in the XML content.<p>
106      *
107      * @throws Exception
108      */

109     public void testMoveUpDown() throws Exception JavaDoc {
110         
111         CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);
112         
113         String JavaDoc content;
114         // unmarshal content definition
115
content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-definition-1.xsd", CmsEncoder.ENCODING_UTF_8);
116         CmsXmlContentDefinition definition = CmsXmlContentDefinition.unmarshal(content, SCHEMA_SYSTEM_ID_1, resolver);
117         // store content definition in entitiy resolver
118
content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-1.xml", CmsEncoder.ENCODING_UTF_8);
119         CmsXmlEntityResolver.cacheSystemId(SCHEMA_SYSTEM_ID_1, definition.getSchema().asXML().getBytes(CmsEncoder.ENCODING_UTF_8));
120         // now create the XML content
121
CmsXmlContent xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
122
123         // this content comes from the file that has been read
124
String JavaDoc nn = "String";
125         CmsXmlStringValue stringValue = (CmsXmlStringValue)xmlcontent.getValue(nn, Locale.ENGLISH, 0);
126         assertEquals("Multitest 1", stringValue.getStringValue(null));
127         
128         // add some more nodes to the content
129
I_CmsXmlContentValue value = xmlcontent.addValue(null, nn, Locale.ENGLISH, 1);
130         value.setStringValue(null, "Node 2");
131         value = xmlcontent.addValue(null, nn, Locale.ENGLISH, 2);
132         value.setStringValue(null, "Node 3");
133         value = xmlcontent.addValue(null, nn, Locale.ENGLISH, 3);
134         String JavaDoc node4 = "Node 4";
135         value.setStringValue(null, node4);
136
137         // we must have 4 "String" nodes now
138
int maxIndex = xmlcontent.getValue(nn, Locale.ENGLISH).getMaxIndex();
139         assertEquals(4, maxIndex);
140         
141         // now we have 4 nodes, check the last node
142
I_CmsXmlContentValue checkValue = xmlcontent.getValue(nn, Locale.ENGLISH, maxIndex - 1);
143         assertEquals(node4, checkValue.getStringValue(null));
144         
145         // move the node down 3 times, then it must be at the first position
146
value.moveDown();
147         value.moveDown();
148         value.moveDown();
149         System.out.println(xmlcontent.toString());
150         checkValue = xmlcontent.getValue(nn, Locale.ENGLISH, 0);
151         assertEquals(node4, checkValue.getStringValue(null));
152         
153         // one more move down should have no effect
154
value.moveDown();
155         checkValue = xmlcontent.getValue(nn, Locale.ENGLISH, 0);
156         assertEquals(node4, checkValue.getStringValue(null));
157
158         // now move the node up again
159
value.moveUp();
160         value.moveUp();
161         value.moveUp();
162         System.out.println(xmlcontent.toString());
163         checkValue = xmlcontent.getValue(nn, Locale.ENGLISH, maxIndex - 1);
164         assertEquals(node4, checkValue.getStringValue(null));
165         
166         // one more move up should have no effect
167
value.moveUp();
168         checkValue = xmlcontent.getValue(nn, Locale.ENGLISH, maxIndex - 1);
169         assertEquals(node4, checkValue.getStringValue(null));
170     }
171 }
Popular Tags