PageRenderTime 53ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/contribs/jersey-guice/src/test/java/com/sun/jersey/guice/ProvidesTest.java

http://github.com/jersey/jersey-1.x
Java | 287 lines | 194 code | 50 blank | 43 comment | 1 complexity | b3e0d7415ba35676d8524b30791428eb MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause-No-Nuclear-License-2014, GPL-2.0
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright (c) 2013 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 com.sun.jersey.guice;
  41. import javax.ws.rs.GET;
  42. import javax.ws.rs.Path;
  43. import javax.ws.rs.Produces;
  44. import javax.ws.rs.QueryParam;
  45. import javax.ws.rs.core.Context;
  46. import javax.ws.rs.core.HttpHeaders;
  47. import javax.ws.rs.core.Request;
  48. import javax.ws.rs.core.SecurityContext;
  49. import javax.ws.rs.core.UriInfo;
  50. import javax.ws.rs.ext.Providers;
  51. import com.google.inject.Guice;
  52. import com.google.inject.Inject;
  53. import com.google.inject.Injector;
  54. import com.google.inject.Provider;
  55. import com.google.inject.Singleton;
  56. import com.google.inject.Stage;
  57. import com.google.inject.servlet.GuiceServletContextListener;
  58. import com.google.inject.servlet.RequestScoped;
  59. import com.sun.jersey.api.client.WebResource;
  60. import com.sun.jersey.api.core.ExtendedUriInfo;
  61. import com.sun.jersey.api.core.HttpContext;
  62. import com.sun.jersey.api.core.HttpRequestContext;
  63. import com.sun.jersey.api.core.HttpResponseContext;
  64. import com.sun.jersey.api.core.ResourceContext;
  65. import com.sun.jersey.core.util.FeaturesAndProperties;
  66. import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
  67. import com.sun.jersey.spi.MessageBodyWorkers;
  68. import com.sun.jersey.spi.container.ExceptionMapperContext;
  69. import com.sun.jersey.spi.container.WebApplication;
  70. /**
  71. *
  72. * @author Paul.Sandoz@Sun.Com
  73. */
  74. public class ProvidesTest extends AbstractGuiceGrizzlyTest {
  75. @Path("/")
  76. @RequestScoped
  77. public static class InjectResource {
  78. @Inject WebApplication wa;
  79. @Inject Providers p;
  80. @Inject FeaturesAndProperties fp;
  81. @Inject MessageBodyWorkers mbws;
  82. @Inject ExceptionMapperContext emc;
  83. @Inject HttpContext hc;
  84. @Inject UriInfo ui;
  85. @Inject ExtendedUriInfo eui;
  86. @Inject HttpRequestContext hrequestc;
  87. @Inject HttpHeaders h;
  88. @Inject Request r;
  89. @Inject SecurityContext sc;
  90. @Inject HttpResponseContext hresponsec;
  91. @Inject
  92. public InjectResource(ResourceContext rc) {
  93. assert rc != null;
  94. }
  95. @GET
  96. @Produces("text/plain")
  97. public String getIt() {
  98. return "OK";
  99. }
  100. }
  101. public static class InjectServletConfig extends GuiceServletContextListener {
  102. @Override
  103. protected Injector getInjector() {
  104. return Guice.createInjector(
  105. Stage.PRODUCTION,
  106. new JerseyServletModule() {
  107. @Override
  108. protected void configureServlets() {
  109. bind(InjectResource.class);
  110. serve("*").with(GuiceContainer.class);
  111. }
  112. }
  113. );
  114. }
  115. }
  116. public void testInjectResource() {
  117. startServer(InjectServletConfig.class);
  118. String s = resource().path("/").get(String.class);
  119. assertEquals(s, "OK");
  120. }
  121. @Path("bound/perrequest")
  122. @RequestScoped
  123. public static class BoundPerRequestResource {
  124. @Inject UriInfo ui;
  125. @QueryParam("x") String x;
  126. @GET
  127. @Produces("text/plain")
  128. public String getIt() {
  129. assertEquals("bound/perrequest", ui.getPath());
  130. assertEquals("x", x);
  131. return "OK";
  132. }
  133. }
  134. @Path("bound/noscope")
  135. public static class BoundNoScopeResource {
  136. @Inject UriInfo ui;
  137. @QueryParam("x") String x;
  138. @GET
  139. @Produces("text/plain")
  140. public String getIt() {
  141. assertEquals("bound/noscope", ui.getPath());
  142. assertEquals("x", x);
  143. return "OK";
  144. }
  145. }
  146. @Path("bound/singleton")
  147. @Singleton
  148. public static class BoundSingletonResource {
  149. @Inject Provider<UriInfo> ui;
  150. @GET
  151. @Produces("text/plain")
  152. public String getIt() {
  153. assertEquals("bound/singleton", ui.get().getPath());
  154. String x = ui.get().getQueryParameters().getFirst("x");
  155. assertEquals("x", x);
  156. return "OK";
  157. }
  158. }
  159. public static class ResourceServletConfig extends GuiceServletContextListener {
  160. @Override
  161. protected Injector getInjector() {
  162. return Guice.createInjector(
  163. Stage.PRODUCTION,
  164. new JerseyServletModule() {
  165. @Override
  166. protected void configureServlets() {
  167. bind(BoundPerRequestResource.class);
  168. bind(BoundNoScopeResource.class);
  169. bind(BoundSingletonResource.class);
  170. serve("*").with(GuiceContainer.class);
  171. }
  172. }
  173. );
  174. }
  175. }
  176. public void testBoundPerRequestResource() {
  177. startServer(ResourceServletConfig.class);
  178. WebResource r = resource().path("/bound/perrequest").queryParam("x", "x");
  179. String s = r.get(String.class);
  180. assertEquals(s, "OK");
  181. }
  182. public void testBoundNoScopeResource() {
  183. startServer(ResourceServletConfig.class);
  184. WebResource r = resource().path("/bound/noscope").queryParam("x", "x");
  185. String s = r.get(String.class);
  186. assertEquals(s, "OK");
  187. }
  188. public void testBoundSingletonResourcee() {
  189. startServer(ResourceServletConfig.class);
  190. WebResource r = resource().path("/bound/singleton").queryParam("x", "x");
  191. String s = r.get(String.class);
  192. assertEquals(s, "OK");
  193. }
  194. @Path("bound")
  195. public static class BoundSubResource {
  196. @Context ResourceContext rc;
  197. @Path("perrequest")
  198. public BoundPerRequestResource getPerRequest() {
  199. return rc.getResource(BoundPerRequestResource.class);
  200. }
  201. @Path("noscope")
  202. public BoundNoScopeResource getNoScope() {
  203. return rc.getResource(BoundNoScopeResource.class);
  204. }
  205. @Path("singleton")
  206. public BoundSingletonResource getSingleton() {
  207. return rc.getResource(BoundSingletonResource.class);
  208. }
  209. }
  210. public static class SubResourceServletConfig extends GuiceServletContextListener {
  211. @Override
  212. protected Injector getInjector() {
  213. return Guice.createInjector(
  214. Stage.PRODUCTION,
  215. new JerseyServletModule() {
  216. @Override
  217. protected void configureServlets() {
  218. bind(BoundSubResource.class);
  219. serve("*").with(GuiceContainer.class);
  220. }
  221. }
  222. );
  223. }
  224. }
  225. public void testBoundSubResource() {
  226. startServer(SubResourceServletConfig.class);
  227. WebResource r = resource().path("/bound/perrequest").queryParam("x", "x");
  228. String s = r.get(String.class);
  229. assertEquals(s, "OK");
  230. r = resource().path("/bound/noscope").queryParam("x", "x");
  231. s = r.get(String.class);
  232. assertEquals(s, "OK");
  233. r = resource().path("/bound/singleton").queryParam("x", "x");
  234. s = r.get(String.class);
  235. assertEquals(s, "OK");
  236. }
  237. }