Browse Source

Complete tests to fulfill specs and refactor

jamming 10 năm trước cách đây
mục cha
commit
d4246e01e9

+ 1 - 1
frameworks/Java/sabina/pom.xml

@@ -125,7 +125,7 @@
                 <configuration>
                     <archive>
                         <manifest>
-                            <mainClass>sabina.Application</mainClass>
+                            <mainClass>sabina.benchmark.Application</mainClass>
                         </manifest>
                     </archive>
                 </configuration>

+ 11 - 11
frameworks/Java/sabina/readme.md

@@ -1,14 +1,14 @@
 
 # Sabina Benchmarking Test
 
-This is the Sabina portion of a [benchmarking test suite](../) comparing a variety of web development platforms.
-The test utilizes Sabina routes, Gson for JSON serialization, Hibernate for ORM and a custom OSIV pattern created
-with Sabina filters.
+This is the Sabina portion of a [benchmarking test suite](../) comparing a variety of web
+development platforms. The test utilizes Sabina routes, Gson for JSON serialization and a custom
+OSIV pattern created with Sabina filters.
 
 
 ## Tests
 
-* [Sabina application](/src/main/java/hello/Application.java)
+* [Sabina application](/src/main/java/sabina/benchmark/Application.java)
 
 
 ## Infrastructure Software Versions
@@ -18,25 +18,25 @@ with Sabina filters.
 
 ## Different test setups
 
-* Local environment with Sabina's built in embedded Jetty (port=4567, context=/)
- * Start application from [Application](/src/main/java/hello/Application.java)'s main method
-* Local environment with Sabina's built in embedded Undertow (port=4567, context=/)
- * Start application from [Application](/src/main/java/hello/Application.java)'s main method
+* Local environment with Sabina's built in embedded Jetty (port=8080, context=/)
+ * Start application from [Application](/src/main/java/sabina/benchmark/Application.java)'s main method
+* Local environment with Sabina's built in embedded Undertow (port=8080, context=/)
+ * Start application from [Application](/src/main/java/sabina/benchmark/Application.java)'s main method
 
 
 ## Test URLs
 
 ### JSON Encoding Test
 
-http://localhost:4567/json
+http://localhost:8080/json
 
 ### Data-Store/Database Mapping Test
 
-http://localhost:4567/db?queries=5
+http://localhost:8080/db?queries=5
 
 ### Plain Text Test
 
-http://localhost:4567/plaintext
+http://localhost:8080/plaintext
 
 ## TODO
 

+ 0 - 5
frameworks/Java/sabina/src/main/java/sabina/World.java

@@ -1,5 +0,0 @@
-package sabina;
-
-public final class World {
-    public int id, randomNumber;
-}

+ 38 - 27
frameworks/Java/sabina/src/main/java/sabina/Application.java → frameworks/Java/sabina/src/main/java/sabina/benchmark/Application.java

@@ -1,10 +1,12 @@
-package sabina;
+package sabina.benchmark;
 
 import static java.lang.Integer.parseInt;
 import static sabina.Sabina.*;
 import static sabina.content.JsonContent.toJson;
 
 import com.mchange.v2.c3p0.ComboPooledDataSource;
+import sabina.Exchange;
+import sabina.Request;
 
 import java.sql.Connection;
 import java.sql.PreparedStatement;
@@ -17,7 +19,12 @@ import java.util.concurrent.ThreadLocalRandom;
 
 import javax.sql.DataSource;
 
-public class Application {
+/**
+ * When it is implemented, add this to benchmark_config
+ * "fortune_url": "/fortune",
+ * "update_url": "/update",
+ */
+final class Application {
     private static final Properties CONFIG = loadConfig ();
     private static final DataSource DS = createSessionFactory ();
     private static final String QUERY = "select * from world where id = ?";
@@ -71,6 +78,11 @@ public class Application {
         }
     }
 
+    private static Object getJson (Exchange it) {
+        it.response.type ("application/json");
+        return toJson (new Message ());
+    }
+
     private static Object getDb (Exchange it) {
         final int queries = getQueries (it.request);
         final World[] worlds = new World[queries];
@@ -93,36 +105,35 @@ public class Application {
             e.printStackTrace ();
         }
 
+        it.response.type ("application/json");
         return toJson (it.request.queryParams ("queries") == null? worlds[0] : worlds);
     }
 
-    public static void main (String[] args) {
-        get ("/json", it -> toJson (new Message ()));
+    private static Object getFortune (Exchange aExchange) {
+        throw new UnsupportedOperationException ();
+    }
 
-        get ("/db", Application::getDb);
+    private static Object getUpdate (Exchange aExchange) {
+        throw new UnsupportedOperationException ();
+    }
 
-        /*
-         * Add this to benchmark_config
-         * "fortune_url": "/fortune",
-         */
-//        get ("/fortune", it -> {
-//            throw new UnsupportedOperationException ();
-//        });
-
-        /*
-         * Add this to benchmark_config
-         * "update_url": "/update",
-         */
-//        get ("/update", it -> {
-//            throw new UnsupportedOperationException ();
-//        });
-
-        get ("/plaintext", it -> {
-            it.response.type (CONTENT_TYPE_TEXT);
-            return MESSAGE;
-        });
-
-        after (it -> it.response.raw ().addDateHeader ("Date", new Date ().getTime ()));
+    private static Object getPlaintext (Exchange it) {
+        it.response.type (CONTENT_TYPE_TEXT);
+        return MESSAGE;
+    }
+
+    private static void addCommonHeaders (Exchange it) {
+        it.header ("Server", "Undertow/1.1.2");
+        it.response.raw ().addDateHeader ("Date", new Date ().getTime ());
+    }
+
+    public static void main (String[] args) {
+        get ("/json", Application::getJson);
+        get ("/db", Application::getDb);
+        get ("/fortune", Application::getFortune);
+        get ("/update", Application::getUpdate);
+        get ("/plaintext", Application::getPlaintext);
+        after (Application::addCommonHeaders);
 
         setIpAddress (CONFIG.getProperty ("web.host"));
         start (parseInt (CONFIG.getProperty ("web.port")));

+ 2 - 2
frameworks/Java/sabina/src/main/java/sabina/Message.java → frameworks/Java/sabina/src/main/java/sabina/benchmark/Message.java

@@ -1,5 +1,5 @@
-package sabina;
+package sabina.benchmark;
 
-public final class Message {
+final class Message {
     public final String message = "Hello, World!";
 }

+ 5 - 0
frameworks/Java/sabina/src/main/java/sabina/benchmark/World.java

@@ -0,0 +1,5 @@
+package sabina.benchmark;
+
+final class World {
+    public int id, randomNumber;
+}

+ 0 - 96
frameworks/Java/sabina/src/test/java/sabina/ApplicationTest.java

@@ -1,96 +0,0 @@
-package sabina;
-
-import static org.apache.http.client.fluent.Request.Get;
-import static org.junit.Assert.*;
-import static sabina.Application.main;
-import static sabina.Sabina.stop;
-
-import java.io.IOException;
-import java.util.List;
-import java.util.Map;
-
-import com.google.gson.Gson;
-import org.apache.http.Header;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-public class ApplicationTest {
-    private static final String ENDPOINT = "http://localhost:8080";
-    private static final Gson GSON = new Gson ();
-
-    @BeforeClass public static void setup () {
-        main (null);
-    }
-
-    @AfterClass public static void close () {
-        stop ();
-    }
-
-    @Test public void json () throws IOException {
-        String result = Get (ENDPOINT + "/json").execute ().returnContent ().asString ();
-        assertEquals ("Hello, World!", GSON.fromJson (result, Map.class).get ("message"));
-    }
-
-    @Test public void plaintext () throws IOException {
-        String result = Get (ENDPOINT + "/plaintext").execute ().returnContent ().asString ();
-        assertEquals ("Hello, World!", result);
-    }
-
-    @Test public void date_header () throws IOException {
-        Header[] result = Get (ENDPOINT + "/json").execute ().returnResponse ().getHeaders ("Date");
-        assertEquals (1, result.length);
-        assertFalse (result[0].getValue ().isEmpty ());
-    }
-
-    @Test public void no_query_parameter () throws IOException {
-        String result = Get (ENDPOINT + "/db").execute ().returnContent ().asString ();
-        Map<?, ?> resultsMap = GSON.fromJson (result, Map.class);
-        assertTrue (resultsMap.containsKey ("id") && resultsMap.containsKey ("randomNumber"));
-    }
-
-    @Test public void empty_query_parameter () throws IOException {
-        String result = Get (ENDPOINT + "/db?queries").execute ().returnContent ().asString ();
-        checkResultItems (result, 1);
-    }
-
-    @Test public void text_query_parameter () throws IOException {
-        String result = Get (ENDPOINT + "/db?queries=text").execute ().returnContent ().asString ();
-        checkResultItems (result, 1);
-    }
-
-    @Test public void zero_queries () throws IOException {
-        String result = Get (ENDPOINT + "/db?queries=0").execute ().returnContent ().asString ();
-        checkResultItems (result, 1);
-    }
-
-    @Test public void one_thousand_queries () throws IOException {
-        String result = Get (ENDPOINT + "/db?queries=1000").execute ().returnContent ().asString ();
-        checkResultItems (result, 500);
-    }
-
-    @Test public void one_query () throws IOException {
-        String result = Get (ENDPOINT + "/db?queries=1").execute ().returnContent ().asString ();
-        checkResultItems (result, 1);
-    }
-
-    @Test public void ten_query () throws IOException {
-        String result = Get (ENDPOINT + "/db?queries=10").execute ().returnContent ().asString ();
-        checkResultItems (result, 10);
-    }
-
-    @Test public void five_hundred_queries () throws IOException {
-        String result = Get (ENDPOINT + "/db?queries=500").execute ().returnContent ().asString ();
-        checkResultItems (result, 500);
-    }
-
-    private void checkResultItems (String result, int size) {
-        List<?> resultsList = GSON.fromJson (result, List.class);
-        assertEquals (size, resultsList.size ());
-
-        for (int ii = 0; ii < size; ii++) {
-            Map<?, ?> r = (Map)resultsList.get (ii);
-            assertTrue (r.containsKey ("id") && r.containsKey ("randomNumber"));
-        }
-    }
-}

+ 136 - 0
frameworks/Java/sabina/src/test/java/sabina/benchmark/ApplicationTest.java

@@ -0,0 +1,136 @@
+package sabina.benchmark;
+
+import static org.apache.http.client.fluent.Request.Get;
+import static org.junit.Assert.*;
+import static sabina.benchmark.Application.main;
+import static sabina.Sabina.stop;
+import static sun.misc.IOUtils.readFully;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+
+import com.google.gson.Gson;
+import org.apache.http.HttpResponse;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public final class ApplicationTest {
+    private static final String ENDPOINT = "http://localhost:8080";
+    private static final Gson GSON = new Gson ();
+
+    @BeforeClass public static void setup () {
+        main (null);
+    }
+
+    @AfterClass public static void close () {
+        stop ();
+    }
+
+    @Test public void json () throws IOException {
+        HttpResponse response = get (ENDPOINT + "/json");
+        String content = getContent (response);
+
+        checkResponse (response, content, "application/json");
+        assertEquals ("Hello, World!", GSON.fromJson (content, Map.class).get ("message"));
+    }
+
+    @Test public void plaintext () throws IOException {
+        HttpResponse response = get (ENDPOINT + "/plaintext");
+        String content = getContent (response);
+
+        checkResponse (response, content, "text/plain");
+        assertEquals ("Hello, World!", content);
+    }
+
+    @Test public void no_query_parameter () throws IOException {
+        HttpResponse response = get (ENDPOINT + "/db");
+        String content = getContent (response);
+
+        checkResponse (response, content, "application/json");
+        Map<?, ?> resultsMap = GSON.fromJson (content, Map.class);
+        assertTrue (resultsMap.containsKey ("id") && resultsMap.containsKey ("randomNumber"));
+    }
+
+    @Test public void empty_query_parameter () throws IOException {
+        HttpResponse response = get (ENDPOINT + "/db?queries");
+        String content = getContent (response);
+
+        checkResponse (response, content, "application/json");
+        checkResultItems (content, 1);
+    }
+
+    @Test public void text_query_parameter () throws IOException {
+        HttpResponse response = get (ENDPOINT + "/db?queries=text");
+        String content = getContent (response);
+
+        checkResponse (response, content, "application/json");
+        checkResultItems (content, 1);
+    }
+
+    @Test public void zero_queries () throws IOException {
+        HttpResponse response = get (ENDPOINT + "/db?queries=0");
+        String content = getContent (response);
+
+        checkResponse (response, content, "application/json");
+        checkResultItems (content, 1);
+    }
+
+    @Test public void one_thousand_queries () throws IOException {
+        HttpResponse response = get (ENDPOINT + "/db?queries=1000");
+        String content = getContent (response);
+
+        checkResponse (response, content, "application/json");
+        checkResultItems (content, 500);
+    }
+
+    @Test public void one_query () throws IOException {
+        HttpResponse response = get (ENDPOINT + "/db?queries=1");
+        String content = getContent (response);
+
+        checkResponse (response, content, "application/json");
+        checkResultItems (content, 1);
+    }
+
+    @Test public void ten_query () throws IOException {
+        HttpResponse response = get (ENDPOINT + "/db?queries=10");
+        String content = getContent (response);
+
+        checkResponse (response, content, "application/json");
+        checkResultItems (content, 10);
+    }
+
+    @Test public void five_hundred_queries () throws IOException {
+        HttpResponse response = get (ENDPOINT + "/db?queries=500");
+        String content = getContent (response);
+
+        checkResponse (response, content, "application/json");
+        checkResultItems (content, 500);
+    }
+
+    private HttpResponse get (String uri) throws IOException {
+        return Get (uri).execute ().returnResponse ();
+    }
+
+    private String getContent (HttpResponse aResponse) throws IOException {
+        return new String (readFully (aResponse.getEntity ().getContent (), -1, true));
+    }
+
+    private void checkResponse (HttpResponse aRes, String aContent, String contentType) {
+        assertTrue (aRes.getFirstHeader ("Server") != null);
+        assertTrue (aRes.getFirstHeader ("Date") != null);
+        assertEquals (aContent.length (), aRes.getEntity ().getContentLength ());
+        assertEquals (contentType, aRes.getEntity ().getContentType ().getValue ());
+    }
+
+    private void checkResultItems (String result, int size) {
+        List<?> resultsList = GSON.fromJson (result, List.class);
+        assertEquals (size, resultsList.size ());
+
+        for (int ii = 0; ii < size; ii++) {
+            Map<?, ?> r = (Map)resultsList.get (ii);
+            assertTrue (r.containsKey ("id") && r.containsKey ("randomNumber"));
+        }
+    }
+}