Repository: tomee
Updated Branches:
  refs/heads/tomee-1.7.x 3b9c0e6c4 -> ebbbc2987


adding ContextProvider fr tests


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/ebbbc298
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/ebbbc298
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/ebbbc298

Branch: refs/heads/tomee-1.7.x
Commit: ebbbc2987528313a5ba6c49ab9e094ee90a880dc
Parents: 3b9c0e6
Author: Romain Manni-Bucau <[email protected]>
Authored: Thu Dec 11 11:06:19 2014 +0100
Committer: Romain Manni-Bucau <[email protected]>
Committed: Thu Dec 11 11:06:19 2014 +0100

----------------------------------------------------------------------
 .../openejb/testing/ApplicationComposers.java   | 11 ++++
 .../openejb/testing/rest/ContextProvider.java   | 54 ++++++++++++++++++++
 .../AppComposerContextInjectionTest.java        | 42 +++++++++++++++
 3 files changed, 107 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/ebbbc298/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
----------------------------------------------------------------------
diff --git 
a/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
 
b/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
index 5855754..449feb3 100644
--- 
a/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
+++ 
b/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
@@ -66,7 +66,9 @@ import org.apache.openejb.jee.oejb3.OpenejbJar;
 import org.apache.openejb.jee.oejb3.PojoDeployment;
 import org.apache.openejb.loader.IO;
 import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.rest.RESTResourceFinder;
 import org.apache.openejb.spi.ContainerSystem;
+import org.apache.openejb.testing.rest.ContextProvider;
 import org.apache.openejb.util.Join;
 import org.apache.openejb.util.NetworkUtil;
 import org.apache.openejb.util.ServiceManagerProxy;
@@ -847,6 +849,15 @@ public final class ApplicationComposers {
                 field.set(inputTestInstance, new InitialContext(new 
Properties() {{
                     setProperty(Context.INITIAL_CONTEXT_FACTORY, 
LocalInitialContextFactory.class.getName());
                 }}));
+            } else if (ContextProvider.class.isAssignableFrom(type)) {
+                RESTResourceFinder finder = 
SystemInstance.get().getComponent(RESTResourceFinder.class);
+                if (finder == null || 
!ContextProvider.class.isInstance(finder)) {
+                    finder = new ContextProvider(finder);
+                    
SystemInstance.get().setComponent(RESTResourceFinder.class, finder);
+                }
+
+                field.setAccessible(true);
+                field.set(inputTestInstance, finder);
             } else {
                 throw new IllegalArgumentException("can't find value for type 
" + type.getName());
             }

http://git-wip-us.apache.org/repos/asf/tomee/blob/ebbbc298/container/openejb-core/src/main/java/org/apache/openejb/testing/rest/ContextProvider.java
----------------------------------------------------------------------
diff --git 
a/container/openejb-core/src/main/java/org/apache/openejb/testing/rest/ContextProvider.java
 
b/container/openejb-core/src/main/java/org/apache/openejb/testing/rest/ContextProvider.java
new file mode 100644
index 0000000..416ed76
--- /dev/null
+++ 
b/container/openejb-core/src/main/java/org/apache/openejb/testing/rest/ContextProvider.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.testing.rest;
+
+import org.apache.openejb.rest.RESTResourceFinder;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class ContextProvider implements RESTResourceFinder {
+    private final Map<Class<?>, Object> instances = new HashMap<Class<?>, 
Object>();
+    private final RESTResourceFinder defaults;
+
+    public ContextProvider() {
+        this(null);
+    }
+
+    public ContextProvider(final RESTResourceFinder fallback) {
+        this.defaults = fallback;
+    }
+
+    @Override
+    public <T> T find(final Class<T> clazz) {
+        final Object obj = instances.get(clazz);
+        if (obj == null) {
+            return defaults != null ? defaults.find(clazz) : null;
+        }
+        return clazz.cast(obj);
+    }
+
+    public ContextProvider deregister(final Class<?> contextType) {
+        instances.remove(contextType);
+        return this;
+    }
+
+    public <T> ContextProvider register(final Class<T> contextType, final T 
instance) {
+        instances.put(contextType, instance);
+        return this;
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/ebbbc298/container/openejb-core/src/test/java/org/apache/openejb/testing/AppComposerContextInjectionTest.java
----------------------------------------------------------------------
diff --git 
a/container/openejb-core/src/test/java/org/apache/openejb/testing/AppComposerContextInjectionTest.java
 
b/container/openejb-core/src/test/java/org/apache/openejb/testing/AppComposerContextInjectionTest.java
index 5366e11..379e6f9 100644
--- 
a/container/openejb-core/src/test/java/org/apache/openejb/testing/AppComposerContextInjectionTest.java
+++ 
b/container/openejb-core/src/test/java/org/apache/openejb/testing/AppComposerContextInjectionTest.java
@@ -19,13 +19,21 @@ package org.apache.openejb.testing;
 import org.apache.openejb.jee.EnterpriseBean;
 import org.apache.openejb.jee.SingletonBean;
 import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.rest.ThreadLocalContextManager;
+import org.apache.openejb.testing.rest.ContextProvider;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
 import javax.naming.Context;
 import javax.naming.NamingException;
+import javax.ws.rs.core.SecurityContext;
+import java.security.Principal;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
 @RunWith(ApplicationComposer.class)
 public class AppComposerContextInjectionTest {
@@ -37,6 +45,9 @@ public class AppComposerContextInjectionTest {
     @AppResource
     private Context context;
 
+    @AppResource
+    private ContextProvider provider;
+
     @Test
     public void lookupShouldWorkOnOpenEJBNames() throws NamingException {
         assertEquals("ok", 
MyBean.class.cast(context.lookup("MyBeanLocalBean")).ok());
@@ -47,6 +58,37 @@ public class AppComposerContextInjectionTest {
         assertEquals("ok", 
MyBean.class.cast(context.lookup("java:global/AppComposerContextInjectionTest/bean/MyBean")).ok());
     }
 
+    @Test
+    public void jaxrs() throws NamingException {
+        assertNotNull(provider);
+        assertNull(provider.find(SecurityContext.class));
+        final SecurityContext securityContext = new SecurityContext() {
+            @Override
+            public Principal getUserPrincipal() {
+                return null;
+            }
+
+            @Override
+            public boolean isUserInRole(final String s) {
+                return "foo".equals(s);
+            }
+
+            @Override
+            public boolean isSecure() {
+                return false;
+            }
+
+            @Override
+            public String getAuthenticationScheme() {
+                return null;
+            }
+        };
+        provider.register(SecurityContext.class, securityContext);
+        assertNotNull(provider.find(SecurityContext.class));
+        
assertTrue(SecurityContext.class.cast(ThreadLocalContextManager.findThreadLocal(SecurityContext.class)).isUserInRole("foo"));
+        
assertFalse(SecurityContext.class.cast(ThreadLocalContextManager.findThreadLocal(SecurityContext.class)).isUserInRole("bar"));
+    }
+
     public static class MyBean {
         public String ok() {
             return "ok";

Reply via email to