PageRenderTime 24ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/helloworld/src/test/java/org/glassfish/jersey/examples/helloworld/HelloWorldTest.java

https://gitlab.com/jaragan/jersey
Java | 253 lines | 178 code | 26 blank | 49 comment | 1 complexity | 5803343561df34c6fafd4faf1a5955cf MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright (c) 2010-2015 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * The contents of this file are subject to the terms of either the GNU
  7. * General Public License Version 2 only ("GPL") or the Common Development
  8. * and Distribution License("CDDL") (collectively, the "License"). You
  9. * may not use this file except in compliance with the License. You can
  10. * obtain a copy of the License at
  11. * http://glassfish.java.net/public/CDDL+GPL_1_1.html
  12. * or packager/legal/LICENSE.txt. See the License for the specific
  13. * language governing permissions and limitations under the License.
  14. *
  15. * When distributing the software, include this License Header Notice in each
  16. * file and include the License file at packager/legal/LICENSE.txt.
  17. *
  18. * GPL Classpath Exception:
  19. * Oracle designates this particular file as subject to the "Classpath"
  20. * exception as provided by Oracle in the GPL Version 2 section of the License
  21. * file that accompanied this code.
  22. *
  23. * Modifications:
  24. * If applicable, add the following below the License Header, with the fields
  25. * enclosed by brackets [] replaced by your own identifying information:
  26. * "Portions Copyright [year] [name of copyright owner]"
  27. *
  28. * Contributor(s):
  29. * If you wish your version of this file to be governed by only the CDDL or
  30. * only the GPL Version 2, indicate your decision by adding "[Contributor]
  31. * elects to include this software in this distribution under the [CDDL or GPL
  32. * Version 2] license." If you don't indicate a single choice of license, a
  33. * recipient has the option to distribute your version of this file under
  34. * either the CDDL, the GPL Version 2 or to extend the choice of license to
  35. * its licensees as provided above. However, if you add GPL Version 2 code
  36. * and therefore, elected the GPL Version 2 license, then the option applies
  37. * only if the new code is made subject to such option by the copyright
  38. * holder.
  39. */
  40. package org.glassfish.jersey.examples.helloworld;
  41. import java.net.HttpURLConnection;
  42. import java.net.URL;
  43. import java.util.concurrent.CountDownLatch;
  44. import java.util.concurrent.TimeUnit;
  45. import java.util.logging.Logger;
  46. import javax.ws.rs.client.Client;
  47. import javax.ws.rs.client.ClientBuilder;
  48. import javax.ws.rs.client.InvocationCallback;
  49. import javax.ws.rs.client.WebTarget;
  50. import javax.ws.rs.core.MediaType;
  51. import javax.ws.rs.core.Response;
  52. import javax.ws.rs.core.UriBuilder;
  53. import org.glassfish.jersey.server.ResourceConfig;
  54. import org.glassfish.jersey.test.JerseyTest;
  55. import org.glassfish.jersey.test.TestProperties;
  56. import org.glassfish.jersey.test.util.runner.ConcurrentRunner;
  57. import org.glassfish.jersey.test.util.runner.RunSeparately;
  58. import org.junit.Ignore;
  59. import org.junit.Test;
  60. import org.junit.runner.RunWith;
  61. import static org.junit.Assert.assertEquals;
  62. import static org.junit.Assert.assertTrue;
  63. @RunWith(ConcurrentRunner.class)
  64. public class HelloWorldTest extends JerseyTest {
  65. @Override
  66. protected ResourceConfig configure() {
  67. // mvn test -Djersey.config.test.container.factory=org.glassfish.jersey.test.inmemory.InMemoryTestContainerFactory
  68. // mvn test -Djersey.config.test.container.factory=org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory
  69. // mvn test -Djersey.config.test.container.factory=org.glassfish.jersey.test.jdkhttp.JdkHttpServerTestContainerFactory
  70. // mvn test -Djersey.config.test.container.factory=org.glassfish.jersey.test.simple.SimpleTestContainerFactory
  71. enable(TestProperties.LOG_TRAFFIC);
  72. // enable(TestProperties.DUMP_ENTITY);
  73. return new ResourceConfig(HelloWorldResource.class);
  74. }
  75. // Uncomment to use Grizzly async client
  76. // @Override
  77. // protected void configureClient(ClientConfig clientConfig) {
  78. // clientConfig.connector(new GrizzlyConnector(clientConfig));
  79. // }
  80. @Test
  81. @Ignore("not compatible with test framework (doesn't use client())")
  82. public void testHelloWorld() throws Exception {
  83. URL getUrl = UriBuilder.fromUri(getBaseUri()).path(App.ROOT_PATH).build().toURL();
  84. HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
  85. try {
  86. connection.setDoOutput(true);
  87. connection.setInstanceFollowRedirects(false);
  88. connection.setRequestMethod("GET");
  89. connection.setRequestProperty("Content-Type", "text/plain");
  90. assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
  91. } finally {
  92. connection.disconnect();
  93. }
  94. }
  95. @Test
  96. public void testConnection() {
  97. Response response = target().path(App.ROOT_PATH).request("text/plain").get();
  98. assertEquals(200, response.getStatus());
  99. }
  100. @Test
  101. public void testClientStringResponse() {
  102. String s = target().path(App.ROOT_PATH).request().get(String.class);
  103. assertEquals(HelloWorldResource.CLICHED_MESSAGE, s);
  104. }
  105. @Test
  106. public void testAsyncClientRequests() throws InterruptedException {
  107. final int REQUESTS = 10;
  108. final CountDownLatch latch = new CountDownLatch(REQUESTS);
  109. final long tic = System.currentTimeMillis();
  110. for (int i = 0; i < REQUESTS; i++) {
  111. final int id = i;
  112. target().path(App.ROOT_PATH).request().async().get(new InvocationCallback<Response>() {
  113. @Override
  114. public void completed(Response response) {
  115. try {
  116. final String result = response.readEntity(String.class);
  117. assertEquals(HelloWorldResource.CLICHED_MESSAGE, result);
  118. } finally {
  119. latch.countDown();
  120. }
  121. }
  122. @Override
  123. public void failed(Throwable error) {
  124. error.printStackTrace();
  125. latch.countDown();
  126. }
  127. });
  128. }
  129. latch.await(10 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
  130. final long toc = System.currentTimeMillis();
  131. Logger.getLogger(HelloWorldTest.class.getName()).info("Executed in: " + (toc - tic));
  132. }
  133. @Test
  134. public void testHead() {
  135. Response response = target().path(App.ROOT_PATH).request().head();
  136. assertEquals(200, response.getStatus());
  137. assertEquals(MediaType.TEXT_PLAIN_TYPE, response.getMediaType());
  138. }
  139. @Test
  140. public void testFooBarOptions() {
  141. Response response = target().path(App.ROOT_PATH).request().header("Accept", "foo/bar").options();
  142. assertEquals(200, response.getStatus());
  143. final String allowHeader = response.getHeaderString("Allow");
  144. _checkAllowContent(allowHeader);
  145. assertEquals("foo/bar", response.getMediaType().toString());
  146. assertEquals(0, response.getLength());
  147. }
  148. @Test
  149. public void testTextPlainOptions() {
  150. Response response = target().path(App.ROOT_PATH).request().header("Accept", MediaType.TEXT_PLAIN).options();
  151. assertEquals(200, response.getStatus());
  152. final String allowHeader = response.getHeaderString("Allow");
  153. _checkAllowContent(allowHeader);
  154. assertEquals(MediaType.TEXT_PLAIN_TYPE, response.getMediaType());
  155. final String responseBody = response.readEntity(String.class);
  156. _checkAllowContent(responseBody);
  157. }
  158. private void _checkAllowContent(final String content) {
  159. assertTrue(content.contains("GET"));
  160. assertTrue(content.contains("HEAD"));
  161. assertTrue(content.contains("OPTIONS"));
  162. }
  163. @Test
  164. public void testMissingResourceNotFound() {
  165. Response response;
  166. response = target().path(App.ROOT_PATH + "arbitrary").request().get();
  167. assertEquals(404, response.getStatus());
  168. response = target().path(App.ROOT_PATH).path("arbitrary").request().get();
  169. assertEquals(404, response.getStatus());
  170. }
  171. @Test
  172. @RunSeparately
  173. public void testLoggingFilterClientClass() {
  174. Client client = client();
  175. client.register(CustomLoggingFilter.class).property("foo", "bar");
  176. CustomLoggingFilter.preFilterCalled = CustomLoggingFilter.postFilterCalled = 0;
  177. String s = target().path(App.ROOT_PATH).request().get(String.class);
  178. assertEquals(HelloWorldResource.CLICHED_MESSAGE, s);
  179. assertEquals(1, CustomLoggingFilter.preFilterCalled);
  180. assertEquals(1, CustomLoggingFilter.postFilterCalled);
  181. }
  182. @Test
  183. @RunSeparately
  184. public void testLoggingFilterClientInstance() {
  185. Client client = client();
  186. client.register(new CustomLoggingFilter()).property("foo", "bar");
  187. CustomLoggingFilter.preFilterCalled = CustomLoggingFilter.postFilterCalled = 0;
  188. String s = target().path(App.ROOT_PATH).request().get(String.class);
  189. assertEquals(HelloWorldResource.CLICHED_MESSAGE, s);
  190. assertEquals(1, CustomLoggingFilter.preFilterCalled);
  191. assertEquals(1, CustomLoggingFilter.postFilterCalled);
  192. }
  193. @Test
  194. @RunSeparately
  195. public void testLoggingFilterTargetClass() {
  196. WebTarget target = target().path(App.ROOT_PATH);
  197. target.register(CustomLoggingFilter.class).property("foo", "bar");
  198. CustomLoggingFilter.preFilterCalled = CustomLoggingFilter.postFilterCalled = 0;
  199. String s = target.request().get(String.class);
  200. assertEquals(HelloWorldResource.CLICHED_MESSAGE, s);
  201. assertEquals(1, CustomLoggingFilter.preFilterCalled);
  202. assertEquals(1, CustomLoggingFilter.postFilterCalled);
  203. }
  204. @Test
  205. @RunSeparately
  206. public void testLoggingFilterTargetInstance() {
  207. WebTarget target = target().path(App.ROOT_PATH);
  208. target.register(new CustomLoggingFilter()).property("foo", "bar");
  209. CustomLoggingFilter.preFilterCalled = CustomLoggingFilter.postFilterCalled = 0;
  210. String s = target.request().get(String.class);
  211. assertEquals(HelloWorldResource.CLICHED_MESSAGE, s);
  212. assertEquals(1, CustomLoggingFilter.preFilterCalled);
  213. assertEquals(1, CustomLoggingFilter.postFilterCalled);
  214. }
  215. @Test
  216. @RunSeparately
  217. public void testConfigurationUpdate() {
  218. Client client1 = client();
  219. client1.register(CustomLoggingFilter.class).property("foo", "bar");
  220. Client client = ClientBuilder.newClient(client1.getConfiguration());
  221. CustomLoggingFilter.preFilterCalled = CustomLoggingFilter.postFilterCalled = 0;
  222. String s = target().path(App.ROOT_PATH).request().get(String.class);
  223. assertEquals(HelloWorldResource.CLICHED_MESSAGE, s);
  224. assertEquals(1, CustomLoggingFilter.preFilterCalled);
  225. assertEquals(1, CustomLoggingFilter.postFilterCalled);
  226. }
  227. }