PageRenderTime 3947ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/metamorphiccode/guice
Java | 195 lines | 143 code | 33 blank | 19 comment | 0 complexity | 4caf14023c5deec0cc4c2cb3ece4db9b MD5 | raw file
  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 junit.framework.TestCase;
  29. import java.lang.annotation.Retention;
  30. import java.lang.annotation.RetentionPolicy;
  31. import java.net.BindException;
  32. import java.rmi.RemoteException;
  33. /**
  34. * Test methods for {@link CheckedProviderMethodsModule}.
  35. */
  36. public class CheckedProviderMethodsModuleTest extends TestCase {
  37. private final TypeLiteral<RpcProvider<String>> rpcProviderOfString
  38. = new TypeLiteral<RpcProvider<String>>() { };
  39. private final TypeLiteral<RpcProvider<Integer>> rpcProviderOfInteger
  40. = new TypeLiteral<RpcProvider<Integer>>() { };
  41. private final TypeLiteral<RpcProvider<Long>> rpcProviderOfLong
  42. = new TypeLiteral<RpcProvider<Long>>() { };
  43. private final TypeLiteral<RpcProvider<Float>> rpcProviderOfFloat
  44. = new TypeLiteral<RpcProvider<Float>>() { };
  45. private final TypeLiteral<RpcProvider<Pair<Double, String>>> rpcProviderOfPair
  46. = new TypeLiteral<RpcProvider<Pair<Double, String>>>() { };
  47. private final TestScope testScope = new TestScope();
  48. interface RpcProvider<T> extends CheckedProvider<T> {
  49. T get() throws RemoteException, BindException;
  50. }
  51. @Retention(RetentionPolicy.RUNTIME)
  52. @BindingAnnotation
  53. @interface TestAnnotation {
  54. }
  55. class TestModule extends AbstractModule {
  56. private int nextIntToReturn = 100;
  57. @Override
  58. protected void configure() {
  59. bindScope(TestScope.Scoped.class, testScope);
  60. install(ThrowingProviderBinder.forModule(this));
  61. install(new TestPrivateModule());
  62. }
  63. @CheckedProvides(RpcProvider.class)
  64. String getSomeStringFromServer() {
  65. return "Works";
  66. }
  67. @CheckedProvides(RpcProvider.class) @TestScope.Scoped
  68. int getSomeIntegerFromServer() {
  69. return nextIntToReturn;
  70. }
  71. @CheckedProvides(RpcProvider.class) @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) @Named("fruit") @Exposed
  97. String provideApples() {
  98. return "apple";
  99. }
  100. }
  101. public void testNoAnnotationNoScope() throws BindException, RemoteException {
  102. Injector injector = Guice.createInjector(new TestModule());
  103. RpcProvider<String> provider = injector
  104. .getInstance(Key.get(rpcProviderOfString));
  105. assertEquals("Works", provider.get());
  106. }
  107. public void testWithScope() throws BindException, RemoteException {
  108. TestModule testModule = new TestModule();
  109. Injector injector = Guice.createInjector(testModule);
  110. RpcProvider<Integer> provider = injector
  111. .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 = injector
  122. .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
  129. .getInstance(Key.get(rpcProviderOfPair));
  130. Pair<Double, String> pair = provider.get();
  131. assertEquals(pair.first, 4.0d);
  132. }
  133. public void testWithThrownException() {
  134. TestModule testModule = new TestModule();
  135. Injector injector = Guice.createInjector(testModule);
  136. RpcProvider<Float> provider = injector
  137. .getInstance(Key.get(rpcProviderOfFloat));
  138. try {
  139. provider.get();
  140. fail();
  141. } catch (RemoteException e) {
  142. fail();
  143. } catch (BindException e) {
  144. // good
  145. }
  146. }
  147. public void testExposedMethod() throws BindException, RemoteException {
  148. TestModule testModule = new TestModule();
  149. Injector injector = Guice.createInjector(testModule);
  150. RpcProvider<String> provider = injector
  151. .getInstance(Key.get(rpcProviderOfString, Names.named("fruit")));
  152. assertEquals("apple", provider.get());
  153. }
  154. private static class Pair<A, B> {
  155. A first;
  156. B second;
  157. Pair(A a, B b) {
  158. this.first= a;
  159. this.second = b;
  160. }
  161. }
  162. }