KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > i18n > TestCmsEncoder


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/i18n/TestCmsEncoder.java,v $
3  * Date : $Date: 2006/03/27 14:52:51 $
4  * Version: $Revision: 1.13 $
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.i18n;
33
34 import java.io.ByteArrayOutputStream JavaDoc;
35 import java.io.OutputStreamWriter JavaDoc;
36 import java.nio.charset.Charset JavaDoc;
37
38 import junit.framework.TestCase;
39
40 /**
41  * Tests for the CmsEncoder.<p>
42  *
43  * @author Alexander Kandzior
44  *
45  * @version $Revision: 1.13 $
46  *
47  * @since 6.0.0
48  */

49 public class TestCmsEncoder extends TestCase {
50
51     private static final String JavaDoc ENC_ISO_8859_1 = "ISO-8859-1";
52     private static final String JavaDoc ENC_ISO_8859_15 = "ISO-8859-15";
53     private static final String JavaDoc ENC_US_ASCII = "US-ASCII";
54     private static final String JavaDoc ENC_UTF_8 = CmsEncoder.ENCODING_UTF_8;
55     private static final String JavaDoc ENC_WINDOWS_1252 = "Cp1252";
56
57     // working around encoding issues (e.g. of CVS) by using unicode values
58
// the values of C_STRING_1 are: ae oe ue Ae Oe Ue scharfes-s euro-symbol
59
private static final String JavaDoc STRING_1 = "Test: \u00e4\u00f6\u00fc\u00c4\u00d6\u00dc\u00df\u20ac";
60     private static final String JavaDoc STRING_2 = "Test: \u00e4\u00f6\u00fc\u00c4\u00d6\u00dc\u00df&#8364;";
61     private static final String JavaDoc STRING_3 = "Test: &#228;&#246;&#252;&#196;&#214;&#220;&#223;&#8364;";
62     private static final String JavaDoc STRING_4 = "\u00e4\u00f6\u00fc\u20ac#|#12|&#12|&#;\u00c4\u00d6\u00dctest";
63     private static final String JavaDoc STRING_5 = "&#228;&#246;&#252;&#8364;#|#12|&#12|&#;&#196;&#214;&#220;test";
64
65     private static final String JavaDoc[][] TESTS_DECODE = {
66         {STRING_3, STRING_2, ENC_ISO_8859_1},
67         {STRING_3, STRING_1, ENC_ISO_8859_15},
68         {STRING_3, STRING_1, ENC_UTF_8},
69         {STRING_3, STRING_3, ENC_US_ASCII},
70         {STRING_3, STRING_1, ENC_WINDOWS_1252},
71         {STRING_5, STRING_4, ENC_UTF_8}};
72
73     private static final String JavaDoc[][] TESTS_ENCODE = {
74         {STRING_1, STRING_2, ENC_ISO_8859_1},
75         {STRING_1, STRING_1, ENC_ISO_8859_15},
76         {STRING_1, STRING_1, ENC_UTF_8},
77         {STRING_1, STRING_3, ENC_US_ASCII},
78         {STRING_1, STRING_1, ENC_WINDOWS_1252},
79         {STRING_4, STRING_5, ENC_US_ASCII}};
80
81     /**
82      * @see CmsEncoder#decodeHtmlEntities(String, String)
83      */

84     public void testDecodeHtmlEntities() {
85
86         for (int i = 0; i < TESTS_DECODE.length; i++) {
87             String JavaDoc source = TESTS_DECODE[i][0];
88             String JavaDoc dest = TESTS_DECODE[i][1];
89             String JavaDoc encoding = TESTS_DECODE[i][2];
90
91             String JavaDoc result = CmsEncoder.decodeHtmlEntities(source, encoding);
92             assertEquals(result, dest);
93         }
94     }
95
96     /**
97      * Tests decoding german "umlaute".<p>
98      *
99      */

100     public void testDecodeUmlauts() {
101
102         Charset JavaDoc defaultCs = Charset.forName(new OutputStreamWriter JavaDoc(new ByteArrayOutputStream JavaDoc()).getEncoding());
103         System.out.println("Default Charset: " + defaultCs.name());
104         String JavaDoc param = "%C3%BC"; // utf-8 bytes for 'ü'
105
String JavaDoc decoded = CmsEncoder.decode(param, CmsEncoder.ENCODING_UTF_8);
106         String JavaDoc decoded2 = CmsEncoder.decode(param, CmsEncoder.ENCODING_ISO_8859_1);
107         assertEquals("ü", decoded);
108         assertFalse("ü".equals(decoded2));
109     }
110
111     /**
112      * Tests wether two subsequent calls to
113      * <code>{@link CmsEncoder#escapeWBlanks(String, String)}</code>
114      * lead to an expected result and ensures that the 2nd call does not
115      * do any further modifications. <p>
116      *
117      */

118     public void testDoubleEncoding() {
119
120         String JavaDoc original = "Online Project (VFS)";
121         String JavaDoc encode1 = CmsEncoder.escapeWBlanks(original, ENC_UTF_8);
122         String JavaDoc encode2 = CmsEncoder.escapeWBlanks(encode1, ENC_UTF_8);
123         assertFalse(encode1.equals(encode2));
124         assertEquals("Online%2520Project%2520%2528VFS%2529", encode2);
125     }
126
127     /**
128      * @see CmsEncoder#encodeHtmlEntities(String, String)
129      */

130     public void testEncodeForHtml() {
131
132         for (int i = 0; i < TESTS_ENCODE.length; i++) {
133             String JavaDoc source = TESTS_ENCODE[i][0];
134             String JavaDoc dest = TESTS_ENCODE[i][1];
135             String JavaDoc encoding = TESTS_ENCODE[i][2];
136
137             String JavaDoc result = CmsEncoder.encodeHtmlEntities(source, encoding);
138             assertEquals(result, dest);
139         }
140     }
141     
142     /**
143      * Tests the encoding of a single parameter.<p>
144      */

145     public void testParamEncoding() {
146         
147         String JavaDoc term = "Test äöüÄÖÜ߀ +-";
148         String JavaDoc encoded = CmsEncoder.encodeParameter(term);
149         String JavaDoc result = CmsEncoder.decodeParameter(encoded);
150         
151         System.out.print(encoded);
152         assertEquals(term, result);
153     }
154
155     /**
156      * Encodes a single '%' and ensures that it is transformed. Encodes
157      * a sequence that is already an encoded special character (e.g.: "%25")
158      * and ensures that this sequence is not encoded several times. <p>
159      *
160      */

161     public void testEncodePercent() {
162
163         String JavaDoc original = "% abc";
164         String JavaDoc encoded = CmsEncoder.encode(original);
165         assertFalse("A single '%' charater must be transformed by encoding.", original.equals(encoded));
166         original = "%25 abc";
167         encoded = CmsEncoder.encode(original);
168         assertFalse("A encoded sequence \"%25\" must be transformed by a further encoding.", original.equals(encoded));
169     }
170
171     /**
172      * @see CmsEncoder#lookupEncoding(String, String)
173      */

174     public void testLookupEncoding() {
175
176         assertEquals(CmsEncoder.lookupEncoding("UTF-8", null), CmsEncoder.ENCODING_UTF_8);
177         assertEquals(CmsEncoder.lookupEncoding("utf-8", null), CmsEncoder.ENCODING_UTF_8);
178         assertEquals(CmsEncoder.lookupEncoding("UTF8", null), CmsEncoder.ENCODING_UTF_8);
179         assertEquals(CmsEncoder.lookupEncoding("utf8", null), CmsEncoder.ENCODING_UTF_8);
180         assertEquals(CmsEncoder.lookupEncoding("ISO-8859-1", null), "ISO-8859-1");
181         assertEquals(CmsEncoder.lookupEncoding("iso-8859-1", null), "ISO-8859-1");
182         assertEquals(CmsEncoder.lookupEncoding("ISO8859-1", null), "ISO-8859-1");
183         assertEquals(CmsEncoder.lookupEncoding("iso8859-1", null), "ISO-8859-1");
184         assertEquals(CmsEncoder.lookupEncoding("ISO_8859-1", null), "ISO-8859-1");
185         assertEquals(CmsEncoder.lookupEncoding("iso_8859-1", null), "ISO-8859-1");
186         assertEquals(CmsEncoder.lookupEncoding("ISO_8859_1", null), "ISO-8859-1");
187         assertEquals(CmsEncoder.lookupEncoding("iso_8859_1", null), "ISO-8859-1");
188         assertEquals(CmsEncoder.lookupEncoding("latin1", null), "ISO-8859-1");
189     }
190
191     /**
192      * Tests encoding of parameters.<p>
193      */

194     public void testParameterEncoding() {
195
196         String JavaDoc param;
197         String JavaDoc result;
198
199         param = "+";
200
201         result = CmsEncoder.encode(param, CmsEncoder.ENCODING_UTF_8);
202         result = CmsEncoder.decode(result, CmsEncoder.ENCODING_UTF_8);
203
204         assertEquals(param, result);
205
206         param = "+Köln -Düsseldorf &value";
207
208         result = CmsEncoder.encode(param, CmsEncoder.ENCODING_UTF_8);
209         result = CmsEncoder.decode(result, CmsEncoder.ENCODING_UTF_8);
210
211         assertEquals(param, result);
212     }
213
214     /**
215      * Tests wether two subsequent calls to
216      * <code>{@link CmsEncoder#escapeWBlanks(String, String)}</code>
217      * are undone by onde decode call (the 2nd encode call must not modify anything.<p>
218      *
219      */

220     public void testRecursiveDecodingOfDoubleEncoded() {
221
222         String JavaDoc original = "Online Project (VFS)";
223         String JavaDoc encode1 = CmsEncoder.escapeWBlanks(original, ENC_UTF_8);
224         String JavaDoc encode2 = CmsEncoder.escapeWBlanks(encode1, ENC_UTF_8);
225         String JavaDoc decoded = CmsEncoder.decode(encode2, ENC_UTF_8);
226         assertEquals(encode1, decoded);
227     }
228
229 }
230
Popular Tags