() {
public T get() {
if(key.getTypeLiteral().getRawType().isAssignableFrom(InjectableValueProvider.class)){
+ Preconditions.checkState(threadLocal.get() != null, "A scoping block is missing");
return (T) threadLocal.get();
} else {
return creator.get();
@@ -36,6 +37,7 @@ public String toString() {
}
public void open(InjectableValueProvider valueProvider){
+ Preconditions.checkState(threadLocal.get() == null, "A scoping block is already in progress");
threadLocal.set(valueProvider);
}
diff --git a/tef-impl/src/main/java/flipkart/tef/guicebridge/TypeListenerForDataInjection.java b/tef-impl/src/main/java/flipkart/tef/guicebridge/TypeListenerForDataInjection.java
index c2282fd..0bb6cec 100644
--- a/tef-impl/src/main/java/flipkart/tef/guicebridge/TypeListenerForDataInjection.java
+++ b/tef-impl/src/main/java/flipkart/tef/guicebridge/TypeListenerForDataInjection.java
@@ -1,13 +1,9 @@
package flipkart.tef.guicebridge;
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-import com.google.inject.Provider;
import com.google.inject.TypeLiteral;
import com.google.inject.spi.TypeEncounter;
import com.google.inject.spi.TypeListener;
import flipkart.tef.annotations.InjectData;
-import flipkart.tef.execution.InjectableValueProvider;
import java.lang.reflect.Field;
diff --git a/tef-impl/src/test/java/flipkart/tef/guicebridge/InjectDataGuiceMembersInjectorTest.java b/tef-impl/src/test/java/flipkart/tef/guicebridge/InjectDataGuiceMembersInjectorTest.java
index 0fe55bf..fddc2e1 100644
--- a/tef-impl/src/test/java/flipkart/tef/guicebridge/InjectDataGuiceMembersInjectorTest.java
+++ b/tef-impl/src/test/java/flipkart/tef/guicebridge/InjectDataGuiceMembersInjectorTest.java
@@ -4,6 +4,7 @@
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
+import com.google.inject.ProvisionException;
import com.google.inject.matcher.Matchers;
import flipkart.tef.annotations.InjectData;
import flipkart.tef.bizlogics.DataAdapterKey;
@@ -11,7 +12,7 @@
import flipkart.tef.exception.TefExecutionException;
import flipkart.tef.execution.DataContext;
import flipkart.tef.execution.InjectableValueProvider;
-import org.junit.Ignore;
+import org.junit.Assert;
import org.junit.Test;
import java.io.Serializable;
@@ -21,10 +22,10 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
public class InjectDataGuiceMembersInjectorTest {
- @Ignore
@Test
public void testRequestScopeBasic() {
@@ -40,7 +41,6 @@ protected void configure() {
new TesterThread(rootInjector).run();
}
- @Ignore
@Test
public void testRequestScopeThreaded() throws InterruptedException {
@@ -64,6 +64,76 @@ protected void configure() {
}
}
+ @Test
+ public void testRequestScopeWithoutEnteringScope() {
+
+ Injector rootInjector = Guice.createInjector(new GuiceBridgeModule(), new AbstractModule() {
+ @Override
+ protected void configure() {
+ bindListener(Matchers.any(), new TypeListenerForDataInjection());
+ }
+ });
+
+ Long threadId = Thread.currentThread().getId();
+ DataContext dataContext = new DataContext();
+ dataContext.put(new DataAdapterResult(new SimpleData()));
+ dataContext.put(new DataAdapterResult(threadId));
+ InjectableValueProvider valueProvider = new InjectableValueProvider() {
+
+ @Override
+ public Object getValueToInject(Class> fieldType, String name) throws TefExecutionException {
+ return dataContext.get(new DataAdapterKey<>(name, fieldType));
+ }
+ };
+ try {
+ SimpleInterface result = rootInjector.getInstance(SampleCallable.class).call();
+ Assert.fail("Injection should have failed");
+ } catch (ProvisionException e) {
+ assertTrue(e.getCause() instanceof IllegalStateException);
+ assertEquals("A scoping block is missing", e.getCause().getMessage());
+ }
+ }
+
+ @Test
+ public void testRequestScopeWithEnteringScopeTwice() {
+
+ Injector rootInjector = Guice.createInjector(new GuiceBridgeModule(), new AbstractModule() {
+ @Override
+ protected void configure() {
+ bindListener(Matchers.any(), new TypeListenerForDataInjection());
+ }
+ });
+
+ Long threadId = Thread.currentThread().getId();
+ try (TefGuiceScope scope = rootInjector.getInstance(TefGuiceScope.class)) {
+ DataContext dataContext = new DataContext();
+ dataContext.put(new DataAdapterResult(new SimpleData()));
+ dataContext.put(new DataAdapterResult(threadId));
+ InjectableValueProvider valueProvider = new InjectableValueProvider() {
+
+ @Override
+ public Object getValueToInject(Class> fieldType, String name) throws TefExecutionException {
+ return dataContext.get(new DataAdapterKey<>(name, fieldType));
+ }
+ };
+
+ InjectableValueProvider valueProvider2 = new InjectableValueProvider() {
+
+ @Override
+ public Object getValueToInject(Class> fieldType, String name) throws TefExecutionException {
+ return dataContext.get(new DataAdapterKey<>(name, fieldType));
+ }
+ };
+
+ scope.open(valueProvider);
+ try {
+ scope.open(valueProvider2);
+ } catch (IllegalStateException e) {
+ assertEquals("A scoping block is already in progress", e.getMessage());
+ }
+ }
+ }
+
static class TesterThread implements Runnable {
Injector rootInjector;
From 15f0ec017551f69f7784e95cf7452eec5a5f6d7e Mon Sep 17 00:00:00 2001
From: bageshwar <2353296+bageshwar@users.noreply.github.com>
Date: Wed, 1 Jun 2022 15:49:19 +0530
Subject: [PATCH 4/7] Remove un-necessary dependency
---
tef-impl/pom.xml | 7 -------
1 file changed, 7 deletions(-)
diff --git a/tef-impl/pom.xml b/tef-impl/pom.xml
index aa3b30e..29d00cb 100644
--- a/tef-impl/pom.xml
+++ b/tef-impl/pom.xml
@@ -40,13 +40,6 @@
4.12
test
-
-
- com.google.inject.extensions
- guice-servlet
- 4.0
-
-
\ No newline at end of file
From 12b6426071c8f6eee700e73a83a6174722d458e1 Mon Sep 17 00:00:00 2001
From: bageshwar <2353296+bageshwar@users.noreply.github.com>
Date: Wed, 1 Jun 2022 17:20:28 +0530
Subject: [PATCH 5/7] Fix and Test for subtype matcher.
---
.../tef/guicebridge/GuiceBridgeModule.java | 4 +
....java => SubclassOrAnnotationMatcher.java} | 14 ++--
.../tef/guicebridge/TefGuiceScope.java | 8 ++
.../tef/guicebridge/TefRequestScoped.java | 8 ++
.../TypeListenerForDataInjection.java | 8 ++
.../InjectDataGuiceMembersInjectorTest.java | 82 +++++++++++++++----
6 files changed, 100 insertions(+), 24 deletions(-)
rename tef-impl/src/main/java/flipkart/tef/guicebridge/{SubclassesOfMatcher.java => SubclassOrAnnotationMatcher.java} (61%)
diff --git a/tef-impl/src/main/java/flipkart/tef/guicebridge/GuiceBridgeModule.java b/tef-impl/src/main/java/flipkart/tef/guicebridge/GuiceBridgeModule.java
index fa06d77..6c5cae5 100644
--- a/tef-impl/src/main/java/flipkart/tef/guicebridge/GuiceBridgeModule.java
+++ b/tef-impl/src/main/java/flipkart/tef/guicebridge/GuiceBridgeModule.java
@@ -4,6 +4,10 @@
import com.google.inject.Injector;
import com.google.inject.Provides;
+/**
+ * Guice module to setup common infra for the bridge to work
+ * Date: 1/06/22
+ */
public class GuiceBridgeModule extends AbstractModule {
private final TefGuiceScope scope;
diff --git a/tef-impl/src/main/java/flipkart/tef/guicebridge/SubclassesOfMatcher.java b/tef-impl/src/main/java/flipkart/tef/guicebridge/SubclassOrAnnotationMatcher.java
similarity index 61%
rename from tef-impl/src/main/java/flipkart/tef/guicebridge/SubclassesOfMatcher.java
rename to tef-impl/src/main/java/flipkart/tef/guicebridge/SubclassOrAnnotationMatcher.java
index 858abb5..c17b01b 100644
--- a/tef-impl/src/main/java/flipkart/tef/guicebridge/SubclassesOfMatcher.java
+++ b/tef-impl/src/main/java/flipkart/tef/guicebridge/SubclassOrAnnotationMatcher.java
@@ -1,5 +1,6 @@
package flipkart.tef.guicebridge;
+import com.google.inject.TypeLiteral;
import com.google.inject.matcher.AbstractMatcher;
import java.io.Serializable;
@@ -8,27 +9,28 @@
/**
* Guice matcher for matching subclasses using TypeLiteral.
- *
+ *
* Date: 31/05/22
*/
-public class SubclassesOfMatcher extends AbstractMatcher