You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2014/10/31 16:08:58 UTC
git commit: TOMEE-1436 RunAsRule
Repository: tomee
Updated Branches:
refs/heads/develop 5a0ed92e5 -> 5cede8c77
TOMEE-1436 RunAsRule
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/5cede8c7
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/5cede8c7
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/5cede8c7
Branch: refs/heads/develop
Commit: 5cede8c77a6336bd24ecea65f61e87d71ecbec66
Parents: 5a0ed92
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Fri Oct 31 16:08:44 2014 +0100
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Fri Oct 31 16:08:44 2014 +0100
----------------------------------------------------------------------
.../org/apache/openejb/testing/RunAsRule.java | 82 ++++++++++++++++++++
.../apache/openejb/testing/RunAsRuleTest.java | 66 ++++++++++++++++
2 files changed, 148 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tomee/blob/5cede8c7/container/openejb-core/src/main/java/org/apache/openejb/testing/RunAsRule.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/testing/RunAsRule.java b/container/openejb-core/src/main/java/org/apache/openejb/testing/RunAsRule.java
new file mode 100644
index 0000000..ce86b81
--- /dev/null
+++ b/container/openejb-core/src/main/java/org/apache/openejb/testing/RunAsRule.java
@@ -0,0 +1,82 @@
+/*
+ * 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;
+
+import org.apache.openejb.BeanContext;
+import org.apache.openejb.core.ThreadContext;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+import javax.annotation.security.RunAs;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+public class RunAsRule implements TestRule {
+ private final ThreadLocal<String> role = new ThreadLocal<>();
+
+ public void role(final String role) {
+ this.role.set(role);
+ }
+
+ @Override
+ public Statement apply(final Statement base, final Description description) {
+ return new Statement() {
+ @Override
+ public void evaluate() throws Throwable {
+ final RunAs annotation = description.getAnnotation(RunAs.class);
+ final As as = description.getAnnotation(As.class);
+ String currentRole = role.get();
+ if (annotation == null && as == null && currentRole == null) {
+ role.remove();
+ base.evaluate();
+ return;
+ }
+
+ final ThreadContext threadContext = ThreadContext.getThreadContext();
+ if (threadContext == null) {
+ throw new IllegalStateException("No context arounding RunAs rule, start ApplicationComposerRule before please");
+ }
+
+ final BeanContext beanContext = threadContext.getBeanContext();
+ if (currentRole == null) {
+ if (annotation == null) {
+ currentRole = as.value();
+ } else {
+ currentRole = annotation.value();
+ }
+ }
+ beanContext.setRunAs(currentRole);
+ final ThreadContext old = ThreadContext.enter(new ThreadContext(beanContext, null));
+ try {
+ base.evaluate();
+ } finally {
+ role.remove(); // reset for next test
+ ThreadContext.exit(old);
+ }
+ }
+ };
+ }
+
+ @Target({ElementType.METHOD, ElementType.TYPE}) // cause @RunAs doesn't support method
+ @Retention(RetentionPolicy.RUNTIME)
+ public @interface As {
+ String value();
+ }
+}
http://git-wip-us.apache.org/repos/asf/tomee/blob/5cede8c7/container/openejb-core/src/test/java/org/apache/openejb/testing/RunAsRuleTest.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/testing/RunAsRuleTest.java b/container/openejb-core/src/test/java/org/apache/openejb/testing/RunAsRuleTest.java
new file mode 100644
index 0000000..deb838c
--- /dev/null
+++ b/container/openejb-core/src/test/java/org/apache/openejb/testing/RunAsRuleTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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;
+
+import org.apache.openejb.jee.EnterpriseBean;
+import org.apache.openejb.jee.SingletonBean;
+import org.apache.openejb.junit.ApplicationComposerRule;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.RuleChain;
+import org.junit.rules.TestRule;
+
+import javax.annotation.Resource;
+import javax.ejb.EJB;
+import javax.ejb.SessionContext;
+import javax.naming.NamingException;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class RunAsRuleTest {
+ @Rule
+ public final TestRule container = RuleChain.outerRule(new ApplicationComposerRule(this)).around(new RunAsRule());
+
+ @Module
+ public EnterpriseBean bean() {
+ return new SingletonBean(MyBean.class).localBean();
+ }
+
+ @EJB
+ private MyBean ejb;
+
+ @Test
+ public void no() throws NamingException {
+ assertFalse(ejb.isTest());
+ }
+
+ @Test
+ @RunAsRule.As("test")
+ public void yes() throws NamingException {
+ assertTrue(ejb.isTest());
+ }
+
+ public static class MyBean {
+ @Resource
+ private SessionContext sc;
+
+ public boolean isTest() {
+ return sc.isCallerInRole("test");
+ }
+ }
+}