KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/xml/page/TestCmsXmlPageInSystem.java,v $
3  * Date : $Date: 2006/03/27 14:53:03 $
4  * Version: $Revision: 1.22 $
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.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.types.CmsResourceTypeXmlPage;
39 import org.opencms.i18n.CmsEncoder;
40 import org.opencms.report.CmsShellReport;
41 import org.opencms.test.OpenCmsTestCase;
42 import org.opencms.test.OpenCmsTestProperties;
43 import org.opencms.util.CmsFileUtil;
44 import org.opencms.util.CmsStringUtil;
45 import org.opencms.workplace.tools.content.CmsElementRename;
46 import org.opencms.xml.CmsXmlEntityResolver;
47
48 import java.util.ArrayList JavaDoc;
49 import java.util.List JavaDoc;
50 import java.util.Locale JavaDoc;
51
52 import junit.extensions.TestSetup;
53 import junit.framework.Test;
54 import junit.framework.TestSuite;
55
56 /**
57  * Tests for the XML page that require a running OpenCms system.<p>
58  *
59  * @author Alexander Kandzior
60  *
61  * @version $Revision: 1.22 $
62  *
63  * @since 6.0.0
64  */

65 public class TestCmsXmlPageInSystem extends OpenCmsTestCase {
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 TestCmsXmlPageInSystem(String JavaDoc arg0) {
75
76         super(arg0);
77     }
78
79     /**
80      * Test suite for this test class.<p>
81      *
82      * @return the test suite
83      */

84     public static Test suite() {
85
86         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
87
88         TestSuite suite = new TestSuite();
89         suite.setName(TestCmsXmlPageInSystem.class.getName());
90
91         suite.addTest(new TestCmsXmlPageInSystem("testLinkParameterIssue"));
92         suite.addTest(new TestCmsXmlPageInSystem("testSchemaCachePublishIssue"));
93         suite.addTest(new TestCmsXmlPageInSystem("testLinkReplacement"));
94         suite.addTest(new TestCmsXmlPageInSystem("testCommentInSource"));
95         suite.addTest(new TestCmsXmlPageInSystem("testXmlPageRenameElement"));
96         suite.addTest(new TestCmsXmlPageInSystem("testMalformedPage"));
97
98         TestSetup wrapper = new TestSetup(suite) {
99
100             protected void setUp() {
101
102                 setupOpenCms("simpletest", "/sites/default/");
103             }
104
105             protected void tearDown() {
106
107                 removeOpenCms();
108             }
109
110         };
111
112         return wrapper;
113     }
114
115     /**
116      * Tests comments in the page HTML source code.<p>
117      *
118      * @throws Exception if something goes wrong
119      */

120     public void testCommentInSource() throws Exception JavaDoc {
121
122         CmsObject cms = getCmsObject();
123         echo("Testing XML page comment handling");
124
125         String JavaDoc filename = "/folder1/subfolder11/test2.html";
126         List JavaDoc properties = new ArrayList JavaDoc();
127         properties.add(new CmsProperty(CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, UTF8, null));
128         properties.add(new CmsProperty(CmsPropertyDefinition.PROPERTY_LOCALE, Locale.ENGLISH.toString(), null));
129         String JavaDoc content = CmsXmlPageFactory.createDocument(Locale.ENGLISH, UTF8);
130         cms.createResource(filename, CmsResourceTypeXmlPage.getStaticTypeId(), content.getBytes(UTF8), properties);
131
132         CmsFile file = cms.readFile(filename);
133         CmsXmlPage page = CmsXmlPageFactory.unmarshal(cms, file);
134         String JavaDoc element = "test";
135         page.addValue(element, Locale.ENGLISH);
136
137         String JavaDoc result;
138
139         // first test using a simple comment
140
content = "<h1>Comment Test 1</h1>\n<!-- This is a comment -->\nSome text here...";
141         page.setStringValue(cms, element, Locale.ENGLISH, content);
142         result = page.getStringValue(cms, element, Locale.ENGLISH);
143         assertEquals(content, result);
144
145         // more complex comment test
146
content = "<!-- First comment --><h1>Comment Test 2</h1>\n<!-- This is a comment -->\nSome text here...<!-- Another comment -->";
147         page.setStringValue(cms, element, Locale.ENGLISH, content);
148         result = page.getStringValue(cms, element, Locale.ENGLISH);
149         assertEquals(content, result);
150
151         // mix of comment and links
152
content = "<!-- First comment --><img SRC=\"/data/opencms/image.gif\" alt=\"an image\" />\n<!-- This is a comment -->\n<a HREF=\"/data/opencms/index.html\">Link</a><!-- Another comment -->";
153         page.setStringValue(cms, element, Locale.ENGLISH, content);
154         result = page.getStringValue(cms, element, Locale.ENGLISH);
155         assertEquals(content, result);
156
157         // commented out html tags
158
content = "<!-- <img SRC=\"/data/opencms/image.gif\" alt=\"an image\" />\n<h1>some text</h1>--><!-- This is a comment -->\n<a HREF=\"/data/opencms/index.html\">Link</a><!-- Another comment -->";
159         page.setStringValue(cms, element, Locale.ENGLISH, content);
160         result = page.getStringValue(cms, element, Locale.ENGLISH);
161         assertEquals(content, result);
162
163         // nested comments
164
content = "<!-- Start of comment <!-- img SRC=\"/data/opencms/image.gif\" alt=\"an image\" / -->\n<h1>some text</h1><!-- This is a comment -->\n End of comment! --> <a HREF=\"/data/opencms/index.html\">Link</a><!-- Another comment -->";
165         page.setStringValue(cms, element, Locale.ENGLISH, content);
166         result = page.getStringValue(cms, element, Locale.ENGLISH);
167         assertEquals(content, result);
168     }
169
170     /**
171      * Tests link issue with certain parameters.<p>
172      *
173      * Description of the issue:
174      * links with parameters <code>&lt;a HREF="form.jsp?a=b&language=xy"&gt;</code> are replaced by
175      * <code>&lt;a HREF="form.jsp?a=b?uage=xy"&gt;</code>.<p>
176      *
177      * This issue turned out to be a bug in the HtmlParser component,
178      * updating the component from version 1.4 to version 1.5 solved the issue.<p>
179      *
180      * @throws Exception if something goes wrong
181      */

182     public void testLinkParameterIssue() throws Exception JavaDoc {
183
184         CmsObject cms = getCmsObject();
185         echo("Testing XML page link parameter issue");
186
187         String JavaDoc filename = "/folder1/subfolder11/test_param_1.html";
188         String JavaDoc content = CmsXmlPageFactory.createDocument(Locale.ENGLISH, UTF8);
189         List JavaDoc properties = new ArrayList JavaDoc();
190         properties.add(new CmsProperty(CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, UTF8, null));
191         properties.add(new CmsProperty(CmsPropertyDefinition.PROPERTY_LOCALE, Locale.ENGLISH.toString(), null));
192         properties.add(new CmsProperty(CmsXmlPage.PROPERTY_ALLOW_RELATIVE, CmsStringUtil.FALSE, null));
193         cms.createResource(filename, CmsResourceTypeXmlPage.getStaticTypeId(), content.getBytes(UTF8), properties);
194
195         CmsFile file = cms.readFile(filename);
196         CmsXmlPage page = CmsXmlPageFactory.unmarshal(cms, file);
197         String JavaDoc element = "test";
198         page.addValue(element, Locale.ENGLISH);
199         String JavaDoc text;
200
201         page.setStringValue(cms, element, Locale.ENGLISH, "<a HREF=\"index.html?a=b&someparam=de\">link</a>");
202         text = page.getStringValue(cms, element, Locale.ENGLISH);
203         assertEquals("<a HREF=\"/data/opencms/folder1/subfolder11/index.html?a=b&amp;someparam=de\">link</a>", text);
204
205         page.setStringValue(cms, element, Locale.ENGLISH, "<a HREF=\"index.html?language=de\">link</a>");
206         text = page.getStringValue(cms, element, Locale.ENGLISH);
207         assertEquals("<a HREF=\"/data/opencms/folder1/subfolder11/index.html?language=de\">link</a>", text);
208
209         page.setStringValue(cms, element, Locale.ENGLISH, "<a HREF=\"index.html?a=b&language=de\">link</a>");
210         text = page.getStringValue(cms, element, Locale.ENGLISH);
211         assertEquals("<a HREF=\"/data/opencms/folder1/subfolder11/index.html?a=b&amp;language=de\">link</a>", text);
212
213         page.setStringValue(cms, element, Locale.ENGLISH, "<a HREF=\"index_noexist.html?a=b&language=de\">link</a>");
214         text = page.getStringValue(cms, element, Locale.ENGLISH);
215         assertEquals("<a HREF=\"/data/opencms/folder1/subfolder11/index_noexist.html?a=b&amp;language=de\">link</a>", text);
216
217         page.setStringValue(
218             cms,
219             element,
220             Locale.ENGLISH,
221             "<a HREF=\"index_noexist.html?a=b&product=somthing\">link</a>");
222         text = page.getStringValue(cms, element, Locale.ENGLISH);
223         assertEquals(
224             "<a HREF=\"/data/opencms/folder1/subfolder11/index_noexist.html?a=b&amp;product=somthing\">link</a>",
225             text);
226     }
227
228     /**
229      * Tests XML link replacement.<p>
230      *
231      * @throws Exception if something goes wrong
232      */

233     public void testLinkReplacement() throws Exception JavaDoc {
234
235         CmsObject cms = getCmsObject();
236         echo("Testing XML page link replacement");
237
238         String JavaDoc filename = "/folder1/subfolder11/test1.html";
239         String JavaDoc content = CmsXmlPageFactory.createDocument(Locale.ENGLISH, UTF8);
240         List JavaDoc properties = new ArrayList JavaDoc();
241         properties.add(new CmsProperty(CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, UTF8, null));
242         properties.add(new CmsProperty(CmsPropertyDefinition.PROPERTY_LOCALE, Locale.ENGLISH.toString(), null));
243         properties.add(new CmsProperty(CmsXmlPage.PROPERTY_ALLOW_RELATIVE, CmsStringUtil.FALSE, null));
244         cms.createResource(filename, CmsResourceTypeXmlPage.getStaticTypeId(), content.getBytes(UTF8), properties);
245
246         CmsFile file = cms.readFile(filename);
247         CmsXmlPage page = CmsXmlPageFactory.unmarshal(cms, file);
248         String JavaDoc element = "test";
249         page.addValue(element, Locale.ENGLISH);
250         String JavaDoc text;
251
252         // test link replacement with existing file
253
page.setStringValue(cms, element, Locale.ENGLISH, "<a HREF=\"index.html\">link</a>");
254         text = page.getStringValue(cms, element, Locale.ENGLISH);
255         assertEquals("<a HREF=\"/data/opencms/folder1/subfolder11/index.html\">link</a>", text);
256
257         // test link replacement with non-existing file
258
page.setStringValue(cms, element, Locale.ENGLISH, "<a HREF=\"index_noexist.html\">link</a>");
259         text = page.getStringValue(cms, element, Locale.ENGLISH);
260         assertEquals("<a HREF=\"/data/opencms/folder1/subfolder11/index_noexist.html\">link</a>", text);
261     }
262
263     /**
264      * Test malformed page structures.<p>
265      *
266      * @throws Exception in case something goes wrong
267      */

268     public void testMalformedPage() throws Exception JavaDoc {
269
270         CmsObject cms = getCmsObject();
271         echo("Testing malformed page element structures");
272
273         // overwrite an existing page with a bad content
274
String JavaDoc resourcename = "/folder1/page2.html";
275         cms.lockResource(resourcename);
276
277         CmsFile file = cms.readFile(resourcename);
278
279         // read malformed XML page
280
String JavaDoc pageStr = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-5.xml", "ISO-8859-1");
281         file.setContents(pageStr.getBytes("ISO-8859-1"));
282
283         cms.writeFile(file);
284     }
285
286     /**
287      * Test the schema cache publish issue.<p>
288      *
289      * Description of the issue:
290      * After the initial publish, the XML page schema does not work anymore.<p>
291      *
292      * @throws Exception in case something goes wrong
293      */

294     public void testSchemaCachePublishIssue() throws Exception JavaDoc {
295
296         CmsObject cms = getCmsObject();
297         echo("Testing the validation for values in the XML content");
298
299         String JavaDoc resourcename = "/folder1/page1.html";
300         cms.lockResource(resourcename);
301
302         CmsFile file = cms.readFile(resourcename);
303         CmsXmlPage page = CmsXmlPageFactory.unmarshal(cms, file);
304         page.validateXmlStructure(new CmsXmlEntityResolver(cms));
305         page.setStringValue(cms, "body", Locale.ENGLISH, "This is a test");
306         assertEquals("This is a test", page.getValue("body", Locale.ENGLISH).getStringValue(cms));
307         file.setContents(page.marshal());
308         cms.writeFile(file);
309
310         cms.unlockResource(resourcename);
311         cms.publishResource(resourcename);
312
313         cms.lockResource(resourcename);
314
315         file = cms.readFile(resourcename);
316         page = CmsXmlPageFactory.unmarshal(cms, file);
317         page.validateXmlStructure(new CmsXmlEntityResolver(cms));
318         page.setStringValue(cms, "body", Locale.ENGLISH, "This is a another test");
319         assertEquals("This is a another test", page.getValue("body", Locale.ENGLISH).getStringValue(cms));
320         file.setContents(page.marshal());
321         cms.writeFile(file);
322     }
323
324     /**
325      * Tests accessing element names in the XML page.<p>
326      *
327      * @throws Exception in case something goes wrong
328      */

329     public void testXmlPageRenameElement() throws Exception JavaDoc {
330
331         String JavaDoc folder = "/folder1/";
332         String JavaDoc recursive = CmsStringUtil.TRUE;
333         String JavaDoc template = "ALL";
334         String JavaDoc locale = "ALL";
335         String JavaDoc oldElement = "body";
336         String JavaDoc newElement = "NewElement";
337         String JavaDoc removeEmptyElements = CmsStringUtil.FALSE;
338         String JavaDoc validateNewElement = CmsStringUtil.FALSE;
339
340         echo("Testing XML page rename element handling");
341         CmsElementRename wp = new CmsElementRename(
342             null,
343             getCmsObject(),
344             folder,
345             recursive,
346             template,
347             locale,
348             oldElement,
349             newElement,
350             removeEmptyElements,
351             validateNewElement);
352
353         echo("Testing initialize CmsElementRename class");
354         assertEquals(folder, wp.getParamResource());
355         assertEquals(recursive, wp.getParamRecursive());
356         assertEquals(template, wp.getParamTemplate());
357         assertEquals(locale, wp.getParamLocale());
358         assertEquals(oldElement, wp.getParamOldElement());
359         assertEquals(newElement, wp.getParamNewElement());
360         assertEquals(removeEmptyElements, wp.getParamRemoveEmptyElements());
361         assertEquals(validateNewElement, wp.getParamValidateNewElement());
362         echo("CmsElementRename class initialized successfully");
363         echo("Xml Page Element Rename Start");
364         wp.actionRename(new CmsShellReport(Locale.ENGLISH));
365         echo("Xml Page Element Rename End");
366     }
367
368 }
369
Popular Tags