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