1 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 ; 36 37 public class EncoderTest extends SnipTestSupport { 38 public EncoderTest(String name) { 39 super(name); 40 } 41 42 private final static String UTF8_CHARS = "\u65E5\u672C"; private final static String UTF8_ENCODED = "%E6%97%A5%E6%9C%AC"; 44 45 String unencodedString = null; 46 String encodedString = null; 47 48 protected void setUp() throws Exception { 49 super.setUp(); 50 System.setProperty("file.encoding", "UTF-8"); 51 52 StringBuffer longUnencodedString = new StringBuffer (); 53 for (int chars = 0; chars < 20; chars++) { 54 longUnencodedString.append(UTF8_CHARS); 55 } 56 unencodedString = longUnencodedString.toString(); 57 58 StringBuffer longEncodedString = new StringBuffer (); 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 { 74 assertEquals("String UTF-8 is not correctly encoded", 75 encodedString, URLEncoderDecoder.encode(unencodedString, "UTF-8")); 76 } 77 78 public void testUTF8Decoding() throws UnsupportedEncodingException { 79 assertEquals("String UTF-8 is not correctly decoded", 80 unencodedString, URLEncoderDecoder.decode(encodedString, "UTF-8")); 81 } 82 83 public void testCutLength() throws UnsupportedEncodingException { 84 assertEquals(unencodedString.substring(0, 7) + "...", SnipLink.cutLength(unencodedString, 10)); 85 } 86 87 public void testCutLengthLink() throws UnsupportedEncodingException { 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 |