+package de.halbekunst.juplo.utils;
+
+import junit.framework.Assert;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class PercentCodecTest {
+ private final static Logger log = LoggerFactory.getLogger(PercentCodecTest.class);
+
+ public final static char[] decoded = { ' ', '+', 'q', 's', '8', '0', 'x', 'ä', 'ß', 'à', '€', '¢', '@', '/', '?', '#', ';','.', '&', '%' };
+ 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" };
+ 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" };
+
+
+ @Test
+ public void testEncodeLatin1() throws Exception {
+ PercentCodec codec = new PercentCodec("latin1");
+
+ for (int a = 0; a < decoded.length; a++) {
+ for (int b = 0; b < decoded.length; b++) {
+ for (int c = 0; c < decoded.length; c++) {
+ /** Das Zeichen '€' existiert in Latin1 nicht! */
+ if (a == 10 || b == 10 || c == 10)
+ continue;
+ StringBuilder input = new StringBuilder();
+ input.append(decoded[a]);
+ input.append(decoded[b]);
+ input.append(decoded[c]);
+ StringBuilder expected = new StringBuilder();
+ expected.append(encoded_latin1[a]);
+ expected.append(encoded_latin1[b]);
+ expected.append(encoded_latin1[c]);
+ String output = codec.encode(input);
+ log.debug("{}\t-> {}", input, output);
+ Assert.assertEquals("\"" + input + "\" was encoded falsely", expected.toString(), output);
+ }
+ }
+ }
+ }
+
+ @Test
+ public void testDecodeLatin1() throws Exception {
+ PercentCodec codec = new PercentCodec("latin1");
+
+ for (int a = 0; a < decoded.length; a++) {
+ for (int b = 0; b < decoded.length; b++) {
+ for (int c = 0; c < decoded.length; c++) {
+ /** Das Zeichen '€' existiert in Latin1 nicht! */
+ if (a == 10 || b == 10 || c == 10)
+ continue;
+ StringBuilder input = new StringBuilder();
+ input.append(encoded_latin1[a]);
+ input.append(encoded_latin1[b]);
+ input.append(encoded_latin1[c]);
+ StringBuilder expected = new StringBuilder();
+ expected.append(decoded[a]);
+ expected.append(decoded[b]);
+ expected.append(decoded[c]);
+ String output = codec.decode(input);
+ log.debug("{}\t-> {}", input, output);
+ Assert.assertEquals("\"" + input + "\" was decoded falsely", expected.toString(), output);
+ }
+ }
+ }
+ }
+
+ @Test
+ public void testEncodeUtf8() throws Exception {
+ PercentCodec codec = new PercentCodec("UTF-8");
+
+ for (int a = 0; a < decoded.length; a++) {
+ for (int b = 0; b < decoded.length; b++) {
+ for (int c = 0; c < decoded.length; c++) {
+ StringBuilder input = new StringBuilder();
+ input.append(decoded[a]);
+ input.append(decoded[b]);
+ input.append(decoded[c]);
+ StringBuilder expected = new StringBuilder();
+ expected.append(encoded_utf8[a]);
+ expected.append(encoded_utf8[b]);
+ expected.append(encoded_utf8[c]);
+ String output = codec.encode(input);
+ log.debug("{}\t-> {}", input, output);
+ Assert.assertEquals("\"" + input + "\" was encoded falsely", expected.toString(), output);
+ }
+ }
+ }
+ }
+
+ @Test
+ public void testDecodeUtf8() throws Exception {
+ PercentCodec codec = new PercentCodec("UTF-8");
+
+ for (int a = 0; a < decoded.length; a++) {
+ for (int b = 0; b < decoded.length; b++) {
+ for (int c = 0; c < decoded.length; c++) {
+ StringBuilder input = new StringBuilder();
+ input.append(encoded_utf8[a]);
+ input.append(encoded_utf8[b]);
+ input.append(encoded_utf8[c]);
+ StringBuilder expected = new StringBuilder();
+ expected.append(decoded[a]);
+ expected.append(decoded[b]);
+ expected.append(decoded[c]);
+ String output = codec.decode(input);
+ log.debug("{}\t-> {}", input, output);
+ Assert.assertEquals("\"" + input + "\" was decoded falsely", expected.toString(), output);
+ }
+ }
+ }
+ }
+}