{
String tmpDir = System.getProperty("java.io.tmpdir")+System.getProperty("file.separator");
System.setProperty("derby.system.home", tmpDir);
String dbname = "derby-"+(int)(random.nextDouble()*10000);
EmbeddedDataSource eds = new EmbeddedDataSource();
Context comp = null;
Context env = null;
try
{
//make the java:comp/env
InitialContext ic = new InitialContext();
comp = (Context)ic.lookup("java:comp");
env = comp.createSubcontext ("env");
//make a DataSource
eds.setDatabaseName(dbname);
eds.setCreateDatabase("create");
env.createSubcontext("jdbc");
env.bind("ds", eds);
Connection connection = eds.getConnection();
//create tables
String sql = "create table myusers (myuser varchar(32) PRIMARY KEY, mypassword varchar(32))";
Statement createStatement = connection.createStatement();
createStatement.executeUpdate (sql);
sql = " create table myuserroles (myuser varchar(32), myrole varchar(32))";
createStatement.executeUpdate (sql);
createStatement.close();
//insert test users and roles
sql = "insert into myusers (myuser, mypassword) values (?, ?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString (1, "me");
statement.setString (2, "me");
statement.executeUpdate();
sql = "insert into myuserroles (myuser, myrole) values ( ? , ? )";
statement = connection.prepareStatement (sql);
statement.setString (1, "me");
statement.setString (2, "roleA");
statement.executeUpdate();
statement.setString(1, "me");
statement.setString(2, "roleB");
statement.executeUpdate();
statement.close();
connection.close();
//create a JAASUserRealm
JAASUserRealm realm = new JAASUserRealm ("testRealm");
realm.setLoginModuleName ("ds");
JAASUserPrincipal userPrincipal = (JAASUserPrincipal)realm.authenticate ("me", "blah",(Request)null);
assertNull (userPrincipal);
userPrincipal = (JAASUserPrincipal)realm.authenticate ("me", "me", (Request)null);
assertNotNull (userPrincipal);
assertNotNull (userPrincipal.getName());
assertTrue (userPrincipal.getName().equals("me"));
assertTrue (userPrincipal.isUserInRole("roleA"));
assertTrue (userPrincipal.isUserInRole("roleB"));
assertTrue (!userPrincipal.isUserInRole("roleC"));
realm.pushRole (userPrincipal, "roleC");
assertTrue (userPrincipal.isUserInRole("roleC"));
assertTrue (!userPrincipal.isUserInRole("roleA"));
assertTrue (!userPrincipal.isUserInRole("roleB"));
realm.pushRole (userPrincipal, "roleD");
assertTrue (userPrincipal.isUserInRole("roleD"));
assertTrue (!userPrincipal.isUserInRole("roleC"));
assertTrue (!userPrincipal.isUserInRole("roleA"));
assertTrue (!userPrincipal.isUserInRole("roleB"));
realm.popRole(userPrincipal);
assertTrue (userPrincipal.isUserInRole("roleC"));
assertTrue (!userPrincipal.isUserInRole("roleA"));
assertTrue (!userPrincipal.isUserInRole("roleB"));
realm.popRole(userPrincipal);
assertTrue (!userPrincipal.isUserInRole("roleC"));
assertTrue (userPrincipal.isUserInRole("roleA"));
realm.disassociate(userPrincipal);
}
finally
{
comp.destroySubcontext("env");
try
{
Connection c = eds.getConnection();
Statement s = c.createStatement();
s.executeUpdate("drop table myusers");
s.executeUpdate("drop table myuserroles");
s.close();
c.close();