KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/xml/content/TestCmsXmlContentDefinition.java,v $
3  * Date : $Date: 2005/09/09 11:06:11 $
4  * Version: $Revision: 1.11 $
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.CmsXmlException;
39 import org.opencms.xml.types.CmsXmlDateTimeValue;
40 import org.opencms.xml.types.CmsXmlStringValue;
41
42 import java.io.StringWriter JavaDoc;
43 import java.util.Locale JavaDoc;
44
45 import junit.framework.TestCase;
46
47 import org.dom4j.Document;
48 import org.dom4j.io.OutputFormat;
49 import org.dom4j.io.XMLWriter;
50
51 /**
52  * Tests for generating an XML content definition.<p>
53  *
54  * @author Alexander Kandzior
55  * @version $Revision: 1.11 $
56  */

57 public class TestCmsXmlContentDefinition extends TestCase {
58
59     /**
60      * Default JUnit constructor.<p>
61      *
62      * @param arg0 JUnit parameters
63      */

64     public TestCmsXmlContentDefinition(String JavaDoc arg0) {
65         super(arg0);
66     }
67     
68     /**
69      * Basic test for xml content definitions.<p>
70      *
71      * @throws Exception in case an error occured
72      */

73     public void testCmsXmlContentDefiniton() throws Exception JavaDoc {
74         CmsXmlContentDefinition cd1 = new CmsXmlContentDefinition("Article", null);
75         
76         cd1.addType(new CmsXmlStringValue("Author", "1", "1"));
77         cd1.addType(new CmsXmlStringValue("Teaser", "0", "1"));
78         cd1.addType(new CmsXmlStringValue("Toaster", "1", String.valueOf(Integer.MAX_VALUE)));
79         cd1.addType(new CmsXmlStringValue("Rollercoaster", "3", "10"));
80         
81         Document schema;
82         StringWriter JavaDoc out;
83         
84         out = new StringWriter JavaDoc();
85         schema = cd1.getSchema();
86         
87         XMLWriter writer;
88         
89         // output the schema XML
90
writer = new XMLWriter(out, OutputFormat.createPrettyPrint());
91         writer.write(schema);
92         writer.flush();
93             
94         System.out.println(out.toString());
95         
96         CmsXmlContentDefinition cd2 = CmsXmlContentDefinition.unmarshal(out.toString().getBytes(), null, null);
97         
98         out = new StringWriter JavaDoc();
99         schema = cd2.getSchema();
100         
101         // output the schema XML
102
writer = new XMLWriter(out, OutputFormat.createPrettyPrint());
103         writer.write(schema);
104         writer.flush();
105         
106         System.out.println(out.toString());
107         
108         assertEquals(cd1, cd2);
109         
110         cd1.addType(new CmsXmlStringValue("AddedLater", "1", "1"));
111         assertFalse(cd1.equals(cd2));
112     }
113     
114     /**
115      * Tests creation of an XML content from a XML content definition.<p>
116      *
117      * @throws Exception if something goes wrong
118      */

119     public void testCreateXmlContent() throws Exception JavaDoc {
120         
121         String JavaDoc schemaUri = "http://www.opencms.org/test.xsd";
122         CmsXmlContentDefinition cd1 = new CmsXmlContentDefinition("Article", schemaUri);
123         
124         cd1.addType(new CmsXmlStringValue("Author", "1", "1"));
125         cd1.addType(new CmsXmlStringValue("Teaser", "1", "1"));
126         cd1.addType(new CmsXmlStringValue("Text", "1", "1"));
127         cd1.addType(new CmsXmlDateTimeValue("Date", "1", "1"));
128         cd1.addType(new CmsXmlStringValue("Option", "0", "1"));
129         
130         CmsXmlEntityResolver.cacheSystemId(schemaUri, cd1.getSchema().asXML().getBytes(CmsEncoder.ENCODING_UTF_8));
131         CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);
132         
133         Locale JavaDoc locale = Locale.ENGLISH;
134         
135         CmsXmlContent content = CmsXmlContentFactory.createDocument(null, locale, CmsEncoder.ENCODING_UTF_8, cd1);
136         content.validateXmlStructure(resolver);
137
138         // change cd to break validation
139
cd1.addType(new CmsXmlStringValue("Kaputt", "1", "1"));
140         CmsXmlEntityResolver.cacheSystemId(schemaUri, cd1.getSchema().asXML().getBytes(CmsEncoder.ENCODING_UTF_8));
141
142         try {
143             content.validateXmlStructure(resolver);
144             fail("Validation wrongly works with modified cd");
145         } catch (CmsXmlException e) {
146             // this is expected, so no error
147
}
148         
149         String JavaDoc schemaUri2 = "http://www.opencms.org/test2.xsd";
150         CmsXmlContentDefinition cd2 = new CmsXmlContentDefinition("ArticleList", "Article", schemaUri2);
151        
152         cd2.addType(new CmsXmlStringValue("Author", "1", "1"));
153         cd2.addType(new CmsXmlStringValue("Teaser", "1", "1"));
154         cd2.addType(new CmsXmlStringValue("Text", "1", "1"));
155         cd2.addType(new CmsXmlDateTimeValue("Date", "1", "1"));
156         cd2.addType(new CmsXmlStringValue("Option", "0", "1"));
157         
158         CmsXmlEntityResolver.cacheSystemId(schemaUri2, cd2.getSchema().asXML().getBytes(CmsEncoder.ENCODING_UTF_8));
159         CmsXmlEntityResolver resolver2 = new CmsXmlEntityResolver(null);
160         CmsXmlContent content2 = CmsXmlContentFactory.createDocument(null, Locale.ENGLISH, CmsEncoder.ENCODING_UTF_8, cd2);
161         content2.validateXmlStructure(resolver2);
162         
163         // output the schema XML
164
System.out.println(content.toString());
165     }
166     
167     private static final String JavaDoc SCHEMA_SYSTEM_ID_1B = "http://www.opencms.org/test1b.xsd";
168     
169     /**
170      * Tests XML content definition with a different inner / outer sequence name.<p>
171      *
172      * @throws Exception if something goes wrong
173      */

174     public void testDifferentInnerOuterName() throws Exception JavaDoc {
175         
176         CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);
177         String JavaDoc content;
178         
179         // unmarshal content definition
180
content = CmsFileUtil.readFile("org/opencms/xml/content/xmlcontent-definition-1b.xsd", CmsEncoder.ENCODING_UTF_8);
181         CmsXmlContentDefinition cd1 = CmsXmlContentDefinition.unmarshal(content, SCHEMA_SYSTEM_ID_1B, resolver);
182         
183         Document schema;
184         StringWriter JavaDoc out;
185         
186         out = new StringWriter JavaDoc();
187         schema = cd1.getSchema();
188         
189         XMLWriter writer;
190         
191         // output the schema XML
192
writer = new XMLWriter(out, OutputFormat.createPrettyPrint());
193         writer.write(schema);
194         writer.flush();
195             
196         System.out.println(out.toString());
197         
198         CmsXmlContentDefinition cd2 = new CmsXmlContentDefinition("Outer", "Inner", SCHEMA_SYSTEM_ID_1B);
199         
200         cd2.addType(new CmsXmlStringValue("E1", "1", "1"));
201         cd2.addType(new CmsXmlStringValue("E2", "1", "1"));
202         
203         out = new StringWriter JavaDoc();
204         schema = cd2.getSchema();
205         
206         // output the schema XML
207
writer = new XMLWriter(out, OutputFormat.createPrettyPrint());
208         writer.write(schema);
209         writer.flush();
210         
211         System.out.println(out.toString());
212         
213         assertEquals(cd1, cd2);
214         
215         CmsXmlContentDefinition cd3 = CmsXmlContentDefinition.unmarshal(out.toString().getBytes(), null, null);
216         
217         out = new StringWriter JavaDoc();
218         schema = cd3.getSchema();
219         
220         // output the schema XML
221
writer = new XMLWriter(out, OutputFormat.createPrettyPrint());
222         writer.write(schema);
223         writer.flush();
224         
225         System.out.println(out.toString());
226         
227         assertEquals(cd1, cd3);
228         
229         cd2.addType(new CmsXmlStringValue("AddedLater", "1", "1"));
230         assertEquals(cd1, cd3);
231         assertFalse(cd2.equals(cd1));
232         assertFalse(cd2.equals(cd3));
233     }
234 }
235
Popular Tags