bec35fa3ddc84ac282a1f42f89672fc36276ef49
[percentcodec] / percentcodec / src / test / java / de / juplo / percentcodec / PercentCodecTest.java
1 package de.juplo.percentcodec;
2
3 import de.juplo.percentcodec.PercentCodec;
4 import junit.framework.Assert;
5 import org.junit.Test;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8
9
10 public class PercentCodecTest {
11   private final static Logger log = LoggerFactory.getLogger(PercentCodecTest.class);
12
13   public final static char[] decoded = { ' ', '+', 'q', 's', '8', '0', 'x', 'ä', 'ß', 'à', '€', '¢', '@', '/', '?', '#', ';','.', '&', '%' };
14   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" };
15   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" };
16
17
18   @Test
19   public void testEncodeLatin1() throws Exception {
20     PercentCodec codec = new PercentCodec("latin1");
21
22     for (int a = 0; a < decoded.length; a++) {
23       for (int b = 0; b < decoded.length; b++) {
24         for (int c = 0; c < decoded.length; c++) {
25           /** Das Zeichen '€' existiert in Latin1 nicht! */
26           if (a == 10 || b == 10 || c == 10)
27             continue;
28           StringBuilder input = new StringBuilder();
29           input.append(decoded[a]);
30           input.append(decoded[b]);
31           input.append(decoded[c]);
32           StringBuilder expected = new StringBuilder();
33           expected.append(encoded_latin1[a]);
34           expected.append(encoded_latin1[b]);
35           expected.append(encoded_latin1[c]);
36           String output = codec.encode(input);
37           log.debug("{}\t-> {}", input, output);
38           Assert.assertEquals("\"" + input + "\" was encoded falsely", expected.toString(), output);
39         }
40       }
41     }
42   }
43
44   @Test
45   public void testDecodeLatin1() throws Exception {
46     PercentCodec codec = new PercentCodec("latin1");
47
48     for (int a = 0; a < decoded.length; a++) {
49       for (int b = 0; b < decoded.length; b++) {
50         for (int c = 0; c < decoded.length; c++) {
51           /** Das Zeichen '€' existiert in Latin1 nicht! */
52           if (a == 10 || b == 10 || c == 10)
53             continue;
54           StringBuilder input = new StringBuilder();
55           input.append(encoded_latin1[a]);
56           input.append(encoded_latin1[b]);
57           input.append(encoded_latin1[c]);
58           StringBuilder expected = new StringBuilder();
59           expected.append(decoded[a]);
60           expected.append(decoded[b]);
61           expected.append(decoded[c]);
62           String output = codec.decode(input);
63           log.debug("{}\t-> {}", input, output);
64           Assert.assertEquals("\"" + input + "\" was decoded falsely", expected.toString(), output);
65         }
66       }
67     }
68   }
69
70   @Test
71   public void testEncodeUtf8() throws Exception {
72     PercentCodec codec = new PercentCodec("UTF-8");
73
74     for (int a = 0; a < decoded.length; a++) {
75       for (int b = 0; b < decoded.length; b++) {
76         for (int c = 0; c < decoded.length; c++) {
77           StringBuilder input = new StringBuilder();
78           input.append(decoded[a]);
79           input.append(decoded[b]);
80           input.append(decoded[c]);
81           StringBuilder expected = new StringBuilder();
82           expected.append(encoded_utf8[a]);
83           expected.append(encoded_utf8[b]);
84           expected.append(encoded_utf8[c]);
85           String output = codec.encode(input);
86           log.debug("{}\t-> {}", input, output);
87           Assert.assertEquals("\"" + input + "\" was encoded falsely", expected.toString(), output);
88         }
89       }
90     }
91   }
92
93   @Test
94   public void testDecodeUtf8() throws Exception {
95     PercentCodec codec = new PercentCodec("UTF-8");
96
97     for (int a = 0; a < decoded.length; a++) {
98       for (int b = 0; b < decoded.length; b++) {
99         for (int c = 0; c < decoded.length; c++) {
100           StringBuilder input = new StringBuilder();
101           input.append(encoded_utf8[a]);
102           input.append(encoded_utf8[b]);
103           input.append(encoded_utf8[c]);
104           StringBuilder expected = new StringBuilder();
105           expected.append(decoded[a]);
106           expected.append(decoded[b]);
107           expected.append(decoded[c]);
108           String output = codec.decode(input);
109           log.debug("{}\t-> {}", input, output);
110           Assert.assertEquals("\"" + input + "\" was decoded falsely", expected.toString(), output);
111         }
112       }
113     }
114   }
115 }