KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > test > commons > EncoderTest


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.test.commons;
27
28 import junit.framework.Test;
29 import junit.framework.TestSuite;
30 import org.snipsnap.snip.SnipLink;
31 import org.snipsnap.test.snip.SnipTestSupport;
32 import org.snipsnap.util.URLEncoderDecoder;
33 import org.radeox.util.Encoder;
34
35 import java.io.UnsupportedEncodingException JavaDoc;
36
37 public class EncoderTest extends SnipTestSupport {
38   public EncoderTest(String JavaDoc name) {
39     super(name);
40   }
41
42   private final static String JavaDoc UTF8_CHARS = "\u65E5\u672C"; // ?? (nihon)
43
private final static String JavaDoc UTF8_ENCODED = "%E6%97%A5%E6%9C%AC";
44
45   String JavaDoc unencodedString = null;
46   String JavaDoc encodedString = null;
47
48   protected void setUp() throws Exception JavaDoc {
49     super.setUp();
50     System.setProperty("file.encoding", "UTF-8");
51
52     StringBuffer JavaDoc longUnencodedString = new StringBuffer JavaDoc();
53     for (int chars = 0; chars < 20; chars++) {
54       longUnencodedString.append(UTF8_CHARS);
55     }
56     unencodedString = longUnencodedString.toString();
57
58     StringBuffer JavaDoc longEncodedString = new StringBuffer JavaDoc();
59     for (int chars = 0; chars < 20; chars++) {
60       longEncodedString.append(UTF8_ENCODED);
61     }
62     encodedString = longEncodedString.toString();
63   }
64
65   public static Test suite() {
66     return new TestSuite(EncoderTest.class);
67   }
68
69   public void testUTF8Characters() {
70     assertEquals(UTF8_CHARS, unencodedString.substring(0, UTF8_CHARS.length()));
71   }
72
73   public void testUTF8Encoding() throws UnsupportedEncodingException JavaDoc {
74     assertEquals("String UTF-8 is not correctly encoded",
75                  encodedString, URLEncoderDecoder.encode(unencodedString, "UTF-8"));
76   }
77
78   public void testUTF8Decoding() throws UnsupportedEncodingException JavaDoc {
79     assertEquals("String UTF-8 is not correctly decoded",
80                  unencodedString, URLEncoderDecoder.decode(encodedString, "UTF-8"));
81   }
82
83   public void testCutLength() throws UnsupportedEncodingException JavaDoc {
84     assertEquals(unencodedString.substring(0, 7) + "...", SnipLink.cutLength(unencodedString, 10));
85   }
86
87   public void testCutLengthLink() throws UnsupportedEncodingException JavaDoc {
88     assertEquals("Cutting link text is broken",
89                  "<a HREF=\"space/" + encodedString + "\">" + unencodedString.substring(0, 22) + "...</a>",
90                  SnipLink.createLink(unencodedString, SnipLink.cutLength(unencodedString, 25)));
91   }
92
93 }
94
Popular Tags