X-Git-Url: https://juplo.de/gitweb/?p=percentcodec;a=blobdiff_plain;f=percentcodec%2Fsrc%2Ftest%2Fjava%2Fde%2Fjuplo%2Fpercentcodec%2FPercentCodecTest.java;fp=percentcodec%2Fsrc%2Ftest%2Fjava%2Fde%2Fjuplo%2Fpercentcodec%2FPercentCodecTest.java;h=bec35fa3ddc84ac282a1f42f89672fc36276ef49;hp=0000000000000000000000000000000000000000;hb=a6e0e65cce68acb20abc6ca935471611a740c342;hpb=1d395c6002486d485bde0d5dc713886d70bd3f8a diff --git a/percentcodec/src/test/java/de/juplo/percentcodec/PercentCodecTest.java b/percentcodec/src/test/java/de/juplo/percentcodec/PercentCodecTest.java new file mode 100644 index 00000000..bec35fa3 --- /dev/null +++ b/percentcodec/src/test/java/de/juplo/percentcodec/PercentCodecTest.java @@ -0,0 +1,115 @@ +package de.juplo.percentcodec; + +import de.juplo.percentcodec.PercentCodec; +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); + } + } + } + } +}