tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ndegraev <>
Subject Re: JUnit: Sessioncontext.isCallerInRole allways returns false
Date Tue, 08 Feb 2011 10:24:16 GMT

Thanks for the project. When I run it's as expected.

Now I'm trying to apply your examples on my test project and it fails. I
don't know why. It's probably something small but I just don't see it. Can
you have a look?

public interface MyTest {

    boolean hasRole(String role);

    boolean isCallerAdmin();


public class MyTestBean implements MyTest {

    private SessionContext sessionContext;

    public boolean hasRole(final String role) {
        return this.sessionContext.isCallerInRole(role);

    public boolean isCallerAdmin() {
        return this.sessionContext.isCallerInRole("admins");


public class MyTestCase {

    public void setUp() throws Exception {
        final ClassLoader contextClassLoader =
        final URL resource = contextClassLoader.getResource("login.config");

        final Properties properties = new Properties();
        new InitialContext(properties);

    public void test1() throws NamingException {
        System.out.println("test 1...");
        final Context ctx = getCtx("user", "user_pass");
        final MyTest myTest = (MyTest) ctx.lookup("MyTestBeanLocal");
        System.out.println("Is admin? " + myTest.isCallerAdmin());
        System.out.println("Is in 'admins'? " + myTest.hasRole("admins"));
        System.out.println("Is in 'users'? " + myTest.hasRole("users"));

    public void test2() throws NamingException {
        System.out.println("test 2...");
        final Context ctx = getCtx("admin", "admin_pass");
        final MyTest myTest = (MyTest) ctx.lookup("MyTestBeanLocal");
        System.out.println("Is admin? " + myTest.isCallerAdmin());
        System.out.println("Is in role 'admins'? " +
        System.out.println("Is in role 'users'? " +

    private Context getCtx(final String user, final String pass) throws
NamingException {
        final Properties properties = new Properties();
        properties.setProperty(Context.SECURITY_PRINCIPAL, user);
        properties.setProperty(Context.SECURITY_CREDENTIALS, pass);
        return new InitialContext(properties);

},, login.config and are
copied over from your project.

When I run it I get this output:

Apache OpenEJB 3.1.4    build: 20101112-03:32
INFO - openejb.home = C:\dvl\test-openejb
INFO - openejb.base = C:\dvl\test-openejb
INFO - Configuring Service(id=Default Security Service,
type=SecurityService, provider-id=Default Security Service)
INFO - Configuring Service(id=Default Transaction Manager,
type=TransactionManager, provider-id=Default Transaction Manager)
INFO - Found EjbModule in classpath: C:\dvl\test-openejb\target\classes
INFO - Beginning load: C:\dvl\test-openejb\target\classes
INFO - Configuring enterprise application: classpath.ear
INFO - Configuring Service(id=Default Stateless Container, type=Container,
provider-id=Default Stateless Container)
INFO - Auto-creating a container for bean MyTestBean:
Container(type=STATELESS, id=Default Stateless Container)
INFO - Using 'openejb.validation.output.level=VERBOSE'
INFO - Enterprise application "classpath.ear" loaded.
INFO - Assembling app: classpath.ear
INFO - Jndi(name=MyTestBeanLocal) --> Ejb(deployment-id=MyTestBean)
INFO - Created Ejb(deployment-id=MyTestBean, ejb-name=MyTestBean,
container=Default Stateless Container)
INFO - Deployed Application(path=classpath.ear)
test 1...
INFO - Logging in
Is admin? false
Is in 'admins'? false
Is in 'users'? false
test 2...
INFO - Logging in
Is admin? false
Is in role 'admins'? false
Is in role 'users'? false
View this message in context:
Sent from the OpenEJB User mailing list archive at

View raw message