1 package de.halbekunst.juplo.utils;
3 import junit.framework.Assert;
5 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory;
9 public class PercentCodecTest {
10 private final static Logger log = LoggerFactory.getLogger(PercentCodecTest.class);
12 public final static char[] decoded = { ' ', '+', 'q', 's', '8', '0', 'x', 'ä', 'ß', 'à', '€', '¢', '@', '/', '?', '#', ';','.', '&', '%' };
13 public final static String[] encoded_latin1 = { "%20", "%2b", "q", "s", "8", "0", "x", "%e4", "%df", "%e0", "%3f", "%a2", "%40", "%2f", "%3f", "%23", "%3b",".", "%26", "%25" };
14 public final static String[] encoded_utf8 = { "%20", "%2b", "q", "s", "8", "0", "x", "%c3%a4", "%c3%9f", "%c3%a0", "%e2%82%ac", "%c2%a2", "%40", "%2f", "%3f", "%23", "%3b",".", "%26", "%25" };
18 public void testEncodeLatin1() throws Exception {
19 PercentCodec codec = new PercentCodec("latin1");
21 for (int a = 0; a < decoded.length; a++) {
22 for (int b = 0; b < decoded.length; b++) {
23 for (int c = 0; c < decoded.length; c++) {
24 /** Das Zeichen '€' existiert in Latin1 nicht! */
25 if (a == 10 || b == 10 || c == 10)
27 StringBuilder input = new StringBuilder();
28 input.append(decoded[a]);
29 input.append(decoded[b]);
30 input.append(decoded[c]);
31 StringBuilder expected = new StringBuilder();
32 expected.append(encoded_latin1[a]);
33 expected.append(encoded_latin1[b]);
34 expected.append(encoded_latin1[c]);
35 String output = codec.encode(input);
36 log.debug("{}\t-> {}", input, output);
37 Assert.assertEquals("\"" + input + "\" was encoded falsely", expected.toString(), output);
44 public void testDecodeLatin1() throws Exception {
45 PercentCodec codec = new PercentCodec("latin1");
47 for (int a = 0; a < decoded.length; a++) {
48 for (int b = 0; b < decoded.length; b++) {
49 for (int c = 0; c < decoded.length; c++) {
50 /** Das Zeichen '€' existiert in Latin1 nicht! */
51 if (a == 10 || b == 10 || c == 10)
53 StringBuilder input = new StringBuilder();
54 input.append(encoded_latin1[a]);
55 input.append(encoded_latin1[b]);
56 input.append(encoded_latin1[c]);
57 StringBuilder expected = new StringBuilder();
58 expected.append(decoded[a]);
59 expected.append(decoded[b]);
60 expected.append(decoded[c]);
61 String output = codec.decode(input);
62 log.debug("{}\t-> {}", input, output);
63 Assert.assertEquals("\"" + input + "\" was decoded falsely", expected.toString(), output);
70 public void testEncodeUtf8() throws Exception {
71 PercentCodec codec = new PercentCodec("UTF-8");
73 for (int a = 0; a < decoded.length; a++) {
74 for (int b = 0; b < decoded.length; b++) {
75 for (int c = 0; c < decoded.length; c++) {
76 StringBuilder input = new StringBuilder();
77 input.append(decoded[a]);
78 input.append(decoded[b]);
79 input.append(decoded[c]);
80 StringBuilder expected = new StringBuilder();
81 expected.append(encoded_utf8[a]);
82 expected.append(encoded_utf8[b]);
83 expected.append(encoded_utf8[c]);
84 String output = codec.encode(input);
85 log.debug("{}\t-> {}", input, output);
86 Assert.assertEquals("\"" + input + "\" was encoded falsely", expected.toString(), output);
93 public void testDecodeUtf8() throws Exception {
94 PercentCodec codec = new PercentCodec("UTF-8");
96 for (int a = 0; a < decoded.length; a++) {
97 for (int b = 0; b < decoded.length; b++) {
98 for (int c = 0; c < decoded.length; c++) {
99 StringBuilder input = new StringBuilder();
100 input.append(encoded_utf8[a]);
101 input.append(encoded_utf8[b]);
102 input.append(encoded_utf8[c]);
103 StringBuilder expected = new StringBuilder();
104 expected.append(decoded[a]);
105 expected.append(decoded[b]);
106 expected.append(decoded[c]);
107 String output = codec.decode(input);
108 log.debug("{}\t-> {}", input, output);
109 Assert.assertEquals("\"" + input + "\" was decoded falsely", expected.toString(), output);