From 2c56471ef99ac39e305cb0e360d1f6646ff503e2 Mon Sep 17 00:00:00 2001 From: Nykolas Lima Date: Tue, 8 Dec 2015 10:38:40 -0200 Subject: [PATCH 1/3] letting web server commit the response naturally --- .../vraptor/serialization/gson/GsonJSONPSerialization.java | 1 - .../caelum/vraptor/serialization/gson/GsonSerializer.java | 7 ++----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonJSONPSerialization.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonJSONPSerialization.java index 0b021166a..e703b9cbd 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonJSONPSerialization.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonJSONPSerialization.java @@ -74,7 +74,6 @@ public void serialize() { response.getWriter().append(callbackName).append("("); super.serialize(); response.getWriter().append(")"); - response.getWriter().flush(); } catch (IOException e) { throw new ResultException("Unable to serialize data", e); } diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonSerializer.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonSerializer.java index 58e134c88..8919c2ac7 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonSerializer.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonSerializer.java @@ -15,7 +15,6 @@ */ package br.com.caelum.vraptor.serialization.gson; -import static com.google.common.io.Flushables.flushQuietly; import static java.util.Collections.singletonMap; import static java.util.Objects.requireNonNull; @@ -29,13 +28,13 @@ import javax.enterprise.inject.Vetoed; +import com.google.gson.Gson; + import br.com.caelum.vraptor.core.ReflectionProvider; import br.com.caelum.vraptor.interceptor.TypeNameExtractor; import br.com.caelum.vraptor.serialization.Serializer; import br.com.caelum.vraptor.serialization.SerializerBuilder; -import com.google.gson.Gson; - /** * A SerializerBuilder based on Gson. * @@ -144,8 +143,6 @@ public void serialize() { } else { gson.toJson(singletonMap(alias, root), writer); } - - flushQuietly(writer); } @Override From c7381e3153c059844407008db1573ce04e141c1d Mon Sep 17 00:00:00 2001 From: Nykolas Lima Date: Tue, 8 Dec 2015 14:08:43 -0200 Subject: [PATCH 2/3] fixing tests that depends on flush the writter --- .../util/test/MockHttpServletResponse.java | 1 + .../util/test/MockSerializationResult.java | 6 +- .../I18nMessageSerializationTest.java | 13 ++-- .../gson/GsonJSONSerializationTest.java | 69 +++++++++++++++---- 4 files changed, 68 insertions(+), 21 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/util/test/MockHttpServletResponse.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/util/test/MockHttpServletResponse.java index dfe9bb5e1..07a8d64cd 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/util/test/MockHttpServletResponse.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/util/test/MockHttpServletResponse.java @@ -54,6 +54,7 @@ public PrintWriter getWriter() { } public String getContentAsString() { + writer.flush(); return this.content.toString(); } diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/util/test/MockSerializationResult.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/util/test/MockSerializationResult.java index c0fe9c040..4bf10a6fc 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/util/test/MockSerializationResult.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/util/test/MockSerializationResult.java @@ -22,6 +22,9 @@ import javax.enterprise.inject.Vetoed; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonSerializer; + import br.com.caelum.vraptor.View; import br.com.caelum.vraptor.core.DefaultReflectionProvider; import br.com.caelum.vraptor.core.ReflectionProvider; @@ -45,9 +48,6 @@ import br.com.caelum.vraptor.serialization.xstream.XStreamXMLSerialization; import br.com.caelum.vraptor.view.EmptyResult; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonSerializer; - /** * * A mocked Result for testing your serialized objects returns. diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/I18nMessageSerializationTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/I18nMessageSerializationTest.java index 9b225e941..4363681bb 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/I18nMessageSerializationTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/I18nMessageSerializationTest.java @@ -34,6 +34,9 @@ import org.junit.Before; import org.junit.Test; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonSerializer; + import br.com.caelum.vraptor.core.DefaultReflectionProvider; import br.com.caelum.vraptor.environment.Environment; import br.com.caelum.vraptor.interceptor.DefaultTypeNameExtractor; @@ -50,12 +53,11 @@ import br.com.caelum.vraptor.util.test.MockInstanceImpl; import br.com.caelum.vraptor.validator.SingletonResourceBundle; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonSerializer; - public class I18nMessageSerializationTest { + private I18nMessageSerialization serialization; private ByteArrayOutputStream stream; + private PrintWriter writer; @Before public void setup() throws Exception { @@ -64,7 +66,8 @@ public void setup() throws Exception { Environment environment = mock(Environment.class); HttpServletResponse response = mock(HttpServletResponse.class); - when(response.getWriter()).thenReturn(new PrintWriter(stream)); + writer = new PrintWriter(stream); + when(response.getWriter()).thenReturn(writer); DefaultTypeNameExtractor extractor = new DefaultTypeNameExtractor(); XStreamBuilder builder = XStreamBuilderImpl.cleanInstance(new MessageConverter()); XStreamXMLSerialization xmlSerialization = new XStreamXMLSerialization(response, builder, environment); @@ -91,6 +94,8 @@ public void setup() throws Exception { public void shouldCallJsonSerialization() { String expectedResult = "{\"message\":{\"category\":\"success\",\"message\":\"Just another {0} in {1}\"}}"; serialization.from("success", "message.cat").as(json()); + writer.flush(); + assertThat(result(), is(equalTo(expectedResult))); } diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonJSONSerializationTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonJSONSerializationTest.java index 1542bdb15..d0f83849b 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonJSONSerializationTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonJSONSerializationTest.java @@ -25,6 +25,7 @@ import static org.mockito.Mockito.when; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; @@ -40,15 +41,6 @@ import org.junit.Before; import org.junit.Test; -import br.com.caelum.vraptor.core.DefaultReflectionProvider; -import br.com.caelum.vraptor.environment.Environment; -import br.com.caelum.vraptor.interceptor.DefaultTypeNameExtractor; -import br.com.caelum.vraptor.serialization.JSONPSerialization; -import br.com.caelum.vraptor.serialization.JSONSerialization; -import br.com.caelum.vraptor.serialization.Serializee; -import br.com.caelum.vraptor.serialization.SkipSerialization; -import br.com.caelum.vraptor.util.test.MockInstanceImpl; - import com.google.common.collect.ForwardingCollection; import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; @@ -60,6 +52,15 @@ import com.google.gson.JsonSerializer; import com.google.gson.annotations.Since; +import br.com.caelum.vraptor.core.DefaultReflectionProvider; +import br.com.caelum.vraptor.environment.Environment; +import br.com.caelum.vraptor.interceptor.DefaultTypeNameExtractor; +import br.com.caelum.vraptor.serialization.JSONPSerialization; +import br.com.caelum.vraptor.serialization.JSONSerialization; +import br.com.caelum.vraptor.serialization.Serializee; +import br.com.caelum.vraptor.serialization.SkipSerialization; +import br.com.caelum.vraptor.util.test.MockInstanceImpl; + public class GsonJSONSerializationTest { private GsonJSONSerialization serialization; @@ -67,6 +68,7 @@ public class GsonJSONSerializationTest { private HttpServletResponse response; private DefaultTypeNameExtractor extractor; private Environment environment; + private PrintWriter writer; private GsonSerializerBuilder builder; @@ -77,7 +79,8 @@ public void setup() throws Exception { stream = new ByteArrayOutputStream(); response = mock(HttpServletResponse.class); - when(response.getWriter()).thenReturn(new PrintWriter(stream)); + writer = new PrintWriter(stream); + when(response.getWriter()).thenReturn(writer); extractor = new DefaultTypeNameExtractor(); environment = mock(Environment.class); @@ -237,8 +240,8 @@ public void shouldSerializeGenericClass() { GenericWrapper wrapper = new GenericWrapper<>(entityList, entityList.size()); - // serialization.from(wrapper).include("entityList").include("entityList.name").serialize(); serialization.from(wrapper).include("entityList").serialize(); + writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -248,6 +251,7 @@ public void shouldSerializeAllBasicFields() { String expectedResult = "{\"order\":{\"price\":15.0,\"comments\":\"pack it nicely, please\"}}"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(order).serialize(); + writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -256,6 +260,7 @@ public void shouldSerializeAllBasicFieldsIndented() { String expectedResult = "{\n \"order\": {\n \"price\": 15.0,\n \"comments\": \"pack it nicely, please\"\n }\n}"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.indented().from(order).serialize(); + writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -267,6 +272,7 @@ public void shouldIndentedWhenEnvironmentReturnsTrue() { String expectedResult = "{\n \"order\": {\n \"price\": 15.0,\n \"comments\": \"pack it nicely, please\"\n }\n}"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(order).serialize(); + writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -278,6 +284,7 @@ public void shouldNotIndentedWhenEnvironmentReturnsFalse() { String expectedResult = "{\"order\":{\"price\":15.0,\"comments\":\"pack it nicely, please\"}}"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(order).serialize(); + writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -286,6 +293,7 @@ public void shouldUseAlias() { String expectedResult = "{\"customOrder\":{\"price\":15.0,\"comments\":\"pack it nicely, please\"}}"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(order, "customOrder").serialize(); + writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -310,6 +318,7 @@ public void shouldSerializeEnumFields() { Order order = new BasicOrder(new Client("guilherme silveira"), 15.0, "pack it nicely, please", Type.basic); serialization.from(order).serialize(); + writer.flush(); String result = result(); assertThat(result, containsString("\"type\":\"basic\"")); } @@ -322,6 +331,7 @@ public void shouldSerializeCollection() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(Arrays.asList(order, order)).serialize(); + writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -332,6 +342,7 @@ public void shouldSerializeCollectionWithPrefixTag() { expectedResult = "{\"orders\":[" + expectedResult + "]}"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(Arrays.asList(order, order), "orders").serialize(); + writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -340,6 +351,7 @@ public void shouldExcludeNonPrimitiveFieldsFromACollection() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item( "name", 12.99)); serialization.from(Arrays.asList(order, order), "orders").exclude("price").serialize(); + writer.flush(); assertThat(result(), not(containsString("\"items\""))); assertThat(result(), not(containsString("name"))); @@ -353,6 +365,7 @@ public void shouldExcludeOnlyOmmitedFields() { User user = new User("caelum", "pwd12345", new UserPrivateInfo("123.456.789-00", "+55 (11) 1111-1111")); serialization.from(user).recursive().serialize(); + writer.flush(); assertThat(result(), not(containsString("\"pwd12345\""))); assertThat(result(), not(containsString("password"))); @@ -365,6 +378,7 @@ public void shouldExcludeOmmitedClasses() { User user = new User("caelum", "pwd12345", new UserPrivateInfo("123.456.789-00", "+55 (11) 1111-1111")); serialization.from(user).recursive().serialize(); + writer.flush(); assertThat(result(), not(containsString("info"))); assertThat(result(), not(containsString("cpf"))); @@ -381,6 +395,7 @@ public void shouldSerializeParentFields() { // "complex package15.0pack it nicely, please"; Order order = new AdvancedOrder(null, 15.0, "pack it nicely, please", "complex package"); serialization.from(order).serialize(); + writer.flush(); assertThat(result(), containsString("\"notes\":\"complex package\"")); } @@ -392,6 +407,7 @@ public void shouldExcludeNonPrimitiveParentFields() { advanced.order = new AdvancedOrder(new Client("john"), 15.0, "pack it nicely, please", "complex package"); serialization.from(advanced).include("order").serialize(); + writer.flush(); assertThat(result(), not(containsString("\"client\""))); } @@ -399,6 +415,7 @@ public void shouldExcludeNonPrimitiveParentFields() { public void shouldExcludeParentFields() { Order order = new AdvancedOrder(null, 15.0, "pack it nicely, please", "complex package"); serialization.from(order).exclude("comments").serialize(); + writer.flush(); assertThat(result(), not(containsString("\"comments\""))); } @@ -407,6 +424,7 @@ public void shouldOptionallyExcludeFields() { String expectedResult = "{\"order\":{\"comments\":\"pack it nicely, please\"}}"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(order).exclude("price").serialize(); + writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -417,6 +435,7 @@ public void shouldOptionallyIncludeFieldAndNotItsNonPrimitiveFields() { Order order = new Order(new Client("guilherme silveira", new Address("R. Vergueiro")), 15.0, "pack it nicely, please"); serialization.from(order).include("client").serialize(); + writer.flush(); assertThat(result(), containsString("\"name\":\"guilherme silveira\"")); assertThat(result(), not(containsString("R. Vergueiro"))); } @@ -428,6 +447,7 @@ public void shouldOptionallyIncludeChildField() { Order order = new Order(new Client("guilherme silveira", new Address("R. Vergueiro")), 15.0, "pack it nicely, please"); serialization.from(order).include("client", "client.address").serialize(); + writer.flush(); assertThat(result(), containsString("\"street\":\"R. Vergueiro\"")); } @@ -437,6 +457,7 @@ public void shouldOptionallyExcludeChildField() { // " 15.0pack it nicely, please"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(order).include("client").exclude("client.name").serialize(); + writer.flush(); assertThat(result(), containsString("\"client\"")); assertThat(result(), not(containsString("guilherme silveira"))); } @@ -448,6 +469,7 @@ public void shouldOptionallyIncludeListChildFields() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item( "any item", 12.99)); serialization.from(order).include("items").serialize(); + writer.flush(); assertThat(result(), containsString("\"items\"")); assertThat(result(), containsString("\"name\":\"any item\"")); assertThat(result(), containsString("\"price\":12.99")); @@ -460,6 +482,7 @@ public void shouldExcludeAllPrimitiveFieldsInACollection() { orders.add(new Order(new Client("nykolas lima"), 15.0, "gift bags, please")); orders.add(new Order(new Client("Rafael Dipold"), 15.0, "gift bags, please")); serialization.from(orders).excludeAll().serialize(); + writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -470,6 +493,7 @@ public void shouldOptionallyExcludeFieldsFromIncludedListChildFields() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item( "any item", 12.99)); serialization.from(order).include("items").exclude("items.price").serialize(); + writer.flush(); assertThat(result(), containsString("\"items\"")); assertThat(result(), containsString("\"name\":\"any item\"")); assertThat(result(), not(containsString("12.99"))); @@ -480,6 +504,7 @@ public void shouldOptionallyRemoveRoot() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item( "any item", 12.99)); serialization.withoutRoot().from(order).include("items").exclude("items.price").serialize(); + writer.flush(); assertThat(result(), containsString("\"items\"")); assertThat(result(), containsString("\"name\":\"any item\"")); assertThat(result(), not(containsString("12.99"))); @@ -491,13 +516,18 @@ public void shouldOptionallyRemoveRootIndented() { String expected = "{\n \"price\": 15.0,\n \"comments\": \"pack it nicely, please\",\n \"items\": [\n {\n \"name\": \"any item\"\n }\n ]\n}"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item( "any item", 12.99)); - serialization.indented().withoutRoot().from(order).include("items").exclude("items.price") - .serialize(); + serialization.indented().withoutRoot().from(order).include("items").exclude("items.price").serialize(); + writer.flush(); assertThat(result(), equalTo(expected)); } private String result() { - return new String(stream.toByteArray()); + try { + stream.flush(); + return new String(stream.toByteArray()); + } catch (IOException e) { + throw new RuntimeException("Error flushing stream", e); + } } static class MyCollection extends ForwardingCollection { @@ -528,6 +558,7 @@ public void shouldUseCollectionConverterWhenItExists() { String expectedResult = "[\"testing\"]"; serialization.withoutRoot().from(new MyCollection()).serialize(); + writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -538,6 +569,7 @@ public void shouldSerializeCalendarTimeWithISO8601() { c.included.setTimeZone(TimeZone.getTimeZone("GMT-0300")); serialization.from(c).serialize(); + writer.flush(); String result = result(); String expectedResult = "{\"client\":{\"name\":\"renan\",\"included\":\"2012-09-03T01:05:09-03:00\"}}"; @@ -549,6 +581,7 @@ public void shouldSerializeCalendarTimeWithISO8601() { public void shouldSerializeDateWithISO8601() { Date date = new GregorianCalendar(1988, 0, 25, 1, 30, 15).getTime(); serialization.from(date).serialize(); + writer.flush(); String expectedResult = "{\"date\":\"1988-01-25T01:30:15-0300\"}"; assertThat(result(), is(equalTo(expectedResult))); } @@ -558,6 +591,7 @@ public void shouldExcludeAllPrimitiveFields() { String expectedResult = "{\"order\":{}}"; Order order = new Order(new Client("nykolas lima"), 15.0, "gift bags, please"); serialization.from(order).excludeAll().serialize(); + writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -566,6 +600,7 @@ public void shouldExcludeAllPrimitiveParentFields() { String expectedResult = "{\"advancedOrder\":{}}"; Order order = new AdvancedOrder(null, 15.0, "pack it nicely, please", "complex package"); serialization.from(order).excludeAll().serialize(); + writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -574,6 +609,7 @@ public void shouldExcludeAllThanIncludeAndSerialize() { String expectedResult = "{\"order\":{\"price\":15.0}}"; Order order = new Order(new Client("nykolas lima"), 15.0, "gift bags, please"); serialization.from(order).excludeAll().include("price").serialize(); + writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -585,6 +621,7 @@ public void shouldSerializeWithCallback() { String expectedResult = "calculate({\"order\":{\"price\":15.0}})"; Order order = new Order(new Client("nykolas lima"), 15.0, "gift bags, please"); serialization.withCallback("calculate").from(order).excludeAll().include("price").serialize(); + writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -596,6 +633,7 @@ public void shouldSerializeVersionedJsonFieldsWithSinceAnnotation() { String expectedResult = "{\"client\":{\"name\":\"adolfo eloy\"}}"; Client client = new Client("adolfo eloy"); serialization.version(1.0).from(client).serialize(); + writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -608,6 +646,7 @@ public void shouldSerializeRecursivelyVersionedJsonFields() { Client client = new Client("adolfo eloy", new Address("antonio agu", "osasco")); serialization.version(1.0).from(client).recursive().serialize(); + writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -615,6 +654,7 @@ public void shouldSerializeRecursivelyVersionedJsonFields() { public void shouldSerializeNullfieldswhenRequested(){ Address address = new Address("Alameda street", null); serialization.serializeNulls().from(address).serialize(); + writer.flush(); String expectedResult = "{\"address\":{\"street\":\"Alameda street\",\"city\":null}}"; assertThat(result(), is(equalTo(expectedResult))); @@ -624,6 +664,7 @@ public void shouldSerializeNullfieldswhenRequested(){ public void shouldNotSerializeNullFieldsByDefault(){ Address address = new Address("Alameda street", null); serialization.from(address).serialize(); + writer.flush(); String expectedResult = "{\"address\":{\"street\":\"Alameda street\"}}"; assertThat(result(), is(equalTo(expectedResult))); From 9068875584cb3ecc459c097ea77b50fac6a56c22 Mon Sep 17 00:00:00 2001 From: Nykolas Lima Date: Tue, 8 Dec 2015 14:21:53 -0200 Subject: [PATCH 3/3] better flush strategy for tests --- .../gson/GsonJSONSerializationTest.java | 56 ++++++------------- 1 file changed, 17 insertions(+), 39 deletions(-) diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonJSONSerializationTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonJSONSerializationTest.java index d0f83849b..c70290a44 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonJSONSerializationTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonJSONSerializationTest.java @@ -26,6 +26,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; @@ -68,8 +69,6 @@ public class GsonJSONSerializationTest { private HttpServletResponse response; private DefaultTypeNameExtractor extractor; private Environment environment; - private PrintWriter writer; - private GsonSerializerBuilder builder; @Before @@ -79,8 +78,7 @@ public void setup() throws Exception { stream = new ByteArrayOutputStream(); response = mock(HttpServletResponse.class); - writer = new PrintWriter(stream); - when(response.getWriter()).thenReturn(writer); + when(response.getWriter()).thenReturn(new AlwaysFlushWriter(stream)); extractor = new DefaultTypeNameExtractor(); environment = mock(Environment.class); @@ -96,6 +94,20 @@ public void setup() throws Exception { serialization = new GsonJSONSerialization(response, extractor, builder, environment, new DefaultReflectionProvider()); } + private static class AlwaysFlushWriter extends PrintWriter { + + public AlwaysFlushWriter(OutputStream out) { + super(out); + } + + @Override + public void write(String s) { + super.write(s); + super.flush(); + } + + } + @SkipSerialization public static class UserPrivateInfo { String cpf; @@ -241,7 +253,6 @@ public void shouldSerializeGenericClass() { GenericWrapper wrapper = new GenericWrapper<>(entityList, entityList.size()); serialization.from(wrapper).include("entityList").serialize(); - writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -251,7 +262,6 @@ public void shouldSerializeAllBasicFields() { String expectedResult = "{\"order\":{\"price\":15.0,\"comments\":\"pack it nicely, please\"}}"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(order).serialize(); - writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -260,7 +270,6 @@ public void shouldSerializeAllBasicFieldsIndented() { String expectedResult = "{\n \"order\": {\n \"price\": 15.0,\n \"comments\": \"pack it nicely, please\"\n }\n}"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.indented().from(order).serialize(); - writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -272,7 +281,6 @@ public void shouldIndentedWhenEnvironmentReturnsTrue() { String expectedResult = "{\n \"order\": {\n \"price\": 15.0,\n \"comments\": \"pack it nicely, please\"\n }\n}"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(order).serialize(); - writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -284,7 +292,6 @@ public void shouldNotIndentedWhenEnvironmentReturnsFalse() { String expectedResult = "{\"order\":{\"price\":15.0,\"comments\":\"pack it nicely, please\"}}"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(order).serialize(); - writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -293,7 +300,6 @@ public void shouldUseAlias() { String expectedResult = "{\"customOrder\":{\"price\":15.0,\"comments\":\"pack it nicely, please\"}}"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(order, "customOrder").serialize(); - writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -318,7 +324,6 @@ public void shouldSerializeEnumFields() { Order order = new BasicOrder(new Client("guilherme silveira"), 15.0, "pack it nicely, please", Type.basic); serialization.from(order).serialize(); - writer.flush(); String result = result(); assertThat(result, containsString("\"type\":\"basic\"")); } @@ -331,7 +336,6 @@ public void shouldSerializeCollection() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(Arrays.asList(order, order)).serialize(); - writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -342,7 +346,6 @@ public void shouldSerializeCollectionWithPrefixTag() { expectedResult = "{\"orders\":[" + expectedResult + "]}"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(Arrays.asList(order, order), "orders").serialize(); - writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -351,7 +354,6 @@ public void shouldExcludeNonPrimitiveFieldsFromACollection() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item( "name", 12.99)); serialization.from(Arrays.asList(order, order), "orders").exclude("price").serialize(); - writer.flush(); assertThat(result(), not(containsString("\"items\""))); assertThat(result(), not(containsString("name"))); @@ -365,7 +367,6 @@ public void shouldExcludeOnlyOmmitedFields() { User user = new User("caelum", "pwd12345", new UserPrivateInfo("123.456.789-00", "+55 (11) 1111-1111")); serialization.from(user).recursive().serialize(); - writer.flush(); assertThat(result(), not(containsString("\"pwd12345\""))); assertThat(result(), not(containsString("password"))); @@ -378,7 +379,6 @@ public void shouldExcludeOmmitedClasses() { User user = new User("caelum", "pwd12345", new UserPrivateInfo("123.456.789-00", "+55 (11) 1111-1111")); serialization.from(user).recursive().serialize(); - writer.flush(); assertThat(result(), not(containsString("info"))); assertThat(result(), not(containsString("cpf"))); @@ -395,7 +395,6 @@ public void shouldSerializeParentFields() { // "complex package15.0pack it nicely, please"; Order order = new AdvancedOrder(null, 15.0, "pack it nicely, please", "complex package"); serialization.from(order).serialize(); - writer.flush(); assertThat(result(), containsString("\"notes\":\"complex package\"")); } @@ -407,7 +406,6 @@ public void shouldExcludeNonPrimitiveParentFields() { advanced.order = new AdvancedOrder(new Client("john"), 15.0, "pack it nicely, please", "complex package"); serialization.from(advanced).include("order").serialize(); - writer.flush(); assertThat(result(), not(containsString("\"client\""))); } @@ -415,7 +413,6 @@ public void shouldExcludeNonPrimitiveParentFields() { public void shouldExcludeParentFields() { Order order = new AdvancedOrder(null, 15.0, "pack it nicely, please", "complex package"); serialization.from(order).exclude("comments").serialize(); - writer.flush(); assertThat(result(), not(containsString("\"comments\""))); } @@ -424,7 +421,6 @@ public void shouldOptionallyExcludeFields() { String expectedResult = "{\"order\":{\"comments\":\"pack it nicely, please\"}}"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(order).exclude("price").serialize(); - writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -435,7 +431,6 @@ public void shouldOptionallyIncludeFieldAndNotItsNonPrimitiveFields() { Order order = new Order(new Client("guilherme silveira", new Address("R. Vergueiro")), 15.0, "pack it nicely, please"); serialization.from(order).include("client").serialize(); - writer.flush(); assertThat(result(), containsString("\"name\":\"guilherme silveira\"")); assertThat(result(), not(containsString("R. Vergueiro"))); } @@ -447,7 +442,6 @@ public void shouldOptionallyIncludeChildField() { Order order = new Order(new Client("guilherme silveira", new Address("R. Vergueiro")), 15.0, "pack it nicely, please"); serialization.from(order).include("client", "client.address").serialize(); - writer.flush(); assertThat(result(), containsString("\"street\":\"R. Vergueiro\"")); } @@ -457,7 +451,6 @@ public void shouldOptionallyExcludeChildField() { // " 15.0pack it nicely, please"; Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please"); serialization.from(order).include("client").exclude("client.name").serialize(); - writer.flush(); assertThat(result(), containsString("\"client\"")); assertThat(result(), not(containsString("guilherme silveira"))); } @@ -469,7 +462,6 @@ public void shouldOptionallyIncludeListChildFields() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item( "any item", 12.99)); serialization.from(order).include("items").serialize(); - writer.flush(); assertThat(result(), containsString("\"items\"")); assertThat(result(), containsString("\"name\":\"any item\"")); assertThat(result(), containsString("\"price\":12.99")); @@ -482,7 +474,6 @@ public void shouldExcludeAllPrimitiveFieldsInACollection() { orders.add(new Order(new Client("nykolas lima"), 15.0, "gift bags, please")); orders.add(new Order(new Client("Rafael Dipold"), 15.0, "gift bags, please")); serialization.from(orders).excludeAll().serialize(); - writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -493,7 +484,6 @@ public void shouldOptionallyExcludeFieldsFromIncludedListChildFields() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item( "any item", 12.99)); serialization.from(order).include("items").exclude("items.price").serialize(); - writer.flush(); assertThat(result(), containsString("\"items\"")); assertThat(result(), containsString("\"name\":\"any item\"")); assertThat(result(), not(containsString("12.99"))); @@ -504,7 +494,6 @@ public void shouldOptionallyRemoveRoot() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item( "any item", 12.99)); serialization.withoutRoot().from(order).include("items").exclude("items.price").serialize(); - writer.flush(); assertThat(result(), containsString("\"items\"")); assertThat(result(), containsString("\"name\":\"any item\"")); assertThat(result(), not(containsString("12.99"))); @@ -517,7 +506,6 @@ public void shouldOptionallyRemoveRootIndented() { Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item( "any item", 12.99)); serialization.indented().withoutRoot().from(order).include("items").exclude("items.price").serialize(); - writer.flush(); assertThat(result(), equalTo(expected)); } @@ -558,7 +546,6 @@ public void shouldUseCollectionConverterWhenItExists() { String expectedResult = "[\"testing\"]"; serialization.withoutRoot().from(new MyCollection()).serialize(); - writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -569,7 +556,6 @@ public void shouldSerializeCalendarTimeWithISO8601() { c.included.setTimeZone(TimeZone.getTimeZone("GMT-0300")); serialization.from(c).serialize(); - writer.flush(); String result = result(); String expectedResult = "{\"client\":{\"name\":\"renan\",\"included\":\"2012-09-03T01:05:09-03:00\"}}"; @@ -581,7 +567,6 @@ public void shouldSerializeCalendarTimeWithISO8601() { public void shouldSerializeDateWithISO8601() { Date date = new GregorianCalendar(1988, 0, 25, 1, 30, 15).getTime(); serialization.from(date).serialize(); - writer.flush(); String expectedResult = "{\"date\":\"1988-01-25T01:30:15-0300\"}"; assertThat(result(), is(equalTo(expectedResult))); } @@ -591,7 +576,6 @@ public void shouldExcludeAllPrimitiveFields() { String expectedResult = "{\"order\":{}}"; Order order = new Order(new Client("nykolas lima"), 15.0, "gift bags, please"); serialization.from(order).excludeAll().serialize(); - writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -600,7 +584,6 @@ public void shouldExcludeAllPrimitiveParentFields() { String expectedResult = "{\"advancedOrder\":{}}"; Order order = new AdvancedOrder(null, 15.0, "pack it nicely, please", "complex package"); serialization.from(order).excludeAll().serialize(); - writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -609,7 +592,6 @@ public void shouldExcludeAllThanIncludeAndSerialize() { String expectedResult = "{\"order\":{\"price\":15.0}}"; Order order = new Order(new Client("nykolas lima"), 15.0, "gift bags, please"); serialization.from(order).excludeAll().include("price").serialize(); - writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -621,7 +603,6 @@ public void shouldSerializeWithCallback() { String expectedResult = "calculate({\"order\":{\"price\":15.0}})"; Order order = new Order(new Client("nykolas lima"), 15.0, "gift bags, please"); serialization.withCallback("calculate").from(order).excludeAll().include("price").serialize(); - writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -633,7 +614,6 @@ public void shouldSerializeVersionedJsonFieldsWithSinceAnnotation() { String expectedResult = "{\"client\":{\"name\":\"adolfo eloy\"}}"; Client client = new Client("adolfo eloy"); serialization.version(1.0).from(client).serialize(); - writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -646,7 +626,6 @@ public void shouldSerializeRecursivelyVersionedJsonFields() { Client client = new Client("adolfo eloy", new Address("antonio agu", "osasco")); serialization.version(1.0).from(client).recursive().serialize(); - writer.flush(); assertThat(result(), is(equalTo(expectedResult))); } @@ -654,7 +633,6 @@ public void shouldSerializeRecursivelyVersionedJsonFields() { public void shouldSerializeNullfieldswhenRequested(){ Address address = new Address("Alameda street", null); serialization.serializeNulls().from(address).serialize(); - writer.flush(); String expectedResult = "{\"address\":{\"street\":\"Alameda street\",\"city\":null}}"; assertThat(result(), is(equalTo(expectedResult))); @@ -664,9 +642,9 @@ public void shouldSerializeNullfieldswhenRequested(){ public void shouldNotSerializeNullFieldsByDefault(){ Address address = new Address("Alameda street", null); serialization.from(address).serialize(); - writer.flush(); String expectedResult = "{\"address\":{\"street\":\"Alameda street\"}}"; assertThat(result(), is(equalTo(expectedResult))); } + }