KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > xml > page > TestCmsXmlPage


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/xml/page/TestCmsXmlPage.java,v $
3  * Date : $Date: 2006/03/27 14:53:03 $
4  * Version: $Revision: 1.19 $
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.page;
33
34 import org.opencms.configuration.CmsConfigurationManager;
35 import org.opencms.i18n.CmsEncoder;
36 import org.opencms.main.CmsIllegalArgumentException;
37 import org.opencms.main.OpenCms;
38 import org.opencms.staticexport.CmsLink;
39 import org.opencms.staticexport.CmsLinkTable;
40 import org.opencms.util.CmsFileUtil;
41 import org.opencms.util.CmsStringUtil;
42 import org.opencms.xml.CmsXmlContentDefinition;
43 import org.opencms.xml.CmsXmlContentTypeManager;
44 import org.opencms.xml.CmsXmlEntityResolver;
45 import org.opencms.xml.CmsXmlUtils;
46 import org.opencms.xml.types.CmsXmlHtmlValue;
47
48 import java.util.List JavaDoc;
49 import java.util.Locale JavaDoc;
50
51 import junit.framework.TestCase;
52
53 /**
54  * Tests for the XML page that doesn't require a running OpenCms system.<p>
55  *
56  * @author Alexander Kandzior
57  *
58  * @version $Revision: 1.19 $
59  *
60  * @since 6.0.0
61  */

62 public class TestCmsXmlPage extends TestCase {
63
64     private static final String JavaDoc XMLPAGE_SCHEMA_SYSTEM_ID = CmsConfigurationManager.DEFAULT_DTD_PREFIX
65         + "xmlpage.xsd";
66
67     private static final String JavaDoc UTF8 = CmsEncoder.ENCODING_UTF_8;
68
69     /**
70      * Default JUnit constructor.<p>
71      *
72      * @param arg0 JUnit parameters
73      */

74     public TestCmsXmlPage(String JavaDoc arg0) {
75
76         super(arg0);
77     }
78
79     /**
80      * Tests reading and updating link elements from the XML page.<p>
81      *
82      * @throws Exception in case something goes wrong
83      */

84     public void testUpdateXmlPageLink() throws Exception JavaDoc {
85
86         // create a XML entity resolver
87
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);
88
89         CmsXmlPage page;
90         CmsLink link;
91         String JavaDoc content;
92
93         // validate xmlpage 4
94
content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-4.xml", UTF8);
95         page = CmsXmlPageFactory.unmarshal(content, UTF8, resolver);
96         link = page.getLinkTable("body", Locale.ENGLISH).getLink("link0");
97         // test values provided in input
98
assertEquals("/sites/default/test.html", link.getTarget());
99         assertEquals(null, link.getAnchor());
100         assertEquals(null, link.getQuery());
101
102         // validate xmlpage 3
103
content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-3.xml", UTF8);
104         page = CmsXmlPageFactory.unmarshal(content, UTF8, resolver);
105         link = page.getLinkTable("body", Locale.ENGLISH).getLink("link0");
106         // test values provided in input
107
assertEquals("/sites/default/folder1/image2.gif", link.getTarget());
108         assertEquals("test", link.getAnchor());
109         assertEquals("param=1&param2=2", link.getQuery());
110
111         // update xml page link with components
112
link.updateLink("/test/link1/changed2.gif", "foo", "a=b&c=d");
113         // page.marshal();
114
link = page.getLinkTable("body", Locale.ENGLISH).getLink("link0");
115         assertEquals("/test/link1/changed2.gif", link.getTarget());
116         assertEquals("foo", link.getAnchor());
117         assertEquals("a=b&c=d", link.getQuery());
118
119         // update xml page link with uri
120
link.updateLink("/foo/bar/link/test.jpg#bar?c=d&x=y");
121         page.marshal();
122         link = page.getLinkTable("body", Locale.ENGLISH).getLink("link0");
123         assertEquals("/foo/bar/link/test.jpg", link.getTarget());
124         assertEquals("bar", link.getAnchor());
125         assertEquals("c=d&x=y", link.getQuery());
126
127         // update xml page link with components, query null
128
link.updateLink("/test/link1/changed3.jpg", "bizz", null);
129         page.marshal();
130         link = page.getLinkTable("body", Locale.ENGLISH).getLink("link0");
131         assertEquals("/test/link1/changed3.jpg", link.getTarget());
132         assertEquals("bizz", link.getAnchor());
133         assertEquals(null, link.getQuery());
134
135         // update xml page link with components, anchor null
136
link.updateLink("/test/link1/changed4.jpg", null, "c=d&x=y");
137         page.marshal();
138         link = page.getLinkTable("body", Locale.ENGLISH).getLink("link0");
139         assertEquals("/test/link1/changed4.jpg", link.getTarget());
140         assertEquals(null, link.getAnchor());
141         assertEquals("c=d&x=y", link.getQuery());
142
143         // update xml page link with uri without components
144
link.updateLink("/foo/bar/baz/test.png");
145         page.marshal();
146         link = page.getLinkTable("body", Locale.ENGLISH).getLink("link0");
147         assertEquals("/foo/bar/baz/test.png", link.getTarget());
148         assertEquals(null, link.getAnchor());
149         assertEquals(null, link.getQuery());
150     }
151
152     /**
153      * Test validating a XML page with the XML page schema.<p>
154      *
155      * @throws Exception in case something goes wrong
156      */

157     public void testValidateXmlPageWithSchema() throws Exception JavaDoc {
158
159         CmsXmlContentTypeManager typeManager = OpenCms.getXmlContentTypeManager();
160         typeManager.addContentType(CmsXmlHtmlValue.class);
161
162         // create a XML entity resolver
163
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);
164         String JavaDoc content;
165         content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage.xsd", UTF8);
166
167         // store schema in entitiy resolver
168
CmsXmlEntityResolver.cacheSystemId(XMLPAGE_SCHEMA_SYSTEM_ID, content.getBytes(UTF8));
169
170         // validate the minimal xmlpage
171
content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-minimal.xml", UTF8);
172         CmsXmlUtils.validateXmlStructure(content.getBytes(UTF8), resolver);
173
174         // validate the xmlpage 2
175
content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-1.xml", UTF8);
176         CmsXmlUtils.validateXmlStructure(content.getBytes(UTF8), resolver);
177
178         // validate the xmlpage 3
179
content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-2.xml", UTF8);
180         CmsXmlUtils.validateXmlStructure(content.getBytes(UTF8), resolver);
181     }
182
183     /**
184      * Tests using a XML page with a XML content definition.<p>
185      *
186      * @throws Exception in case something goes wrong
187      */

188     public void testXmlPageAsXmlContentDefinition() throws Exception JavaDoc {
189
190         CmsXmlContentTypeManager typeManager = OpenCms.getXmlContentTypeManager();
191         typeManager.addContentType(CmsXmlHtmlValue.class);
192
193         CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);
194
195         String JavaDoc content;
196
197         // unmarshal content definition
198
content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage.xsd", UTF8);
199         CmsXmlContentDefinition cd1 = CmsXmlContentDefinition.unmarshal(content, XMLPAGE_SCHEMA_SYSTEM_ID, resolver);
200
201         // create new content definition form objects
202
CmsXmlContentDefinition cd2 = new CmsXmlContentDefinition("page", XMLPAGE_SCHEMA_SYSTEM_ID);
203         cd2.addType(new CmsXmlHtmlValue("element", "0", String.valueOf(Integer.MAX_VALUE)));
204
205         // ensure content definitions are equal
206
assertEquals(cd1, cd2);
207
208         // obtain content definition from a XML page
209
String JavaDoc pageStr = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-minimal.xml", UTF8);
210         CmsXmlPage page = CmsXmlPageFactory.unmarshal(pageStr, UTF8, resolver);
211         CmsXmlContentDefinition cd3 = page.getContentDefinition();
212
213         // ensure content definitions are equal
214
assertEquals(cd1, cd3);
215     }
216
217     /**
218      * Tests creating a XMl page (final version) with the API.<p>
219      *
220      * @throws Exception in case something goes wrong
221      */

222     public void testXmlPageCreateMinimal() throws Exception JavaDoc {
223
224         // create a XML entity resolver
225
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);
226
227         String JavaDoc pageStr = CmsXmlPageFactory.createDocument(Locale.ENGLISH, UTF8);
228         System.out.println("Testing creation of a minimal valid XML page:\n");
229         System.out.println(pageStr);
230
231         // now compare against stored version of minimal XML page
232
String JavaDoc minimalPageStr = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-minimal.xml", UTF8);
233         // remove windows-style linebreaks
234
minimalPageStr = CmsStringUtil.substitute(minimalPageStr, "\r\n", "\n");
235         assertEquals(pageStr, minimalPageStr);
236
237         // create a new XML page with this content, marshal it and compare
238
CmsXmlPage page = CmsXmlPageFactory.unmarshal(pageStr, UTF8, resolver);
239         byte[] bytes = page.marshal();
240         String JavaDoc newPageStr = new String JavaDoc(bytes, UTF8);
241         assertEquals(pageStr, newPageStr);
242     }
243
244     /**
245      * Tests accessing element names in the XML page.<p>
246      *
247      * @throws Exception in case something goes wrong
248      */

249     public void testXmlPageElementNames() throws Exception JavaDoc {
250
251         // create a XML entity resolver
252
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);
253
254         System.out.println("Testing element name access in the XML page\n");
255
256         // load stored XML page
257
String JavaDoc pageStr = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-2.xml", UTF8);
258
259         // create a new XML page with this content
260
CmsXmlPage page = CmsXmlPageFactory.unmarshal(pageStr, UTF8, resolver);
261
262         assertTrue(page.hasValue("body", Locale.ENGLISH));
263         assertTrue(page.hasValue("body2", Locale.ENGLISH));
264         assertTrue(page.hasValue("body", Locale.GERMAN));
265
266         List JavaDoc names;
267
268         names = page.getNames(Locale.ENGLISH);
269         assertEquals(2, names.size());
270         assertTrue(names.contains("body"));
271         assertTrue(names.contains("body2"));
272
273         names = page.getNames(Locale.GERMAN);
274         assertEquals(1, names.size());
275         assertTrue(names.contains("body"));
276
277         page.addLocale(null, Locale.FRENCH);
278         page.addValue("newbody", Locale.FRENCH);
279         page.addValue("newbody2", Locale.FRENCH);
280         page.addValue("anotherbody", Locale.FRENCH);
281
282         names = page.getNames(Locale.FRENCH);
283         assertEquals(3, names.size());
284         assertTrue(names.contains("newbody"));
285         assertTrue(names.contains("newbody2"));
286         assertTrue(names.contains("anotherbody"));
287
288         page.removeValue("body2", Locale.ENGLISH);
289         names = page.getNames(Locale.ENGLISH);
290         assertEquals(1, names.size());
291         assertTrue(names.contains("body"));
292
293         page.removeLocale(Locale.GERMAN);
294         names = page.getNames(Locale.GERMAN);
295         assertEquals(0, names.size());
296
297         boolean success = false;
298         try {
299             page.addValue("body[0]", Locale.ENGLISH);
300         } catch (CmsIllegalArgumentException e) {
301             success = true;
302         }
303         if (!success) {
304             throw new Exception JavaDoc("Multiple element name creation possible");
305         }
306
307         success = false;
308         try {
309             page.addValue("body[1]", Locale.ENGLISH);
310         } catch (CmsIllegalArgumentException e) {
311             success = true;
312         }
313         if (!success) {
314             throw new Exception JavaDoc("Page element name creation with index [1] possible");
315         }
316     }
317
318     /**
319      * Tests acessing XML page values via locales.<p>
320      *
321      * @throws Exception in case something goes wrong
322      */

323     public void testXmlPageLocaleAccess() throws Exception JavaDoc {
324
325         // create a XML entity resolver
326
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);
327
328         CmsXmlPage page;
329         String JavaDoc content;
330
331         content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-2.xml", UTF8);
332         page = CmsXmlPageFactory.unmarshal(content, UTF8, resolver);
333
334         List JavaDoc locales;
335
336         locales = page.getLocales("body");
337         assertEquals(2, locales.size());
338         assertTrue(locales.contains(Locale.ENGLISH));
339         assertTrue(locales.contains(Locale.GERMAN));
340
341         locales = page.getLocales("body2");
342         assertEquals(1, locales.size());
343         assertTrue(locales.contains(Locale.ENGLISH));
344     }
345
346     
347     /**
348      * Tests copying, moving and removing locales from a XML page.<p>
349      *
350      * @throws Exception in case something goes wrong
351      */

352     public void testXmlPageLocaleCopyMoveRemove() throws Exception JavaDoc {
353
354         // create a XML entity resolver
355
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);
356
357         CmsXmlPage page;
358         String JavaDoc content;
359
360         content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-2.xml", UTF8);
361         page = CmsXmlPageFactory.unmarshal(content, UTF8, resolver);
362
363         
364         assertEquals(2, page.getLocales().size());
365         assertTrue(page.hasLocale(Locale.ENGLISH));
366         assertTrue(page.hasLocale(Locale.GERMAN));
367
368         page.copyLocale(Locale.GERMAN, Locale.FRENCH);
369         assertEquals(3, page.getLocales().size());
370         assertTrue(page.hasLocale(Locale.ENGLISH));
371         assertTrue(page.hasLocale(Locale.GERMAN));
372         assertTrue(page.hasLocale(Locale.FRENCH));
373
374         page.moveLocale(Locale.FRENCH, Locale.ITALIAN);
375         assertEquals(3, page.getLocales().size());
376         assertTrue(page.hasLocale(Locale.ENGLISH));
377         assertTrue(page.hasLocale(Locale.GERMAN));
378         assertTrue(page.hasLocale(Locale.ITALIAN));
379         
380         page.removeLocale(Locale.ITALIAN);
381         assertEquals(2, page.getLocales().size());
382         assertTrue(page.hasLocale(Locale.ENGLISH));
383         assertTrue(page.hasLocale(Locale.GERMAN));
384         
385         page.removeLocale(Locale.ENGLISH);
386         assertEquals(1, page.getLocales().size());
387         assertTrue(page.hasLocale(Locale.GERMAN));
388     }
389     
390     /**
391      * Tests reading elements from the updated, final version of the XML page.<p>
392      *
393      * @throws Exception in case something goes wrong
394      */

395     public void testXmlPageReadFinalVersion() throws Exception JavaDoc {
396
397         // create a XML entity resolver
398
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);
399
400         CmsXmlPage page;
401         String JavaDoc content;
402
403         // validate "final" xmlpage 1
404
content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-1.xml", UTF8);
405         page = CmsXmlPageFactory.unmarshal(content, UTF8, resolver);
406         assertTrue(page.hasValue("body", Locale.ENGLISH));
407         CmsLinkTable table = page.getLinkTable("body", Locale.ENGLISH);
408         assertTrue(table.getLink("link0").isInternal());
409         assertEquals("English! Image <img SRC=\"/sites/default/folder1/image2.gif\" />", page.getStringValue(
410             null,
411             "body",
412             Locale.ENGLISH));
413
414         // validate "final" xmlpage 2
415
content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-2.xml", UTF8);
416         page = CmsXmlPageFactory.unmarshal(content, UTF8, resolver);
417         assertTrue(page.hasValue("body", Locale.ENGLISH));
418         assertTrue(page.hasValue("body", Locale.GERMAN));
419         assertTrue(page.hasValue("body2", Locale.ENGLISH));
420         assertEquals("English! Image <img SRC=\"/sites/default/folder1/image2.gif\" />", page.getStringValue(
421             null,
422             "body",
423             Locale.ENGLISH));
424         assertEquals("English 2!", page.getStringValue(null, "body2", Locale.ENGLISH));
425         assertEquals("Deutsch! Image <img SRC=\"/sites/default/folder1/image2.gif\" />", page.getStringValue(
426             null,
427             "body",
428             Locale.GERMAN));
429     }
430
431     /**
432      * Tests reading elements from the "old", pre 5.5.0 version of the XML page.<p>
433      *
434      * @throws Exception in case something goes wrong
435      */

436     public void testXmlPageReadOldVersion() throws Exception JavaDoc {
437
438         // create a XML entity resolver
439
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);
440
441         CmsXmlPage page;
442         String JavaDoc content;
443
444         // validate "old" xmlpage 1
445
content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-old-1.xml", UTF8);
446         page = CmsXmlPageFactory.unmarshal(content, UTF8, resolver);
447         assertTrue(page.hasValue("body", Locale.ENGLISH));
448         CmsLinkTable table = page.getLinkTable("body", Locale.ENGLISH);
449         assertTrue(table.getLink("link0").isInternal());
450         assertEquals("English! Image <img SRC=\"/sites/default/folder1/image2.gif\" />", page.getStringValue(
451             null,
452             "body",
453             Locale.ENGLISH));
454
455         // validate "old" xmlpage 2
456
content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-old-2.xml", UTF8);
457         page = CmsXmlPageFactory.unmarshal(content, UTF8, resolver);
458         assertTrue(page.hasValue("body", Locale.ENGLISH));
459         assertTrue(page.hasValue("body", Locale.GERMAN));
460         assertTrue(page.hasValue("body2", Locale.ENGLISH));
461         assertEquals("English! Image <img SRC=\"/sites/default/folder1/image2.gif\" />", page.getStringValue(
462             null,
463             "body",
464             Locale.ENGLISH));
465         assertEquals("English 2!", page.getStringValue(null, "body2", Locale.ENGLISH));
466         assertEquals("Deutsch! Image <img SRC=\"/sites/default/folder1/image2.gif\" />", page.getStringValue(
467             null,
468             "body",
469             Locale.GERMAN));
470     }
471
472     /**
473      * Tests accessing element names in the XML page.<p>
474      *
475      * @throws Exception in case something goes wrong
476      */

477     public void testXmlPageRenameElement() throws Exception JavaDoc {
478
479         // create a XML entity resolver for test case
480
CmsXmlContentTypeManager.createTypeManagerForTestCases();
481         CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);
482
483         System.out.println("Testing renaming element in the XML page\n");
484
485         // load stored XML page
486
String JavaDoc pageStr = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-2.xml", UTF8);
487
488         // create a new XML page with this content
489
CmsXmlPage page = CmsXmlPageFactory.unmarshal(pageStr, UTF8, resolver);
490
491         page.renameValue("body2", "bodyNEW", Locale.ENGLISH);
492         page.validateXmlStructure(resolver);
493         page.marshal();
494         // check if page has the element 'body2NEW'
495
assertTrue(page.hasValue("bodyNEW", Locale.ENGLISH));
496         // check if page has the element 'body2'
497
assertFalse(page.hasValue("body2", Locale.ENGLISH));
498         System.out.println(page.toString());
499     }
500
501     /**
502      * Tests writing elements to the updated, final version of the XML page.<p>
503      *
504      * @throws Exception in case something goes wrong
505      */

506     public void testXmlPageWriteFinalVersion() throws Exception JavaDoc {
507
508         // create a XML entity resolver
509
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);
510
511         CmsXmlPage page;
512         String JavaDoc content;
513
514         // validate "final" xmlpage 1
515
content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-1.xml", UTF8);
516         page = CmsXmlPageFactory.unmarshal(content, UTF8, resolver);
517         page.addValue("body3", Locale.ENGLISH);
518         page.setStringValue(null, "body3", Locale.ENGLISH, "English WRITTEN! Image <img SRC=\"/test/image.gif\" />");
519         assertTrue(page.hasValue("body3", Locale.ENGLISH));
520         CmsLinkTable table = page.getLinkTable("body3", Locale.ENGLISH);
521         assertTrue(table.getLink("link0").isInternal());
522         assertEquals("English WRITTEN! Image <img alt=\"\" SRC=\"/test/image.gif\" />", page.getStringValue(
523             null,
524             "body3",
525             Locale.ENGLISH));
526     }
527
528     /**
529      * Tests writing elements to the "old", pre 5.5.0 version of the XML page.<p>
530      *
531      * @throws Exception in case something goes wrong
532      */

533     public void testXmlPageWriteOldVersion() throws Exception JavaDoc {
534
535         // create a XML entity resolver
536
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);
537
538         CmsXmlPage page;
539         String JavaDoc content;
540
541         // validate "old" xmlpage 1
542
content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-old-1.xml", UTF8);
543         page = CmsXmlPageFactory.unmarshal(content, UTF8, resolver);
544         page.addValue("body3", Locale.ENGLISH);
545         page.setStringValue(null, "body3", Locale.ENGLISH, "English WRITTEN! Image <img SRC=\"/test/image.gif\" />");
546         assertTrue(page.hasValue("body3", Locale.ENGLISH));
547         CmsLinkTable table = page.getLinkTable("body3", Locale.ENGLISH);
548         assertTrue(table.getLink("link0").isInternal());
549         assertEquals("English WRITTEN! Image <img alt=\"\" SRC=\"/test/image.gif\" />", page.getStringValue(
550             null,
551             "body3",
552             Locale.ENGLISH));
553     }
554 }
555
Popular Tags