openejb-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ndegraev <nick.degra...@telenet.be>
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?

MyTest.java:

@Local
public interface MyTest {

    boolean hasRole(String role);

    boolean isCallerAdmin();

}

MyTestBean.java:

@Stateless
public class MyTestBean implements MyTest {

    @Resource
    private SessionContext sessionContext;

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

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

}

MyTestCase.java:

public class MyTestCase {

    @Before
    public void setUp() throws Exception {
        final ClassLoader contextClassLoader =
Thread.currentThread().getContextClassLoader();
        final URL resource = contextClassLoader.getResource("login.config");
        System.setProperty("java.security.auth.login.config",
resource.toExternalForm());

        final Properties properties = new Properties();
        properties.setProperty("openejb.authentication.realmName",
"PropertiesLogin");
        new InitialContext(properties);
    }

    @Test
    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"));
    }

    @Test
    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'? " +
myTest.hasRole("admins"));
        System.out.println("Is in role 'users'? " +
myTest.hasRole("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);
    }

}

groups.properties, users.properties, login.config and jndi.properties are
copied over from your project.

When I run it I get this output:

Apache OpenEJB 3.1.4    build: 20101112-03:32
http://openejb.apache.org/
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: http://openejb.979440.n4.nabble.com/JUnit-Sessioncontext-isCallerInRole-allways-returns-false-tp3257408p3275679.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Mime
View raw message