1 package de.juplo.percentcodec;
3 import de.juplo.percentcodec.PercentCodec;
4 import junit.framework.Assert;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
10 public class PercentCodecTest {
11 private final static Logger log = LoggerFactory.getLogger(PercentCodecTest.class);
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" };
19 public void testEncodeLatin1() throws Exception {
20 PercentCodec codec = new PercentCodec("latin1");
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)
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);
45 public void testDecodeLatin1() throws Exception {
46 PercentCodec codec = new PercentCodec("latin1");
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)
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);
71 public void testEncodeUtf8() throws Exception {
72 PercentCodec codec = new PercentCodec("UTF-8");
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);
94 public void testDecodeUtf8() throws Exception {
95 PercentCodec codec = new PercentCodec("UTF-8");
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);