PageRenderTime 37ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/servlet/test/com/google/inject/servlet/TransferRequestIntegrationTest.java

https://gitlab.com/metamorphiccode/guice
Java | 215 lines | 168 code | 28 blank | 19 comment | 1 complexity | b873b9f8255504226c4de7ebfe2b2462 MD5 | raw file
  1. /**
  2. * Copyright (C) 2012 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.inject.servlet;
  17. import com.google.common.collect.ImmutableMap;
  18. import com.google.inject.AbstractModule;
  19. import com.google.inject.Guice;
  20. import com.google.inject.Injector;
  21. import com.google.inject.Key;
  22. import com.google.inject.OutOfScopeException;
  23. import com.google.inject.Provides;
  24. import junit.framework.TestCase;
  25. import java.util.concurrent.Callable;
  26. import java.util.concurrent.ExecutorService;
  27. import java.util.concurrent.Executors;
  28. import java.util.concurrent.Future;
  29. import java.util.concurrent.TimeUnit;
  30. import java.util.concurrent.TimeoutException;
  31. // TODO: Add test for HTTP transferring.
  32. /**
  33. * Tests transferring of entire request scope.
  34. */
  35. public class TransferRequestIntegrationTest extends TestCase {
  36. private final Callable<Boolean> FALSE_CALLABLE = new Callable<Boolean>() {
  37. @Override public Boolean call() {
  38. return false;
  39. }
  40. };
  41. public void testTransferHttp_outOfScope() {
  42. try {
  43. ServletScopes.transferRequest(FALSE_CALLABLE);
  44. fail();
  45. } catch (OutOfScopeException expected) {}
  46. }
  47. public void testTransferNonHttp_outOfScope() {
  48. try {
  49. ServletScopes.transferRequest(FALSE_CALLABLE);
  50. fail();
  51. } catch (OutOfScopeException expected) {}
  52. }
  53. public void testTransferNonHttp_outOfScope_closeable() {
  54. try {
  55. ServletScopes.transferRequest();
  56. fail();
  57. } catch (OutOfScopeException expected) {}
  58. }
  59. public void testTransferNonHttpRequest() throws Exception {
  60. final Injector injector = Guice.createInjector(new AbstractModule() {
  61. @Override protected void configure() {
  62. bindScope(RequestScoped.class, ServletScopes.REQUEST);
  63. }
  64. @Provides @RequestScoped Object provideObject() {
  65. return new Object();
  66. }
  67. });
  68. Callable<Callable<Boolean>> callable = new Callable<Callable<Boolean>>() {
  69. @Override public Callable<Boolean> call() {
  70. final Object original = injector.getInstance(Object.class);
  71. return ServletScopes.transferRequest(new Callable<Boolean>() {
  72. @Override public Boolean call() {
  73. return original == injector.getInstance(Object.class);
  74. }
  75. });
  76. }
  77. };
  78. ImmutableMap<Key<?>, Object> seedMap = ImmutableMap.of();
  79. Callable<Boolean> transfer = ServletScopes.scopeRequest(callable, seedMap).call();
  80. ExecutorService executor = Executors.newSingleThreadExecutor();
  81. assertTrue(executor.submit(transfer).get());
  82. executor.shutdownNow();
  83. }
  84. public void testTransferNonHttpRequest_closeable() throws Exception {
  85. final Injector injector = Guice.createInjector(new AbstractModule() {
  86. @Override protected void configure() {
  87. bindScope(RequestScoped.class, ServletScopes.REQUEST);
  88. }
  89. @Provides @RequestScoped Object provideObject() {
  90. return new Object();
  91. }
  92. });
  93. class Data {
  94. Object object;
  95. RequestScoper scoper;
  96. }
  97. Callable<Data> callable = new Callable<Data>() {
  98. @Override public Data call() {
  99. Data data = new Data();
  100. data.object = injector.getInstance(Object.class);
  101. data.scoper = ServletScopes.transferRequest();
  102. return data;
  103. }
  104. };
  105. ImmutableMap<Key<?>, Object> seedMap = ImmutableMap.of();
  106. Data data = ServletScopes.scopeRequest(callable, seedMap).call();
  107. ExecutorService executor = Executors.newSingleThreadExecutor();
  108. RequestScoper.CloseableScope scope = data.scoper.open();
  109. try {
  110. assertSame(data.object, injector.getInstance(Object.class));
  111. } finally {
  112. scope.close();
  113. executor.shutdownNow();
  114. }
  115. }
  116. public void testTransferNonHttpRequest_concurrentUseBlocks() throws Exception {
  117. Callable<Boolean> callable = new Callable<Boolean>() {
  118. @Override public Boolean call() throws Exception {
  119. ExecutorService executor = Executors.newSingleThreadExecutor();
  120. try {
  121. Future<Boolean> future = executor.submit(ServletScopes.transferRequest(FALSE_CALLABLE));
  122. try {
  123. return future.get(100, TimeUnit.MILLISECONDS);
  124. } catch (TimeoutException e) {
  125. return true;
  126. }
  127. } finally {
  128. executor.shutdownNow();
  129. }
  130. }
  131. };
  132. ImmutableMap<Key<?>, Object> seedMap = ImmutableMap.of();
  133. assertTrue(ServletScopes.scopeRequest(callable, seedMap).call());
  134. }
  135. public void testTransferNonHttpRequest_concurrentUseBlocks_closeable() throws Exception {
  136. Callable<Boolean> callable = new Callable<Boolean>() {
  137. @Override public Boolean call() throws Exception {
  138. final RequestScoper scoper = ServletScopes.transferRequest();
  139. ExecutorService executor = Executors.newSingleThreadExecutor();
  140. try {
  141. Future<Boolean> future = executor.submit(new Callable<Boolean>() {
  142. @Override public Boolean call() {
  143. RequestScoper.CloseableScope scope = scoper.open();
  144. try {
  145. return false;
  146. } finally {
  147. scope.close();
  148. }
  149. }
  150. });
  151. try {
  152. return future.get(100, TimeUnit.MILLISECONDS);
  153. } catch (TimeoutException e) {
  154. return true;
  155. }
  156. } finally {
  157. executor.shutdownNow();
  158. }
  159. }
  160. };
  161. ImmutableMap<Key<?>, Object> seedMap = ImmutableMap.of();
  162. assertTrue(ServletScopes.scopeRequest(callable, seedMap).call());
  163. }
  164. public void testTransferNonHttpRequest_concurrentUseSameThreadOk() throws Exception {
  165. Callable<Boolean> callable = new Callable<Boolean>() {
  166. @Override public Boolean call() throws Exception {
  167. return ServletScopes.transferRequest(FALSE_CALLABLE).call();
  168. }
  169. };
  170. ImmutableMap<Key<?>, Object> seedMap = ImmutableMap.of();
  171. assertFalse(ServletScopes.scopeRequest(callable, seedMap).call());
  172. }
  173. public void testTransferNonHttpRequest_concurrentUseSameThreadOk_closeable() throws Exception {
  174. Callable<Boolean> callable = new Callable<Boolean>() {
  175. @Override public Boolean call() throws Exception {
  176. RequestScoper.CloseableScope scope = ServletScopes.transferRequest().open();
  177. try {
  178. return false;
  179. } finally {
  180. scope.close();
  181. }
  182. }
  183. };
  184. ImmutableMap<Key<?>, Object> seedMap = ImmutableMap.of();
  185. assertFalse(ServletScopes.scopeRequest(callable, seedMap).call());
  186. }
  187. }