You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/06/01 19:29:28 UTC
svn commit: r780737 - in /incubator/pivot/trunk/core: .settings/
test/pivot/serialization/test/ test/pivot/util/concurrent/test/
Author: gbrown
Date: Mon Jun 1 17:29:28 2009
New Revision: 780737
URL: http://svn.apache.org/viewvc?rev=780737&view=rev
Log:
Restore some inadvertently deleted test cases.
Added:
incubator/pivot/trunk/core/.settings/
incubator/pivot/trunk/core/test/pivot/serialization/test/BinarySerializerTest.java
incubator/pivot/trunk/core/test/pivot/serialization/test/ByteArraySerializerTest.java
incubator/pivot/trunk/core/test/pivot/serialization/test/CSVSerializerTest.java
incubator/pivot/trunk/core/test/pivot/serialization/test/PropertiesSerializerTest.java
Modified:
incubator/pivot/trunk/core/test/pivot/serialization/test/JSONSerializerTest.java
incubator/pivot/trunk/core/test/pivot/util/concurrent/test/TaskTest.java
Added: incubator/pivot/trunk/core/test/pivot/serialization/test/BinarySerializerTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/pivot/serialization/test/BinarySerializerTest.java?rev=780737&view=auto
==============================================================================
--- incubator/pivot/trunk/core/test/pivot/serialization/test/BinarySerializerTest.java (added)
+++ incubator/pivot/trunk/core/test/pivot/serialization/test/BinarySerializerTest.java Mon Jun 1 17:29:28 2009
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package pivot.serialization.test;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import pivot.serialization.Serializer;
+import pivot.serialization.BinarySerializer;
+
+public class BinarySerializerTest {
+
+ public static void main(String[] args) {
+ Serializer<Object> serializer = new BinarySerializer();
+
+ Object[] testData = {
+ "Hello World",
+ 123.456,
+ true
+ };
+
+ ByteArrayOutputStream outputStream = null;
+ try {
+ try {
+ outputStream = new ByteArrayOutputStream();
+ serializer.writeObject(testData, outputStream);
+ } finally {
+ outputStream.close();
+ }
+ } catch(Exception exception) {
+ System.out.println(exception);
+ }
+
+ ByteArrayInputStream inputStream = null;
+ try {
+ try {
+ inputStream = new ByteArrayInputStream(outputStream.toByteArray());
+ testData = (Object[])serializer.readObject(inputStream);
+
+ for (int i = 0, n = testData.length; i < n; i++) {
+ System.out.println("[" + i + "] " + testData[i]);
+ }
+ } finally {
+ inputStream.close();
+ }
+ } catch(Exception exception) {
+ System.out.println(exception);
+ }
+ }
+}
Added: incubator/pivot/trunk/core/test/pivot/serialization/test/ByteArraySerializerTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/pivot/serialization/test/ByteArraySerializerTest.java?rev=780737&view=auto
==============================================================================
--- incubator/pivot/trunk/core/test/pivot/serialization/test/ByteArraySerializerTest.java (added)
+++ incubator/pivot/trunk/core/test/pivot/serialization/test/ByteArraySerializerTest.java Mon Jun 1 17:29:28 2009
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package pivot.serialization.test;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.junit.Test;
+
+import pivot.serialization.ByteArraySerializer;
+import pivot.serialization.SerializationException;
+import pivot.serialization.Serializer;
+
+public class ByteArraySerializerTest {
+ public static final String testStrings = "// \n" + "// Hello from "
+ + ByteArraySerializerTest.class.getName() + "\n" + "// \n";
+ public static final byte[] testBytes = testStrings.getBytes();
+
+ public void log(String msg) {
+ System.out.println(msg);
+ }
+
+ @Test(timeout = 2000)
+ public void readValues() throws IOException, SerializationException {
+ log("readValues()");
+
+ Serializer<byte[]> serializer = new ByteArraySerializer();
+
+ ByteArrayInputStream inputStream = new ByteArrayInputStream(testBytes);
+ byte[] result = serializer.readObject(inputStream);
+ assertNotNull(result);
+
+ // dump content, but useful only for text resources ...
+ String dump = new String(result);
+ int dumpLength = dump.getBytes().length;
+ log("Result: " + dumpLength + " bytes \n" + dump);
+
+ assertTrue(dumpLength > 0);
+ }
+
+ @Test(timeout = 2000)
+ public void writeValues() throws IOException, SerializationException {
+ log("writeValues()");
+
+ Serializer<byte[]> serializer = new ByteArraySerializer();
+
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+ serializer.writeObject(testBytes, outputStream);
+
+ outputStream.flush();
+ outputStream.close();
+
+ byte[] result = outputStream.toByteArray();
+ assertNotNull(result);
+
+ // dump content, but useful only for text resources ...
+ String dump = new String(result);
+ int dumpLength = dump.getBytes().length;
+ log("Result: " + dumpLength + " bytes \n" + dump);
+
+ assertTrue(dumpLength > 0);
+ }
+}
Added: incubator/pivot/trunk/core/test/pivot/serialization/test/CSVSerializerTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/pivot/serialization/test/CSVSerializerTest.java?rev=780737&view=auto
==============================================================================
--- incubator/pivot/trunk/core/test/pivot/serialization/test/CSVSerializerTest.java (added)
+++ incubator/pivot/trunk/core/test/pivot/serialization/test/CSVSerializerTest.java Mon Jun 1 17:29:28 2009
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package pivot.serialization.test;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+import pivot.collections.List;
+import pivot.serialization.CSVSerializer;
+
+public class CSVSerializerTest {
+ public static String[] testStrings = {
+ "\"Y\"\"HO,O\",26.11,-0.33,X",
+ "0,1,2,3\nQ,5,6,7\n8,9,10,11",
+ "a,b,c,d",
+ "hello,world",
+ "2,4,6,8,10"
+ };
+
+ public static void main(String[] args) {
+ CSVSerializer csvSerializer = new CSVSerializer();
+ csvSerializer.getKeys().add("A");
+ csvSerializer.getKeys().add("B");
+ csvSerializer.getKeys().add("C");
+ csvSerializer.getKeys().add("D");
+
+ for (int i = 0, n = testStrings.length; i < n; i++) {
+ try {
+ System.out.println("Input: " + testStrings[i]);
+ List<?> objects = csvSerializer.readObject(new StringReader(testStrings[i]));
+
+ StringWriter writer = new StringWriter();
+ csvSerializer.writeObject(objects, writer);
+ System.out.println("Output: " + writer);
+ } catch(Exception exception) {
+ System.out.println(exception);
+ }
+ }
+ }
+}
Modified: incubator/pivot/trunk/core/test/pivot/serialization/test/JSONSerializerTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/pivot/serialization/test/JSONSerializerTest.java?rev=780737&r1=780736&r2=780737&view=diff
==============================================================================
--- incubator/pivot/trunk/core/test/pivot/serialization/test/JSONSerializerTest.java (original)
+++ incubator/pivot/trunk/core/test/pivot/serialization/test/JSONSerializerTest.java Mon Jun 1 17:29:28 2009
@@ -16,6 +16,206 @@
*/
package pivot.serialization.test;
+import java.io.InputStream;
+// import java.io.OutputStream;
+import java.io.StringReader;
+import java.io.StringWriter;
+
+import pivot.collections.HashMap;
+import pivot.collections.List;
+import pivot.collections.Map;
+import pivot.serialization.JSONSerializer;
+
public class JSONSerializerTest {
- // TODO
+ public static String[] testStrings = {
+ "'hey\there'",
+ "'hey\\there'",
+ "'hey\\\\there'",
+ " null",
+ "\"Hello\\\" World\"",
+ "'Hello\\\' \"World'",
+ "\"ABCD",
+ " 10",
+ "+10",
+ " -10",
+ "10.1",
+ "+10.1",
+ "-1s0.1",
+ "true",
+ "false",
+ " [ 0, 1, 2, [3, 4]",
+ " [ \"A\", \"B\", \t\"C\", [\t0, 1, 2, 'abc', true]]",
+ "['A', 'B', 'C']",
+ "{ }",
+ "{null: 'foo'}",
+ "{: 'foo'}",
+ "{\"\": \"foo\"}",
+ "{ my0: 'ABCD\"ABCD' , 'my' : '\"My \\ example 3\"', null: null}",
+ "{a:null}",
+ "{a:''}",
+ "{a:1, b:2",
+ "{\"1a\" : 0, bc : 'hello', n:-100.56, c:true, d:{e:10, f:20}, g:{aa:10, bb:20, cc:30}, m:[1,2, 4]}",
+ "{\"a#b\" : '#ff'}"
+ };
+
+ public static void main(String[] args) {
+ // test0();
+ // test1();
+ // test2();
+ // test3();
+ // test4();
+ test5();
+ }
+
+ public static void test0() {
+ HashMap<String, Object> a = new HashMap<String, Object>();
+ a.put("b", 100);
+
+ HashMap<String, Object> c = new HashMap<String, Object>();
+ c.put("d", "Hello World");
+ a.put("c", c);
+
+ StringWriter writer = new StringWriter();
+ JSONSerializer jsonSerializer = new JSONSerializer();
+ try {
+ jsonSerializer.writeObject(a, writer);
+ } catch(Exception exception) {
+ System.out.println(exception);
+ }
+
+ System.out.println("Output: " + writer);
+ }
+
+ public static void test1() {
+ JSONSerializer jsonSerializer = new JSONSerializer();
+
+ for (int i = 0, n = testStrings.length; i < n; i++) {
+ try {
+ System.out.println("Input: " + testStrings[i]);
+ Object object = jsonSerializer.readObject(new StringReader(testStrings[i]));
+ System.out.println("Object: " + object);
+ StringWriter writer = new StringWriter();
+ jsonSerializer.writeObject(object, writer);
+
+ System.out.println("Output: " + writer);
+ } catch(Exception exception) {
+ System.out.println(exception);
+ }
+ }
+
+ int i = Integer.MAX_VALUE;
+ long l1 = (long)i + 1;
+ long l2 = Long.MAX_VALUE;
+ float f = Float.MAX_VALUE;
+ double d1 = (double)f + 1;
+ double d2 = Double.MAX_VALUE;
+ String listString = "[" + i + ", " + l1 + ", " + l2 + ", "
+ + f + ", " + d1 + ", " + d2 + "]";
+ List<?> list = JSONSerializer.parseList(listString);
+ for (Object item : list) {
+ System.out.println(item);
+ }
+
+ Map<String, ?> map = JSONSerializer.parseMap("{a:100, b:200, c:300}");
+ for (String key : map) {
+ System.out.println(key + ":" + map.get(key));
+ }
+ }
+
+ public static void test2() {
+ testMap("{a: {b: [{cd:'hello'}, {c:'world'}]}}", "a.b[0].cd");
+ testMap("{a: {b: [{c:'hello'}, {c:'world'}]}}", "['a'].b[0].c");
+ testMap("{a: {b: [{c:'hello'}, {c:'world'}]}}", "a[\"b\"][0]['c']");
+ testMap("{a: {b: [{c:'hello'}, {c:'world'}]}}", "a.");
+ testMap("{a: {b: [{c:'hello', d:[0, 1, 2, 3, 4]}, {c:'world'}]}}", "a.b[0].d[2]");
+ testMap("{a: {b: [{c:'hello', d:[0, 1, 2, 3, 4]}, {c:'world'}]}}", "a.....");
+ testMap("{abc: {def: [{ghi:'hello', d:[0, 1, 2, 3, 4]}, {c:'world'}]}}", "abc.def[0].ghi");
+
+ testList("[[0, 1, 2], [3, 4, 5]]", "[1]");
+ testList("[[0, 1, 2], [3, 4, 5]]", "[1][0]");
+ testList("[[0, 1, 2], [3, 4, 5]]", "[1][0].c");
+ testList("[[0, 1, 2], [3, 4, 5]]", "[1][]");
+ testList("[[0, 1, 2], [3, 4, 5]]", "[1][0][0]");
+ }
+
+ public static void test3() {
+ JSONSerializer serializer = new JSONSerializer("ISO-8859-1");
+ InputStream inputStream = JSONSerializerTest.class.getResourceAsStream("json_serializer_test.json");
+
+ Object root = null;
+ try {
+ root = serializer.readObject(inputStream);
+ } catch(Exception exception) {
+ System.out.println(exception);
+ }
+
+ if (root != null) {
+ System.out.println(JSONSerializer.getString(root, "foo"));
+ System.out.println(JSONSerializer.getString(root, "bar"));
+ }
+
+ try {
+ serializer.writeObject(root, System.out);
+ } catch(Exception exception) {
+ System.out.println(exception);
+ }
+ }
+
+ private static void testList(String list, String path) {
+ JSONSerializer jsonSerializer = new JSONSerializer();
+
+ try {
+ jsonSerializer.writeObject(JSONSerializer.get(JSONSerializer.parseList(list), path),
+ System.out);
+ System.out.println();
+ } catch(Exception exception) {
+ System.out.println(exception);
+ }
+ }
+
+ private static void testMap(String map, String path) {
+ JSONSerializer jsonSerializer = new JSONSerializer();
+
+ try {
+ jsonSerializer.writeObject(JSONSerializer.get(JSONSerializer.parseMap(map), path),
+ System.out);
+ System.out.println();
+ } catch(Exception exception) {
+ System.out.println(exception);
+ }
+ }
+
+ public static void test4() {
+ Object root = JSONSerializer.parse("{a:{b:{c:'hello', d:'world'}, e:[1, 2, 3], f:false}, h:null}");
+ testGet(root, "a");
+ testGet(root, "a.b");
+ testGet(root, "a.b.c");
+ testGet(root, "a.b.d");
+ testGet(root, "a['e']");
+ testGet(root, "a['e'][1]");
+ testGet(root, "a['f']");
+ testGet(root, "a['h']");
+
+ JSONSerializer.put(root, "a['h']", 100);
+ testGet(root, "a['h']");
+
+ JSONSerializer.remove(root, "a['h']");
+
+ System.out.println("a['h'] exists: " + JSONSerializer.containsKey(root, "a['h']"));
+ }
+
+ public static void test5() {
+ JSONSerializer jsonSerializer = new JSONSerializer();
+
+ try {
+ jsonSerializer.writeObject(JSONSerializer.parse("// This is a comment\n\n['a', /*FOO*/ //dfsdf\n 'b' // FSKJHJKDSF\r /*ASDKHASD*/]"), System.out);
+ } catch(Exception exception) {
+ System.err.println(exception);
+ }
+ }
+
+ private static void testGet(Object root, String path) {
+ Object value = JSONSerializer.get(root, path);
+ System.out.println(path + ": " + value);
+ }
}
Added: incubator/pivot/trunk/core/test/pivot/serialization/test/PropertiesSerializerTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/pivot/serialization/test/PropertiesSerializerTest.java?rev=780737&view=auto
==============================================================================
--- incubator/pivot/trunk/core/test/pivot/serialization/test/PropertiesSerializerTest.java (added)
+++ incubator/pivot/trunk/core/test/pivot/serialization/test/PropertiesSerializerTest.java Mon Jun 1 17:29:28 2009
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package pivot.serialization.test;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+
+import pivot.collections.HashMap;
+import pivot.collections.Map;
+import pivot.serialization.PropertiesSerializer;
+import pivot.serialization.Serializer;
+
+public class PropertiesSerializerTest
+{
+ @SuppressWarnings({"unchecked"})
+ public static void main(String[] args) {
+ Serializer serializer = new PropertiesSerializer();
+
+ Map<String, Object> testMap = new HashMap<String, Object>();
+ testMap.put("hello", "Hello World");
+ testMap.put("number", 123.456);
+ testMap.put("boolean", true);
+ testMap.put("i18n", "Ã⬠& ÃÂÃËÃÅâé"); // test some chars to encode ...
+ testMap.put("object", new Object());
+
+ ByteArrayOutputStream outputStream = null;
+ try {
+ try {
+ outputStream = new ByteArrayOutputStream();
+ serializer.writeObject(testMap, outputStream);
+ } finally {
+ outputStream.close();
+
+ String dump = new String(outputStream.toByteArray());
+ System.out.println("Succesfully Written");
+ System.out.println(dump);
+ }
+ } catch(Exception exception) {
+ System.out.println(exception);
+ }
+
+ ByteArrayInputStream inputStream = null;
+ Map<String, Object> readData = null;
+ try {
+ try {
+ inputStream = new ByteArrayInputStream(outputStream.toByteArray());
+ readData = (Map<String, Object>) serializer.readObject(inputStream);
+
+ System.out.println("Succesfully Read");
+ for (String key : readData) {
+ System.out.println(key + "=" + readData.get(key));
+ }
+ } finally {
+ inputStream.close();
+ }
+ } catch(Exception exception) {
+ System.out.println(exception);
+ }
+ }
+
+}
\ No newline at end of file
Modified: incubator/pivot/trunk/core/test/pivot/util/concurrent/test/TaskTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/pivot/util/concurrent/test/TaskTest.java?rev=780737&r1=780736&r2=780737&view=diff
==============================================================================
--- incubator/pivot/trunk/core/test/pivot/util/concurrent/test/TaskTest.java (original)
+++ incubator/pivot/trunk/core/test/pivot/util/concurrent/test/TaskTest.java Mon Jun 1 17:29:28 2009
@@ -16,6 +16,102 @@
*/
package pivot.util.concurrent.test;
+import pivot.util.concurrent.Task;
+import pivot.util.concurrent.TaskGroup;
+import pivot.util.concurrent.TaskListener;
+import pivot.util.concurrent.TaskSequence;
+
public class TaskTest {
- // TODO
+ public static class SleepTask extends Task<Void> {
+ private long timeout = 0;
+
+ public SleepTask(long timeout) {
+ this.timeout = timeout;
+ }
+
+ @Override
+ public Void execute() {
+ System.out.println("Starting task " + this + "...");
+
+ try {
+ Thread.sleep(timeout);
+ } catch (InterruptedException exception) {
+ System.out.println(exception);
+ }
+
+ System.out.println("...done");
+
+ return null;
+ }
+
+ @Override
+ public String toString() {
+ return Long.toString(timeout);
+ }
+ }
+
+ public static void main(String[] args) {
+ TaskListener<Void> taskListener = new TaskListener<Void>() {
+ public synchronized void taskExecuted(Task<Void> task) {
+ System.out.println("EXECUTED");
+ notify();
+ }
+
+ public synchronized void executeFailed(Task<Void> task) {
+ System.out.println("FAILED: " + task.getFault());
+ notify();
+ }
+ };
+
+ testTaskSequence(taskListener);
+ testTaskGroup(taskListener);
+ }
+
+ private static void testTaskSequence(TaskListener<Void> taskListener) {
+ System.out.println("Testing task sequence");
+
+ TaskSequence<Void> taskSequence = new TaskSequence<Void>();
+
+ SleepTask task1 = new SleepTask(2000);
+ taskSequence.add(task1);
+
+ SleepTask task2 = new SleepTask(500);
+ taskSequence.add(task2);
+
+ SleepTask task3 = new SleepTask(1000);
+ taskSequence.add(task3);
+
+ synchronized (taskListener) {
+ taskSequence.execute(taskListener);
+
+ try {
+ taskListener.wait();
+ } catch (InterruptedException exception) {
+ }
+ }
+ }
+
+ private static void testTaskGroup(TaskListener<Void> taskListener) {
+ System.out.println("Testing task group");
+
+ TaskGroup<Void> taskGroup = new TaskGroup<Void>();
+
+ SleepTask task1 = new SleepTask(2000);
+ taskGroup.add(task1);
+
+ SleepTask task2 = new SleepTask(500);
+ taskGroup.add(task2);
+
+ SleepTask task3 = new SleepTask(1000);
+ taskGroup.add(task3);
+
+ synchronized (taskListener) {
+ taskGroup.execute(taskListener);
+
+ try {
+ taskListener.wait();
+ } catch (InterruptedException exception) {
+ }
+ }
+ }
}