KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > util > TestCmsHtmlConverter


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/util/TestCmsHtmlConverter.java,v $
3  * Date : $Date: 2006/03/27 14:52:42 $
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.util;
33
34 import org.opencms.i18n.CmsEncoder;
35 import org.opencms.test.OpenCmsTestCase;
36
37 import java.io.File JavaDoc;
38
39 /**
40  * @author Michael Emmerich
41  * @version $Revision: 1.10 $
42  */

43 public class TestCmsHtmlConverter extends OpenCmsTestCase {
44     
45     // some test Strings
46
private static final String JavaDoc STRING_1 = "Test: äöüÄÖÜß";
47     private static final String JavaDoc STRING_2 = "Test: äöüÄÖÜ߀";
48     private static final String JavaDoc STRING_1_UTF8_RESULT = "Test: \u00e4\u00f6\u00fc\u00c4\u00d6\u00dc\u00df";
49     private static final String JavaDoc STRING_2_UTF8_RESULT = "Test: \u00e4\u00f6\u00fc\u00c4\u00d6\u00dc\u00df\u20ac";
50   
51     private static final String JavaDoc SIMPLE_HTML = "<h1>Test</h1><div><p>This is a test<p>some content<p>last line</div><pre>Some pre<br>\r\n More pre\r\n</pre>Final line.";
52     
53     /**
54      * Default JUnit constructor.<p>
55      *
56      * @param arg0 JUnit parameters
57      */

58     public TestCmsHtmlConverter(String JavaDoc arg0) {
59         super(arg0);
60     }
61
62     /**
63      * Tests converstion of ISO-encoded entities.<p>
64      *
65      * @throws Exception in case the test fails
66      */

67     public void testISO() throws Exception JavaDoc {
68         System.out.println("Testing US-ASCII conversion");
69         CmsHtmlConverter converter = new CmsHtmlConverter(CmsEncoder.ENCODING_US_ASCII, CmsHtmlConverter.PARAM_WORD);
70         String JavaDoc convertedHtml1 = converter.convertToString(STRING_1);
71         String JavaDoc convertedHtml2 = converter.convertToString(STRING_2);
72                 
73         assertEquals(STRING_1 , convertedHtml1);
74         assertEquals(STRING_2 , convertedHtml2);
75     }
76
77     /**
78      * Tests converstion of UTF8-encoded entities.<p>
79      *
80      * @throws Exception in case the test fails
81      */

82     public void testUTF8() throws Exception JavaDoc {
83         System.out.println("Testing UTF-8 conversion");
84         CmsHtmlConverter converter = new CmsHtmlConverter(CmsEncoder.ENCODING_UTF_8, CmsHtmlConverter.PARAM_WORD);
85         String JavaDoc convertedHtml1 = converter.convertToString(STRING_1);
86         String JavaDoc convertedHtml2 = converter.convertToString(STRING_2);
87              
88         assertEquals(STRING_1_UTF8_RESULT , convertedHtml1);
89         assertEquals(STRING_2_UTF8_RESULT , convertedHtml2);
90     }
91         
92     /**
93      * Tests if simple pretty printing works.<p>
94      *
95      * @throws Exception in case the test fails
96      */

97     public void testPrettyPrint() throws Exception JavaDoc {
98         
99         System.out.println("Testing HTML pretty printing");
100         CmsHtmlConverter converter = new CmsHtmlConverter(CmsEncoder.ENCODING_ISO_8859_1, CmsHtmlConverter.PARAM_ENABLED);
101         String JavaDoc result = converter.convertToString(SIMPLE_HTML);
102         
103         System.out.println("----------------");
104         System.out.println(result);
105         System.out.println("----------------");
106     }
107     
108     /**
109      * Tests if all word tags are removed.<p>
110      *
111      * @throws Exception in case the test fails
112      */

113     public void testRemoveWordTags() throws Exception JavaDoc {
114
115         System.out.println("Testing Word conversion");
116         CmsHtmlConverter converter = new CmsHtmlConverter(CmsEncoder.ENCODING_ISO_8859_1, CmsHtmlConverter.PARAM_WORD
117             + ";"
118             + CmsHtmlConverter.PARAM_XHTML);
119
120         // read a file and convert it
121
File JavaDoc inputfile = new File JavaDoc(getTestDataPath("test2.html"));
122         byte[] htmlInput = CmsFileUtil.readFile(inputfile);
123         String JavaDoc outputContent = converter.convertToString(htmlInput);
124         System.out.println(outputContent);
125         // now check if all word specific tags are removed
126
assertContainsNot(outputContent, "<o:p>");
127         assertContainsNot(outputContent, "<o:smarttagtype");
128         assertContainsNot(outputContent, "<?xml:namespace ");
129     }
130 }
Popular Tags