[jackson-jaxrs-providers] 100/162: Further testing improvements

Timo Aaltonen tjaalton at moszumanska.debian.org
Mon Sep 8 22:16:32 UTC 2014


This is an automated email from the git hooks/post-receive script.

tjaalton pushed a commit to branch master
in repository jackson-jaxrs-providers.

commit 155b97d0145d82066d9ba1d1cb2b4edfbdbecd05
Author: Tatu Saloranta <tatu.saloranta at iki.fi>
Date:   Sat Oct 12 17:13:35 2013 -0700

    Further testing improvements
---
 .../jaxrs/json/dw/TestWriteModifications.java      | 75 ++++++++++++++++++++--
 1 file changed, 69 insertions(+), 6 deletions(-)

diff --git a/json/src/test/java/com/fasterxml/jackson/jaxrs/json/dw/TestWriteModifications.java b/json/src/test/java/com/fasterxml/jackson/jaxrs/json/dw/TestWriteModifications.java
index 14603a5..7bfffb5 100644
--- a/json/src/test/java/com/fasterxml/jackson/jaxrs/json/dw/TestWriteModifications.java
+++ b/json/src/test/java/com/fasterxml/jackson/jaxrs/json/dw/TestWriteModifications.java
@@ -7,6 +7,7 @@ import javax.servlet.*;
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 
@@ -22,6 +23,8 @@ import com.fasterxml.jackson.jaxrs.json.ResourceTestBase;
 public class TestWriteModifications extends ResourceTestBase
 {
     final static int TEST_PORT = 6021;
+
+    final static int TEST_PORT2 = 6022;
     
     static class Point {
         public int x, y;
@@ -43,13 +46,42 @@ public class TestWriteModifications extends ResourceTestBase
         }
     }
 
+    @Path("/point")
+    public static class IndentingResource
+    {
+        @GET
+        @Produces(MediaType.APPLICATION_JSON)
+        public Point getPoint(@QueryParam("indent") String indent) {
+            if ("true".equals(indent)) {
+                ObjectWriterInjector.set(new IndentingModifier(true));
+            } else {
+                ObjectWriterInjector.set(new IndentingModifier(false));
+            }
+            return new Point(1, 2);
+        }
+    }
+    
     public static class SimpleResourceApp extends JsonApplication {
         public SimpleResourceApp() { super(new SimpleResource()); }
     }
 
+    public static class SimpleIndentingApp extends JsonApplication {
+        public SimpleIndentingApp() { super(new IndentingResource()); }
+    }
+    
     public static class IndentingModifier extends ObjectWriterModifier
     {
         public static boolean doIndent = false;
+
+        public final Boolean _indent;
+        
+        public IndentingModifier() {
+            this(null);
+        }
+
+        public IndentingModifier(Boolean indent) {
+            _indent = indent;
+        }
         
         @Override
         public ObjectWriter modify(EndpointConfigBase<?> endpoint,
@@ -57,8 +89,14 @@ public class TestWriteModifications extends ResourceTestBase
                 Object valueToWrite, ObjectWriter w, JsonGenerator g)
             throws IOException
         {
-            if (doIndent) {
-                g.useDefaultPrettyPrinter();
+            if (_indent != null) {
+                if (_indent.booleanValue()) {
+                    g.useDefaultPrettyPrinter();
+                }
+            } else {
+                if (doIndent) {
+                    g.useDefaultPrettyPrinter();
+                }
             }
             return w;
         }
@@ -86,8 +124,12 @@ public class TestWriteModifications extends ResourceTestBase
     /* Test methods
     /**********************************************************
      */
-    
-    public void testIndentation() throws Exception
+
+    /**
+     * Test in which writer/generator modification is handled by
+     * changing state from Servlet Filter.
+     */
+    public void testIndentationWithFilter() throws Exception
     {
         // We need a filter to inject modifier that enables
         Server server = startServer(TEST_PORT, SimpleResourceApp.class,
@@ -102,12 +144,33 @@ public class TestWriteModifications extends ResourceTestBase
     
             // and then with indentation
             IndentingModifier.doIndent = true;
-            final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
-            conn.connect();
             json = readUTF8(url.openStream());
             assertEquals(aposToQuotes("{\n  'x' : 1,\n  'y' : 2\n}"), json);
         } finally {
             server.stop();
         }
     }
+
+    /**
+     * Test in which output writer/generator is modified by assignment from
+     * resource method itself.
+     */
+    public void testIndentationWithResource() throws Exception
+    {
+        // We need a filter to inject modifier that enables
+        Server server = startServer(TEST_PORT2, SimpleIndentingApp.class);
+        final String URL_BASE = "http://localhost:"+TEST_PORT2+"/point";
+
+        try {
+            // First, without indent:
+            String json = readUTF8(new URL(URL_BASE).openStream());
+            assertEquals(aposToQuotes("{'x':1,'y':2}"), json);
+    
+            // and then with indentation
+            json = readUTF8(new URL(URL_BASE + "?indent=true").openStream());
+            assertEquals(aposToQuotes("{\n  'x' : 1,\n  'y' : 2\n}"), json);
+        } finally {
+            server.stop();
+        }
+    }
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/jackson-jaxrs-providers.git



More information about the pkg-java-commits mailing list