PageRenderTime 2ms CodeModel.GetById 2ms app.highlight 7ms 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
 17package com.google.inject.servlet;
 18
 19import com.google.common.collect.ImmutableMap;
 20import com.google.inject.AbstractModule;
 21import com.google.inject.Guice;
 22import com.google.inject.Injector;
 23import com.google.inject.Key;
 24import com.google.inject.OutOfScopeException;
 25import com.google.inject.Provides;
 26
 27import junit.framework.TestCase;
 28
 29import java.util.concurrent.Callable;
 30import java.util.concurrent.ExecutorService;
 31import java.util.concurrent.Executors;
 32import java.util.concurrent.Future;
 33import java.util.concurrent.TimeUnit;
 34import java.util.concurrent.TimeoutException;
 35
 36// TODO: Add test for HTTP transferring.
 37/**
 38 * Tests transferring of entire request scope.
 39 */
 40
 41public class TransferRequestIntegrationTest extends TestCase {
 42  private final Callable<Boolean> FALSE_CALLABLE = new Callable<Boolean>() {
 43    @Override public Boolean call() {
 44      return false;
 45    }
 46  };
 47
 48  public void testTransferHttp_outOfScope() {
 49    try {
 50      ServletScopes.transferRequest(FALSE_CALLABLE);
 51      fail();
 52    } catch (OutOfScopeException expected) {}
 53  }
 54
 55  public void testTransferNonHttp_outOfScope() {
 56    try {
 57      ServletScopes.transferRequest(FALSE_CALLABLE);
 58      fail();
 59    } catch (OutOfScopeException expected) {}
 60  }
 61
 62  public void testTransferNonHttp_outOfScope_closeable() {
 63    try {
 64      ServletScopes.transferRequest();
 65      fail();
 66    } catch (OutOfScopeException expected) {}
 67  }
 68
 69  public void testTransferNonHttpRequest() throws Exception {
 70    final Injector injector = Guice.createInjector(new AbstractModule() {
 71      @Override protected void configure() {
 72        bindScope(RequestScoped.class, ServletScopes.REQUEST);
 73      }
 74
 75      @Provides @RequestScoped Object provideObject() {
 76        return new Object();
 77      }
 78    });
 79
 80    Callable<Callable<Boolean>> callable = new Callable<Callable<Boolean>>() {
 81      @Override public Callable<Boolean> call() {
 82        final Object original = injector.getInstance(Object.class);
 83        return ServletScopes.transferRequest(new Callable<Boolean>() {
 84          @Override public Boolean call() {
 85            return original == injector.getInstance(Object.class);
 86          }
 87        });
 88      }
 89    };
 90
 91    ImmutableMap<Key<?>, Object> seedMap = ImmutableMap.of();
 92    Callable<Boolean> transfer = ServletScopes.scopeRequest(callable, seedMap).call();
 93
 94    ExecutorService executor = Executors.newSingleThreadExecutor();
 95    assertTrue(executor.submit(transfer).get());
 96    executor.shutdownNow();
 97  }
 98
 99  public void testTransferNonHttpRequest_closeable() throws Exception {
100    final Injector injector = Guice.createInjector(new AbstractModule() {
101      @Override protected void configure() {
102        bindScope(RequestScoped.class, ServletScopes.REQUEST);
103      }
104
105      @Provides @RequestScoped Object provideObject() {
106        return new Object();
107      }
108    });
109
110    class Data {
111      Object object;
112      RequestScoper scoper;
113    }
114
115    Callable<Data> callable = new Callable<Data>() {
116      @Override public Data call() {
117        Data data = new Data();
118        data.object = injector.getInstance(Object.class);
119        data.scoper = ServletScopes.transferRequest();
120        return data;
121      }
122    };
123
124    ImmutableMap<Key<?>, Object> seedMap = ImmutableMap.of();
125    Data data = ServletScopes.scopeRequest(callable, seedMap).call();
126
127    ExecutorService executor = Executors.newSingleThreadExecutor();
128    RequestScoper.CloseableScope scope = data.scoper.open();
129    try {
130      assertSame(data.object, injector.getInstance(Object.class));
131    } finally {
132      scope.close();
133      executor.shutdownNow();
134    }
135  }
136
137  public void testTransferNonHttpRequest_concurrentUseBlocks() throws Exception {
138    Callable<Boolean> callable = new Callable<Boolean>() {
139      @Override public Boolean call() throws Exception {
140        ExecutorService executor = Executors.newSingleThreadExecutor();
141        try {
142          Future<Boolean> future = executor.submit(ServletScopes.transferRequest(FALSE_CALLABLE));
143          try {
144            return future.get(100, TimeUnit.MILLISECONDS);
145          } catch (TimeoutException e) {
146            return true;
147          }
148        } finally {
149          executor.shutdownNow();
150        }
151      }
152    };
153
154    ImmutableMap<Key<?>, Object> seedMap = ImmutableMap.of();
155    assertTrue(ServletScopes.scopeRequest(callable, seedMap).call());
156  }
157
158  public void testTransferNonHttpRequest_concurrentUseBlocks_closeable() throws Exception {
159    Callable<Boolean> callable = new Callable<Boolean>() {
160      @Override public Boolean call() throws Exception {
161        final RequestScoper scoper = ServletScopes.transferRequest();
162        ExecutorService executor = Executors.newSingleThreadExecutor();
163        try {
164          Future<Boolean> future = executor.submit(new Callable<Boolean>() {
165            @Override public Boolean call() {
166              RequestScoper.CloseableScope scope = scoper.open();
167              try {
168                return false;
169              } finally {
170                scope.close();
171              }
172            }
173          });
174          try {
175            return future.get(100, TimeUnit.MILLISECONDS);
176          } catch (TimeoutException e) {
177            return true;
178          }
179        } finally {
180          executor.shutdownNow();
181        }
182      }
183    };
184
185    ImmutableMap<Key<?>, Object> seedMap = ImmutableMap.of();
186    assertTrue(ServletScopes.scopeRequest(callable, seedMap).call());
187  }
188
189  public void testTransferNonHttpRequest_concurrentUseSameThreadOk() throws Exception {
190    Callable<Boolean> callable = new Callable<Boolean>() {
191      @Override public Boolean call() throws Exception {
192        return ServletScopes.transferRequest(FALSE_CALLABLE).call();
193      }
194    };
195
196    ImmutableMap<Key<?>, Object> seedMap = ImmutableMap.of();
197    assertFalse(ServletScopes.scopeRequest(callable, seedMap).call());
198  }
199
200  public void testTransferNonHttpRequest_concurrentUseSameThreadOk_closeable() throws Exception {
201    Callable<Boolean> callable = new Callable<Boolean>() {
202      @Override public Boolean call() throws Exception {
203        RequestScoper.CloseableScope scope = ServletScopes.transferRequest().open();
204        try {
205          return false;
206        } finally {
207          scope.close();
208        }
209      }
210    };
211
212    ImmutableMap<Key<?>, Object> seedMap = ImmutableMap.of();
213    assertFalse(ServletScopes.scopeRequest(callable, seedMap).call());
214  }
215}