[tor-commits] [onionoo/master] Write our own dummy DocumentStore for testing.

karsten at torproject.org karsten at torproject.org
Thu Nov 28 14:07:23 UTC 2013


commit 1cfbf760352d73ef9c558ac1c25954067897fd19
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Wed Nov 27 16:19:49 2013 +0100

    Write our own dummy DocumentStore for testing.
    
    Previously, we wrote out/summary and out/update files to a temp directory.
    Avoiding this step makes tests faster and prepares for more sophisticated
    tests.
---
 src/org/torproject/onionoo/ResourceServlet.java    |   11 +--
 .../org/torproject/onionoo/DummyDocumentStore.java |   77 ++++++++++++++++++++
 .../torproject/onionoo/ResourceServletTest.java    |   46 ++++--------
 3 files changed, 98 insertions(+), 36 deletions(-)

diff --git a/src/org/torproject/onionoo/ResourceServlet.java b/src/org/torproject/onionoo/ResourceServlet.java
index 2869d5e..165bc01 100644
--- a/src/org/torproject/onionoo/ResourceServlet.java
+++ b/src/org/torproject/onionoo/ResourceServlet.java
@@ -31,17 +31,18 @@ public class ResourceServlet extends HttpServlet {
         config.getInitParameter("maintenance") != null
         && config.getInitParameter("maintenance").equals("1");
     File outDir = new File(config.getInitParameter("outDir"));
-    this.init(maintenanceMode, outDir, new Time());
+    Time time = new Time();
+    DocumentStore documentStore = new DocumentStore(outDir, time);
+    this.init(maintenanceMode, documentStore, time);
   }
 
   /* Called (indirectly) by servlet container and (directly) by test
    * class. */
-  protected void init(boolean maintenanceMode, File outDir,
-      Time time) {
+  protected void init(boolean maintenanceMode,
+      DocumentStore documentStore, Time time) {
     this.maintenanceMode = maintenanceMode;
     if (!maintenanceMode) {
-      ResponseBuilder.initialize(new DocumentStore(outDir, time),
-          time);
+      ResponseBuilder.initialize(documentStore, time);
     }
   }
 
diff --git a/test/org/torproject/onionoo/DummyDocumentStore.java b/test/org/torproject/onionoo/DummyDocumentStore.java
new file mode 100644
index 0000000..0fce5d9
--- /dev/null
+++ b/test/org/torproject/onionoo/DummyDocumentStore.java
@@ -0,0 +1,77 @@
+package org.torproject.onionoo;
+
+import java.io.File;
+import java.util.SortedMap;
+import java.util.SortedSet;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+public class DummyDocumentStore extends DocumentStore {
+
+  private long lastModified;
+
+  public DummyDocumentStore(long lastModified, Time time) {
+    super((File) null, time);
+    this.lastModified = lastModified;
+  }
+
+  private SortedMap<String, NodeStatus> nodeStatuses =
+      new TreeMap<String, NodeStatus>();
+  void addNodeStatus(String nodeStatusString) {
+    NodeStatus nodeStatus = NodeStatus.fromString(nodeStatusString);
+    this.nodeStatuses.put(nodeStatus.getFingerprint(), nodeStatus);
+  }
+
+  public void flushDocumentCache() {
+    throw new RuntimeException("Not implemented.");
+  }
+
+  public String getStatsString() {
+    throw new RuntimeException("Not implemented.");
+  }
+
+  public <T extends Document> SortedSet<String> list(
+      Class<T> documentType, boolean includeArchive) {
+    if (documentType.equals(NodeStatus.class)) {
+      return new TreeSet<String>(this.nodeStatuses.keySet());
+    }
+    throw new RuntimeException("Not implemented.");
+  }
+
+  public <T extends Document> boolean remove(Class<T> documentType) {
+    throw new RuntimeException("Not implemented.");
+  }
+
+  public <T extends Document> boolean remove(Class<T> documentType,
+      String fingerprint) {
+    throw new RuntimeException("Not implemented.");
+  }
+
+  public <T extends Document> T retrieve(Class<T> documentType,
+      boolean parse) {
+    if (documentType.equals(UpdateStatus.class)) {
+      UpdateStatus updateStatus = new UpdateStatus();
+      updateStatus.documentString = String.valueOf(this.lastModified);
+      return documentType.cast(updateStatus);
+    }
+    throw new RuntimeException("Not implemented.");
+  }
+
+  public <T extends Document> T retrieve(Class<T> documentType,
+      boolean parse, String fingerprint) {
+    if (documentType.equals(NodeStatus.class)) {
+      return documentType.cast(this.nodeStatuses.get(fingerprint));
+    }
+    throw new RuntimeException("Not implemented.");
+  }
+
+  public <T extends Document> boolean store(T document) {
+    throw new RuntimeException("Not implemented.");
+  }
+
+  public <T extends Document> boolean store(T document,
+      String fingerprint) {
+    throw new RuntimeException("Not implemented.");
+  }
+}
+
diff --git a/test/org/torproject/onionoo/ResourceServletTest.java b/test/org/torproject/onionoo/ResourceServletTest.java
index ca7cd69..55cf53c 100644
--- a/test/org/torproject/onionoo/ResourceServletTest.java
+++ b/test/org/torproject/onionoo/ResourceServletTest.java
@@ -8,9 +8,6 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileWriter;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
@@ -22,9 +19,7 @@ import java.util.SortedMap;
 import java.util.TreeMap;
 
 import org.junit.Before;
-import org.junit.Rule;
 import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
 import org.torproject.onionoo.ResourceServlet.HttpServletRequestWrapper;
 import org.torproject.onionoo.ResourceServlet.HttpServletResponseWrapper;
 
@@ -34,6 +29,8 @@ public class ResourceServletTest {
 
   private SortedMap<String, String> relays, bridges;
 
+  private DummyDocumentStore documentStore;
+
   // 2013-04-24 12:22:22
   private static long lastModified = 1366806142000L;
 
@@ -155,7 +152,7 @@ public class ResourceServletTest {
   private void runTest(String requestURI,
       Map<String, String[]> parameterMap) {
     try {
-      this.writeSummaryFile();
+      this.createDummyDocumentStore();
       this.makeRequest(requestURI, parameterMap);
       this.parseResponse();
     } catch (IOException e) {
@@ -163,28 +160,25 @@ public class ResourceServletTest {
     }
   }
 
-  /* TODO Instead of writing out/summary and out/update to a temp
-   * directory, we could also write our own DocumentStore instance. */
-  private void writeSummaryFile() throws IOException {
-    File summaryFile = new File(this.tempOutDir, "summary");
-    BufferedWriter bw = new BufferedWriter(new FileWriter(summaryFile));
-    for (String relay : this.relays.values()) {
-      bw.write(relay + "\n");
+  private void createDummyDocumentStore() {
+    /* TODO Incrementing static lastModified is necessary for
+     * ResponseBuilder to read state from the newly created DocumentStore.
+     * Otherwise, ResponseBuilder would use data from the previous test
+     * run.  This is bad design and should be fixed. */
+    this.documentStore = new DummyDocumentStore(lastModified++,
+        this.testingTime);
+    for (String relay : relays.values()) {
+      documentStore.addNodeStatus(relay);
     }
-    for (String bridge : this.bridges.values()) {
-      bw.write(bridge + "\n");
+    for (String bridge : bridges.values()) {
+      documentStore.addNodeStatus(bridge);
     }
-    bw.close();
-    File updateFile = new File(this.tempOutDir, "update");
-    bw = new BufferedWriter(new FileWriter(updateFile));
-    bw.write(String.valueOf(lastModified++));
-    bw.close();
   }
 
   private void makeRequest(String requestURI,
       Map<String, String[]> parameterMap) throws IOException {
     ResourceServlet rs = new ResourceServlet();
-    rs.init(maintenanceMode, this.tempOutDir, this.testingTime);
+    rs.init(maintenanceMode, this.documentStore, this.testingTime);
     this.request = new TestingHttpServletRequestWrapper(requestURI,
        parameterMap);
     this.response = new TestingHttpServletResponseWrapper();
@@ -261,16 +255,6 @@ public class ResourceServletTest {
     return parameters;
   }
 
-  @Rule
-  public TemporaryFolder tempFolder = new TemporaryFolder();
-
-  private File tempOutDir;
-
-  @Before
-  public void createTempOutDir() throws IOException {
-    this.tempOutDir = this.tempFolder.newFolder("out");
-  }
-
   private static class SummaryDocument {
     private String relays_published;
     private RelaySummary[] relays;





More information about the tor-commits mailing list