Skip to content

Commit

Permalink
yegor256#953 SrvTake test and Grizzly update
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriciofx committed Jan 24, 2019
1 parent afd95b0 commit be2fac4
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 5 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ SOFTWARE.
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.sun.grizzly</groupId>
<artifactId>grizzly-servlet-webserver</artifactId>
<version>1.9.65</version>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-servlet-server</artifactId>
<version>2.4.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/takes/servlet/SrvTake.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
* Servlet for take.
*
* @since 2.0
* @todo #865:30min Continue to integrate with Servlet API,
* @todo #953:30min Integration with Servlets Session API,
* see https://github.com/yegor256/takes/pull/865 discussion for details.
* Also add unit tests for servlet related class SrvTake.
* Add support and tests to Servlet Session.
*/
public final class SrvTake extends HttpServlet {
/**
Expand Down
70 changes: 70 additions & 0 deletions src/test/java/org/takes/servlet/SrvTakeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2019 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.takes.servlet;

import com.jcabi.http.request.JdkRequest;
import com.jcabi.http.response.RestResponse;
import java.net.HttpURLConnection;
import org.cactoos.text.FormattedText;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.servlet.ServletRegistration;
import org.glassfish.grizzly.servlet.WebappContext;
import org.hamcrest.core.StringContains;
import org.junit.jupiter.api.Test;

/**
* Test case for {@link SrvTake}.
*
* @since 1.16
*/
public final class SrvTakeTest {
@Test
public void executeATakesAsAServlet() throws Exception {
final String name = "webapp";
final HttpServer server = HttpServer.createSimpleServer("./", 18080);
final WebappContext context = new WebappContext(name);
final ServletRegistration servlet = context.addServlet(
"takes",
SrvTake.class
);
servlet.setInitParameter("take", TkServletApp.class.getName());
servlet.addMapping("/test");
context.deploy(server);
server.start();
new JdkRequest("http://localhost:18080/test")
.fetch()
.as(RestResponse.class)
.assertStatus(HttpURLConnection.HTTP_OK)
.assertBody(
new StringContains(
new FormattedText(
"Hello servlet! Using [%s] ServletContext.",
name
).asString()
)
);
server.shutdownNow();
}
}
66 changes: 66 additions & 0 deletions src/test/java/org/takes/servlet/TkServletApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2019 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.takes.servlet;

import java.io.IOException;
import javax.servlet.ServletContext;
import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;
import org.takes.Request;
import org.takes.Response;
import org.takes.Take;
import org.takes.rs.RsText;

/**
* Fake TkServletApp (for {@link SrvTake} test only).
*
* @since 1.16
*/
public final class TkServletApp implements Take {
/**
* ServletContext.
*/
private final ServletContext context;

/**
* Ctor.
* @param ctx A ServletContext
*/
public TkServletApp(final ServletContext ctx) {
this.context = ctx;
}

@Override
public Response act(final Request req) throws IOException {
return new RsText(
new UncheckedText(
new FormattedText(
"Hello servlet! Using [%s] ServletContext.",
this.context.getServletContextName()
)
).asString()
);
}
}

0 comments on commit be2fac4

Please sign in to comment.