PageRenderTime 224ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/resteasy-2.3.2.Final/resteasy-jaxrs/src/test/java/org/jboss/resteasy/test/finegrain/InternalDispatcherTest.java

#
Java | 298 lines | 243 code | 43 blank | 12 comment | 2 complexity | fbe5617ff29d2d85b387e62c4e8b5046 MD5 | raw file
  1. package org.jboss.resteasy.test.finegrain;
  2. import junit.framework.Assert;
  3. import org.jboss.resteasy.annotations.ClientResponseType;
  4. import org.jboss.resteasy.client.ClientResponse;
  5. import org.jboss.resteasy.client.ProxyFactory;
  6. import org.jboss.resteasy.core.Dispatcher;
  7. import org.jboss.resteasy.core.MessageBodyParameterInjector;
  8. import org.jboss.resteasy.spi.BadRequestException;
  9. import org.jboss.resteasy.spi.InternalDispatcher;
  10. import org.jboss.resteasy.spi.ResteasyProviderFactory;
  11. import org.jboss.resteasy.test.EmbeddedContainer;
  12. import org.junit.AfterClass;
  13. import org.junit.Before;
  14. import org.junit.BeforeClass;
  15. import org.junit.Test;
  16. import javax.ws.rs.Consumes;
  17. import javax.ws.rs.DELETE;
  18. import javax.ws.rs.DefaultValue;
  19. import javax.ws.rs.GET;
  20. import javax.ws.rs.POST;
  21. import javax.ws.rs.PUT;
  22. import javax.ws.rs.Path;
  23. import javax.ws.rs.PathParam;
  24. import javax.ws.rs.Produces;
  25. import javax.ws.rs.QueryParam;
  26. import javax.ws.rs.core.Context;
  27. import javax.ws.rs.core.Response;
  28. import javax.ws.rs.core.UriInfo;
  29. import java.util.Stack;
  30. import static org.jboss.resteasy.test.TestPortProvider.*;
  31. /**
  32. * Test for InternalDispatcher
  33. *
  34. * @author <a href="mailto:sduskis@gmail.com">Solomon Duskis</a>
  35. * @version $Revision: 1 $
  36. */
  37. public class InternalDispatcherTest
  38. {
  39. private static Dispatcher dispatcher;
  40. private static ForwardingResource forwardingResource;
  41. @Path("/")
  42. public interface Client
  43. {
  44. @GET
  45. @Path("basic")
  46. @Produces("text/plain")
  47. String getBasic();
  48. @PUT
  49. @Path("basic")
  50. @Consumes("text/plain")
  51. void putBasic(String body);
  52. @GET
  53. @Path("forward/basic")
  54. @Produces("text/plain")
  55. String getForwardBasic();
  56. @PUT
  57. @Path("forward/basic")
  58. @Consumes("text/plain")
  59. void putForwardBasic(String body);
  60. @POST
  61. @Path("forward/basic")
  62. @Consumes("text/plain")
  63. void postForwardBasic(String body);
  64. @DELETE
  65. @Path("/forward/basic")
  66. void deleteForwardBasic();
  67. @GET
  68. @Produces("text/plain")
  69. @Path("/forward/object/{id}")
  70. @ClientResponseType(entityType = String.class)
  71. Response getForwardedObject(@PathParam("id") Integer id);
  72. @GET
  73. @Produces("text/plain")
  74. @Path("/object/{id}")
  75. @ClientResponseType(entityType = String.class)
  76. Response getObject(@PathParam("id") Integer id);
  77. @GET
  78. @Produces("text/plain")
  79. @Path("/infinite-forward")
  80. int infiniteForward();
  81. }
  82. @Path("/")
  83. public static class ForwardingResource
  84. {
  85. public Stack<String> uriStack = new Stack<String>();
  86. String basic = "basic";
  87. @Context
  88. UriInfo uriInfo;
  89. @GET
  90. @Produces("text/plain")
  91. @Path("/basic")
  92. public String getBasic()
  93. {
  94. uriStack.push(uriInfo.getAbsolutePath().toString());
  95. return basic;
  96. }
  97. @GET
  98. @Produces("text/plain")
  99. @Path("/forward/basic")
  100. public String forwardBasic(@Context InternalDispatcher dispatcher)
  101. {
  102. uriStack.push(uriInfo.getAbsolutePath().toString());
  103. return (String) dispatcher.getEntity("/basic");
  104. }
  105. @PUT
  106. @POST
  107. @Consumes("text/plain")
  108. @Path("/basic")
  109. public void putBasic(String basic)
  110. {
  111. uriStack.push(uriInfo.getAbsolutePath().toString());
  112. this.basic = basic;
  113. }
  114. @DELETE
  115. @Path("/basic")
  116. public void deleteBasic()
  117. {
  118. uriStack.push(uriInfo.getAbsolutePath().toString());
  119. this.basic = "basic";
  120. }
  121. @PUT
  122. @Consumes("text/plain")
  123. @Path("/forward/basic")
  124. public void putForwardBasic(String basic,
  125. @Context InternalDispatcher dispatcher)
  126. {
  127. uriStack.push(uriInfo.getAbsolutePath().toString());
  128. dispatcher.putEntity("/basic", basic);
  129. }
  130. @POST
  131. @Consumes("text/plain")
  132. @Path("/forward/basic")
  133. public void postForwardBasic(String basic,
  134. @Context InternalDispatcher dispatcher)
  135. {
  136. uriStack.push(uriInfo.getAbsolutePath().toString());
  137. dispatcher.postEntity("/basic", basic);
  138. }
  139. @DELETE
  140. @Path("/forward/basic")
  141. public void deleteForwardBasic(@Context InternalDispatcher dispatcher)
  142. {
  143. uriStack.push(uriInfo.getAbsolutePath().toString());
  144. dispatcher.delete("/basic");
  145. }
  146. @GET
  147. @Produces("text/plain")
  148. @Path("/object/{id}")
  149. public Response getObject(@PathParam("id") Integer id)
  150. {
  151. uriStack.push(uriInfo.getAbsolutePath().toString());
  152. if (id == 0)
  153. return Response.noContent().build();
  154. else
  155. return Response.ok("object" + id).build();
  156. }
  157. @GET
  158. @Path("/forward/object/{id}")
  159. public Response forwardObject(@PathParam("id") Integer id,
  160. @Context InternalDispatcher dispatcher)
  161. {
  162. uriStack.push(uriInfo.getAbsolutePath().toString());
  163. return dispatcher.getResponse("/object/" + id);
  164. }
  165. @GET
  166. @Path("/infinite-forward")
  167. public int infinitFoward(@Context InternalDispatcher dispatcher,
  168. @QueryParam("count") @DefaultValue("0") int count)
  169. {
  170. uriStack.push(uriInfo.getAbsolutePath().toString());
  171. try
  172. {
  173. dispatcher.getEntity("/infinite-forward?count=" + (count + 1));
  174. // we'll never reach 20, since the max count of times through the
  175. // system is 20, and first time through is 0
  176. Assert.assertNotSame(20, count);
  177. }
  178. catch (BadRequestException e)
  179. {
  180. }
  181. finally
  182. {
  183. Assert
  184. .assertEquals(count, MessageBodyParameterInjector.bodyCount());
  185. Assert.assertEquals(count + 1, ResteasyProviderFactory
  186. .getContextDataLevelCount());
  187. }
  188. return ResteasyProviderFactory.getContextDataLevelCount();
  189. }
  190. }
  191. @BeforeClass
  192. public static void before() throws Exception
  193. {
  194. dispatcher = EmbeddedContainer.start().getDispatcher();
  195. forwardingResource = new ForwardingResource();
  196. dispatcher.getRegistry().addSingletonResource(forwardingResource);
  197. }
  198. @Before
  199. public void setup() {
  200. forwardingResource.uriStack.clear();
  201. }
  202. @AfterClass
  203. public static void after() throws Exception
  204. {
  205. EmbeddedContainer.stop();
  206. }
  207. @Test
  208. public void testClientResponse() throws Exception
  209. {
  210. Client client = ProxyFactory.create(Client.class, generateBaseUrl());
  211. Assert.assertEquals("basic", client.getBasic());
  212. Assert.assertEquals("basic", client.getForwardBasic());
  213. Assert.assertEquals("object1", client.getObject(1).getEntity());
  214. Assert.assertEquals("object1", client.getForwardedObject(1).getEntity());
  215. ClientResponse<?> cr = (ClientResponse<?>) client.getObject(0);
  216. Assert.assertEquals(Response.Status.NO_CONTENT.getStatusCode(), cr.getStatus());
  217. cr.releaseConnection();
  218. cr = (ClientResponse<?>) client.getForwardedObject(0);
  219. Assert.assertEquals(Response.Status.NO_CONTENT.getStatusCode(), cr.getStatus());
  220. cr.releaseConnection();
  221. client.putForwardBasic("testBasic");
  222. Assert.assertEquals("testBasic", client.getBasic());
  223. client.postForwardBasic("testBasic1");
  224. Assert.assertEquals("testBasic1", client.getBasic());
  225. client.deleteForwardBasic();
  226. Assert.assertEquals("basic", client.getBasic());
  227. }
  228. @Test
  229. public void testInfinitForward()
  230. {
  231. Client client = ProxyFactory.create(Client.class, generateBaseUrl());
  232. // assert that even though there were infinite forwards, there still was
  233. // only 1 level of "context" data and that clean up occurred correctly.
  234. // This should not spin forever, since RESTEasy stops the recursive loop
  235. // after 20 internal dispatches
  236. Assert.assertEquals(1, client.infiniteForward());
  237. }
  238. @Test
  239. public void testUriInfoBasic() {
  240. String baseUrl = generateBaseUrl();
  241. Client client = ProxyFactory.create(Client.class, baseUrl);
  242. client.getBasic();
  243. Assert.assertEquals(baseUrl + "/basic", forwardingResource.uriStack.pop());
  244. Assert.assertTrue(forwardingResource.uriStack.isEmpty());
  245. }
  246. @Test
  247. public void testUriInfoForwardBasic() {
  248. String baseUrl = generateBaseUrl();
  249. Client client = ProxyFactory.create(Client.class, baseUrl);
  250. client.getForwardBasic();
  251. Assert.assertEquals(baseUrl + "/basic", forwardingResource.uriStack.pop());
  252. Assert.assertEquals(baseUrl + "/forward/basic", forwardingResource.uriStack.pop());
  253. Assert.assertTrue(forwardingResource.uriStack.isEmpty());
  254. }
  255. }