Changed folder structure of test classes and renamed some packages

This commit is contained in:
Ziver Koc 2016-03-04 17:48:59 +01:00
parent ccd50ec104
commit 884c5d64c3
76 changed files with 5305 additions and 5375 deletions

View file

@ -0,0 +1,48 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package zutil.parser;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class BBCodeParserTest{
@Test
public void BBCodeParser() {
BBCodeParser parser = new BBCodeParser();
assertEquals("1234", parser.read("1234"));
assertEquals("<i>1234</i>", parser.read("[i]1234[/i]"));
assertEquals("[apa]lol[/apa]", parser.read("[apa]lol[/apa]"));
assertEquals("jshdkj <u>lol [apa]lol[/apa]</u>", parser.read("jshdkj [u]lol [apa]lol[/apa]"));
//assertEquals("jshdkj [m]lol[/k] <i>lol</i>", parser.read("jshdkj [m]lol[/k] [i]lol[/i]"));
assertEquals("jshdkj [m", parser.read("jshdkj [m"));
assertEquals("jshdkj [/m", parser.read("jshdkj [/m"));
assertEquals("jshdkj m]", parser.read("jshdkj m]"));
assertEquals("jshdkj <br />", parser.read("jshdkj <br />"));
}
}

View file

@ -0,0 +1,76 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package zutil.parser;
import org.junit.Test;
import java.util.Base64;
import static org.junit.Assert.assertEquals;
public class Base64Test {
@Test
public void decode() {
assertEquals( "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.",
Base64Decoder.decode("TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=")
);
Base64Decoder decoder = new Base64Decoder();
decoder.clear();
decoder.read("YW55IGNhcm5hbCBwbGVhc3VyZQ==");
assertEquals( "any carnal pleasure", decoder.toString() );
decoder.clear();
decoder.read("bGVhc3VyZS4=");
assertEquals( "leasure.", decoder.toString() );
decoder.clear();
decoder.read("YW55IGNhcm5hbCBwbGVhc3Vy");
assertEquals( "any carnal pleasur", decoder.toString() );
decoder.clear();
}
@Test
public void encode() {
assertEquals("TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=" ,
Base64Encoder.encode("Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.")
);
assertEquals("YW55IGNhcm5hbCBwbGVhc3VyZQ==", Base64Encoder.encode("any carnal pleasure"));
assertEquals("bGVhc3VyZS4=", Base64Encoder.encode("leasure."));
assertEquals("YW55IGNhcm5hbCBwbGVhc3Vy", Base64Encoder.encode("any carnal pleasur"));
}
@Test
public void encodeJavaUtil() {
assertEquals("TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=" ,
Base64.getEncoder().encodeToString("Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.".getBytes())
);
assertEquals("YW55IGNhcm5hbCBwbGVhc3VyZQ==", Base64.getEncoder().encodeToString("any carnal pleasure".getBytes()));
assertEquals("bGVhc3VyZS4=", Base64.getEncoder().encodeToString("leasure.".getBytes()));
assertEquals("YW55IGNhcm5hbCBwbGVhc3Vy", Base64.getEncoder().encodeToString("any carnal pleasur".getBytes()));
}
}

View file

@ -0,0 +1,109 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package zutil.parser;
import org.junit.Test;
import java.io.IOException;
import java.io.StringReader;
import static org.junit.Assert.assertEquals;
/**
* Created by ezivkoc on 2015-07-30.
*/
public class CSVParserTest {
@Test
public void emptyTest(){
DataNode node = CSVParser.read("");
assertEquals(null, node);
}
@Test
public void simpleTest(){
DataNode node = CSVParser.read("hello,world,you");
assertEquals(3, node.size());
assertEquals("hello", node.get(0).getString());
assertEquals("world", node.get(1).getString());
assertEquals("you", node.get(2).getString());
}
@Test
public void simpleHeaderTest() throws IOException {
CSVParser parser = new CSVParser(new StringReader("where,what,who\nhello,world,you"), true);
DataNode node = parser.read();
assertEquals(3, node.size());
assertEquals("hello", node.get(0).getString());
assertEquals("world", node.get(1).getString());
assertEquals("you", node.get(2).getString());
node = parser.getHeaders();
assertEquals("where", node.get(0).getString());
assertEquals("what", node.get(1).getString());
assertEquals("who", node.get(2).getString());
}
@Test
public void simpleMultilineTest() throws IOException {
CSVParser parser = new CSVParser(
new StringReader("hello,world,you\nhello,world,you\nhello,world,you"));
int rows=0;
for(DataNode node = parser.read(); node != null; node=parser.read(), ++rows) {
assertEquals(3, node.size());
assertEquals("hello", node.get(0).getString());
assertEquals("world", node.get(1).getString());
assertEquals("you", node.get(2).getString());
}
assertEquals(3, rows);
}
@Test
public void quotedTest(){
DataNode node = CSVParser.read("\"hello\",\"world\",\"you\"");
assertEquals(3, node.size());
assertEquals("hello", node.get(0).getString());
assertEquals("world", node.get(1).getString());
assertEquals("you", node.get(2).getString());
}
@Test
public void quotedIncorrectlyTest(){
DataNode node = CSVParser.read("hello\",wo\"rl\"d,\"you\"");
assertEquals(3, node.size());
assertEquals("hello\"", node.get(0).getString());
assertEquals("wo\"rl\"d", node.get(1).getString());
assertEquals("you", node.get(2).getString());
}
@Test
public void quotedCommaTest(){
DataNode node = CSVParser.read("hello,\"world,you\"");
assertEquals(2, node.size());
assertEquals("hello", node.get(0).getString());
assertEquals("world,you", node.get(1).getString());
}
}

View file

@ -0,0 +1,305 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package zutil.parser;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
/**
* Created by Ziver on 2015-03-23.
*/
public class TemplatorTest {
class TestClass{
public String attr;
}
class TestSubClass extends TestClass{
public String subAttr;
}
class TestFuncClass{
public boolean isTrue(){
return true;
}
public boolean isFalse(){
return false;
}
}
@Test
public void tagIncorrectTest(){
assertEquals("<HTML>{{</HTML>",
new Templator("<HTML>{{</HTML>").compile());
assertEquals("<HTML>}}</HTML>",
new Templator("<HTML>}}</HTML>").compile());
assertEquals("<HTML></HTML>}}",
new Templator("<HTML></HTML>}}").compile());
assertEquals("<HTML></HTML>{{",
new Templator("<HTML></HTML>{{").compile());
assertEquals("<HTML>{</HTML>",
new Templator("<HTML>{</HTML>").compile());
assertEquals("<HTML>}</HTML>",
new Templator("<HTML>}</HTML>").compile());
assertEquals("<HTML>{}</HTML>",
new Templator("<HTML>{}</HTML>").compile());
assertEquals("<HTML>{test}</HTML>",
new Templator("<HTML>{test}</HTML>").compile());
}
@Test
public void attributeEmptyTest(){
Templator tmpl = new Templator("<HTML>{{test}}</HTML>");
assertEquals("<HTML>null</HTML>", tmpl.compile());
}
@Test
public void attributeSimpleTest() {
Templator tmpl = new Templator("<HTML>{{test}}</HTML>");
tmpl.set("test", "1234");
assertEquals("<HTML>1234</HTML>", tmpl.compile());
}
@Test
public void attributeObjectTest(){
Templator tmpl = new Templator("<HTML>{{test.attr}}</HTML>");
TestClass obj = new TestClass();
obj.attr = "1234";
tmpl.set("test", obj);
assertEquals("<HTML>1234</HTML>", tmpl.compile());
}
@Test
public void conditionIncompleteTest(){
assertEquals("<HTML>{{#key}}</HTML>",
new Templator("<HTML>{{#key}}</HTML>").compile());
assertEquals("<HTML>{{/key}}</HTML>",
new Templator("<HTML>{{/key}}</HTML>").compile());
assertEquals("",
new Templator("{{#key}}{{/key}}").compile());
assertEquals("<HTML></HTML>",
new Templator("<HTML>{{#key}}{{/key}}</HTML>").compile());
}
@Test
public void conditionEmptyTest(){
Templator tmpl = new Templator(
"<HTML>{{#key}}123456789{{/key}}</HTML>");
assertEquals(
"<HTML></HTML>",
tmpl.compile());
}
@Test
public void conditionSimpleTest(){
Templator tmpl = new Templator(
"<HTML>{{#key}}123456789{{/key}}</HTML>");
tmpl.set("key", "set");
assertEquals(
"<HTML>123456789</HTML>",
tmpl.compile());
}
@Test
public void conditionObjectTest(){
Templator tmpl = new Templator("<HTML>{{#test.attr}}5678{{/test.attr}}</HTML>");
TestClass obj = new TestClass();
obj.attr = "1234";
tmpl.set("test", obj);
assertEquals("<HTML>5678</HTML>", tmpl.compile());
tmpl.clear();
tmpl.set("test", new TestClass());
assertEquals("<HTML></HTML>", tmpl.compile());
}
@Test
public void conditionBooleanTest(){
Templator tmpl = new Templator(
"<HTML>{{#key}}123456789{{/key}}</HTML>");
tmpl.set("key", true);
assertEquals(
"<HTML>123456789</HTML>", tmpl.compile());
tmpl.set("key", false);
assertEquals(
"<HTML></HTML>", tmpl.compile());
}
@Test
public void conditionIteratorEmptyTest(){
Templator tmpl = new Templator("<HTML>{{#list}}1234 {{.}} {{/list}}</HTML>");
tmpl.set("list", new ArrayList());
assertEquals(
"<HTML></HTML>", tmpl.compile());
}
@Test
public void conditionIteratorTest(){
Templator tmpl = new Templator("<HTML>{{#list}}{{.}}{{/list}}</HTML>");
tmpl.set("list", Arrays.asList(1,2,3,4,5,6,7,8,9));
assertEquals(
"<HTML>123456789</HTML>", tmpl.compile());
}
@Test
public void conditionArrayTest(){
Templator tmpl = new Templator("<HTML>{{#list}}{{.}}{{/list}}</HTML>");
tmpl.set("list", new int[]{1,2,3,4,5,6,7,8,9});
assertEquals(
"<HTML>123456789</HTML>", tmpl.compile());
}
@Test
public void conditionArrayLength(){
Templator tmpl = new Templator("<HTML>{{#list.length}}run once{{/list.length}}</HTML>");
tmpl.set("list", new int[]{});
assertEquals(
"<HTML></HTML>", tmpl.compile());
tmpl.set("list", new int[]{1});
assertEquals(
"<HTML>run once</HTML>", tmpl.compile());
tmpl.set("list", new int[]{1,2,3,4,5,6,7,8,9});
assertEquals(
"<HTML>run once</HTML>", tmpl.compile());
}
@Test
public void negativeConditionEmptyTest(){
Templator tmpl = new Templator(
"<HTML>{{^key}}123456789{{/key}}</HTML>");
assertEquals(
"<HTML>123456789</HTML>", tmpl.compile());
}
@Test
public void negativeConditionSetTest(){
Templator tmpl = new Templator(
"<HTML>{{^key}}123456789{{/key}}</HTML>");
tmpl.set("key", "set");
assertEquals(
"<HTML></HTML>", tmpl.compile());
}
@Test
public void negativeConditionObjectTest(){
Templator tmpl = new Templator("<HTML>{{^test.attr}}5678{{/test.attr}}</HTML>");
TestClass obj = new TestClass();
obj.attr = "1234";
tmpl.set("test", obj);
assertEquals("<HTML></HTML>", tmpl.compile());
tmpl.clear();
tmpl.set("test", new TestClass());
assertEquals("<HTML>5678</HTML>", tmpl.compile());
}
@Test
public void negativeConditionIteratorTest(){
Templator tmpl = new Templator(
"<HTML>{{^key}}123456789{{/key}}</HTML>");
tmpl.set("key", Arrays.asList(1,2,3,4,5,6,7,8,9));
assertEquals(
"<HTML></HTML>", tmpl.compile());
}
@Test
public void negativeConditionIteratorEmptyTest(){
Templator tmpl = new Templator(
"<HTML>{{^key}}123456789{{/key}}</HTML>");
tmpl.set("key", new ArrayList());
assertEquals(
"<HTML>123456789</HTML>", tmpl.compile());
}
@Test
public void negativeConditionBooleanTest(){
Templator tmpl = new Templator(
"<HTML>{{^key}}123456789{{/key}}</HTML>");
tmpl.set("key", true);
assertEquals(
"<HTML></HTML>", tmpl.compile());
tmpl.set("key", false);
assertEquals(
"<HTML>123456789</HTML>", tmpl.compile());
}
@Test
public void commentTest(){
Templator tmpl = new Templator(
"<HTML>{{! This is a comment}}</HTML>");
assertEquals(
"<HTML></HTML>", tmpl.compile());
}
@Test
public void functionCallTest(){
Templator tmpl = new Templator(
"<HTML>{{#obj.isTrue()}}it is true{{/obj.isTrue()}}</HTML>");
tmpl.set("obj", new TestFuncClass());
assertEquals(
"<HTML>it is true</HTML>", tmpl.compile());
tmpl = new Templator(
"<HTML>{{#obj.isFalse()}}it is true{{/obj.isFalse()}}</HTML>");
tmpl.set("obj", new TestFuncClass());
assertEquals(
"<HTML></HTML>", tmpl.compile());
}
@Test
public void functionInArrayTest(){
Templator tmpl = new Templator(
"<HTML>{{#list}}{{#.isTrue()}}1{{/.isTrue()}}{{/list}}</HTML>");
tmpl.set("list", new TestFuncClass[]{
new TestFuncClass(),new TestFuncClass(),new TestFuncClass()
});
assertEquals(
"<HTML>111</HTML>", tmpl.compile());
}
@Test
public void recursiveTemplateorTest(){
Templator tmpl2 = new Templator(
"{{value1}},{{value2}}");
tmpl2.set("value1", "sub1");
tmpl2.set("value2", "sub2");
Templator tmpl = new Templator(
"<HTML>{{parent}}:{{child}}</HTML>");
tmpl.set("parent", "super");
tmpl.set("child", tmpl2);
assertEquals(
"<HTML>super:sub1,sub2</HTML>", tmpl.compile());
}
@Test
public void subClassTest(){
Templator tmpl = new Templator("<HTML>{{test.attr}}:{{test.subAttr}}</HTML>");
TestSubClass obj = new TestSubClass();
obj.attr = "1234";
obj.subAttr = "5678";
tmpl.set("test", obj);
assertEquals("<HTML>1234:5678</HTML>", tmpl.compile());
}
}

View file

@ -0,0 +1,62 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package zutil.parser;
import org.junit.Test;
import zutil.parser.URLDecoder;
import java.io.UnsupportedEncodingException;
import static org.junit.Assert.assertEquals;
/**
* Created by ezivkoc on 2015-12-11.
*/
public class URLDecoderTest {
@Test
public void simpleTest(){
assertEquals(null, URLDecoder.decode(null));
assertEquals("", URLDecoder.decode(""));
assertEquals("space space", URLDecoder.decode("space space"));
assertEquals("space space", URLDecoder.decode("space+space"));
assertEquals("space space", URLDecoder.decode("space%20space"));
}
@Test
public void percentTest(){
assertEquals("test+", URLDecoder.decode("test%2B"));
assertEquals("test%2", URLDecoder.decode("test%2"));
assertEquals("test+test", URLDecoder.decode("test%2Btest"));
assertEquals("test+test", URLDecoder.decode("test%2btest"));
}
@Test
public void percentMultibyteTest() throws UnsupportedEncodingException {
assertEquals("Ängen", java.net.URLDecoder.decode("%C3%84ngen", "UTF-8"));
assertEquals("Ängen", URLDecoder.decode("%C3%84ngen"));
}
}

View file

@ -0,0 +1,128 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package zutil.parser.binary;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
/**
* Created by Ziver on 2016-01-28.
*/
public class BinaryStructTest {
interface BinaryTestStruct extends BinaryStruct{
void assertObj();
}
@Test
public void basicIntTest(){
BinaryTestStruct struct = new BinaryTestStruct() {
@BinaryField(index=1, length=32)
public int i1;
@BinaryField(index=2, length=32)
public int i2;
public void assertObj(){
assertEquals(1, i1);
assertEquals(2, i2);
}
};
BinaryStructParser.parse(struct, new byte[]{0,0,0,1, 0,0,0,2});
struct.assertObj();
}
@Test
public void basicBooleanTest(){
BinaryTestStruct struct = new BinaryTestStruct() {
@BinaryField(index=1, length=1)
public boolean b1;
@BinaryField(index=2, length=1)
public boolean b2;
public void assertObj(){
assertFalse(b1);
assertFalse(b2);
}
};
BinaryStructParser.parse(struct, new byte[]{0b0100_000});
struct.assertObj();
}
@Test
public void basicStringTest(){
BinaryTestStruct struct = new BinaryTestStruct() {
@BinaryField(index=1, length=8*12)
public String s1;
public void assertObj(){
assertEquals(s1, "hello world!");
}
};
BinaryStructParser.parse(struct, "hello world!".getBytes());
struct.assertObj();
}
@Test
public void nonLinedLength(){
BinaryTestStruct struct = new BinaryTestStruct() {
@BinaryField(index=1, length=12)
public int i1;
@BinaryField(index=2, length=12)
public int i2;
public void assertObj(){
assertEquals(1, i1);
assertEquals(2048, i2);
}
};
BinaryStructParser.parse(struct, new byte[]{0b0000_0000,0b0001_1000,0b0000_0000});
struct.assertObj();
}
// TODO: add full non lined length support
// @Test
public void nonLinedLength2(){
BinaryTestStruct struct = new BinaryTestStruct() {
@BinaryField(index=1, length=12)
public int i1;
@BinaryField(index=2, length=12)
public int i2;
public void assertObj(){
assertEquals(17, i1);
assertEquals(2048, i2);
}
};
BinaryStructParser.parse(struct, new byte[]{0b0000_0001,0b0001_1000,0b0000_0000});
struct.assertObj();
}
}

View file

@ -0,0 +1,183 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package zutil.parser.json;
import org.junit.Test;
import zutil.parser.DataNode;
import zutil.parser.DataNode.DataType;
import zutil.parser.json.JSONParser;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class JSONParserTest {
@Test
public void nullString(){
DataNode data = JSONParser.read(null);
assertNull(data);
}
@Test
public void emptyString(){
DataNode data = JSONParser.read("");
assertNull(data);
}
@Test
public void emptyMap(){
DataNode data = JSONParser.read("{}");
assertEquals(DataType.Map, data.getType());
assertEquals( 0, data.size());
}
@Test
public void emptyList(){
DataNode data = JSONParser.read("[]");
assertEquals(DataType.List, data.getType());
assertEquals( 0, data.size());
}
@Test
public void valueInt(){
DataNode data = JSONParser.read("1234");
assert(data.isValue());
assertEquals( DataType.Number, data.getType());
assertEquals( 1234, data.getInt());
}
@Test
public void valueDouble(){
DataNode data = JSONParser.read("12.34");
assert(data.isValue());
assertEquals( DataType.Number, data.getType());
assertEquals( 12.34, data.getDouble(), 0);
}
@Test
public void valueBoolean(){
DataNode data = JSONParser.read("false");
assert(data.isValue());
assertEquals( DataType.Boolean, data.getType());
assertEquals( false, data.getBoolean());
}
@Test
public void valueBooleanUpperCase(){
DataNode data = JSONParser.read("TRUE");
assert(data.isValue());
assertEquals( DataType.Boolean, data.getType());
assertEquals( true, data.getBoolean());
}
@Test
public void valueStringNoQuotes(){
DataNode data = JSONParser.read("teststring");
assert(data.isValue());
assertEquals( DataType.String, data.getType());
assertEquals( "teststring", data.getString());
}
@Test
public void toManyCommasInList(){
DataNode data = JSONParser.read("[1,2,3,]");
assertEquals(DataType.List, data.getType());
assertEquals( 1, data.get(0).getInt());
assertEquals( 2, data.get(1).getInt());
assertEquals( 3, data.get(2).getInt());
}
@Test
public void toManyCommasInMap(){
DataNode data = JSONParser.read("{1:1,2:2,3:3,}");
assertEquals(DataType.Map, data.getType());
assertEquals( 1, data.get("1").getInt());
assertEquals( 2, data.get("2").getInt());
assertEquals( 3, data.get("3").getInt());
}
@Test
public void nullValueInList(){
DataNode data = JSONParser.read("[1,null,3,]");
assertEquals(DataType.List, data.getType());
assertEquals(3, data.size());
assertEquals( "1", data.get(0).getString());
assertNull(data.get(1));
assertEquals( "3", data.get(2).getString());
}
@Test
public void nullValueInMap(){
DataNode data = JSONParser.read("{1:1,2:null,3:3,}");
assertEquals(DataType.Map, data.getType());
assertEquals(3, data.size());
assertEquals( "1", data.getString("1"));
assertNull(data.get("2"));
assertEquals( "3", data.getString("3"));
}
@Test
public void emptyStringInMap(){
DataNode data = JSONParser.read("{1:{2:\"\"}}");
assertEquals(DataType.Map, data.getType());
assertEquals(1, data.size());
assertEquals( "", data.get("1").getString("2"));
}
@Test
public void complexMap() {
String json = "{" +
"\"test1\": 1234," +
"\"TEST1\": 5678," +
"\"test3\": 1234.99," +
"\"test4\": \"91011\"," +
"\"test5\": [12,13,14,15]," +
"\"test6\": [\"a\",\"b\",\"c\",\"d\"]" +
"}";
DataNode data = JSONParser.read(json);
assert( data.isMap() );
assert( 1234 == data.get("test1").getInt() );
assert( 5678 == data.get("TEST1").getInt() );
assert( 1234.99 == data.get("test3").getDouble() );
assertEquals( "91011", data.get("test4").getString() );
assert( data.get("test5").isList() );
assertEquals( 4, data.get("test5").size() );
assertEquals( 12, data.get("test5").get(0).getInt() );
assertEquals( 13, data.get("test5").get(1).getInt() );
assertEquals( 14, data.get("test5").get(2).getInt() );
assertEquals( 15, data.get("test5").get(3).getInt() );
assert( data.get("test6").isList() );
assertEquals( 4, data.get("test6").size() );
assertEquals( "a", data.get("test6").get(0).getString() );
assertEquals( "b", data.get("test6").get(1).getString() );
assertEquals( "c", data.get("test6").get(2).getString() );
assertEquals( "d", data.get("test6").get(3).getString() );
}
}

View file

@ -0,0 +1,126 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package zutil.parser.json;
import com.carrotsearch.junitbenchmarks.BenchmarkRule;
import org.junit.Rule;
import org.junit.Test;
import zutil.parser.json.JSONSerializerTest.TestClass;
import java.io.*;
import static org.junit.Assert.assertEquals;
public class JSONSerializerBenchmark {
private static final int TEST_EXECUTIONS = 2000;
@Rule
public BenchmarkRule benchmarkRun = new BenchmarkRule();
@Test
public void testJavaLegacySerialize() throws InterruptedException, IOException, ClassNotFoundException{
for(int i=0; i<TEST_EXECUTIONS; i++){
TestClass sourceObj = new TestClass().init();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout);
out.writeObject(sourceObj);
out.flush();
out.close();
byte[] data = bout.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(data);
ObjectInputStream in = new ObjectInputStream(bin);
TestClass targetObj = (TestClass) in.readObject();
in.close();
assertEquals( sourceObj, targetObj );
}
}
@Test
public void testJavaJSONSerialize() throws InterruptedException, IOException, ClassNotFoundException{
for(int i=0; i<TEST_EXECUTIONS; i++){
TestClass sourceObj = new TestClass().init();
TestClass targetObj = JSONSerializerTest.sendReceiveObject(sourceObj);
assertEquals( sourceObj, targetObj );
}
}
@Test
public void testOutputJavaLegacySerialize() throws InterruptedException, IOException, ClassNotFoundException{
for(int i=0; i<TEST_EXECUTIONS; i++){
TestClass sourceObj = new TestClass().init();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout);
out.writeObject(sourceObj);
out.flush();
out.close();
byte[] data = bout.toByteArray();
}
}
@Test
public void testOutputJavaJSONSerialize() throws InterruptedException, IOException, ClassNotFoundException{
for(int i=0; i<TEST_EXECUTIONS; i++){
TestClass sourceObj = new TestClass().init();
String targetObj = JSONSerializerTest.writeObjectToJson(sourceObj);
}
}
@Test
public void testInputJavaLegacySerialize() throws InterruptedException, IOException, ClassNotFoundException{
TestClass sourceObj = new TestClass().init();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout);
out.writeObject(sourceObj);
out.flush();
out.close();
byte[] data = bout.toByteArray();
for(int i=0; i<TEST_EXECUTIONS; i++){
ByteArrayInputStream bin = new ByteArrayInputStream(data);
ObjectInputStream in = new ObjectInputStream(bin);
TestClass targetObj = (TestClass) in.readObject();
in.close();
}
}
@Test
public void testInputJavaJSONSerialize() throws InterruptedException, IOException, ClassNotFoundException{
TestClass sourceObj = new TestClass().init();
String sourceStr = JSONSerializerTest.writeObjectToJson(sourceObj);
for(int i=0; i<TEST_EXECUTIONS; i++){
TestClass targetObj = JSONSerializerTest.readObjectFromJson(sourceStr);
}
}
}

View file

@ -0,0 +1,308 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package zutil.parser.json;
import org.junit.Assert;
import org.junit.Test;
import zutil.io.StringOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
public class JSONSerializerTest{
@Test
public void testOutputSerializerWithPrimitives() throws InterruptedException, IOException, ClassNotFoundException{
TestClass sourceObj = new TestClass().init();
String data = writeObjectToJson(sourceObj);
data = data.replace("\"", "'");
assertThat(data, containsString("'str': 'abcd'"));
assertThat(data, containsString("'value': 42"));
assertThat(data, containsString("'decimal': 1.1"));
}
@Test
public void testInputSerializerWithPrimitives() throws InterruptedException, IOException, ClassNotFoundException{
TestClass sourceObj = new TestClass().init();
TestClass targetObj = sendReceiveObject(sourceObj);
TestClass.assertEquals(sourceObj, targetObj);
}
@Test
public void testOutputSerializerWithClones() throws InterruptedException, IOException, ClassNotFoundException{
TestClassObjClone sourceObj = new TestClassObjClone().init();
String data = writeObjectToJson(sourceObj);
data = data.replace("\"", "'");
assertThat(data, containsString("'@class': 'zutil.test.JSONSerializerTest$TestClassObjClone'"));
assertThat(data, containsString("'obj1': {'@class': 'zutil.test.JSONSerializerTest$TestObj', '@object_id': 2, 'value': 42}"));
assertThat(data, containsString("'obj2': {'@object_id': 2}"));
}
@Test
public void testInputSerializerWithClones() throws InterruptedException, IOException, ClassNotFoundException{
TestClassObjClone sourceObj = new TestClassObjClone().init();
TestClassObjClone targetObj = sendReceiveObject(sourceObj);
assertEquals( sourceObj, targetObj );
}
@Test
public void testOutputSerializerWithArrays() throws InterruptedException, IOException, ClassNotFoundException{
TestClassArray sourceObj = new TestClassArray().init();
String data = writeObjectToJson(sourceObj);
data = data.replace("\"", "'");
assertEquals(
"{'@class': 'zutil.test.JSONSerializerTest$TestClassArray', 'array': [1, 2, 3, 4], '@object_id': 1}\n",
data);
}
@Test
public void testInputSerializerWithArrays() throws InterruptedException, IOException, ClassNotFoundException{
TestClassArray sourceObj = new TestClassArray().init();
TestClassArray targetObj = sendReceiveObject(sourceObj);
assertEquals( sourceObj, targetObj );
}
@Test
public void testInputSerializerWithStringArrays() throws InterruptedException, IOException, ClassNotFoundException{
TestClassStringArray sourceObj = new TestClassStringArray().init();
TestClassStringArray targetObj = sendReceiveObject(sourceObj);
assertEquals( sourceObj, targetObj );
}
@Test
public void testSerializerWithNullFieldsHidden() throws InterruptedException, IOException, ClassNotFoundException{
TestClass sourceObj = new TestClass();
String data = writeObjectToJson(sourceObj, false);
data = data.replace("\"", "'");
assertEquals(
"{'decimal': 0.0}\n",
data);
TestClass targetObj = sendReceiveObject(sourceObj);
TestClass.assertEquals(sourceObj, targetObj);
}
@Test
public void testSerializerWithMapField() throws InterruptedException, IOException, ClassNotFoundException{
TestClassMap sourceObj = new TestClassMap().init();
TestClassMap targetObj = sendReceiveObject(sourceObj);
TestClassMap.assertEquals(sourceObj, targetObj);
}
@Test
public void testSerializerWithMapFieldWithNullEntry() throws InterruptedException, IOException, ClassNotFoundException{
TestClassMap sourceObj = new TestClassMap().init();
sourceObj.map.put("key1", null);
TestClassMap targetObj = sendReceiveObject(sourceObj);
sourceObj.map.remove("key1"); // key1 should not be set in destination
TestClassMap.assertEquals(sourceObj, targetObj);
}
@Test
public void testSerializerWithListField() throws InterruptedException, IOException, ClassNotFoundException{
TestClassList sourceObj = new TestClassList().init();
String data = writeObjectToJson(sourceObj);
data = data.replace("\"", "'");
assertThat(data, containsString("'list': ['value1', 'value2']"));
TestClassList targetObj = sendReceiveObject(sourceObj);
TestClassList.assertEquals(sourceObj, targetObj);
}
/******************* Utility Functions ************************************/
public static <T> T sendReceiveObject(T sourceObj) throws IOException{
return readObjectFromJson(
writeObjectToJson(sourceObj));
}
@SuppressWarnings("unchecked")
public static <T> T readObjectFromJson(String json) throws IOException{
StringReader bin = new StringReader(json);
JSONObjectInputStream in = new JSONObjectInputStream(bin);
T targetObj = (T) in.readObject();
in.close();
return targetObj;
}
public static <T> String writeObjectToJson(T sourceObj) throws IOException{
return writeObjectToJson(sourceObj, true);
}
public static <T> String writeObjectToJson(T sourceObj, boolean metadata) throws IOException{
StringOutputStream bout = new StringOutputStream();
JSONObjectOutputStream out = new JSONObjectOutputStream(bout);
out.enableMetaData(metadata);
out.writeObject(sourceObj);
out.flush();
out.close();
String data = bout.toString();
System.out.println(data);
return data;
}
/******************** Test Classes ************************************/
public static class TestClass implements Serializable{
private static final long serialVersionUID = 1L;
String str;
double decimal;
TestObj obj1;
TestObj obj2;
public TestClass init(){
this.str = "abcd";
this.decimal = 1.1;
this.obj1 = new TestObj().init();
this.obj2 = new TestObj().init();
return this;
}
public static void assertEquals(TestClass obj1, TestClass obj2){
Assert.assertEquals(obj1.str, obj2.str);
Assert.assertEquals(obj1.decimal, obj2.decimal, 0.001);
Assert.assertEquals(obj1.obj1, obj2.obj1);
Assert.assertEquals(obj1.obj2, obj2.obj2);
}
}
public static class TestClassObjClone{
TestObj obj1;
TestObj obj2;
public TestClassObjClone init(){
this.obj1 = this.obj2 = new TestObj().init();
return this;
}
public boolean equals(Object obj){
return obj instanceof TestClassObjClone &&
this.obj1.equals(((TestClassObjClone)obj).obj1) &&
this.obj1 == this.obj2 &&
((TestClassObjClone)obj).obj1 == ((TestClassObjClone)obj).obj2;
}
}
public static class TestObj implements Serializable{
private static final long serialVersionUID = 1L;
int value;
public TestObj init(){
this.value = 42;
return this;
}
public boolean equals(Object obj){
return obj instanceof TestObj &&
this.value == ((TestObj)obj).value;
}
}
public static class TestClassArray{
private int[] array;
public TestClassArray init(){
array = new int[]{1,2,3,4};
return this;
}
public boolean equals(Object obj){
return obj instanceof TestClassArray &&
Arrays.equals(this.array ,((TestClassArray)obj).array);
}
}
public static class TestClassStringArray{
private String[] array;
public TestClassStringArray init(){
array = new String[]{"test","string","array"};
return this;
}
public boolean equals(Object obj){
return obj instanceof TestClassStringArray &&
Arrays.equals(this.array ,((TestClassStringArray)obj).array);
}
}
public static class TestClassMap{
private HashMap<String,String> map;
public TestClassMap init(){
map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
return this;
}
public static void assertEquals(TestClassMap obj1, TestClassMap obj2){
Assert.assertEquals(obj1.map, obj2.map);
}
}
public static class TestClassList{
private ArrayList<String> list;
public TestClassList init(){
list = new ArrayList<>();
list.add("value1");
list.add("value2");
return this;
}
public static void assertEquals(TestClassList obj1, TestClassList obj2){
Assert.assertEquals(obj1.list, obj2.list);
}
}
}