Modified:
trunk/src/atunit/guice/GuiceContainer.java
trunk/test/atunit/guice/GuiceContainerTests.java
Log:
fixed Guice bindings for test fields with generic types
Modified: trunk/src/atunit/guice/GuiceContainer.java
==============================================================================
--- trunk/src/atunit/guice/GuiceContainer.java (original)
+++ trunk/src/atunit/guice/GuiceContainer.java Mon Mar 24 08:12:33 2008
@@ -17,6 +17,7 @@
package atunit.guice;
import java.lang.reflect.Field;
+import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Map;
@@ -26,7 +27,9 @@
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
+import com.google.inject.Key;
import com.google.inject.Module;
+import com.google.inject.TypeLiteral;
import atunit.core.Container;
@@ -58,17 +61,18 @@
protected void configure() {
// map field values by type
- Multimap<Class, Field> fieldsByType = Multimaps.newHashMultimap();
+ Multimap<Type, Field> fieldsByType = Multimaps.newHashMultimap();
for ( Field field : fields.keySet() ) {
- fieldsByType.put(field.getType(), field);
+ fieldsByType.put(field.getGenericType(), field);
}
// for any types that don't have duplicates, bind instances.
- for ( Class type : fieldsByType.keySet() ) {
+ for ( Type type : fieldsByType.keySet() ) {
Collection<Field> fields = fieldsByType.get(type);
if ( fields.size() == 1 ) {
Field field = Iterables.getOnlyElement(fields);
- bind(type).toInstance(this.fields.get(field));
+ TypeLiteral literal = TypeLiteral.get(type);
+ bind(literal).toInstance(this.fields.get(field));
}
}
}
Modified: trunk/test/atunit/guice/GuiceContainerTests.java
==============================================================================
--- trunk/test/atunit/guice/GuiceContainerTests.java (original)
+++ trunk/test/atunit/guice/GuiceContainerTests.java Mon Mar 24
08:12:33 2008
@@ -3,6 +3,8 @@
import static org.junit.Assert.*;
import java.lang.reflect.Field;
+import java.util.LinkedList;
+import java.util.List;
import java.util.Map;
import org.junit.Before;
@@ -10,6 +12,7 @@
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
+import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.inject.Inject;
@@ -58,6 +61,17 @@
// this should have our value
assertEquals(3, df.field3.intValue());
}
+
+ @Test
+ public void tGenericFieldType() throws Exception {
+ GuiceContainer container = new GuiceContainer();
+ Map<Field,Object> fieldValues = Maps.newHashMap();
+ List<String> stringList = Lists.newLinkedList();
+ fieldValues.put(
GenericFieldType.class.getDeclaredField("stringList"), stringList);
+ GenericFieldType gft =
(GenericFieldType)container.createTest(GenericFieldType.class, fieldValues);
+
+ assertSame(stringList, gft.stringList);
+ }
protected static class Inheritance extends ExampleGuiceTest {
public Inheritance() {}
@@ -67,5 +81,9 @@
@Inject public String field1;
@Inject public String field2;
@Inject public Integer field3;
+ }
+
+ protected static class GenericFieldType {
+ @Inject public List<String> stringList;
}
}