Return-Path: Delivered-To: apmail-openejb-users-archive@www.apache.org Received: (qmail 93959 invoked from network); 3 Feb 2011 11:16:00 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 3 Feb 2011 11:16:00 -0000 Received: (qmail 45351 invoked by uid 500); 3 Feb 2011 11:16:00 -0000 Delivered-To: apmail-openejb-users-archive@openejb.apache.org Received: (qmail 45124 invoked by uid 500); 3 Feb 2011 11:15:57 -0000 Mailing-List: contact users-help@openejb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@openejb.apache.org Delivered-To: mailing list users@openejb.apache.org Received: (qmail 45116 invoked by uid 99); 3 Feb 2011 11:15:56 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 03 Feb 2011 11:15:56 +0000 X-ASF-Spam-Status: No, hits=3.5 required=5.0 tests=HTML_MESSAGE,NORMAL_HTTP_TO_IP,RCVD_IN_DNSWL_LOW,SPF_NEUTRAL,URI_HEX X-Spam-Check-By: apache.org Received-SPF: neutral (nike.apache.org: local policy) Received: from [209.85.216.48] (HELO mail-qw0-f48.google.com) (209.85.216.48) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 03 Feb 2011 11:15:48 +0000 Received: by qwe4 with SMTP id 4so835847qwe.35 for ; Thu, 03 Feb 2011 03:15:27 -0800 (PST) MIME-Version: 1.0 Received: by 10.229.74.129 with SMTP id u1mr7334039qcj.82.1296731727204; Thu, 03 Feb 2011 03:15:27 -0800 (PST) Received: by 10.229.250.15 with HTTP; Thu, 3 Feb 2011 03:15:27 -0800 (PST) X-Originating-IP: [70.81.2.208] In-Reply-To: <1296717849351-3257408.post@n4.nabble.com> References: <1296717849351-3257408.post@n4.nabble.com> Date: Thu, 3 Feb 2011 07:15:27 -0400 Message-ID: Subject: Re: JUnit: Sessioncontext.isCallerInRole allways returns false From: Thiago Veronezi To: users@openejb.apache.org Content-Type: multipart/alternative; boundary=00163646d92ef811eb049b5ee10e X-Virus-Checked: Checked by ClamAV on apache.org --00163646d92ef811eb049b5ee10e Content-Type: text/plain; charset=ISO-8859-1 Hi nick, This is the default behaviour of EJB. According to the EJB3.1 specs: 17.3.4.1: Run-as Establishing a run-as identity for an enterprise bean does not affect the identities of its callers, which are the identities tested for permission to access the methods of the enterprise bean. The run-as identity establishes the identity the enterprise bean will use when it makes calls. []s, Thiago. On Thu, Feb 3, 2011 at 3:24 AM, ndegraev wrote: > > I need to write a session bean that somewhere in the code checks if the > current user has some role(s). > > To unittest my EJB3 I'm trying out OpenEJB. I followed the example about > testing security but if I test in my code for the role with > SessionContect.isCallerInRole() it always returns false. > > Why doesn't it work? > > I've written some code to illustrate. > > My local interface: > > @Local > public interface MyBean { > > boolean doSomething(); > > } > > My EJB: > > @Stateless > public class MyBeanImpl implements MyBean { > > @Resource > private SessionContext sessionContext; > > @Override > public boolean doSomething() { > return this.sessionContext.isCallerInRole("role1"); > } > > } > > My test: > > public class MyBeanTest { > > private Context context; > > @Before > public void setUp() throws Exception { > final Properties properties = new Properties(); > properties.put(Context.INITIAL_CONTEXT_FACTORY, > "org.apache.openejb.client.LocalInitialContextFactory"); > > this.context = new InitialContext(properties); > } > > @Test > public void test1() throws Exception { > final Caller roleBean = (Caller) > this.context.lookup("RoleBeanLocal"); > roleBean.call(new Callable() { > > @Override > public Object call() throws Exception { > final MyBean myBean = (MyBean) > MyBeanTest.this.context.lookup("MyBeanImplLocal"); > Assert.assertTrue(myBean.doSomething()); // FAILURE > return null; > } > }); > } > > @Test > public void test2() throws Exception { > final Caller role2Bean = (Caller) > this.context.lookup("Role2BeanLocal"); > role2Bean.call(new Callable() { > > @Override > public Object call() throws Exception { > final MyBean myBean = (MyBean) > MyBeanTest.this.context.lookup("MyBeanImplLocal"); > Assert.assertFalse(myBean.doSomething()); > return null; > } > }); > } > > public static interface Caller { > > V call(Callable callable) throws Exception; > > } > > @Stateless > @RunAs("role1") > public static class RoleBean implements Caller { > > @Override > public V call(final Callable callable) throws Exception { > return callable.call(); > } > > } > > @Stateless > @RunAs("role2") > public static class Role2Bean implements Caller { > > @Override > public V call(final Callable callable) throws Exception { > return callable.call(); > } > > } > } > > > -- > View this message in context: > http://openejb.979440.n4.nabble.com/JUnit-Sessioncontext-isCallerInRole-allways-returns-false-tp3257408p3257408.html > Sent from the OpenEJB User mailing list archive at Nabble.com. > --00163646d92ef811eb049b5ee10e--