aboutsummaryrefslogtreecommitdiff
path: root/app/src/test
diff options
context:
space:
mode:
authorCamil Staps2015-05-30 16:31:31 +0200
committerCamil Staps2015-05-30 16:31:31 +0200
commit91620ce7c5451a5e7538c7524440ee015a42fd81 (patch)
tree10b8bdae0a44e3532a28e1c12ccdfa05c15f6fd7 /app/src/test
parentFixed app title issue (diff)
Using JSON for serialization of keywords, feeds and filters
Diffstat (limited to 'app/src/test')
-rw-r--r--app/src/test/java/org/rssin/http/SimpleFetcher.java43
-rw-r--r--app/src/test/java/org/rssin/serialization/JsonSerializationTest.java110
2 files changed, 153 insertions, 0 deletions
diff --git a/app/src/test/java/org/rssin/http/SimpleFetcher.java b/app/src/test/java/org/rssin/http/SimpleFetcher.java
new file mode 100644
index 0000000..b7e9871
--- /dev/null
+++ b/app/src/test/java/org/rssin/http/SimpleFetcher.java
@@ -0,0 +1,43 @@
+package org.rssin.http;
+
+import org.rssin.listener.ErrorListener;
+import org.rssin.listener.Listener;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+/**
+ * This Fetcher doesn't work on Android, but can be used for automated tests
+ * @author Camil Staps
+ */
+public class SimpleFetcher implements Fetcher {
+
+ @Override
+ /**
+ * @todo implement when needed.
+ */
+ public void fetch(Request request) {
+ }
+
+ @Override
+ public void fetch(Request request, Listener listener) {
+ try {
+ BufferedReader in = new BufferedReader(new InputStreamReader(request.getURL().openStream()));
+
+ StringBuilder everything = new StringBuilder();
+ String inputLine;
+ while ((inputLine = in.readLine()) != null)
+ everything.append(inputLine);
+ in.close();
+
+ listener.onReceive(everything.toString());
+ } catch (IOException e) {
+ if (ErrorListener.class.isAssignableFrom(listener.getClass())) {
+ ErrorListener errorListener = (ErrorListener) listener;
+ errorListener.onError(e);
+ }
+ }
+ }
+
+}
diff --git a/app/src/test/java/org/rssin/serialization/JsonSerializationTest.java b/app/src/test/java/org/rssin/serialization/JsonSerializationTest.java
new file mode 100644
index 0000000..c8cda01
--- /dev/null
+++ b/app/src/test/java/org/rssin/serialization/JsonSerializationTest.java
@@ -0,0 +1,110 @@
+package org.rssin.serialization;
+
+import junit.framework.TestCase;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+import java.util.UUID;
+
+/**
+ * Automated tests for the JsonSerializer
+ * @author Camil Staps
+ */
+public class JsonSerializationTest extends TestCase {
+
+ Random r;
+
+ public JsonSerializationTest() {
+ r = new Random(System.currentTimeMillis());
+ }
+
+ /**
+ * Test that the readObject and writeObject of the inner class SimpleJsonableExample are setup
+ * correctly; otherwise the tests fail because of that.
+ */
+ public void testSimpleJsonableExample() {
+ for (int i = 0; i < 10; i++) {
+ SimpleJsonableExample ex = randomSimpleJsonableExample();
+ SimpleJsonableExample test = new SimpleJsonableExample();
+ try {
+ test.fromJson(ex.toJson());
+ } catch (JSONException e) {
+ fail("JSONException: " + e.getMessage());
+ }
+ assertEquals(ex, test);
+ }
+ }
+
+ /**
+ * Test {@link JsonSerializer#listToJson(List)} and {@link JsonSerializer#listFromJson(JSONArray, List)}
+ * @throws JSONException
+ */
+ public void testLists() throws JSONException {
+ List<SimpleJsonableExample> list = new ArrayList<>();
+ List<SimpleJsonableExample> second_list = new ArrayList<>();
+ for (int i = 0; i < 10; i++) {
+ list.add(randomSimpleJsonableExample());
+ second_list.add(new SimpleJsonableExample());
+ }
+ JsonSerializer.listFromJson(JsonSerializer.listToJson(list), second_list);
+ for (int i = 0; i < 10; i++) {
+ assertEquals(list.get(i), second_list.get(i));
+ }
+ }
+
+ /**
+ * Get a random SimpleJsonableExample
+ * @return
+ */
+ private SimpleJsonableExample randomSimpleJsonableExample() {
+ return new SimpleJsonableExample(UUID.randomUUID().toString(), r.nextInt());
+ }
+
+ /**
+ * A simple Jsonable object with a String and an int.
+ */
+ private static class SimpleJsonableExample implements Jsonable {
+
+ private String myString;
+ private int myInt;
+
+ public SimpleJsonableExample() {
+ }
+
+ public SimpleJsonableExample(String s, int i) {
+ myString = s;
+ myInt = i;
+ }
+
+ @Override
+ public JSONObject toJson() throws JSONException {
+ JSONObject json = new JSONObject();
+ json.put("myString", myString);
+ json.put("myInt", myInt);
+ return json;
+ }
+
+ @Override
+ public void fromJson(JSONObject json) throws JSONException {
+ myString = json.getString("myString");
+ myInt = json.getInt("myInt");
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (o == null || o.getClass() != SimpleJsonableExample.class)
+ return false;
+
+ SimpleJsonableExample another = (SimpleJsonableExample) o;
+
+ return another.myString == myString &&
+ another.myInt == myInt;
+ }
+ }
+
+}