From d6fc9563bc79d1627e1bdcfba4cc88f0f13edb81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dal=C3=AD=20Freire?= Date: Thu, 3 Mar 2016 07:43:19 -0300 Subject: [PATCH 1/9] For issue #461 FkContentType should have a Take rather than a Response. --- .../org/takes/facets/fork/FkContentType.java | 15 +++-- .../org/takes/facets/fork/TkConsumes.java | 2 +- .../takes/facets/fork/FkContentTypeTest.java | 66 +++++++++++++------ 3 files changed, 55 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/takes/facets/fork/FkContentType.java b/src/main/java/org/takes/facets/fork/FkContentType.java index 0d051fd29..349c5aff6 100644 --- a/src/main/java/org/takes/facets/fork/FkContentType.java +++ b/src/main/java/org/takes/facets/fork/FkContentType.java @@ -27,6 +27,7 @@ import lombok.EqualsAndHashCode; import org.takes.Request; import org.takes.Response; +import org.takes.Take; import org.takes.misc.Opt; import org.takes.rq.RqHeaders; @@ -40,7 +41,7 @@ * @since 1.0 * @see RsFork */ -@EqualsAndHashCode(of = { "type", "origin" }) +@EqualsAndHashCode(of = { "type", "take" }) public final class FkContentType implements Fork { /** @@ -49,25 +50,25 @@ public final class FkContentType implements Fork { private final transient MediaTypes type; /** - * Response to return. + * Take to handle the request and dynamically return the response. */ - private final transient Response origin; + private final transient Take take; /** * Ctor. * @param atype Accepted type - * @param response Response to return + * @param take Take to handle the request dynamically. */ - public FkContentType(final String atype, final Response response) { + public FkContentType(final String atype, final Take take) { this.type = new MediaTypes(atype); - this.origin = response; + this.take = take; } @Override public Opt route(final Request req) throws IOException { final Opt resp; if (FkContentType.getType(req).contains(this.type)) { - resp = new Opt.Single(this.origin); + resp = new Opt.Single(this.take.act(req)); } else { resp = new Opt.Empty(); } diff --git a/src/main/java/org/takes/facets/fork/TkConsumes.java b/src/main/java/org/takes/facets/fork/TkConsumes.java index 017e72bb9..737b21149 100644 --- a/src/main/java/org/takes/facets/fork/TkConsumes.java +++ b/src/main/java/org/takes/facets/fork/TkConsumes.java @@ -55,7 +55,7 @@ public TkConsumes(final Take take, final String type) { public Response act(final Request req) throws IOException { return new RsFork( req, - new FkContentType(type, take.act(req)) + new FkContentType(type, take) ); } } diff --git a/src/test/java/org/takes/facets/fork/FkContentTypeTest.java b/src/test/java/org/takes/facets/fork/FkContentTypeTest.java index 751376759..56e8f82ef 100644 --- a/src/test/java/org/takes/facets/fork/FkContentTypeTest.java +++ b/src/test/java/org/takes/facets/fork/FkContentTypeTest.java @@ -29,6 +29,9 @@ import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; +import org.takes.Request; +import org.takes.Response; +import org.takes.Take; import org.takes.rq.RqFake; import org.takes.rq.RqWithHeader; import org.takes.rs.RsEmpty; @@ -59,14 +62,20 @@ public final class FkContentTypeTest { @Test public void matchesWithAnyTypes() throws IOException { MatcherAssert.assertThat( - new FkContentType("text/xml", new RsEmpty()).route( - new RqWithHeader( - new RqFake(), - FkContentTypeTest.CONTENT_TYPE, - "*/* " - ) - ).has(), - Matchers.is(true) + new FkContentType( + "text/xml", new Take() { + @Override + public Response act(final Request req) throws IOException { + return new RsEmpty(); + } + }).route( + new RqWithHeader( + new RqFake(), + FkContentTypeTest.CONTENT_TYPE, + "*/* " + ) + ).has(), + Matchers.is(true) ); } @@ -78,8 +87,12 @@ public void matchesWithAnyTypes() throws IOException { public void matchesDifferentTypes() throws IOException { MatcherAssert.assertThat( new FkContentType( - "application/json charset=utf-8", new RsEmpty() - ).route( + "application/json charset=utf-8", new Take() { + @Override + public Response act(final Request req) throws IOException { + return new RsEmpty(); + } + }).route( new RqWithHeader( new RqFake(), FkContentTypeTest.CONTENT_TYPE, @@ -98,8 +111,12 @@ public void matchesDifferentTypes() throws IOException { public void matchesIdenticalTypes() throws IOException { MatcherAssert.assertThat( new FkContentType( - FkContentTypeTest.CTYPE, new RsEmpty() - ).route( + FkContentTypeTest.CTYPE, new Take() { + @Override + public Response act(final Request req) throws IOException { + return new RsEmpty(); + } + }).route( new RqWithHeader( new RqFake(), FkContentTypeTest.CONTENT_TYPE, @@ -117,12 +134,17 @@ FkContentTypeTest.CTYPE, new RsEmpty() @Test public void matchesEmptyType() throws IOException { MatcherAssert.assertThat( - new FkContentType("*/*", new RsEmpty()).route( - new RqWithHeader( - new RqFake(), FkContentTypeTest.CONTENT_TYPE, "" - ) - ).has(), - Matchers.is(true) + new FkContentType( + "*/*", new Take() { + @Override + public Response act(final Request req) throws IOException { + return new RsEmpty(); + } + }).route( + new RqWithHeader( + new RqFake(), FkContentTypeTest.CONTENT_TYPE, "" + ) + ).has(), Matchers.is(true) ); } @@ -134,8 +156,12 @@ public void matchesEmptyType() throws IOException { public void matchesDifferentEncodingsTypes() throws IOException { MatcherAssert.assertThat( new FkContentType( - FkContentTypeTest.CTYPE, new RsEmpty() - ).route( + FkContentTypeTest.CTYPE, new Take() { + @Override + public Response act(final Request req) throws IOException { + return new RsEmpty(); + } + }).route( new RqWithHeader( new RqFake(), FkContentTypeTest.CONTENT_TYPE, From 4cf9e553f64520e1983a322b3ce30c9820f2f9f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dal=C3=AD=20Freire?= Date: Thu, 3 Mar 2016 08:14:04 -0300 Subject: [PATCH 2/9] Fixing TooManyMethods PMD rule. --- .../takes/facets/fork/FkContentTypeTest.java | 58 +++++++++---------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/src/test/java/org/takes/facets/fork/FkContentTypeTest.java b/src/test/java/org/takes/facets/fork/FkContentTypeTest.java index 56e8f82ef..08a80b52d 100644 --- a/src/test/java/org/takes/facets/fork/FkContentTypeTest.java +++ b/src/test/java/org/takes/facets/fork/FkContentTypeTest.java @@ -63,12 +63,9 @@ public final class FkContentTypeTest { public void matchesWithAnyTypes() throws IOException { MatcherAssert.assertThat( new FkContentType( - "text/xml", new Take() { - @Override - public Response act(final Request req) throws IOException { - return new RsEmpty(); - } - }).route( + "text/xml", + FkContentTypeTest.createTakeWithEmptyResponse() + ).route( new RqWithHeader( new RqFake(), FkContentTypeTest.CONTENT_TYPE, @@ -87,12 +84,9 @@ public Response act(final Request req) throws IOException { public void matchesDifferentTypes() throws IOException { MatcherAssert.assertThat( new FkContentType( - "application/json charset=utf-8", new Take() { - @Override - public Response act(final Request req) throws IOException { - return new RsEmpty(); - } - }).route( + "application/json charset=utf-8", + FkContentTypeTest.createTakeWithEmptyResponse() + ).route( new RqWithHeader( new RqFake(), FkContentTypeTest.CONTENT_TYPE, @@ -111,12 +105,9 @@ public Response act(final Request req) throws IOException { public void matchesIdenticalTypes() throws IOException { MatcherAssert.assertThat( new FkContentType( - FkContentTypeTest.CTYPE, new Take() { - @Override - public Response act(final Request req) throws IOException { - return new RsEmpty(); - } - }).route( + FkContentTypeTest.CTYPE, + FkContentTypeTest.createTakeWithEmptyResponse() + ).route( new RqWithHeader( new RqFake(), FkContentTypeTest.CONTENT_TYPE, @@ -135,12 +126,9 @@ public Response act(final Request req) throws IOException { public void matchesEmptyType() throws IOException { MatcherAssert.assertThat( new FkContentType( - "*/*", new Take() { - @Override - public Response act(final Request req) throws IOException { - return new RsEmpty(); - } - }).route( + "*/*", + FkContentTypeTest.createTakeWithEmptyResponse() + ).route( new RqWithHeader( new RqFake(), FkContentTypeTest.CONTENT_TYPE, "" ) @@ -156,12 +144,9 @@ public Response act(final Request req) throws IOException { public void matchesDifferentEncodingsTypes() throws IOException { MatcherAssert.assertThat( new FkContentType( - FkContentTypeTest.CTYPE, new Take() { - @Override - public Response act(final Request req) throws IOException { - return new RsEmpty(); - } - }).route( + FkContentTypeTest.CTYPE, + FkContentTypeTest.createTakeWithEmptyResponse() + ).route( new RqWithHeader( new RqFake(), FkContentTypeTest.CONTENT_TYPE, @@ -182,4 +167,17 @@ public void equalsAndHashCodeEqualTest() throws Exception { .suppress(Warning.TRANSIENT_FIELDS) .verify(); } + + /** + * Create a Take instance with empty response. + * @return Take + */ + private static Take createTakeWithEmptyResponse() { + return new Take() { + @Override + public Response act(final Request req) throws IOException { + return new RsEmpty(); + } + }; + } } From 43e2c2062cdb6ff3282eea9967ba9e740efdf83b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dal=C3=AD=20Freire?= Date: Thu, 3 Mar 2016 23:19:52 -0300 Subject: [PATCH 3/9] Indentation --- .../takes/facets/fork/FkContentTypeTest.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/test/java/org/takes/facets/fork/FkContentTypeTest.java b/src/test/java/org/takes/facets/fork/FkContentTypeTest.java index 08a80b52d..3fd8dde77 100644 --- a/src/test/java/org/takes/facets/fork/FkContentTypeTest.java +++ b/src/test/java/org/takes/facets/fork/FkContentTypeTest.java @@ -65,14 +65,14 @@ public void matchesWithAnyTypes() throws IOException { new FkContentType( "text/xml", FkContentTypeTest.createTakeWithEmptyResponse() - ).route( - new RqWithHeader( - new RqFake(), - FkContentTypeTest.CONTENT_TYPE, - "*/* " - ) - ).has(), - Matchers.is(true) + ).route( + new RqWithHeader( + new RqFake(), + FkContentTypeTest.CONTENT_TYPE, + "*/* " + ) + ).has(), + Matchers.is(true) ); } @@ -86,14 +86,14 @@ public void matchesDifferentTypes() throws IOException { new FkContentType( "application/json charset=utf-8", FkContentTypeTest.createTakeWithEmptyResponse() - ).route( + ).route( new RqWithHeader( new RqFake(), FkContentTypeTest.CONTENT_TYPE, "images/*" ) ).has(), - Matchers.is(false) + Matchers.is(false) ); } From 424db4ef18636f9390a45df8386a02baea8f9e77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dal=C3=AD=20Freire?= Date: Fri, 4 Mar 2016 05:27:27 -0300 Subject: [PATCH 4/9] Indentation --- src/test/java/org/takes/facets/fork/FkContentTypeTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/takes/facets/fork/FkContentTypeTest.java b/src/test/java/org/takes/facets/fork/FkContentTypeTest.java index 3fd8dde77..9f87e1ec9 100644 --- a/src/test/java/org/takes/facets/fork/FkContentTypeTest.java +++ b/src/test/java/org/takes/facets/fork/FkContentTypeTest.java @@ -72,7 +72,7 @@ public void matchesWithAnyTypes() throws IOException { "*/* " ) ).has(), - Matchers.is(true) + Matchers.is(true) ); } @@ -93,7 +93,7 @@ public void matchesDifferentTypes() throws IOException { "images/*" ) ).has(), - Matchers.is(false) + Matchers.is(false) ); } @@ -107,7 +107,7 @@ public void matchesIdenticalTypes() throws IOException { new FkContentType( FkContentTypeTest.CTYPE, FkContentTypeTest.createTakeWithEmptyResponse() - ).route( + ).route( new RqWithHeader( new RqFake(), FkContentTypeTest.CONTENT_TYPE, From a68bb2092a374e696308d1337856b6ff71021c96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dal=C3=AD=20Freire?= Date: Fri, 4 Mar 2016 10:55:05 -0300 Subject: [PATCH 5/9] Indentation --- src/test/java/org/takes/facets/fork/FkContentTypeTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/takes/facets/fork/FkContentTypeTest.java b/src/test/java/org/takes/facets/fork/FkContentTypeTest.java index 9f87e1ec9..90dd83994 100644 --- a/src/test/java/org/takes/facets/fork/FkContentTypeTest.java +++ b/src/test/java/org/takes/facets/fork/FkContentTypeTest.java @@ -128,7 +128,7 @@ public void matchesEmptyType() throws IOException { new FkContentType( "*/*", FkContentTypeTest.createTakeWithEmptyResponse() - ).route( + ).route( new RqWithHeader( new RqFake(), FkContentTypeTest.CONTENT_TYPE, "" ) From 820b0a98312d03e04587411b63631651a107049c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dal=C3=AD=20Freire?= Date: Fri, 4 Mar 2016 13:42:03 -0300 Subject: [PATCH 6/9] Indentation --- .../java/org/takes/facets/fork/FkContentTypeTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/takes/facets/fork/FkContentTypeTest.java b/src/test/java/org/takes/facets/fork/FkContentTypeTest.java index 90dd83994..79ae71b14 100644 --- a/src/test/java/org/takes/facets/fork/FkContentTypeTest.java +++ b/src/test/java/org/takes/facets/fork/FkContentTypeTest.java @@ -129,10 +129,10 @@ public void matchesEmptyType() throws IOException { "*/*", FkContentTypeTest.createTakeWithEmptyResponse() ).route( - new RqWithHeader( - new RqFake(), FkContentTypeTest.CONTENT_TYPE, "" - ) - ).has(), Matchers.is(true) + new RqWithHeader( + new RqFake(), FkContentTypeTest.CONTENT_TYPE, "" + ) + ).has(), Matchers.is(true) ); } From 2e6b8780a53327b2e852cac64d22135e4b16485e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dal=C3=AD=20Freire?= Date: Fri, 4 Mar 2016 13:52:36 -0300 Subject: [PATCH 7/9] Indentation --- src/test/java/org/takes/facets/fork/FkContentTypeTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/takes/facets/fork/FkContentTypeTest.java b/src/test/java/org/takes/facets/fork/FkContentTypeTest.java index 79ae71b14..12bb26f4e 100644 --- a/src/test/java/org/takes/facets/fork/FkContentTypeTest.java +++ b/src/test/java/org/takes/facets/fork/FkContentTypeTest.java @@ -146,7 +146,7 @@ public void matchesDifferentEncodingsTypes() throws IOException { new FkContentType( FkContentTypeTest.CTYPE, FkContentTypeTest.createTakeWithEmptyResponse() - ).route( + ).route( new RqWithHeader( new RqFake(), FkContentTypeTest.CONTENT_TYPE, From e2ec249ca22ca9c85609c2349ea6304cc80cd948 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dal=C3=AD=20Freire?= Date: Sat, 5 Mar 2016 09:56:56 -0300 Subject: [PATCH 8/9] Review fixes. - Putting back the old constructor; - Rename the builder method name in the FkContentTypeTest class; --- .../java/org/takes/facets/fork/FkContentType.java | 10 ++++++++++ .../org/takes/facets/fork/FkContentTypeTest.java | 12 ++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/takes/facets/fork/FkContentType.java b/src/main/java/org/takes/facets/fork/FkContentType.java index 349c5aff6..dbcb2327a 100644 --- a/src/main/java/org/takes/facets/fork/FkContentType.java +++ b/src/main/java/org/takes/facets/fork/FkContentType.java @@ -30,6 +30,7 @@ import org.takes.Take; import org.takes.misc.Opt; import org.takes.rq.RqHeaders; +import org.takes.tk.TkFixed; /** * Fork by Content-type accepted by "Content-Type" HTTP header. @@ -54,6 +55,15 @@ public final class FkContentType implements Fork { */ private final transient Take take; + /** + * Ctor. + * @param atype Accepted type + * @param response Response to return + */ + public FkContentType(final String atype, final Response response) { + this(atype, new TkFixed(response)); + } + /** * Ctor. * @param atype Accepted type diff --git a/src/test/java/org/takes/facets/fork/FkContentTypeTest.java b/src/test/java/org/takes/facets/fork/FkContentTypeTest.java index 12bb26f4e..7afb7f0ba 100644 --- a/src/test/java/org/takes/facets/fork/FkContentTypeTest.java +++ b/src/test/java/org/takes/facets/fork/FkContentTypeTest.java @@ -64,7 +64,7 @@ public void matchesWithAnyTypes() throws IOException { MatcherAssert.assertThat( new FkContentType( "text/xml", - FkContentTypeTest.createTakeWithEmptyResponse() + new RsEmpty() ).route( new RqWithHeader( new RqFake(), @@ -85,7 +85,7 @@ public void matchesDifferentTypes() throws IOException { MatcherAssert.assertThat( new FkContentType( "application/json charset=utf-8", - FkContentTypeTest.createTakeWithEmptyResponse() + FkContentTypeTest.emptyResponse() ).route( new RqWithHeader( new RqFake(), @@ -106,7 +106,7 @@ public void matchesIdenticalTypes() throws IOException { MatcherAssert.assertThat( new FkContentType( FkContentTypeTest.CTYPE, - FkContentTypeTest.createTakeWithEmptyResponse() + FkContentTypeTest.emptyResponse() ).route( new RqWithHeader( new RqFake(), @@ -127,7 +127,7 @@ public void matchesEmptyType() throws IOException { MatcherAssert.assertThat( new FkContentType( "*/*", - FkContentTypeTest.createTakeWithEmptyResponse() + FkContentTypeTest.emptyResponse() ).route( new RqWithHeader( new RqFake(), FkContentTypeTest.CONTENT_TYPE, "" @@ -145,7 +145,7 @@ public void matchesDifferentEncodingsTypes() throws IOException { MatcherAssert.assertThat( new FkContentType( FkContentTypeTest.CTYPE, - FkContentTypeTest.createTakeWithEmptyResponse() + FkContentTypeTest.emptyResponse() ).route( new RqWithHeader( new RqFake(), @@ -172,7 +172,7 @@ public void equalsAndHashCodeEqualTest() throws Exception { * Create a Take instance with empty response. * @return Take */ - private static Take createTakeWithEmptyResponse() { + private static Take emptyResponse() { return new Take() { @Override public Response act(final Request req) throws IOException { From c54de62433edb20a9bf3db57b385fa4c3e605e6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dal=C3=AD=20Freire?= Date: Sat, 5 Mar 2016 19:40:16 -0300 Subject: [PATCH 9/9] Indentation --- src/test/java/org/takes/facets/fork/FkContentTypeTest.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/test/java/org/takes/facets/fork/FkContentTypeTest.java b/src/test/java/org/takes/facets/fork/FkContentTypeTest.java index 7afb7f0ba..8c55abdff 100644 --- a/src/test/java/org/takes/facets/fork/FkContentTypeTest.java +++ b/src/test/java/org/takes/facets/fork/FkContentTypeTest.java @@ -62,10 +62,7 @@ public final class FkContentTypeTest { @Test public void matchesWithAnyTypes() throws IOException { MatcherAssert.assertThat( - new FkContentType( - "text/xml", - new RsEmpty() - ).route( + new FkContentType("text/xml", new RsEmpty()).route( new RqWithHeader( new RqFake(), FkContentTypeTest.CONTENT_TYPE,