Browse Source

Merge pull request #1761 from jamming/sabina-1.3.4

Update framework version to 1.3.5
Mike Smith 9 years ago
parent
commit
26a75a8968

+ 29 - 2
frameworks/Java/sabina/pom.xml

@@ -31,12 +31,11 @@
 
         <maven.compiler.source>${java.specification.version}</maven.compiler.source>
         <maven.compiler.target>${java.specification.version}</maven.compiler.target>
-        <maven.compiler.optimize>true</maven.compiler.optimize>
         <maven.compiler.debug>false</maven.compiler.debug>
 
         <db.host>localhost</db.host>
 
-        <sabina-version>1.3.0</sabina-version>
+        <sabina-version>1.3.5</sabina-version>
     </properties>
 
     <repositories>
@@ -147,6 +146,34 @@
                     </archive>
                 </configuration>
             </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-war-plugin</artifactId>
+                <version>2.6</version>
+                <configuration>
+                    <warName>ROOT</warName>
+                    <failOnMissingWebXml>false</failOnMissingWebXml>
+                    <packagingExcludes>
+                        WEB-INF/lib/jetty-*.jar,
+                        WEB-INF/lib/undertow-*.jar,
+                        WEB-INF/lib/freemarker-*.jar,
+                        WEB-INF/lib/rythm-*.jar,
+                        WEB-INF/lib/mysql-*.jar,
+                        WEB-INF/lib/xnio-*.jar,
+                        WEB-INF/lib/scribe-*.jar,
+                        WEB-INF/lib/jboss-*.jar
+                    </packagingExcludes>
+                </configuration>
+                <executions>
+                    <execution>
+                        <id>webapp</id>
+                        <goals>
+                            <goal>war</goal>
+                        </goals>
+                        <phase>package</phase>
+                    </execution>
+                </executions>
+            </plugin>
         </plugins>
     </build>
 </project>

+ 55 - 45
frameworks/Java/sabina/src/main/java/sabina/benchmark/Application.java

@@ -16,25 +16,18 @@ package sabina.benchmark;
 
 import static java.lang.Integer.parseInt;
 import static java.lang.System.getProperty;
-import static sabina.Sabina.*;
 import static sabina.content.JsonContent.toJson;
 import static sabina.view.MustacheView.renderMustache;
 
 import sabina.Request;
-import sabina.server.MatcherFilter;
+import sabina.server.ServletApplication;
 
 import java.util.*;
 import java.util.Date;
-import javax.servlet.FilterConfig;
-import javax.servlet.annotation.WebFilter;
+import javax.servlet.annotation.WebListener;
 
-/**
- * .
- */
-@WebFilter ("/*")
-final class Application extends MatcherFilter {
+@WebListener public final class Application extends sabina.Application {
     static final String SETTINGS_RESOURCE = "/server.properties";
-    static final Repository REPOSITORY = loadRepository ();
     static final int DB_ROWS = 10000;
 
     private static final String MESSAGE = "Hello, World!";
@@ -42,10 +35,12 @@ final class Application extends MatcherFilter {
     private static final String CONTENT_TYPE_JSON = "application/json";
     private static final String QUERIES_PARAM = "queries";
 
+    static Repository repository = loadRepository ();
+
     static Properties loadConfiguration () {
         try {
             Properties settings = new Properties ();
-            settings.load (Class.class.getResourceAsStream (SETTINGS_RESOURCE));
+            settings.load (Application.class.getResourceAsStream (SETTINGS_RESOURCE));
             return settings;
         }
         catch (Exception ex) {
@@ -54,7 +49,7 @@ final class Application extends MatcherFilter {
     }
 
     static Repository loadRepository () {
-        switch (getProperty ("sabina.benchmark.repository", "mysql")) {
+        switch (getProperty ("sabina.benchmark.repository", "mongodb")) {
             case "mongodb":
                 return new MongoDbRepository (loadConfiguration ());
             case "mysql":
@@ -63,34 +58,46 @@ final class Application extends MatcherFilter {
         }
     }
 
-    private static Object getDb (Request it) {
+    private Object getDb (Request it) {
         try {
-            final World[] worlds = REPOSITORY.getWorlds (getQueries (it), false);
+            final World[] worlds = repository.getWorlds (getQueries (it), false);
             it.response.type (CONTENT_TYPE_JSON);
             return toJson (it.queryParams (QUERIES_PARAM) == null? worlds[0] : worlds);
         }
-        catch (Exception e){
+        catch (Exception e) {
             e.printStackTrace ();
-            throw e;
+            return e.getMessage ();
         }
     }
 
-    private static Object getFortunes (Request it) {
-        List<Fortune> fortunes = REPOSITORY.getFortunes ();
-        fortunes.add (new Fortune (0, "Additional fortune added at request time."));
-        fortunes.sort ((a, b) -> a.message.compareTo (b.message));
+    private Object getFortunes (Request it) {
+        try {
+            List<Fortune> fortunes = repository.getFortunes ();
+            fortunes.add (new Fortune (0, "Additional fortune added at request time."));
+            fortunes.sort ((a, b) -> a.message.compareTo (b.message));
 
-        it.response.type ("text/html; charset=utf-8");
-        return renderMustache ("/fortunes.mustache", fortunes);
+            it.response.type ("text/html; charset=utf-8");
+            return renderMustache ("fortunes.mustache", fortunes);
+        }
+        catch (Exception e) {
+            e.printStackTrace ();
+            return e.getMessage ();
+        }
     }
 
-    private static Object getUpdates (Request it) {
-        World[] worlds = REPOSITORY.getWorlds (getQueries (it), true);
-        it.response.type (CONTENT_TYPE_JSON);
-        return toJson (it.queryParams (QUERIES_PARAM) == null? worlds[0] : worlds);
+    private Object getUpdates (Request it) {
+        try {
+            World[] worlds = repository.getWorlds (getQueries (it), true);
+            it.response.type (CONTENT_TYPE_JSON);
+            return toJson (it.queryParams (QUERIES_PARAM) == null? worlds[0] : worlds);
+        }
+        catch (Exception e) {
+            e.printStackTrace ();
+            return e.getMessage ();
+        }
     }
 
-    private static int getQueries (final Request request) {
+    private int getQueries (final Request request) {
         try {
             String parameter = request.queryParams (QUERIES_PARAM);
             if (parameter == null)
@@ -109,41 +116,44 @@ final class Application extends MatcherFilter {
         }
     }
 
-    private static Object getPlaintext (Request it) {
+    private Object getPlaintext (Request it) {
         it.response.type (CONTENT_TYPE_TEXT);
         return MESSAGE;
     }
 
-    private static Object getJson (Request it) {
+    private Object getJson (Request it) {
         it.response.type (CONTENT_TYPE_JSON);
         return toJson (new Message ());
     }
 
-    private static void addCommonHeaders (Request it) {
+    private void addCommonHeaders (Request it) {
         it.header ("Server", "Undertow/1.1.2");
         it.response.addDateHeader ("Date", new Date ().getTime ());
     }
 
-    private static void routes () {
-        get ("/json", Application::getJson);
-        get ("/db", Application::getDb);
-        get ("/query", Application::getDb);
-        get ("/fortune", Application::getFortunes);
-        get ("/update", Application::getUpdates);
-        get ("/plaintext", Application::getPlaintext);
-        after (Application::addCommonHeaders);
-    }
-
-    public static void main (String[] args) {
+    public Application () {
         routes ();
 
         Properties settings = loadConfiguration ();
-        host (settings.getProperty ("web.host"));
-        port (settings.getProperty ("web.port"));
+
+        bind (settings.getProperty ("web.host"));
+        port (parseInt (settings.getProperty ("web.port")));
+
         start ();
     }
 
-    @Override protected void routes (FilterConfig filterConfig) {
-        routes ();
+    public static void main (String[] args) {
+        new Application ();
+    }
+
+//    @Override
+    protected void routes () {
+        get ("/json", this::getJson);
+        get ("/db", this::getDb);
+        get ("/query", this::getDb);
+        get ("/fortune", this::getFortunes);
+        get ("/update", this::getUpdates);
+        get ("/plaintext", this::getPlaintext);
+        after (this::addCommonHeaders);
     }
 }

+ 13 - 14
frameworks/Java/sabina/src/main/java/sabina/benchmark/MongoDbRepository.java

@@ -47,8 +47,12 @@ final class MongoDbRepository implements Repository {
         List<Fortune> fortunes = new ArrayList<> ();
 
         fortuneCollection.find ().forEach ((Block<Document>)doc ->
-            fortunes.add (new Fortune (doc.get ("_id", Double.class).intValue (), (String)doc.get
-                ("message")))
+            fortunes.add (
+                new Fortune (
+                    doc.get ("_id", Number.class).intValue (),
+                    (String)doc.get ("message")
+                )
+            )
         );
 
         return fortunes;
@@ -67,24 +71,19 @@ final class MongoDbRepository implements Repository {
     }
 
     private World findWorld (int id) {
-        return createWorld (worldCollection.find(eq ("_id", (double)id)).first ());
+        return createWorld (worldCollection.find(eq ("_id", id)).first ());
     }
 
     private World createWorld (Document world) {
-        try {
-            return new World (world.get ("_id", Double.class).intValue (), world.get
-                ("randomNumber", Double.class).intValue ());
-        }
-        catch (ClassCastException e) {
-            return new World (world.get ("_id", Double.class).intValue (), world.get
-                ("randomNumber", Integer.class));
-        }
+        return new World (
+            world.get ("_id", Number.class).intValue (),
+            world.get ("randomNumber", Number.class).intValue ()
+        );
     }
 
     public World updateWorld (int id, int random) {
-        Document newWorld = new Document ("_id", (double)id).append ("randomNumber", (double)
-            random);
-        worldCollection.replaceOne (eq ("_id", (double)id), newWorld);
+        Document newWorld = new Document ("_id", id).append ("randomNumber", random);
+        worldCollection.replaceOne (eq ("_id", id), newWorld);
 
         return new World (id, random);
     }

+ 8 - 8
frameworks/Java/sabina/src/main/resources/server.properties

@@ -12,16 +12,16 @@
 # and limitations under the License.
 #
 
-web.port = 5050
-web.host = 0.0.0.0
+web.port=5050
+web.host=0.0.0.0
 
-mongodb.host = ${db.host}
-mongodb.port = 27017
-mongodb.database = hello_world
-mongodb.world.collection = world
-mongodb.fortune.collection = fortune
+mongodb.host=${db.host}
+mongodb.port=27017
+mongodb.database=hello_world
+mongodb.world.collection=world
+mongodb.fortune.collection=fortune
 
-mysql.uri = jdbc:mysql://${db.host}:3306/hello_world?\
+mysql.uri=jdbc:mysql://${db.host}:3306/hello_world?\
 user=benchmarkdbuser&\
 password=benchmarkdbpass&\
 jdbcCompliantTruncation=false&\

+ 26 - 47
frameworks/Java/sabina/src/test/java/sabina/benchmark/ApplicationTest.java

@@ -15,8 +15,6 @@
 package sabina.benchmark;
 
 import static org.apache.http.client.fluent.Request.Get;
-import static org.testng.AssertJUnit.*;
-import static sabina.Sabina.stop;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -34,150 +32,131 @@ import org.testng.annotations.Test;
  * <p>TODO
  * Write article about stress test with TestNG (scenarios, combine different tests in scenarios,
  * adding random pauses...)
- *
- * <p>TODO Change assert's order
  */
-public final class ApplicationTest {
-    private static final int THREADS = 1, EXECUTIONS = 1;
-
+@Test public final class ApplicationTest {
     private static final String ENDPOINT = "http://localhost:5050";
     private static final Gson GSON = new Gson ();
 
-    @BeforeClass public static void setup () {
-        Application.main (null);
+    private static Application application;
+
+    @BeforeClass public void setup () throws IOException {
+        application = new Application ();
     }
 
-    @AfterClass public static void close () {
-        stop ();
+    @AfterClass public void close () {
+        application.stop ();
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
     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"));
+        assert "Hello, World!".equals (GSON.fromJson (content, Map.class).get ("message"));
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
     public void plaintext () throws IOException {
         HttpResponse response = get (ENDPOINT + "/plaintext");
         String content = getContent (response);
 
         checkResponse (response, content, "text/plain");
-        assertEquals ("Hello, World!", content);
+        assert "Hello, World!".equals (content);
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
     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"));
+        assert resultsMap.containsKey ("id") && resultsMap.containsKey ("randomNumber");
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
     public void empty_query_parameter () throws IOException {
         checkDbRequest ("/query?queries", 1);
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
     public void text_query_parameter () throws IOException {
         checkDbRequest ("/query?queries=text", 1);
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
     public void zero_queries () throws IOException {
         checkDbRequest ("/query?queries=0", 1);
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
     public void one_thousand_queries () throws IOException {
         checkDbRequest ("/query?queries=1000", 500);
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
     public void one_query () throws IOException {
         checkDbRequest ("/query?queries=1", 1);
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
     public void ten_queries () throws IOException {
         checkDbRequest ("/query?queries=10", 10);
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
     public void one_hundred_queries () throws IOException {
         checkDbRequest ("/query?queries=100", 100);
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
     public void five_hundred_queries () throws IOException {
         checkDbRequest ("/query?queries=500", 500);
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
+    public void five_hundred_and_one_queries () throws IOException {
+        checkDbRequest ("/query?queries=501", 500);
+    }
+
     public void fortunes () throws IOException {
         HttpResponse response = get (ENDPOINT + "/fortune");
         String content = getContent (response);
         String contentType = response.getEntity ().getContentType ().getValue ();
 
-        assertTrue (response.getFirstHeader ("Server") != null);
-        assertTrue (response.getFirstHeader ("Date") != null);
-        assertTrue (content.contains ("&lt;script&gt;alert(&quot;This should not be displayed"));
-        assertTrue (content.contains ("フレームワークのベンチマーク"));
-        assertEquals ("text/html; charset=utf-8", contentType.toLowerCase ());
+        assert response.getFirstHeader ("Server") != null;
+        assert response.getFirstHeader ("Date") != null;
+        assert content.contains ("&lt;script&gt;alert(&quot;This should not be displayed");
+        assert content.contains ("フレームワークのベンチマーク");
+        assert "text/html; charset=utf-8".equals (contentType.toLowerCase ());
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
     public void no_updates_parameter () throws IOException {
         HttpResponse response = get (ENDPOINT + "/update");
         String content = getContent (response);
 
         checkResponse (response, content, "application/json");
         Map<?, ?> resultsMap = GSON.fromJson (content, Map.class);
-        assertTrue (resultsMap.containsKey ("id") && resultsMap.containsKey ("randomNumber"));
+        assert resultsMap.containsKey ("id") && resultsMap.containsKey ("randomNumber");
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
     public void empty_updates_parameter () throws IOException {
         checkDbRequest ("/update?queries", 1);
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
     public void text_updates_parameter () throws IOException {
         checkDbRequest ("/update?queries=text", 1);
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
     public void zero_updates () throws IOException {
         checkDbRequest ("/update?queries=0", 1);
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
     public void one_thousand_updates () throws IOException {
         checkDbRequest ("/update?queries=1000", 500);
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
     public void one_update () throws IOException {
         checkDbRequest ("/update?queries=1", 1);
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
     public void ten_updates () throws IOException {
         checkDbRequest ("/update?queries=10", 10);
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
     public void one_hundred_updates () throws IOException {
         checkDbRequest ("/update?queries=100", 100);
     }
 
-    @Test(threadPoolSize = THREADS, invocationCount = EXECUTIONS)
     public void five_hundred_updates () throws IOException {
         checkDbRequest ("/update?queries=500", 500);
     }
@@ -200,19 +179,19 @@ public final class ApplicationTest {
     }
 
     private void checkResponse (HttpResponse res, String content, String contentType) {
-        assertTrue (res.getFirstHeader ("Server") != null);
-        assertTrue (res.getFirstHeader ("Date") != null);
-        assertEquals (content.length (), res.getEntity ().getContentLength ());
-        assertTrue (res.getEntity ().getContentType ().getValue ().contains (contentType));
+        assert res.getFirstHeader ("Server") != null;
+        assert res.getFirstHeader ("Date") != null;
+        assert content.length () == res.getEntity ().getContentLength ();
+        assert res.getEntity ().getContentType ().getValue ().contains (contentType);
     }
 
     private void checkResultItems (String result, int size) {
         List<?> resultsList = GSON.fromJson (result, List.class);
-        assertEquals (size, resultsList.size ());
+        assert size == resultsList.size ();
 
         for (int ii = 0; ii < size; ii++) {
             Map<?, ?> r = (Map)resultsList.get (ii);
-            assertTrue (r.containsKey ("id") && r.containsKey ("randomNumber"));
+            assert r.containsKey ("id") && r.containsKey ("randomNumber");
         }
     }
 }