PageRenderTime 825ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/throwingproviders/test/com/google/inject/throwingproviders/CheckedProviderMethodsModuleTest.java

https://github.com/sonatype/sisu-guice
Java | 189 lines | 143 code | 29 blank | 17 comment | 0 complexity | d587f61898f22d039c6ce6cef672299f MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright (C) 2009 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.throwingproviders;
  17. import com.google.inject.AbstractModule;
  18. import com.google.inject.BindingAnnotation;
  19. import com.google.inject.Exposed;
  20. import com.google.inject.Guice;
  21. import com.google.inject.Injector;
  22. import com.google.inject.Key;
  23. import com.google.inject.PrivateModule;
  24. import com.google.inject.Provides;
  25. import com.google.inject.TypeLiteral;
  26. import com.google.inject.name.Named;
  27. import com.google.inject.name.Names;
  28. import java.lang.annotation.Retention;
  29. import java.lang.annotation.RetentionPolicy;
  30. import java.net.BindException;
  31. import java.rmi.RemoteException;
  32. import junit.framework.TestCase;
  33. /** Test methods for {@link CheckedProviderMethodsModule}. */
  34. public class CheckedProviderMethodsModuleTest extends TestCase {
  35. private final TypeLiteral<RpcProvider<String>> rpcProviderOfString =
  36. new TypeLiteral<RpcProvider<String>>() {};
  37. private final TypeLiteral<RpcProvider<Integer>> rpcProviderOfInteger =
  38. new TypeLiteral<RpcProvider<Integer>>() {};
  39. private final TypeLiteral<RpcProvider<Long>> rpcProviderOfLong =
  40. new TypeLiteral<RpcProvider<Long>>() {};
  41. private final TypeLiteral<RpcProvider<Float>> rpcProviderOfFloat =
  42. new TypeLiteral<RpcProvider<Float>>() {};
  43. private final TypeLiteral<RpcProvider<Pair<Double, String>>> rpcProviderOfPair =
  44. new TypeLiteral<RpcProvider<Pair<Double, String>>>() {};
  45. private final TestScope testScope = new TestScope();
  46. interface RpcProvider<T> extends CheckedProvider<T> {
  47. @Override
  48. T get() throws RemoteException, BindException;
  49. }
  50. @Retention(RetentionPolicy.RUNTIME)
  51. @BindingAnnotation
  52. @interface TestAnnotation {}
  53. class TestModule extends AbstractModule {
  54. private int nextIntToReturn = 100;
  55. @Override
  56. protected void configure() {
  57. bindScope(TestScope.Scoped.class, testScope);
  58. install(ThrowingProviderBinder.forModule(this));
  59. install(new TestPrivateModule());
  60. }
  61. @CheckedProvides(RpcProvider.class)
  62. String getSomeStringFromServer() {
  63. return "Works";
  64. }
  65. @CheckedProvides(RpcProvider.class)
  66. @TestScope.Scoped
  67. int getSomeIntegerFromServer() {
  68. return nextIntToReturn;
  69. }
  70. @CheckedProvides(RpcProvider.class)
  71. @TestAnnotation
  72. long getSomeLongFromServer() {
  73. return 0xffL;
  74. }
  75. @Provides
  76. double getSomeDouble() {
  77. return 2.0d;
  78. }
  79. @CheckedProvides(RpcProvider.class)
  80. Pair<Double, String> getSomePair(Double input) {
  81. return new Pair<Double, String>(input * 2, "foo");
  82. }
  83. @CheckedProvides(RpcProvider.class)
  84. float getFloat() throws BindException {
  85. throw new BindException("foo");
  86. }
  87. void setNextIntToReturn(int next) {
  88. nextIntToReturn = next;
  89. }
  90. }
  91. class TestPrivateModule extends PrivateModule {
  92. @Override
  93. protected void configure() {
  94. install(ThrowingProviderBinder.forModule(this));
  95. }
  96. @CheckedProvides(RpcProvider.class)
  97. @Named("fruit")
  98. @Exposed
  99. String provideApples() {
  100. return "apple";
  101. }
  102. }
  103. public void testNoAnnotationNoScope() throws BindException, RemoteException {
  104. Injector injector = Guice.createInjector(new TestModule());
  105. RpcProvider<String> provider = injector.getInstance(Key.get(rpcProviderOfString));
  106. assertEquals("Works", provider.get());
  107. }
  108. public void testWithScope() throws BindException, RemoteException {
  109. TestModule testModule = new TestModule();
  110. Injector injector = Guice.createInjector(testModule);
  111. RpcProvider<Integer> provider = injector.getInstance(Key.get(rpcProviderOfInteger));
  112. assertEquals((Integer) 100, provider.get());
  113. testModule.setNextIntToReturn(120);
  114. assertEquals((Integer) 100, provider.get());
  115. testScope.beginNewScope();
  116. assertEquals((Integer) 120, provider.get());
  117. }
  118. public void testWithAnnotation() throws BindException, RemoteException {
  119. TestModule testModule = new TestModule();
  120. Injector injector = Guice.createInjector(testModule);
  121. RpcProvider<Long> provider =
  122. injector.getInstance(Key.get(rpcProviderOfLong, TestAnnotation.class));
  123. assertEquals((Long) 0xffL, provider.get());
  124. }
  125. public void testWithInjectedParameters() throws BindException, RemoteException {
  126. TestModule testModule = new TestModule();
  127. Injector injector = Guice.createInjector(testModule);
  128. RpcProvider<Pair<Double, String>> provider = injector.getInstance(Key.get(rpcProviderOfPair));
  129. Pair<Double, String> pair = provider.get();
  130. assertEquals(pair.first, 4.0d, 0.0);
  131. }
  132. public void testWithThrownException() {
  133. TestModule testModule = new TestModule();
  134. Injector injector = Guice.createInjector(testModule);
  135. RpcProvider<Float> provider = injector.getInstance(Key.get(rpcProviderOfFloat));
  136. try {
  137. provider.get();
  138. fail();
  139. } catch (RemoteException e) {
  140. fail();
  141. } catch (BindException e) {
  142. // good
  143. }
  144. }
  145. public void testExposedMethod() throws BindException, RemoteException {
  146. TestModule testModule = new TestModule();
  147. Injector injector = Guice.createInjector(testModule);
  148. RpcProvider<String> provider =
  149. injector.getInstance(Key.get(rpcProviderOfString, Names.named("fruit")));
  150. assertEquals("apple", provider.get());
  151. }
  152. private static class Pair<A, B> {
  153. A first;
  154. B second;
  155. Pair(A a, B b) {
  156. this.first = a;
  157. this.second = b;
  158. }
  159. }
  160. }