[jboss-cvs] JBossAS SVN: r77373 - trunk/testsuite/src/main/org/jboss/test/security/test.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Aug 22 17:18:45 EDT 2008


Author: sguilhen at redhat.com
Date: 2008-08-22 17:18:44 -0400 (Fri, 22 Aug 2008)
New Revision: 77373

Modified:
   trunk/testsuite/src/main/org/jboss/test/security/test/EJBSpecUnitTestCase.java
Log:
JBAS-5054: increased JMS message timeout for the MDB tests and provided a better cleanup to ensure the JMS session and connection objects are closed even when an exception is raised.



Modified: trunk/testsuite/src/main/org/jboss/test/security/test/EJBSpecUnitTestCase.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/security/test/EJBSpecUnitTestCase.java	2008-08-22 21:18:31 UTC (rev 77372)
+++ trunk/testsuite/src/main/org/jboss/test/security/test/EJBSpecUnitTestCase.java	2008-08-22 21:18:44 UTC (rev 77373)
@@ -1,24 +1,24 @@
 /*
-  * JBoss, Home of Professional Open Source
-  * Copyright 2005, JBoss Inc., and individual contributors as indicated
-  * by the @authors tag. See the copyright.txt in the distribution for a
-  * full listing of individual contributors.
-  *
-  * This is free software; you can redistribute it and/or modify it
-  * under the terms of the GNU Lesser General Public License as
-  * published by the Free Software Foundation; either version 2.1 of
-  * the License, or (at your option) any later version.
-  *
-  * This software is distributed in the hope that it will be useful,
-  * but WITHOUT ANY WARRANTY; without even the implied warranty of
-  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  * Lesser General Public License for more details.
-  *
-  * You should have received a copy of the GNU Lesser General Public
-  * License along with this software; if not, write to the Free
-  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
-  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-  */
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
 package org.jboss.test.security.test;
 
 import java.rmi.RemoteException;
@@ -65,21 +65,23 @@
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
-
-/** Test of EJB spec conformace using the security-spec.jar
- deployment unit. These test the basic role based access model.
- 
- @author Scott.Stark at jboss.org
- @version $Revision$
+/**
+ * Test of EJB spec conformace using the security-spec.jar deployment unit. These test the basic role based access
+ * model.
+ * 
+ * @author Scott.Stark at jboss.org
+ * @version $Revision$
  */
-public class EJBSpecUnitTestCase
-   extends JBossTestCase
+public class EJBSpecUnitTestCase extends JBossTestCase
 {
    static String username = "scott";
+
    static char[] password = "echoman".toCharArray();
+
    static String QUEUE_FACTORY = "ConnectionFactory";
-   
+
    LoginContext lc;
+
    boolean loggedIn;
 
    public EJBSpecUnitTestCase(String name)
@@ -87,18 +89,18 @@
       super(name);
    }
 
-   /** Validate that the users have the expected logins and roles.
+   /**
+    * Validate that the users have the expected logins and roles.
     * 
     * @throws Exception
-    */ 
+    */
    public void testSecurityDomain() throws Exception
    {
       log.info("+++ testSecurityDomain, domain=spec-test");
-      MBeanServerConnection conn = (MBeanServerConnection) getServer();
+      MBeanServerConnection conn = getServer();
       ObjectName secMgrName = new ObjectName("jboss.security:service=JaasSecurityManager");
-      JaasSecurityManagerServiceMBean secMgr = (JaasSecurityManagerServiceMBean)
-         MBeanServerInvocationHandler.newProxyInstance(conn, secMgrName,
-         JaasSecurityManagerServiceMBean.class, false);
+      JaasSecurityManagerServiceMBean secMgr = (JaasSecurityManagerServiceMBean) MBeanServerInvocationHandler
+            .newProxyInstance(conn, secMgrName, JaasSecurityManagerServiceMBean.class, false);
 
       // Test the spec-test security domain
       String domain = "spec-test";
@@ -137,7 +139,7 @@
       SimplePrincipal projectUser = new SimplePrincipal("ProjectUser");
       testRole.add(projectUser);
       hasRole = secMgr.doesUserHaveRole(domain, user, password, testRole);
-      assertTrue("scott has ProjectUser role", hasRole);      
+      assertTrue("scott has ProjectUser role", hasRole);
       Set roles = secMgr.getUserRoles(domain, user, password);
       assertTrue(roles != null);
       assertTrue("roles contains EchoLocal", roles.contains(echoLocal));
@@ -147,8 +149,8 @@
       assertTrue("badpass is an invalid password for scott", isValid == false);
    }
 
-   /** Test the use of getCallerPrincipal from within the ejbCreate
-    * in a stateful session bean
+   /**
+    * Test the use of getCallerPrincipal from within the ejbCreate in a stateful session bean
     */
    public void testStatefulCreateCaller() throws Exception
    {
@@ -168,13 +170,11 @@
       logout();
    }
 
-   /** Test that:
-    1. SecureBean returns a non-null principal when getCallerPrincipal
-    is called with a security context and that this is propagated
-    to its Entity bean ref.
-    
-    2. UnsecureBean throws an IllegalStateException when getCallerPrincipal
-    is called without a security context.
+   /**
+    * Test that: 1. SecureBean returns a non-null principal when getCallerPrincipal is called with a security context
+    * and that this is propagated to its Entity bean ref.
+    * 
+    * 2. UnsecureBean throws an IllegalStateException when getCallerPrincipal is called without a security context.
     */
    public void testGetCallerPrincipal() throws Exception
    {
@@ -186,19 +186,19 @@
       log.debug("Found Unsecure StatelessSessionHome");
       StatelessSession bean = home.create();
       log.debug("Created spec.UnsecureStatelessSession2");
-      
+
       try
       {
          // This should fail because echo calls getCallerPrincipal()
          bean.echo("Hello from nobody?");
          fail("Was able to call StatelessSession.echo");
       }
-      catch(RemoteException e)
+      catch (RemoteException e)
       {
          log.debug("echo failed as expected");
       }
       bean.remove();
-      
+
       login();
       obj = getInitialContext().lookup("spec.StatelessSession2");
       obj = PortableRemoteObject.narrow(obj, StatelessSessionHome.class);
@@ -208,16 +208,16 @@
       log.debug("Created spec.StatelessSession2");
       // Test that the Entity bean sees username as its principal
       String echo = bean.echo(username);
-      log.debug("bean.echo(username) = "+echo);
+      log.debug("bean.echo(username) = " + echo);
       assertTrue("username == echo", echo.equals(username));
       bean.remove();
    }
 
    /**
-    * Test that a call interacting with different security domains does not
-    * change the 
+    * Test that a call interacting with different security domains does not change the
+    * 
     * @throws Exception
-    */ 
+    */
    public void testDomainInteraction() throws Exception
    {
       logout();
@@ -233,10 +233,11 @@
       roles.add("Role1");
       roles.add("Role2");
       bean.testDomainInteraction(roles);
-      bean.remove();      
+      bean.remove();
    }
 
-   /** Test that the calling principal is propagated across bean calls.
+   /**
+    * Test that the calling principal is propagated across bean calls.
     */
    public void testPrincipalPropagation() throws Exception
    {
@@ -249,13 +250,13 @@
       log.debug("Found Unsecure StatelessSessionHome");
       StatelessSession bean = home.create();
       log.debug("Created spec.UnsecureStatelessSession2");
-      log.debug("Bean.forward('testPrincipalPropagation') -> "+bean.forward("testPrincipalPropagation"));
+      log.debug("Bean.forward('testPrincipalPropagation') -> " + bean.forward("testPrincipalPropagation"));
       bean.remove();
    }
-   
-   /** Test that the echo method is accessible by an Echo
-    role. Since the noop() method of the StatelessSession
-    bean was not assigned any permissions it should be unchecked.
+
+   /**
+    * Test that the echo method is accessible by an Echo role. Since the noop() method of the StatelessSession bean was
+    * not assigned any permissions it should be unchecked.
     */
    public void testMethodAccess() throws Exception
    {
@@ -267,7 +268,7 @@
       log.debug("Found StatelessSessionHome");
       StatelessSession bean = home.create();
       log.debug("Created spec.StatelessSession");
-      log.debug("Bean.echo('Hello') -> "+bean.echo("Hello"));
+      log.debug("Bean.echo('Hello') -> " + bean.echo("Hello"));
 
       try
       {
@@ -275,18 +276,17 @@
          bean.noop();
          fail("Was able to call StatelessSession.noop");
       }
-      catch(RemoteException e)
+      catch (RemoteException e)
       {
          log.debug("StatelessSession.noop failed as expected");
       }
       bean.remove();
    }
 
-   /** Test that the echo method is accessible by an Echo
-    role. Since the excluded() method of the StatelessSession
-    bean has been placed into the excluded set it should not
-    accessible by any user. This uses the security domain of the
-    JaasSecurityDomain service to test its use as an authentication mgr.
+   /**
+    * Test that the echo method is accessible by an Echo role. Since the excluded() method of the StatelessSession bean
+    * has been placed into the excluded set it should not accessible by any user. This uses the security domain of the
+    * JaasSecurityDomain service to test its use as an authentication mgr.
     */
    public void testDomainMethodAccess() throws Exception
    {
@@ -298,7 +298,7 @@
       log.debug("Found StatelessSessionInDomain home");
       StatelessSession bean = home.create();
       log.debug("Created spec.StatelessSessionInDomain");
-      log.debug("Bean.echo('testDomainMethodAccess') -> "+bean.echo("testDomainMethodAccess"));
+      log.debug("Bean.echo('testDomainMethodAccess') -> " + bean.echo("testDomainMethodAccess"));
 
       try
       {
@@ -306,16 +306,16 @@
          bean.excluded();
          fail("Was able to call StatelessSession.excluded");
       }
-      catch(RemoteException e)
+      catch (RemoteException e)
       {
          log.debug("StatelessSession.excluded failed as expected");
       }
       bean.remove();
    }
 
-   /** Test that the permissions assigned to the stateless session bean:
-    with ejb-name=org/jboss/test/security/ejb/StatelessSession_test
-    are read correctly.
+   /**
+    * Test that the permissions assigned to the stateless session bean: with
+    * ejb-name=org/jboss/test/security/ejb/StatelessSession_test are read correctly.
     */
    public void testMethodAccess2() throws Exception
    {
@@ -328,13 +328,13 @@
       log.debug("Found StatelessSessionHome");
       StatelessSession bean = home.create();
       log.debug("Created spec.StatelessSession_test");
-      log.debug("Bean.echo('testMethodAccess2') -> "+bean.echo("testMethodAccess2"));
+      log.debug("Bean.echo('testMethodAccess2') -> " + bean.echo("testMethodAccess2"));
       bean.remove();
    }
 
-   /** Test a user with Echo and EchoLocal roles can access the CalleeBean
-    through its local interface by calling the CallerBean and that a user
-    with only a EchoLocal cannot call the CallerBean.
+   /**
+    * Test a user with Echo and EchoLocal roles can access the CalleeBean through its local interface by calling the
+    * CallerBean and that a user with only a EchoLocal cannot call the CallerBean.
     */
    public void testLocalMethodAccess() throws Exception
    {
@@ -347,14 +347,16 @@
       log.debug("Found spec.CallerBean Home");
       CalledSession bean = home.create();
       log.debug("Created spec.CallerBean");
-      log.debug("Bean.invokeEcho('testLocalMethodAccess') -> "+bean.invokeEcho("testLocalMethodAccess"));
+      log.debug("Bean.invokeEcho('testLocalMethodAccess') -> " + bean.invokeEcho("testLocalMethodAccess"));
       bean.remove();
    }
 
-   /** Test access to a bean with a mix of remote interface permissions and
-    * unchecked permissions with the unchecked permissions declared first.
+   /**
+    * Test access to a bean with a mix of remote interface permissions and unchecked permissions with the unchecked
+    * permissions declared first.
+    * 
     * @throws Exception
-    */ 
+    */
    public void testUncheckedRemote() throws Exception
    {
       log.debug("+++ testUncheckedRemote");
@@ -365,24 +367,26 @@
       log.debug("Found UncheckedSessionRemoteLast");
       StatelessSession bean = home.create();
       log.debug("Created spec.UncheckedSessionRemoteLast");
-      log.debug("Bean.echo('testUncheckedRemote') -> "+bean.echo("testUncheckedRemote"));
+      log.debug("Bean.echo('testUncheckedRemote') -> " + bean.echo("testUncheckedRemote"));
       try
       {
          bean.excluded();
          fail("Was able to call UncheckedSessionRemoteLast.excluded");
       }
-      catch(RemoteException e)
+      catch (RemoteException e)
       {
-         log.debug("UncheckedSessionRemoteLast.excluded failed as expected");         
+         log.debug("UncheckedSessionRemoteLast.excluded failed as expected");
       }
       bean.remove();
       logout();
    }
 
-   /** Test access to a bean with a mix of remote interface permissions and
-    * unchecked permissions with the unchecked permissions declared last.
+   /**
+    * Test access to a bean with a mix of remote interface permissions and unchecked permissions with the unchecked
+    * permissions declared last.
+    * 
     * @throws Exception
-    */ 
+    */
    public void testRemoteUnchecked() throws Exception
    {
       log.debug("+++ testRemoteUnchecked");
@@ -393,23 +397,23 @@
       log.debug("Found UncheckedSessionRemoteFirst");
       StatelessSession bean = home.create();
       log.debug("Created spec.UncheckedSessionRemoteFirst");
-      log.debug("Bean.echo('testRemoteUnchecked') -> "+bean.echo("testRemoteUnchecked"));
+      log.debug("Bean.echo('testRemoteUnchecked') -> " + bean.echo("testRemoteUnchecked"));
       try
       {
          bean.excluded();
          fail("Was able to call UncheckedSessionRemoteFirst.excluded");
       }
-      catch(RemoteException e)
+      catch (RemoteException e)
       {
-         log.debug("UncheckedSessionRemoteFirst.excluded failed as expected");         
+         log.debug("UncheckedSessionRemoteFirst.excluded failed as expected");
       }
       bean.remove();
       logout();
    }
 
-   /** Test that a user with a role that has not been assigned any
-    method permissions in the ejb-jar descriptor is able to access a
-    method that has been marked as unchecked.
+   /**
+    * Test that a user with a role that has not been assigned any method permissions in the ejb-jar descriptor is able
+    * to access a method that has been marked as unchecked.
     */
    public void testUnchecked() throws Exception
    {
@@ -430,8 +434,8 @@
       logout();
    }
 
-   /** Test that a user with a valid role is able to access a
-    bean for which all methods have been marked as unchecked.
+   /**
+    * Test that a user with a valid role is able to access a bean for which all methods have been marked as unchecked.
     */
    public void testUncheckedWithLogin() throws Exception
    {
@@ -449,11 +453,10 @@
       logout();
    }
 
-   /** Test that user scott who has the Echo role is not able to
-    access the StatelessSession2.excluded method even though
-    the Echo role has been granted access to all methods of
-    StatelessSession2 to test that the excluded-list takes
-    precendence over the method-permissions.
+   /**
+    * Test that user scott who has the Echo role is not able to access the StatelessSession2.excluded method even though
+    * the Echo role has been granted access to all methods of StatelessSession2 to test that the excluded-list takes
+    * precendence over the method-permissions.
     */
    public void testExcluded() throws Exception
    {
@@ -470,24 +473,21 @@
          bean.excluded();
          fail("Was able to call Bean.excluded()");
       }
-      catch(Exception e)
+      catch (Exception e)
       {
          log.debug("Bean.excluded() failed as expected");
          // This is what we expect
       }
       logout();
    }
-   
-   /** This method tests the following call chains:
-    1. RunAsStatelessSession.echo() -> PrivateEntity.echo()
-    2. RunAsStatelessSession.noop() -> RunAsStatelessSession.excluded()
-    3. RunAsStatelessSession.forward() -> StatelessSession.echo()
-    1. Should succeed because the run-as identity of RunAsStatelessSession
-    is valid for accessing PrivateEntity.
-    2. Should succeed because the run-as identity of RunAsStatelessSession
-    is valid for accessing RunAsStatelessSession.excluded().
-    3. Should fail because the run-as identity of RunAsStatelessSession
-    is not Echo.
+
+   /**
+    * This method tests the following call chains: 1. RunAsStatelessSession.echo() -> PrivateEntity.echo() 2.
+    * RunAsStatelessSession.noop() -> RunAsStatelessSession.excluded() 3. RunAsStatelessSession.forward() ->
+    * StatelessSession.echo() 1. Should succeed because the run-as identity of RunAsStatelessSession is valid for
+    * accessing PrivateEntity. 2. Should succeed because the run-as identity of RunAsStatelessSession is valid for
+    * accessing RunAsStatelessSession.excluded(). 3. Should fail because the run-as identity of RunAsStatelessSession is
+    * not Echo.
     */
    public void testRunAs() throws Exception
    {
@@ -499,29 +499,28 @@
       log.debug("Found RunAsStatelessSession Home");
       StatelessSession bean = home.create();
       log.debug("Created spec.RunAsStatelessSession");
-      log.debug("Bean.echo('testRunAs') -> "+bean.echo("testRunAs"));
+      log.debug("Bean.echo('testRunAs') -> " + bean.echo("testRunAs"));
       bean.noop();
       log.debug("Bean.noop(), ok");
-      
+
       try
       {
          // This should not be allowed
          bean.forward("Hello");
          fail("Was able to call RunAsStatelessSession.forward");
       }
-      catch(RemoteException e)
+      catch (RemoteException e)
       {
          log.debug("StatelessSession.forward failed as expected");
       }
       bean.remove();
    }
 
-   /** This method tests the following call chain:
-    Level1CallerBean.callEcho() -> Level2CallerBean.invokeEcho()
-      -> Level3CalleeBean.echo()
-    The Level1CallerBean uses a run-as of InternalRole and the Level2CallerBean
-    and Level3CalleeBean are only accessible by InternalRole.
-    */   
+   /**
+    * This method tests the following call chain: Level1CallerBean.callEcho() -> Level2CallerBean.invokeEcho() ->
+    * Level3CalleeBean.echo() The Level1CallerBean uses a run-as of InternalRole and the Level2CallerBean and
+    * Level3CalleeBean are only accessible by InternalRole.
+    */
    public void testDeepRunAs() throws Exception
    {
       log.debug("+++ testDeepRunAs");
@@ -546,12 +545,12 @@
          bean = home.create();
          fail("Was able to create Level2CallerBean");
       }
-      catch(ServerException e)
+      catch (ServerException e)
       {
          AccessException ae = (AccessException) e.detail;
          log.debug("Caught AccessException as expected", ae);
       }
-      catch(AccessException e)
+      catch (AccessException e)
       {
          log.debug("Caught AccessException as expected", e);
       }
@@ -573,11 +572,10 @@
    }
 
    /**
-    * Test the run-as side-effects raised in 
-    * http://jira.jboss.com/jira/browse/JBAS-1852
+    * Test the run-as side-effects raised in http://jira.jboss.com/jira/browse/JBAS-1852
     * 
     * @throws Exception
-    */ 
+    */
    public void testJBAS1852() throws Exception
    {
       log.info("+++ testJBAS1852");
@@ -592,118 +590,155 @@
       bean.remove();
    }
 
-   /** Test that an MDB with a run-as identity is able to access secure EJBs
-    that require the identity.
+   /**
+    * Test that an MDB with a run-as identity is able to access secure EJBs that require the identity.
     */
    public void testMDBRunAs() throws Exception
    {
-      //fail("FIXME");
-      log.debug("+++ testMDBRunAs");
-      logout();
+      // fail("FIXME");
+      log.debug("Running test testMDBRunAs");
+      this.logout();
       QueueConnectionFactory queueFactory = (QueueConnectionFactory) getInitialContext().lookup(QUEUE_FACTORY);
       Queue queA = (Queue) getInitialContext().lookup("queue/QueueA");
       Queue queB = (Queue) getInitialContext().lookup("queue/QueueB");
-      QueueConnection queueConn = queueFactory.createQueueConnection();
-      QueueSession session = queueConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
-      Message msg = session.createMessage();
-      msg.setStringProperty("arg", "testMDBRunAs");
-      msg.setJMSReplyTo(queB);
-      QueueSender sender = session.createSender(queA);
-      sender.send(msg);
-      sender.close();
-      log.debug("Sent msg to queue/QueueA");
-      queueConn.start();
-      QueueReceiver recv = session.createReceiver(queB);
-      msg = recv.receive(5000);
-      log.debug("Recv msg: "+msg);
-      String info = msg.getStringProperty("reply");
-      recv.close();
-      session.close();
-      queueConn.close();
-
-      if( info == null || info.startsWith("Failed") )
+      QueueConnection queueConn = null;
+      QueueSession session = null;
+      try
       {
-         fail("Recevied exception reply, info="+info);
+         queueConn = queueFactory.createQueueConnection();
+         session = queueConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
+         queueConn.start();
+         // create a JMS message.
+         Message msg = session.createMessage();
+         msg.setStringProperty("arg", "testMDBRunAs");
+         msg.setJMSReplyTo(queB);
+         // send the constructed message to queue A.
+         QueueSender sender = session.createSender(queA);
+         sender.send(msg);
+         sender.close();
+         log.debug("Sent msg to queue/QueueA");
+         // receive the message at queue B.
+         QueueReceiver recv = session.createReceiver(queB);
+         msg = recv.receive(15000);
+         recv.close();
+         log.debug("Recv msg: " + msg);
+         // get the message's content.
+         String info = msg.getStringProperty("reply");
+         if (info == null || info.startsWith("Failed"))
+         {
+            fail("Recevied exception reply, info=" + info);
+         }
       }
+      finally
+      {
+         if (session != null)
+            session.close();
+         if (queueConn != null)
+            queueConn.close();
+      }
    }
 
-   /** Test that an MDB with a run-as identity is able to access secure EJBs
-    that require the identity. DeepRunAsMDB -> Level1MDBCallerBean.callEcho() ->
-      Level2CallerBean.invokeEcho() -> Level3CalleeBean.echo()
-    The MDB uses a run-as of InternalRole and the Level2CallerBean
-    and Level3CalleeBean are only accessible by InternalRole.
+   /**
+    * Test that an MDB with a run-as identity is able to access secure EJBs that require the identity. DeepRunAsMDB ->
+    * Level1MDBCallerBean.callEcho() -> Level2CallerBean.invokeEcho() -> Level3CalleeBean.echo() The MDB uses a run-as
+    * of InternalRole and the Level2CallerBean and Level3CalleeBean are only accessible by InternalRole.
     */
    public void testMDBDeepRunAs() throws Exception
    {
-      //fail("FIXME");
-      log.debug("+++ testMDBDeepRunAs");
-      logout();
+      // fail("FIXME");
+      log.debug("Running test testMDBDeepRunAs");
+      this.logout();
       QueueConnectionFactory queueFactory = (QueueConnectionFactory) getInitialContext().lookup(QUEUE_FACTORY);
       Queue queD = (Queue) getInitialContext().lookup("queue/QueueD");
       Queue queB = (Queue) getInitialContext().lookup("queue/QueueB");
-      QueueConnection queueConn = queueFactory.createQueueConnection();
-      QueueSession session = queueConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
-      Message msg = session.createMessage();
-      msg.setStringProperty("arg", "testMDBDeepRunAs");
-      msg.setJMSReplyTo(queB);
-      QueueSender sender = session.createSender(queD);
-      sender.send(msg);
-      sender.close();
-      log.debug("Sent msg to "+queD);
-      queueConn.start();
-      QueueReceiver recv = session.createReceiver(queB);
-      msg = recv.receive(5000);
-      log.debug("Recv msg: "+msg);
-      String info = msg.getStringProperty("reply");
-      recv.close();
-      session.close();
-      queueConn.close();
-
-      if( info == null || info.startsWith("Failed") )
+      QueueConnection queueConn = null;
+      QueueSession session = null;
+      try
       {
-         fail("Recevied exception reply, info="+info);
+         queueConn = queueFactory.createQueueConnection();
+         session = queueConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
+         queueConn.start();
+         // create a JMS message.
+         Message msg = session.createMessage();
+         msg.setStringProperty("arg", "testMDBDeepRunAs");
+         msg.setJMSReplyTo(queB);
+         // send the constructed message to queue D.
+         QueueSender sender = session.createSender(queD);
+         sender.send(msg);
+         sender.close();
+         log.debug("Sent msg to " + queD);
+         // receive the message at queue B
+         QueueReceiver recv = session.createReceiver(queB);
+         msg = recv.receive(15000);
+         recv.close();
+         log.debug("Recv msg: " + msg);
+         // get the message's content.
+         String info = msg.getStringProperty("reply");
+         if (info == null || info.startsWith("Failed"))
+         {
+            fail("Recevied exception reply, info=" + info);
+         }
       }
+      finally
+      {
+         if (session != null)
+            session.close();
+         if (queueConn != null)
+            queueConn.close();
+      }
    }
 
-   /** This method tests that the RunAsWithRolesMDB is assigned multiple roles
-    * within its onMessage so that it can call into the ProjRepository session
-    * bean's methods that required ProjectAdmin, CreateFolder and DeleteFolder
-    * roles.
+   /**
+    * This method tests that the RunAsWithRolesMDB is assigned multiple roles within its onMessage so that it can call
+    * into the ProjRepository session bean's methods that required ProjectAdmin, CreateFolder and DeleteFolder roles.
     */
    public void testRunAsWithRoles() throws Exception
    {
-      //fail("FIXME");
-      log.debug("+++ testRunAsWithRoles");
-      logout();
+      log.debug("Running test testRunAsWithRoles");
+      this.logout();
       QueueConnectionFactory queueFactory = (QueueConnectionFactory) getInitialContext().lookup(QUEUE_FACTORY);
       Queue queC = (Queue) getInitialContext().lookup("queue/QueueC");
       Queue queB = (Queue) getInitialContext().lookup("queue/QueueB");
-      QueueConnection queueConn = queueFactory.createQueueConnection();
-      QueueSession session = queueConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
-      Message msg = session.createMessage();
-      msg.setStringProperty("name", "testRunAsWithRoles");
-      msg.setJMSReplyTo(queB);
-      QueueSender sender = session.createSender(queC);
-      sender.send(msg);
-      sender.close();
-      log.debug("Sent msg to queue/QueueC");
-      queueConn.start();
-      QueueReceiver recv = session.createReceiver(queB);
-      msg = recv.receive(5000);
-      log.debug("Recv msg: "+msg);
-      String info = msg.getStringProperty("reply");
-      recv.close();
-      session.close();
-      queueConn.close();
+      QueueConnection queueConn = null;
+      QueueSession session = null;
 
-      if( info == null || info.startsWith("Failed") )
+      try
       {
-         fail("Recevied exception reply, info="+info);
+         queueConn = queueFactory.createQueueConnection();
+         session = queueConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
+         queueConn.start();
+         // create a JMS message.
+         Message msg = session.createMessage();
+         msg.setStringProperty("name", "testRunAsWithRoles");
+         msg.setJMSReplyTo(queB);
+         // send the constructed message to queue C.
+         QueueSender sender = session.createSender(queC);
+         sender.send(msg);
+         sender.close();
+         log.debug("Sent msg to queue/QueueC");
+         // receive the message at queue B.
+         QueueReceiver recv = session.createReceiver(queB);
+         msg = recv.receive(5000);
+         log.debug("Recv msg: " + msg);
+         recv.close();
+         // get the message's content.
+         String info = msg.getStringProperty("reply");
+         if (info == null || info.startsWith("Failed"))
+         {
+            fail("Recevied exception reply, info=" + info);
+         }
       }
+      finally
+      {
+         if (session != null)
+            session.close();
+         if (queueConn != null)
+            queueConn.close();
+      }
    }
 
-   /** Test the security behavior of handles. To obtain secured bean from
-      a handle that the handle be 
+   /**
+    * Test the security behavior of handles. To obtain secured bean from a handle that the handle be
     */
    public void testHandle() throws Exception
    {
@@ -716,23 +751,23 @@
       StatelessSession bean = home.create();
       log.debug("Created spec.StatelessSession");
       Handle h = bean.getHandle();
-      log.debug("Obtained handle: "+h);
+      log.debug("Obtained handle: " + h);
       bean = (StatelessSession) h.getEJBObject();
-      log.debug("Obtained bean from handle: "+bean);
-      log.debug("Bean.echo('testHandle') -> "+bean.echo("testHandle"));
+      log.debug("Obtained bean from handle: " + bean);
+      log.debug("Bean.echo('testHandle') -> " + bean.echo("testHandle"));
       logout();
 
-      /* Attempting to obtain the EJB fron the handle without security
-       association present should fail
-      */
+      /*
+       * Attempting to obtain the EJB fron the handle without security association present should fail
+       */
       try
       {
          bean = (StatelessSession) h.getEJBObject();
          fail("Should not be able to obtain a bean without login info");
       }
-      catch(Exception e)
+      catch (Exception e)
       {
-         log.debug("Obtaining bean from handle failed as expected, e="+e.getMessage());
+         log.debug("Obtaining bean from handle failed as expected, e=" + e.getMessage());
       }
 
       // One should be able to obtain a handle without a login
@@ -740,13 +775,14 @@
       login();
       // Now we should be able to obtain and use the secure bean
       bean = (StatelessSession) h.getEJBObject();
-      log.debug("Obtained bean from handle: "+bean);
-      log.debug("Bean.echo('testHandle2') -> "+bean.echo("testHandle2"));
+      log.debug("Obtained bean from handle: " + bean);
+      log.debug("Bean.echo('testHandle2') -> " + bean.echo("testHandle2"));
       logout();
    }
 
-   /** Test the security behavior of stateful handles. To obtain secured bean
-    from a handle requires that there be a security context to obtain the ejb.
+   /**
+    * Test the security behavior of stateful handles. To obtain secured bean from a handle requires that there be a
+    * security context to obtain the ejb.
     */
    public void testStatefulHandle() throws Exception
    {
@@ -759,23 +795,23 @@
       StatefulSession bean = home.create("testStatefulHandle");
       log.debug("Created spec.StatelessSession");
       Handle h = bean.getHandle();
-      log.debug("Obtained handle: "+h);
+      log.debug("Obtained handle: " + h);
       bean = (StatefulSession) h.getEJBObject();
-      log.debug("Obtained bean from handle: "+bean);
-      log.debug("Bean.echo('Hello') -> "+bean.echo("Hello"));
+      log.debug("Obtained bean from handle: " + bean);
+      log.debug("Bean.echo('Hello') -> " + bean.echo("Hello"));
       logout();
 
-      /* Attempting to obtain the EJB fron the handle without security
-       association present should fail
-      */
+      /*
+       * Attempting to obtain the EJB fron the handle without security association present should fail
+       */
       try
       {
          bean = (StatefulSession) h.getEJBObject();
          fail("Should not be able to obtain a bean without login info");
       }
-      catch(Exception e)
+      catch (Exception e)
       {
-         log.debug("Obtaining bean from handle failed as expected, e="+e.getMessage());
+         log.debug("Obtaining bean from handle failed as expected, e=" + e.getMessage());
       }
 
       // One should be able to obtain a handle without a login
@@ -783,63 +819,63 @@
       login();
       // Now we should be able to obtain and use the secure bean
       bean = (StatefulSession) h.getEJBObject();
-      log.debug("Obtained bean from handle: "+bean);
-      log.debug("Bean.echo('Hello') -> "+bean.echo("Hello"));
+      log.debug("Obtained bean from handle: " + bean);
+      log.debug("Bean.echo('Hello') -> " + bean.echo("Hello"));
       logout();
    }
 
-   /** Stress test declarative security.
+   /**
+    * Stress test declarative security.
     */
    public void testStress() throws Exception
    {
-      //fail("FIXME");
+      // fail("FIXME");
       log.debug("+++ testStress");
       int count = Integer.getInteger("jbosstest.threadcount", 2).intValue();
       int iterations = 10;
-      /* FIXME, Use a minimum of 100 iterations
-      iterations = Integer.getInteger("jbosstest.iterationcount", 5).intValue();
-      if( iterations < 100 )
-         iterations = 100;
-      */
-      log.info("Creating "+count+" threads doing "+iterations+" iterations");
+      /*
+       * FIXME, Use a minimum of 100 iterations iterations = Integer.getInteger("jbosstest.iterationcount",
+       * 5).intValue(); if( iterations < 100 ) iterations = 100;
+       */
+      log.info("Creating " + count + " threads doing " + iterations + " iterations");
       Thread[] testThreads = new Thread[count];
       StressTester[] testers = new StressTester[count];
-      
-      for(int t = 0; t < count; t ++)
+
+      for (int t = 0; t < count; t++)
       {
          StressTester test = new StressTester(getInitialContext(), iterations);
          testers[t] = test;
-         Thread thr = new Thread(test, "Tester#"+t);
+         Thread thr = new Thread(test, "Tester#" + t);
          thr.start();
          testThreads[t] = thr;
       }
 
       int errorCount = 0;
-      for(int t = 0; t < count; t ++)
+      for (int t = 0; t < count; t++)
       {
          Thread thr = testThreads[t];
          thr.join();
          StressTester test = testers[t];
-         if( test.error != null )
+         if (test.error != null)
          {
-            errorCount ++;
+            errorCount++;
          }
       }
       assertTrue("Thread error count == 0", errorCount == 0);
    }
 
-   /** Stress test declarative security with the JAAS cache disabled.
+   /**
+    * Stress test declarative security with the JAAS cache disabled.
     */
    public void testStressNoJaasCache() throws Exception
    {
-      //fail("FIXME");
+      // fail("FIXME");
       log.info("+++ testStressNoJaasCache, domain=spec-test");
       // Disable caching for the spec-test domain
-      MBeanServerConnection conn = (MBeanServerConnection) getServer();
+      MBeanServerConnection conn = getServer();
       ObjectName secMgrName = new ObjectName("jboss.security:service=JaasSecurityManager");
-      JaasSecurityManagerServiceMBean secMgr = (JaasSecurityManagerServiceMBean)
-         MBeanServerInvocationHandler.newProxyInstance(conn, secMgrName,
-         JaasSecurityManagerServiceMBean.class, false);
+      JaasSecurityManagerServiceMBean secMgr = (JaasSecurityManagerServiceMBean) MBeanServerInvocationHandler
+            .newProxyInstance(conn, secMgrName, JaasSecurityManagerServiceMBean.class, false);
       secMgr.setCacheTimeout("spec-test", 0, 0);
 
       Exception failed = null;
@@ -848,20 +884,22 @@
          // Now execute the testStress access
          testStress();
       }
-      catch(Exception e)
+      catch (Exception e)
       {
          failed = e;
       }
 
       secMgr.setCacheTimeout("spec-test", 60, 60);
-      if( failed != null )
+      if (failed != null)
          throw failed;
    }
 
    private static class StressTester implements Runnable
    {
       InitialContext ctx;
+
       int iterations;
+
       Throwable error;
 
       StressTester(InitialContext ctx, int iterations) throws Exception
@@ -869,16 +907,17 @@
          this.ctx = ctx;
          this.iterations = iterations;
       }
+
       public void run()
       {
          Thread t = Thread.currentThread();
          Logger log = Logger.getLogger(t.getName());
-         log.info("Begin run, t="+t);
+         log.info("Begin run, t=" + t);
          try
          {
             AppCallbackHandler handler = new AppCallbackHandler(EJBSpecUnitTestCase.username,
-               EJBSpecUnitTestCase.password);
-            for(int i = 0; i < iterations; i ++)
+                  EJBSpecUnitTestCase.password);
+            for (int i = 0; i < iterations; i++)
             {
                LoginContext lc = new LoginContext("spec-test-multi-threaded", handler);
                lc.login();
@@ -888,57 +927,58 @@
                log.debug("Found StatelessSessionHome");
                StatelessSession bean = home.create();
                log.debug("Created spec.StatelessSession");
-               log.debug("Bean.echo('Hello') -> "+bean.echo("Hello"));
+               log.debug("Bean.echo('Hello') -> " + bean.echo("Hello"));
                bean.remove();
                log.debug("Removed bean");
                lc.logout();
             }
          }
-         catch(Throwable e)
+         catch (Throwable e)
          {
             error = e;
             log.error("Security failure", e);
          }
-         log.info("End run, t="+Thread.currentThread());
+         log.info("End run, t=" + Thread.currentThread());
       }
    }
 
-   /** Login as user scott using the conf.name login config or
-    'spec-test' if conf.name is not defined.
+   /**
+    * Login as user scott using the conf.name login config or 'spec-test' if conf.name is not defined.
     */
    private void login() throws Exception
    {
       login(username, password);
    }
+
    private void login(String username, char[] password) throws Exception
    {
-      if( loggedIn )
+      if (loggedIn)
          return;
-      
+
       lc = null;
       String confName = System.getProperty("conf.name", "spec-test");
       AppCallbackHandler handler = new AppCallbackHandler(username, password);
-      log.debug("Creating LoginContext("+confName+")");
+      log.debug("Creating LoginContext(" + confName + ")");
       lc = new LoginContext(confName, handler);
       lc.login();
-      log.debug("Created LoginContext, subject="+lc.getSubject());
+      log.debug("Created LoginContext, subject=" + lc.getSubject());
       loggedIn = true;
    }
+
    private void logout() throws Exception
    {
-      if( loggedIn )
+      if (loggedIn)
       {
          loggedIn = false;
          lc.logout();
       }
    }
 
-
    @Override
    protected void setUp() throws Exception
    {
       super.setUp();
-      if(System.getProperty("java.security.auth.login.config") == null)
+      if (System.getProperty("java.security.auth.login.config") == null)
       {
          System.setProperty("java.security.auth.login.config", "output/resources/security/auth.conf");
       }
@@ -955,6 +995,7 @@
       // Create an initializer for the test suite
       TestSetup wrapper = new JBossTestSetup(suite)
       {
+         @Override
          protected void setUp() throws Exception
          {
             super.setUp();
@@ -962,11 +1003,13 @@
             redeploy("security-spec.jar");
             flushAuthCache();
          }
+
+         @Override
          protected void tearDown() throws Exception
          {
             undeploy("security-spec.jar");
             super.tearDown();
-         
+
          }
       };
       return wrapper;




More information about the jboss-cvs-commits mailing list