}
private static void testShoppingCart() throws Exception
{
// Establish the proxy with an incorrect security identity
SecurityClient securityClient = SecurityClientFactory.getSecurityClient();
securityClient.setSimple("bill", "incorrectpassword");
securityClient.login();
Context ctx = new InitialContext();
ShoppingCart cart = (ShoppingCart) ctx.lookup("ShoppingCart");
System.out.println("Attempting to buy 1 memory stick with incorrect password");
try
{
cart.buy("Memory stick", 1);
throw new RuntimeException("ERROR - User with incorrect password was able to access the bean");
}
catch (javax.ejb.EJBAccessException e)
{
System.out.println("Caught javax.ejb.EJBAccessException as expected");
}
System.out.println("Setting user/password");
securityClient.logout();
securityClient.setSimple("bill", "password-test");
securityClient.login();
System.out.println("bill is a shopper, so is allowed to buy");
System.out.println("Buying 1 memory stick");
cart.buy("Memory stick", 1);
System.out.println("Buying another memory stick");
cart.buy("Memory stick", 1);
System.out.println("Buying a laptop");
cart.buy("Laptop", 1);
System.out.println("Print cart:");
HashMap<String, Integer> fullCart = cart.getCartContents();
for (String product : fullCart.keySet())
{
System.out.println(fullCart.get(product) + " " + product);
}
System.out.println("bill is not a clerk, so is not allowed to price check");
try
{
cart.priceCheck("Laptop");
throw new RuntimeException("ERROR - User with insufficient access rights allowed to access bean method");
}
catch (javax.ejb.EJBAccessException ex)
{
System.out.println("Caught EJBAccessException as expected");
}
System.out.println("Checkout");
cart.checkout();
System.out.println("Should throw an object not found exception by invoking on cart after @Remove method");
try
{
cart.getCartContents();
throw new RuntimeException("ERROR - Bean not discarded");
}
catch (NoSuchEJBException e)
{
System.out.println("Successfully caught no such object exception.");
}
// logout the user
securityClient.logout();
}