[jboss-cvs] JBossAS SVN: r57957 - in trunk/testsuite/src/main/org/jboss/test/jbossmessaging: . test

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Oct 31 17:37:37 EST 2006


Author: rachmatowicz at jboss.com
Date: 2006-10-31 17:37:36 -0500 (Tue, 31 Oct 2006)
New Revision: 57957

Added:
   trunk/testsuite/src/main/org/jboss/test/jbossmessaging/JMSBase.java
   trunk/testsuite/src/main/org/jboss/test/jbossmessaging/test/JoramUnitTestCase.java
   trunk/testsuite/src/main/org/jboss/test/jbossmessaging/test/SecurityUnitTestCase.java
Log:
More refactored generic JMS test cases

Added: trunk/testsuite/src/main/org/jboss/test/jbossmessaging/JMSBase.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/jbossmessaging/JMSBase.java	2006-10-31 21:46:53 UTC (rev 57956)
+++ trunk/testsuite/src/main/org/jboss/test/jbossmessaging/JMSBase.java	2006-10-31 22:37:36 UTC (rev 57957)
@@ -0,0 +1,888 @@
+/*
+  * 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.jbossmessaging;
+
+import java.util.Enumeration;
+
+import javax.jms.Connection;
+import javax.jms.DeliveryMode;
+import javax.jms.Destination;
+import javax.jms.ExceptionListener;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageListener;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.QueueBrowser;
+import javax.jms.QueueConnection;
+import javax.jms.QueueConnectionFactory;
+import javax.jms.QueueSender;
+import javax.jms.QueueSession;
+import javax.jms.Session;
+import javax.jms.Topic;
+import javax.jms.TopicConnection;
+import javax.jms.TopicConnectionFactory;
+import javax.jms.TopicPublisher;
+import javax.jms.TopicSession;
+import javax.naming.Context;
+import javax.naming.NamingException;
+
+import org.jboss.test.jbossmessaging.JMSTestCase;
+import org.apache.log4j.Category;
+
+/**
+ * JMS tests base class.
+ *
+ * Your test extends this class, and can then use common methods. To do
+ * the tests you use TopicWorker or QueueWorker and the MessageCreator,
+ * MessageFilter and perhaps MessageQos classes, directly or by extending
+ * them.
+ *
+ * You can change the connection factories and destinations used by the
+ * properties:   jbosstest.queuefactory, jbosstest.topicfactory, 
+ * jbosstest.queue or jbosstest.topic.
+ *
+ * @author <a href="mailto:richard.achmatowicz at jboss.com">Richard Achmatowicz</a>
+ * @author    <a href="pra at tim.se">Peter Antman</a>
+ * @version $Revision: 37406 $
+ */
+public class JMSBase extends JMSTestCase
+{
+   public static final int PUBLISHER = 0;
+   public static final int SUBSCRIBER = 1;
+   public static final int GETTER = 2;
+   public static final int CONNECTOR = 3;
+   public static final int FAILSAFE_SUBSCRIBER = 4;
+   public static final int TRANS_NONE = 0;
+   public static final int TRANS_INDIVIDUAL = 1;
+   public static final int TRANS_TOTAL = 2;
+   public static final String[] TRANS_DESC = {"NOT", "individually", "totally"};
+   public static final int DEFAULT_RUNSLEEP = 50;
+   public final Category log = getLog();
+
+   // Provider specific
+   public String TOPIC_FACTORY = "ConnectionFactory";
+   public String QUEUE_FACTORY = "ConnectionFactory";
+
+   public String TEST_QUEUE = "queue/testQueue";
+   public String TEST_TOPIC = "topic/testTopic";
+
+   public Context context;
+   public QueueConnectionFactory queueFactory;
+   public TopicConnectionFactory topicFactory;
+
+   public JMSBase(String name)
+   {
+      super(name);
+   }
+
+   public long getRunSleep()
+   {
+      log.info("run.sleep: " + System.getProperty("run.sleep"));
+      return 1000L * Integer.getInteger("run.sleep", DEFAULT_RUNSLEEP).intValue();
+   }
+
+   public void sleep(long sleep)
+   {
+      try
+      {
+         Thread.sleep(sleep);
+      }
+      catch (InterruptedException e)
+      {
+      }
+   }
+
+   public void drainTopic() throws JMSException
+   {
+      TopicWorker sub1 = new TopicWorker(GETTER,
+         TRANS_NONE,
+         null
+      );
+      sub1.connect();
+      sub1.get();
+      sub1.close();
+   }
+
+   public void drainQueue() throws JMSException
+   {
+      QueueWorker sub1 = new QueueWorker(GETTER,
+         TRANS_NONE,
+         null
+      );
+      sub1.connect();
+      sub1.get();
+      sub1.close();
+   }
+
+   /**
+    * The JUnit setup method
+    *
+    * @exception Exception  Description of Exception
+    */
+   protected void setUp() throws Exception
+   {
+      // Reconfigure acording to props
+      QUEUE_FACTORY = System.getProperty("jbosstest.queuefactory", QUEUE_FACTORY);
+      TOPIC_FACTORY = System.getProperty("jbosstest.topicfactory", TOPIC_FACTORY);
+      TEST_QUEUE = System.getProperty("jbosstest.queue", TEST_QUEUE);
+      TEST_TOPIC = System.getProperty("jbosstest.topic", TEST_TOPIC);
+
+      if (context == null)
+      {
+
+         context = getInitialContext();
+
+         queueFactory = (QueueConnectionFactory) context.lookup(QUEUE_FACTORY);
+         topicFactory = (TopicConnectionFactory) context.lookup(TOPIC_FACTORY);
+
+         getLog().debug("Connection to JMS provider established.");
+      }
+
+   }
+
+
+   public static void main(String[] args)
+   {
+
+   }
+
+   public abstract class JMSWorker implements Runnable, MessageListener, ExceptionListener
+   {
+
+      protected boolean stopRequested = false;
+      protected int messageHandled = 0;
+      protected Exception runEx = null;
+      protected MessageFilter filter;
+      protected MessageCreator creator;
+      protected int number = 1;
+      protected int type = -1;
+      protected int transacted;
+      protected QosConfig qosConfig = new QosConfig();
+      protected String userName;
+      protected String password;
+      protected String clientID;
+
+      // Generic ones, should be set by sublcasses
+      public Connection connection;
+      public Destination destination;
+      public Session session;
+      public MessageProducer producer;
+      public MessageConsumer consumer;
+
+      /**
+       * Create one without any settings, use mutators instead. Makes it easier to owerride.
+       */
+      public JMSWorker()
+      {
+      }
+
+      public JMSWorker(int type, int transacted, MessageFilter filter)
+      {
+         this.type = type;
+         this.transacted = transacted;
+         this.filter = filter;
+      }
+
+      public JMSWorker(int type,
+         int transacted,
+         MessageCreator creator,
+         int number
+         )
+      {
+         this.type = type;
+         this.transacted = transacted;
+         this.creator = creator;
+         this.number = number;
+      }
+
+      public void setSubscriberAttrs(int type, int transacted, MessageFilter filter)
+      {
+         this.type = type;
+         this.transacted = transacted;
+         this.filter = filter;
+      }
+
+      public void setPublisherAttrs(int type,
+         int transacted,
+         MessageCreator creator,
+         int number)
+      {
+         this.type = type;
+         this.transacted = transacted;
+         this.creator = creator;
+         this.number = number;
+      }
+
+      public void setUser(String userName, String password)
+      {
+         this.userName = userName;
+         this.password = password;
+      }
+
+      public void setClientID(String ID)
+      {
+         this.clientID = ID;
+      }
+
+      abstract public void publish() throws JMSException;
+
+      abstract public void publish(int nr) throws JMSException;
+
+      /**
+       * Subsribes, collects, checking any set filters. A messageComsumer must be created before calling this.
+       */
+      public void subscribe() throws JMSException
+      {
+         subscribe(false);
+      }
+
+      /**
+       * Subsribes, collects, checking any set filters. A messageComsumer must be created before calling this. If arg set to true, do a failsafe sub
+       */
+      public void subscribe(boolean failsafe) throws JMSException
+      {
+         if (consumer == null)
+            throw new JMSException("No messageConsumer created");
+
+         if (failsafe)
+            connection.setExceptionListener(this);
+
+         consumer.setMessageListener(this);
+
+      }
+
+      public void get() throws JMSException
+      {
+         Message msg = consumer.receive(2000);
+         while (msg != null)
+         {
+            if (filter != null)
+            {
+               if (filter.ok(msg))
+                  messageHandled++;
+            }
+            else
+            {
+               messageHandled++;
+            }
+            msg = consumer.receive(2000);
+         }
+      }
+
+      abstract public void connect() throws JMSException;
+
+      public void setQosConfig(QosConfig qosConfig)
+      {
+         this.qosConfig = qosConfig;
+      }
+
+      public void setStoped() throws JMSException
+      {
+         stopRequested = true;
+      }
+
+      public int getMessageHandled()
+      {
+         return messageHandled;
+      }
+
+      public Exception getException()
+      {
+         return runEx;
+      }
+
+      public void reset()
+      {
+         messageHandled = 0;
+         stopRequested = false;
+         runEx = null;
+      }
+
+      public void close()
+      {
+         try
+         {
+            if (consumer != null)
+               consumer.close();
+            if (producer != null)
+               producer.close();
+            if (session != null)
+               session.close();
+         }
+         catch (JMSException ex)
+         {
+         }
+         finally
+         {
+            if (connection != null)
+            {
+               try
+               {
+                  connection.close();
+               }
+               catch (JMSException ex)
+               {
+               }
+            }
+         }
+      }
+
+      public void onMessage(Message msg)
+      {
+         try
+         {
+            if (filter != null)
+            {
+               if (filter.ok(msg))
+                  messageHandled++;
+            }
+            else
+            {
+               messageHandled++;
+            }
+            if (session.getTransacted())
+               session.commit();
+         }
+         catch (Exception ex)
+         {
+            log.warn("Exception in on message: " + ex, ex);
+            runEx = ex;
+         }
+      }
+
+      /**
+       * onException handling is only for subscriber. Will try to to
+       * a connect followed by a subscribe
+       */
+      public void onException(JMSException ex)
+      {
+         log.error("Ex in connection: " + ex);
+
+         try
+         {
+            connection.setExceptionListener(null);
+            close();
+         }
+         catch (JMSException c)
+         {
+         }
+         
+         // Try reconnect, loops until success or shut down
+         try
+         {
+            boolean tryIt = true;
+            while (tryIt && !stopRequested)
+            {
+               log.info("Trying reconnect...");
+               try
+               {
+                  Thread.sleep(10000);
+               }
+               catch (InterruptedException ie)
+               {
+               }
+               try
+               {
+                  connect();
+                  subscribe(true);
+                  tryIt = false;
+                  log.info("Reconnect OK");
+                  //return;
+               }
+               catch (JMSException e)
+               {
+                  log.error("Error in reconnect: " + e);
+               }
+            }
+
+         }
+         catch (Exception je)
+         {
+            log.error("Strange error in failsafe handling" + je, je);
+         }
+      }
+
+      public void run()
+      {
+         try
+         {
+            switch (type)
+            {
+               case -1:
+                  log.info("Nothing to do for type " + type);
+                  break;
+               case PUBLISHER:
+                  connect();
+                  publish();
+                  break;
+               case SUBSCRIBER:
+                  connect();
+                  subscribe();
+                  break;
+               case GETTER:
+                  connect();
+                  get();
+                  break;
+               case CONNECTOR:
+                  connect();
+                  break;
+               case FAILSAFE_SUBSCRIBER:
+                  connect();
+                  subscribe(true);
+                  break;
+            }
+
+            //if the method does not hold an own thread, we do it here
+            while (!stopRequested)
+            {
+               try
+               {
+                  Thread.sleep(1000);
+               }
+               catch (InterruptedException ex)
+               {
+
+               }
+            }
+         }
+         catch (JMSException ex)
+         {
+            runEx = ex;
+            log.error("Could not run: " + ex, ex);
+         }
+      }
+   }
+
+   public interface MessageCreator
+   {
+      public void setSession(Session session);
+
+      public Message createMessage(int nr) throws JMSException;
+   }
+
+   public abstract class BaseMessageCreator implements MessageCreator
+   {
+      protected Session session;
+      protected String property;
+
+      public BaseMessageCreator(String property)
+      {
+         this.property = property;
+      }
+
+      public void setSession(Session session)
+      {
+         this.session = session;
+      }
+
+      abstract public Message createMessage(int nr) throws JMSException;
+   }
+
+
+   public class IntRangeMessageCreator extends BaseMessageCreator
+   {
+      int start = 0;
+
+      public IntRangeMessageCreator(String property)
+      {
+         super(property);
+      }
+
+      public IntRangeMessageCreator(String property, int start)
+      {
+         super(property);
+         this.start = start;
+      }
+
+      public Message createMessage(int nr) throws JMSException
+      {
+         if (session == null)
+            throw new JMSException("Session not allowed to be null");
+
+         Message msg = session.createMessage();
+         msg.setStringProperty(property, String.valueOf(start + nr));
+         return msg;
+      }
+   }
+
+   public interface MessageFilter
+   {
+      public boolean ok(Message msg) throws JMSException;
+   }
+
+   public class IntRangeMessageFilter implements MessageFilter
+   {
+      Class messageClass;
+      String className;
+      String property;
+      int low;
+      int max;
+      int counter = 0;
+      int report = 1000;
+
+      public IntRangeMessageFilter(Class messageClass, String property, int low, int max)
+      {
+         this.messageClass = messageClass;
+         this.property = property;
+         className = messageClass.getName();
+         this.low = low;
+         this.max = max;
+      }
+
+      private boolean validateClass(Message msg)
+      {
+         Class clazz = null;
+         if (msg instanceof javax.jms.TextMessage)
+            clazz = javax.jms.TextMessage.class;
+         else if (msg instanceof javax.jms.BytesMessage)
+            clazz = javax.jms.BytesMessage.class;
+         else if (msg instanceof javax.jms.MapMessage)
+            clazz = javax.jms.MapMessage.class;
+         else if (msg instanceof javax.jms.ObjectMessage)
+            clazz = javax.jms.ObjectMessage.class;
+         else if (msg instanceof javax.jms.StreamMessage)
+            clazz = javax.jms.StreamMessage.class;
+         else
+            clazz = javax.jms.Message.class;
+
+         return clazz.equals(messageClass);
+      }
+
+      public boolean ok(Message msg) throws JMSException
+      {
+         boolean res = false;
+         if (validateClass(msg))
+         {
+            if (msg.propertyExists(property))
+            {
+               String p = msg.getStringProperty(property);
+               try
+               {
+                  int i = Integer.parseInt(p);
+                  //log.debug("Received message " + property +"=" +i);
+                  if (i >= low && i < max)
+                     res = true;
+               }
+               catch (NumberFormatException ex)
+               {
+                  throw new JMSException("Property " + property + " was not int: " + p);
+               }
+            }
+         }
+         counter++;
+         int mod = counter % report;
+         if (mod == 0)
+            log.debug("Have received " + counter + " messages");
+         return res;
+      }
+
+   }
+
+   /*  
+   public class REMessageFilter implements MessageFilter {
+      Class messageClass;
+      String className;
+      String property;
+      RE re = null;
+      public REMessageFilter(Class messageClass, String property, String regexp) throws REException{
+         this.messageClass = messageClass;
+         this.property = property;
+         re = new RE(regexp);
+         className = messageClass.getName();
+      }
+      
+      public boolean ok(Message msg) throws JMSException{
+         boolean res = false;
+         if (className.equals(msg.getClass().getName())) {
+            if (msg.propertyExists(property)) {
+               String p = msg.getStringProperty(property);
+               if (re.getMatch(p)!=null)
+                  res = true;
+            } 
+         }
+         return true;
+      }
+   }
+   */
+   /**
+    * Defines quality of service for message publishing. Defaults are the same
+    * ase defined in SpyMessage.
+    */
+   public class QosConfig
+   {
+      int deliveryMode = DeliveryMode.PERSISTENT;
+      int priority = 4;
+      long ttl = 0;
+   }
+
+   public class TopicWorker extends JMSWorker
+   {
+      String durableHandle;
+
+      /**
+       * If using this, use mutators to add attrs.
+       */
+      public TopicWorker()
+      {
+         super();
+      }
+
+      public TopicWorker(int type, int transacted, MessageFilter filter)
+      {
+         super(type, transacted, filter);
+      }
+
+      public TopicWorker(int type,
+         int transacted,
+         MessageCreator creator,
+         int number
+         )
+      {
+         super(type, transacted, creator, number);
+      }
+
+
+      public void publish() throws JMSException
+      {
+         publish(number);
+      }
+
+      public void publish(int nr) throws JMSException
+      {
+         if (producer == null)
+            producer = ((TopicSession) session).createPublisher((Topic) destination);
+         if (creator == null)
+            throw new JMSException("Publish must have a MessageCreator set");
+
+         creator.setSession(session);
+         log.debug("Publishing " + nr + " messages");
+         for (int i = 0; i < nr; i++)
+         {
+            if (qosConfig != null)
+            {
+               ((TopicPublisher) producer).publish(creator.createMessage(i),
+                  qosConfig.deliveryMode,
+                  qosConfig.priority,
+                  qosConfig.ttl);
+            }
+            else
+            {
+               ((TopicPublisher) producer).publish(creator.createMessage(i));
+            }
+
+            messageHandled++;
+         }
+         if (session.getTransacted())
+            session.commit();
+         log.debug("Finished publishing");
+      }
+
+      public void subscribe() throws JMSException
+      {
+         subscribe(false);
+      }
+
+      public void subscribe(boolean failsafe) throws JMSException
+      {
+         if (durableHandle != null)
+            consumer = ((TopicSession) session).createDurableSubscriber((Topic) destination, durableHandle);
+         else
+            consumer = ((TopicSession) session).createSubscriber((Topic) destination);
+         super.subscribe(failsafe);
+         connection.start();
+      }
+
+      public void get() throws JMSException
+      {
+         consumer = ((TopicSession) session).createSubscriber((Topic) destination);
+         super.subscribe();
+         connection.start();
+      }
+
+      public void connect() throws JMSException
+      {
+         log.debug("Connecting: " + this.toString());
+         if (userName != null)
+            connection = topicFactory.createTopicConnection(userName, password);
+         else
+            connection = topicFactory.createTopicConnection();
+
+         if (clientID != null)
+         {
+            log.debug("Setting clientID" + clientID);
+            connection.setClientID(clientID);
+         }
+
+         session = ((TopicConnection) connection).createTopicSession(transacted != TRANS_NONE, Session.AUTO_ACKNOWLEDGE);
+         try
+         {
+            destination = (Destination) context.lookup(TEST_TOPIC);
+         }
+         catch (NamingException ex)
+         {
+            throw new JMSException("Could not lookup topic " + ex);
+         }
+      }
+
+      // Topic specific stuff
+      public void setDurable(String userId, String pwd, String handle)
+      {
+         this.userName = userId;
+         this.password = pwd;
+         this.durableHandle = handle;
+      }
+
+      public void setDurable(String handle)
+      {
+         this.durableHandle = handle;
+      }
+
+      public void unsubscribe() throws JMSException
+      {
+         if (durableHandle != null)
+            ((TopicSession) session).unsubscribe(durableHandle);
+      }
+
+      public String toString()
+      {
+         return "(userId=" + userName + " pwd=" + password + " handle=" + durableHandle + ")";
+      }
+
+   }
+
+   public class QueueWorker extends JMSWorker
+   {
+      String userId;
+      String pwd;
+      String handle;
+
+      /**
+       * If using this, use mutators to add attrs.
+       */
+      public QueueWorker()
+      {
+         super();
+      }
+
+      public QueueWorker(int type, int transacted, MessageFilter filter)
+      {
+         super(type, transacted, filter);
+      }
+
+      public QueueWorker(int type,
+         int transacted,
+         MessageCreator creator,
+         int number
+         )
+      {
+         super(type, transacted, creator, number);
+      }
+
+
+      public void publish() throws JMSException
+      {
+         publish(number);
+      }
+
+      public void publish(int nr) throws JMSException
+      {
+         if (producer == null)
+            producer = ((QueueSession) session).createSender((Queue) destination);
+         if (creator == null)
+            throw new JMSException("Publish must have a MessageCreator set");
+
+         creator.setSession(session);
+         log.debug("Publishing " + nr + " messages");
+         for (int i = 0; i < nr; i++)
+         {
+            if (qosConfig != null)
+            {
+               ((QueueSender) producer).send(creator.createMessage(i),
+                  qosConfig.deliveryMode,
+                  qosConfig.priority,
+                  qosConfig.ttl);
+            }
+            else
+            {
+               ((QueueSender) producer).send(creator.createMessage(i));
+            }
+
+            messageHandled++;
+         }
+         if (session.getTransacted())
+            session.commit();
+         log.debug("Finished publishing");
+      }
+
+      public void subscribe() throws JMSException
+      {
+         subscribe(false);
+      }
+
+      public void subscribe(boolean failsafe) throws JMSException
+      {
+
+         consumer = ((QueueSession) session).createReceiver((Queue) destination);
+         super.subscribe(failsafe);
+         connection.start();
+      }
+
+      public void get() throws JMSException
+      {
+         consumer = ((QueueSession) session).createReceiver((Queue) destination);
+         super.subscribe();
+         connection.start();
+      }
+
+      public void connect() throws JMSException
+      {
+         log.debug("Connecting: " + this.toString());
+         if (userName != null)
+            connection = queueFactory.createQueueConnection(userName, password);
+         else
+            connection = queueFactory.createQueueConnection();
+
+         if (clientID != null)
+            connection.setClientID(clientID);
+
+         session = ((QueueConnection) connection).createQueueSession(transacted != TRANS_NONE, Session.AUTO_ACKNOWLEDGE);
+         try
+         {
+            destination = (Destination) context.lookup(TEST_QUEUE);
+         }
+         catch (NamingException ex)
+         {
+            throw new JMSException("Could not lookup topic " + ex);
+         }
+      }
+
+
+      // Queue specific
+      public Enumeration browse() throws JMSException
+      {
+         QueueBrowser b = ((QueueSession) session).createBrowser((Queue) destination);
+         return b.getEnumeration();
+      }
+   }
+} // JMSBase

Added: trunk/testsuite/src/main/org/jboss/test/jbossmessaging/test/JoramUnitTestCase.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/jbossmessaging/test/JoramUnitTestCase.java	2006-10-31 21:46:53 UTC (rev 57956)
+++ trunk/testsuite/src/main/org/jboss/test/jbossmessaging/test/JoramUnitTestCase.java	2006-10-31 22:37:36 UTC (rev 57957)
@@ -0,0 +1,112 @@
+/*
+  * 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.jbossmessaging.test;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.jboss.test.JBossTestSetup;
+import org.jboss.test.jbossmessaging.JMSTestCase;
+
+import org.objectweb.jtests.jms.conform.connection.ConnectionTest;
+import org.objectweb.jtests.jms.conform.connection.TopicConnectionTest;
+import org.objectweb.jtests.jms.conform.message.MessageBodyTest;
+import org.objectweb.jtests.jms.conform.message.MessageDefaultTest;
+import org.objectweb.jtests.jms.conform.message.MessageTypeTest;
+import org.objectweb.jtests.jms.conform.message.headers.MessageHeaderTest;
+import org.objectweb.jtests.jms.conform.message.properties.JMSXPropertyTest;
+import org.objectweb.jtests.jms.conform.message.properties.MessagePropertyConversionTest;
+import org.objectweb.jtests.jms.conform.message.properties.MessagePropertyTest;
+import org.objectweb.jtests.jms.conform.queue.QueueBrowserTest;
+import org.objectweb.jtests.jms.conform.queue.TemporaryQueueTest;
+import org.objectweb.jtests.jms.conform.selector.SelectorSyntaxTest;
+import org.objectweb.jtests.jms.conform.selector.SelectorTest;
+import org.objectweb.jtests.jms.conform.session.QueueSessionTest;
+import org.objectweb.jtests.jms.conform.session.SessionTest;
+import org.objectweb.jtests.jms.conform.session.TopicSessionTest;
+import org.objectweb.jtests.jms.conform.session.UnifiedSessionTest;
+import org.objectweb.jtests.jms.conform.topic.TemporaryTopicTest;
+
+/**
+ * Joram unit tests
+ *
+ * @author <a href="mailto:richard.achmatowicz at jboss.com">Richard Achmatowicz</a>
+ * @version $Revision: 37406 $
+ */
+public class JoramUnitTestCase extends JMSTestCase
+{
+   public JoramUnitTestCase(String name)
+   {
+      super(name);
+   }
+
+   public static junit.framework.Test suite() throws Exception
+   {
+      TestSuite suite = new TestSuite();
+
+      suite.addTest(ConnectionTest.suite());
+      suite.addTest(TopicConnectionTest.suite());
+      suite.addTest(MessageBodyTest.suite());
+      suite.addTest(MessageDefaultTest.suite());
+      suite.addTest(MessageTypeTest.suite());
+      suite.addTest(MessageHeaderTest.suite());
+      suite.addTest(JMSXPropertyTest.suite());
+      suite.addTest(MessagePropertyConversionTest.suite());
+      suite.addTest(MessagePropertyTest.suite());
+      suite.addTest(QueueBrowserTest.suite());
+      suite.addTest(TemporaryQueueTest.suite());
+      suite.addTest(SelectorSyntaxTest.suite());
+      suite.addTest(SelectorTest.suite());
+      suite.addTest(QueueSessionTest.suite());
+      suite.addTest(SessionTest.suite());
+      suite.addTest(TopicSessionTest.suite());
+      suite.addTest(UnifiedSessionTest.suite());
+      suite.addTest(TemporaryTopicTest.suite());
+
+      // Create an initializer for the test suite
+      Test wrapper = new JBossTestSetup(suite)
+      {
+         protected void setUp() throws Exception
+         {
+            super.setUp();
+            ClassLoader loader = Thread.currentThread().getContextClassLoader();
+	    String resourceName = getJMSResourceRelativePathname("test-destinations-service.xml") ;
+
+            deploy (loader.getResource(resourceName).toString());
+         }
+         protected void tearDown() throws Exception
+         {
+            super.tearDown();
+            ClassLoader loader = Thread.currentThread().getContextClassLoader();
+	    String resourceName = getJMSResourceRelativePathname("test-destinations-service.xml") ;
+
+            undeploy (loader.getResource(resourceName).toString());
+         }
+      };
+
+      return wrapper;
+   }
+
+   public static void main(String[] args)
+   {
+   }
+} // SecurityTest

Added: trunk/testsuite/src/main/org/jboss/test/jbossmessaging/test/SecurityUnitTestCase.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/jbossmessaging/test/SecurityUnitTestCase.java	2006-10-31 21:46:53 UTC (rev 57956)
+++ trunk/testsuite/src/main/org/jboss/test/jbossmessaging/test/SecurityUnitTestCase.java	2006-10-31 22:37:36 UTC (rev 57957)
@@ -0,0 +1,1265 @@
+/*
+  * 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.jbossmessaging.test;
+
+import javax.management.ObjectName;
+import junit.framework.Assert;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.jboss.test.JBossTestSetup;
+import org.jboss.test.jbossmessaging.JMSBase;
+/**
+ * Test of security features in JMS providers
+ *
+ * @author <a href="mailto:richard.achmatowicz">Richard Achmatowicz</a>
+ * @author     <a href="pra at tim.se">Peter Antman</a>
+ * @version $Revision: 37406 $
+ */
+public class SecurityUnitTestCase extends JMSBase
+{
+   public SecurityUnitTestCase(String name)
+   {
+      super(name);
+   }
+
+   public void runLoginTest() throws Exception
+   {
+      TopicWorker sub1 = null;
+      TopicWorker pub1 = null;
+      try
+      {
+         drainTopic();
+         int ic = 5;
+         IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message.class, "USER_NR", 0, ic);
+         sub1 = new TopicWorker(SUBSCRIBER, TRANS_NONE, f1);
+         sub1.setUser("john", "needle");
+         Thread t1 = new Thread(sub1);
+         t1.start();
+         // Publish
+         IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
+         pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, ic);
+         pub1.connect();
+         pub1.publish();
+         Assert.assertEquals("Publisher did not publish correct number of messages " + pub1.getMessageHandled(), ic,
+               pub1.getMessageHandled());
+         // let sub1 have some time to handle the messages.
+         log.debug("Sleeping for " + ((ic * 10) / 60000) + " minutes");
+         sleep(ic * 100);
+         Assert.assertEquals("Subscriber did not get correct number of messages " + sub1.getMessageHandled(), ic, sub1
+               .getMessageHandled());
+         sub1.close();
+         pub1.close();
+      }
+      catch (Throwable t)
+      {
+         if (t instanceof junit.framework.AssertionFailedError)
+            throw (junit.framework.AssertionFailedError) t;
+         log.error("Error in test: " + t, t);
+         throw new Exception(t.getMessage());
+      }
+      finally
+      {
+         try
+         {
+            if (sub1 != null)
+               sub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+         try
+         {
+            if (pub1 != null)
+               pub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   /**
+    Tests that check authentication
+    1. Login without cred
+    2. Login with valid usedid,pwd
+    3. Login with valid user, unvalid pwd
+    4. Login with unvalid user.
+    */
+   public void runLoginNoCred() throws Exception
+   {
+      TopicWorker pub1 = null;
+      try
+      {
+         drainTopic();
+         pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, null, 0);
+         pub1.connect();
+      }
+      catch (Exception ex)
+      {
+         Assert.fail("Could lot login without any cred");
+      }
+      finally
+      {
+         try
+         {
+            pub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runLoginValidCred() throws Exception
+   {
+      TopicWorker pub1 = null;
+      try
+      {
+         drainTopic();
+         pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, null, 0);
+         pub1.setUser("john", "needle");
+         pub1.connect();
+      }
+      catch (Exception ex)
+      {
+         Assert.fail("Could lot login with valid cred");
+      }
+      finally
+      {
+         try
+         {
+            pub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runLoginInvalidPwd() throws Exception
+   {
+      TopicWorker pub1 = null;
+      try
+      {
+         drainTopic();
+         pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, null, 0);
+         pub1.setUser("john", "bogus");
+         Exception e = null;
+         try
+         {
+            pub1.connect();
+         }
+         catch (Exception ex)
+         {
+            e = ex;
+         }
+         log.debug(e);
+         Assert.assertTrue("Loggin in with invalid password did not throw correct exception",
+               e instanceof javax.jms.JMSSecurityException);
+      }
+      finally
+      {
+         try
+         {
+            pub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runLoginInvalidCred() throws Exception
+   {
+      TopicWorker pub1 = null;
+      try
+      {
+         drainTopic();
+         pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, null, 0);
+         pub1.setUser("bogus", "bogus");
+         Exception e = null;
+         try
+         {
+            pub1.connect();
+         }
+         catch (Exception ex)
+         {
+            e = ex;
+         }
+         log.debug(e);
+         Assert.assertTrue("Loggin in with invalid user did not throw correct exception",
+               e instanceof javax.jms.JMSSecurityException);
+      }
+      finally
+      {
+         try
+         {
+            pub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   /**
+    An number of tests to verrify that clientID works as expected:
+    
+    1. Nothing. getClientID should return a string starting withID
+    2. user/pwd with preconfigured clientID, should return preconf
+    3. setClientID, should return the set clientID
+    4. setClienID starting with ID, should trow invalid clientID
+    5. setClientID same as a preconf, should trow invalid clientID
+    6. setClientID after any method beeing invoked on con, throw invalid state
+    */
+   public void runClientIDNormalTest() throws Exception
+   {
+      TopicWorker pub1 = null;
+      try
+      {
+         drainTopic();
+         int ic = 5;
+         // Publish
+         IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
+         pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, ic);
+         pub1.connect();
+         pub1.publish();
+         Assert.assertTrue("Client did not get a valid clientID", pub1.connection.getClientID().startsWith("ID"));
+         pub1.close();
+      }
+      catch (Throwable t)
+      {
+         if (t instanceof junit.framework.AssertionFailedError)
+            throw (junit.framework.AssertionFailedError) t;
+         log.error("Error in test: " + t, t);
+         throw new Exception(t.getMessage());
+      }
+      finally
+      {
+         try
+         {
+            pub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runClientIDPreconfTest() throws Exception
+   {
+      TopicWorker pub1 = null;
+      try
+      {
+         drainTopic();
+         int ic = 5;
+         // Publish
+         IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
+         pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, ic);
+         pub1.setUser("john", "needle");
+         pub1.connect();
+         pub1.publish();
+         Assert.assertEquals("Client did not get a valid clientID", "DurableSubscriberExample", pub1.connection
+               .getClientID());
+         pub1.close();
+      }
+      catch (Throwable t)
+      {
+         if (t instanceof junit.framework.AssertionFailedError)
+            throw (junit.framework.AssertionFailedError) t;
+         log.error("Error in test: " + t, t);
+         throw new Exception(t.getMessage());
+      }
+      finally
+      {
+         try
+         {
+            pub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runClientIDSetTest() throws Exception
+   {
+      TopicWorker pub1 = null;
+      try
+      {
+         drainTopic();
+         int ic = 5;
+         // Publish
+         IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
+         pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, ic);
+         pub1.setClientID("myId");
+         pub1.connect();
+         pub1.publish();
+         Assert.assertEquals("Client did not get a valid clientID", "myId", pub1.connection.getClientID());
+         pub1.close();
+      }
+      catch (Throwable t)
+      {
+         if (t instanceof junit.framework.AssertionFailedError)
+            throw (junit.framework.AssertionFailedError) t;
+         log.error("Error in test: " + t, t);
+         throw new Exception(t.getMessage());
+      }
+      finally
+      {
+         try
+         {
+            pub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runClientIDSetSteelPreconf() throws Exception
+   {
+      TopicWorker pub1 = null;
+      try
+      {
+         drainTopic();
+         int ic = 5;
+         // Publish
+         IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
+         pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, ic);
+         pub1.setClientID("DurableSubscriberExample");
+         Exception e = null;
+         try
+         {
+            pub1.connect();
+         }
+         catch (Exception ex)
+         {
+            e = ex;
+         }
+         log.debug(e);
+         Assert.assertTrue("Setting a clientID wich is preconfigured did not throw correct exception",
+               e instanceof javax.jms.InvalidClientIDException);
+         pub1.close();
+      }
+      catch (Throwable t)
+      {
+         if (t instanceof junit.framework.AssertionFailedError)
+            throw (junit.framework.AssertionFailedError) t;
+         log.error("Error in test: " + t, t);
+         throw new Exception(t.getMessage());
+      }
+      finally
+      {
+         try
+         {
+            pub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runClientIDSetAfterInvoke() throws Exception
+   {
+      TopicWorker pub1 = null;
+      try
+      {
+         drainTopic();
+         int ic = 5;
+         // Publish
+         IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
+         pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, ic);
+         pub1.connect();
+         pub1.publish();
+         Exception e = null;
+         try
+         {
+            pub1.connection.setClientID("myID");
+         }
+         catch (Exception ex)
+         {
+            e = ex;
+         }
+         log.debug(e);
+         Assert.assertTrue("Setting a clientID after connection is used did not throw correct exception: " + e,
+               e instanceof javax.jms.IllegalStateException);
+         pub1.close();
+      }
+      catch (Throwable t)
+      {
+         if (t instanceof junit.framework.AssertionFailedError)
+            throw (junit.framework.AssertionFailedError) t;
+         log.error("Error in test: " + t, t);
+         throw new Exception(t.getMessage());
+      }
+      finally
+      {
+         try
+         {
+            pub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   /**
+    Tests to check autorization.
+    
+    Remember there are actuallt two types of fails:
+    a) You are a user, but do no belong to a group that has acl.
+    b) You belong to a group, but that group does not have acl.
+    we test the first for topics and the second for queues, by
+    configuration in jbossmq-testsuite-service.xml
+    
+    Tests that check autorization.
+    1. test valid topic publisher
+    2. test invalid topic publisher
+    3. test valid topic subscriber
+    4. test invalid topic subscriber
+    5. test valid queue sender
+    6. test invalid queue sender
+    7. test valid queue receiver
+    8. test invalid queue receiver
+    9. test valid queue browser.
+    10. test invalid queue browser
+    11. test preconf dur sub, to valid dest.
+    12. test preconf dur sub, to invalid dest.
+    13. test dyn dur sub, to valid dest.
+    14. test  dyn dur sub, to valid dest.
+    */
+   public void runAuzValidTopicPublisher() throws Exception
+   {
+      TopicWorker pub1 = null;
+      try
+      {
+         IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
+         pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, 1);
+         pub1.setUser("john", "needle");
+         pub1.connect();
+         pub1.publish();
+      }
+      catch (Exception ex)
+      {
+         Assert.fail("Could not publish to valid destination");
+      }
+      finally
+      {
+         try
+         {
+            pub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runAuzValidTopicPublisherTransaction() throws Exception
+   {
+      TopicWorker pub1 = null;
+      try
+      {
+         IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
+         pub1 = new TopicWorker(PUBLISHER, TRANS_INDIVIDUAL, c1, 1);
+         pub1.setUser("john", "needle");
+         pub1.connect();
+         pub1.publish();
+      }
+      catch (Exception ex)
+      {
+         Assert.fail("Could not publish to valid destination");
+      }
+      finally
+      {
+         try
+         {
+            pub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runAuzInvalidTopicPublisher() throws Exception
+   {
+      TopicWorker pub1 = null;
+      try
+      {
+         IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
+         pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, 1);
+         pub1.setUser("nobody", "nobody");
+         pub1.connect();
+         Exception e = null;
+         try
+         {
+            pub1.publish();
+         }
+         catch (Exception ex)
+         {
+            e = ex;
+         }
+         log.debug(e);
+         Assert.assertTrue("Unauz topic publishing throw wrong exception: " + e,
+               e instanceof javax.jms.JMSSecurityException);
+      }
+      finally
+      {
+         try
+         {
+            pub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runAuzInvalidTopicPublisherTransaction() throws Exception
+   {
+      TopicWorker pub1 = null;
+      try
+      {
+         IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
+         pub1 = new TopicWorker(PUBLISHER, TRANS_INDIVIDUAL, c1, 1);
+         pub1.setUser("nobody", "nobody");
+         pub1.connect();
+         Exception e = null;
+         try
+         {
+            pub1.publish();
+         }
+         catch (Exception ex)
+         {
+            e = ex;
+         }
+         log.debug(e);
+         Assert.assertTrue("Unauz topic publishing throw wrong exception: " + e,
+               e instanceof javax.jms.JMSSecurityException);
+      }
+      finally
+      {
+         try
+         {
+            pub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runAuzValidTopicSubscriber() throws Exception
+   {
+      TopicWorker sub1 = null;
+      try
+      {
+         drainTopic();
+         IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message.class, "USER_NR", 0, 1);
+         sub1 = new TopicWorker(SUBSCRIBER, TRANS_NONE, f1);
+         sub1.setUser("john", "needle");
+         Thread t1 = new Thread(sub1);
+         t1.start();
+         sleep(1000L);
+         Exception ex = sub1.getException();
+         t1.interrupt();
+         Assert.assertTrue("Autz topic subscriber did not work", ex == null);
+      }
+      finally
+      {
+         try
+         {
+            sub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runAuzValidTopicSubscriberTransaction() throws Exception
+   {
+      TopicWorker sub1 = null;
+      try
+      {
+         drainTopic();
+         IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message.class, "USER_NR", 0, 1);
+         sub1 = new TopicWorker(SUBSCRIBER, TRANS_INDIVIDUAL, f1);
+         sub1.setUser("john", "needle");
+         Thread t1 = new Thread(sub1);
+         t1.start();
+         sleep(1000L);
+         Exception ex = sub1.getException();
+         t1.interrupt();
+         Assert.assertTrue("Autz topic subscriber did not work", ex == null);
+      }
+      finally
+      {
+         try
+         {
+            sub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runAuzInvalidTopicSubscriber() throws Exception
+   {
+      TopicWorker sub1 = null;
+      try
+      {
+         drainTopic();
+         IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message.class, "USER_NR", 0, 1);
+         sub1 = new TopicWorker(SUBSCRIBER, TRANS_NONE, f1);
+         sub1.setUser("nobody", "nobody");
+         Thread t1 = new Thread(sub1);
+         t1.start();
+         sleep(1000L);
+         Exception ex = sub1.getException();
+         t1.interrupt();
+         Assert.assertTrue("Unautz topic subscriber throw wrong exception: " + ex,
+               ex instanceof javax.jms.JMSSecurityException);
+      }
+      finally
+      {
+         try
+         {
+            sub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runAuzInvalidTopicSubscriberTransaction() throws Exception
+   {
+      TopicWorker sub1 = null;
+      try
+      {
+         drainTopic();
+         IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message.class, "USER_NR", 0, 1);
+         sub1 = new TopicWorker(SUBSCRIBER, TRANS_INDIVIDUAL, f1);
+         sub1.setUser("nobody", "nobody");
+         Thread t1 = new Thread(sub1);
+         t1.start();
+         sleep(1000L);
+         Exception ex = sub1.getException();
+         t1.interrupt();
+         Assert.assertTrue("Unautz topic subscriber throw wrong exception: " + ex,
+               ex instanceof javax.jms.JMSSecurityException);
+      }
+      finally
+      {
+         try
+         {
+            sub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runAuzValidQueueSender() throws Exception
+   {
+      QueueWorker pub1 = null;
+      try
+      {
+         IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
+         pub1 = new QueueWorker(PUBLISHER, TRANS_NONE, c1, 1);
+         pub1.setUser("john", "needle");
+         pub1.connect();
+         pub1.publish();
+      }
+      catch (Exception ex)
+      {
+         Assert.fail("Could not publish to valid destination");
+      }
+      finally
+      {
+         try
+         {
+            pub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runAuzValidQueueSenderTransaction() throws Exception
+   {
+      QueueWorker pub1 = null;
+      try
+      {
+         IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
+         pub1 = new QueueWorker(PUBLISHER, TRANS_INDIVIDUAL, c1, 1);
+         pub1.setUser("john", "needle");
+         pub1.connect();
+         pub1.publish();
+      }
+      catch (Exception ex)
+      {
+         Assert.fail("Could not publish to valid destination");
+      }
+      finally
+      {
+         try
+         {
+            pub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runAuzInvalidQueueSender() throws Exception
+   {
+      QueueWorker pub1 = null;
+      try
+      {
+         IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
+         pub1 = new QueueWorker(PUBLISHER, TRANS_NONE, c1, 1);
+         pub1.setUser("nobody", "nobody");
+         pub1.connect();
+         Exception e = null;
+         try
+         {
+            pub1.publish();
+         }
+         catch (Exception ex)
+         {
+            e = ex;
+         }
+         log.debug(e);
+         Assert.assertTrue("Unauz queue publishing throw wrong exception: " + e,
+               e instanceof javax.jms.JMSSecurityException);
+      }
+      finally
+      {
+         try
+         {
+            pub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runAuzInvalidQueueSenderTransaction() throws Exception
+   {
+      QueueWorker pub1 = null;
+      try
+      {
+         IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
+         pub1 = new QueueWorker(PUBLISHER, TRANS_INDIVIDUAL, c1, 1);
+         pub1.setUser("nobody", "nobody");
+         pub1.connect();
+         Exception e = null;
+         try
+         {
+            pub1.publish();
+         }
+         catch (Exception ex)
+         {
+            e = ex;
+         }
+         log.debug(e);
+         Assert.assertTrue("Unauz queue publishing throw wrong exception: " + e,
+               e instanceof javax.jms.JMSSecurityException);
+      }
+      finally
+      {
+         try
+         {
+            pub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runAuzValidQueueReceiver() throws Exception
+   {
+      QueueWorker sub1 = null;
+      try
+      {
+         IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message.class, "USER_NR", 0, 1);
+         sub1 = new QueueWorker(GETTER, TRANS_NONE, f1);
+         sub1.setUser("john", "needle");
+         sub1.connect();
+         Exception ex = null;
+         try
+         {
+            sub1.get();
+         }
+         catch (Exception e)
+         {
+            ex = e;
+            log.error("ValidQueueReceiver got an exception: " + e, e);
+         }
+         Assert.assertTrue("Autz queue receiver did not work", ex == null);
+      }
+      finally
+      {
+         try
+         {
+            sub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runAuzValidQueueReceiverTransaction() throws Exception
+   {
+      QueueWorker sub1 = null;
+      try
+      {
+         IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message.class, "USER_NR", 0, 1);
+         sub1 = new QueueWorker(GETTER, TRANS_INDIVIDUAL, f1);
+         sub1.setUser("john", "needle");
+         sub1.connect();
+         Exception ex = null;
+         try
+         {
+            sub1.get();
+         }
+         catch (Exception e)
+         {
+            ex = e;
+            log.error("ValidQueueReceiver got an exception: " + e, e);
+         }
+         Assert.assertTrue("Autz queue receiver did not work", ex == null);
+      }
+      finally
+      {
+         try
+         {
+            sub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runAuzInvalidQueueReceiver() throws Exception
+   {
+      QueueWorker sub1 = null;
+      try
+      {
+         IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message.class, "USER_NR", 0, 1);
+         sub1 = new QueueWorker(GETTER, TRANS_NONE, f1);
+         sub1.setUser("nobody", "nobody");
+         sub1.connect();
+         Exception ex = null;
+         try
+         {
+            sub1.get();
+         }
+         catch (Exception e)
+         {
+            ex = e;
+         }
+         Assert.assertTrue("Unautz queue receiver throw wrong exception: " + ex,
+               ex instanceof javax.jms.JMSSecurityException);
+      }
+      finally
+      {
+         try
+         {
+            sub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runAuzInvalidQueueReceiverTransaction() throws Exception
+   {
+      QueueWorker sub1 = null;
+      try
+      {
+         IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message.class, "USER_NR", 0, 1);
+         sub1 = new QueueWorker(GETTER, TRANS_INDIVIDUAL, f1);
+         sub1.setUser("nobody", "nobody");
+         sub1.connect();
+         Exception ex = null;
+         try
+         {
+            sub1.get();
+         }
+         catch (Exception e)
+         {
+            ex = e;
+         }
+         Assert.assertTrue("Unautz queue receiver throw wrong exception: " + ex,
+               ex instanceof javax.jms.JMSSecurityException);
+      }
+      finally
+      {
+         try
+         {
+            sub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runAuzValidQueueBrowser() throws Exception
+   {
+      QueueWorker sub1 = null;
+      try
+      {
+         IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message.class, "USER_NR", 0, 1);
+         sub1 = new QueueWorker(GETTER, TRANS_NONE, f1);
+         sub1.setUser("john", "needle");
+         sub1.connect();
+         Exception ex = null;
+         try
+         {
+            sub1.browse();
+         }
+         catch (Exception e)
+         {
+            ex = e;
+            log.error("ValidQueueBrowser throw exception: " + e, e);
+         }
+         Assert.assertTrue("Autz queue receiver did not work", ex == null);
+      }
+      finally
+      {
+         try
+         {
+            sub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runAuzInvalidQueueBrowser() throws Exception
+   {
+      QueueWorker sub1 = null;
+      try
+      {
+         IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message.class, "USER_NR", 0, 1);
+         sub1 = new QueueWorker(GETTER, TRANS_NONE, f1);
+         sub1.setUser("nobody", "nobody");
+         sub1.connect();
+         Exception ex = null;
+         try
+         {
+            sub1.browse();
+         }
+         catch (Exception e)
+         {
+            ex = e;
+         }
+         Assert.assertTrue("Unautz queue receiver throw wrong exception: " + ex,
+               ex instanceof javax.jms.JMSSecurityException);
+      }
+      finally
+      {
+         try
+         {
+            sub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runValidPreconfDurSub() throws Exception
+   {
+      TopicWorker sub1 = null;
+      TopicWorker pub1 = null;
+      try
+      {
+         // Clean testarea up
+         drainTopic();
+         int ic = 5;
+         // Set up a durable subscriber
+         IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message.class, "DURABLE_NR", 0, ic);
+         sub1 = new TopicWorker(SUBSCRIBER, TRANS_NONE, f1);
+         sub1.setDurable("john", "needle", "sub2");
+         Thread t1 = new Thread(sub1);
+         t1.start();
+         // Let is take some time to really set up the dur sub
+         sleep(2000);
+         // Publish
+         IntRangeMessageCreator c1 = new IntRangeMessageCreator("DURABLE_NR", 0);
+         pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, ic);
+         pub1.connect();
+         pub1.publish();
+         Assert.assertEquals("Publisher did not publish correct number of messages " + pub1.getMessageHandled(), ic,
+               pub1.getMessageHandled());
+         // let sub1 have some time to handle the messages.
+         log.debug("Sleeping for " + ((ic * 100) / 60000) + " minutes");
+         sleep(ic * 100);
+         Exception ex = sub1.getException();
+         if (ex != null)
+            log.error("ValidPreconfDurSub got an exception: " + ex, ex);
+         Assert.assertTrue("ValidPreconfDurSub did not work", ex == null);
+         Assert.assertEquals("Subscriber did not get correct number of messages " + sub1.getMessageHandled(), ic, sub1
+               .getMessageHandled());
+         t1.interrupt();
+      }
+      finally
+      {
+         try
+         {
+            pub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+         try
+         {
+            // if this stops working it might be that we have become spec
+            // compliant an do not allow unsubscribe with an open consumer.
+            sub1.unsubscribe();
+            sub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runInvalidPreconfDurSub() throws Exception
+   {
+      TopicWorker sub1 = null;
+      try
+      {
+         // Clean testarea up
+         TEST_TOPIC = "topic/securedTopic";
+         //drainTopic();
+         int ic = 5;
+         // Set up a durable subscriber
+         IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message.class, "DURABLE_NR", 0, ic);
+         sub1 = new TopicWorker(SUBSCRIBER, TRANS_NONE, f1);
+         sub1.setDurable("john", "needle", "sub3");
+         Thread t1 = new Thread(sub1);
+         t1.start();
+         // Let is take some time to really set up the dur sub
+         sleep(2000);
+         Exception ex = sub1.getException();
+         Assert.assertTrue("InvalidPreconfDurSub did not get correct exception:" + ex,
+               ex instanceof javax.jms.JMSSecurityException);
+         t1.interrupt();
+      }
+      finally
+      {
+         try
+         {
+            sub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runValidDynDurSub() throws Exception
+   {
+      TopicWorker sub1 = null;
+      TopicWorker pub1 = null;
+      try
+      {
+         // Clean testarea up
+         drainTopic();
+         int ic = 5;
+         // Set up a durable subscriber
+         IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message.class, "DURABLE_NR", 0, ic);
+         sub1 = new TopicWorker(SUBSCRIBER, TRANS_NONE, f1);
+         sub1.setDurable("dynsub", "dynsub", "sub4");
+         sub1.setClientID("myId");
+         Thread t1 = new Thread(sub1);
+         t1.start();
+         // Let is take some time to really set up the dur sub
+         sleep(2000);
+         // Publish
+         IntRangeMessageCreator c1 = new IntRangeMessageCreator("DURABLE_NR", 0);
+         pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, ic);
+         pub1.connect();
+         pub1.publish();
+         Assert.assertEquals("Publisher did not publish correct number of messages " + pub1.getMessageHandled(), ic,
+               pub1.getMessageHandled());
+         // let sub1 have some time to handle the messages.
+         log.debug("Sleeping for " + ((ic * 100) / 60000) + " minutes");
+         sleep(ic * 100);
+         Exception ex = sub1.getException();
+         if (ex != null)
+            log.error("ValidDynDurSub got an exception: " + ex, ex);
+         Assert.assertTrue("ValidDynDurSub did not work", ex == null);
+         Assert.assertEquals("Subscriber did not get correct number of messages " + sub1.getMessageHandled(), ic, sub1
+               .getMessageHandled());
+         t1.interrupt();
+      }
+      finally
+      {
+         try
+         {
+            pub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+         try
+         {
+            // if this stops working it might be that we have become spec
+            // compliant an do not allow unsubscribe with an open consumer.
+            sub1.unsubscribe();
+            sub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public void runInvalidDynDurSub() throws Exception
+   {
+      TopicWorker sub1 = null;
+      try
+      {
+         // Clean testarea up
+         TEST_TOPIC = "topic/securedTopic";
+         //drainTopic();
+         int ic = 5;
+         // Set up a durable subscriber
+         IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message.class, "DURABLE_NR", 0, ic);
+         sub1 = new TopicWorker(SUBSCRIBER, TRANS_NONE, f1);
+         sub1.setDurable("dynsub", "dynsub", "sub5");
+         sub1.setClientID("myId2");
+         Thread t1 = new Thread(sub1);
+         t1.start();
+         // Let is take some time to really set up the dur sub
+         sleep(2000);
+         Exception ex = sub1.getException();
+         Assert.assertTrue("InvalidDynDurSub did not get correct exception:" + ex,
+               ex instanceof javax.jms.JMSSecurityException);
+         t1.interrupt();
+      }
+      finally
+      {
+         try
+         {
+            sub1.close();
+         }
+         catch (Exception ex)
+         {
+         }
+      }
+   }
+
+   public static junit.framework.Test suite() throws Exception
+   {
+      TestSuite suite = new TestSuite();
+
+      suite.addTest(new SecurityUnitTestCase("runLoginTest"));
+      // Authentication tests
+      suite.addTest(new SecurityUnitTestCase("runLoginNoCred"));
+      suite.addTest(new SecurityUnitTestCase("runLoginValidCred"));
+      suite.addTest(new SecurityUnitTestCase("runLoginInvalidPwd"));
+      suite.addTest(new SecurityUnitTestCase("runLoginInvalidCred"));
+      // ClientID tests
+      suite.addTest(new SecurityUnitTestCase("runClientIDNormalTest"));
+      suite.addTest(new SecurityUnitTestCase("runClientIDPreconfTest"));
+      suite.addTest(new SecurityUnitTestCase("runClientIDSetTest"));
+      suite.addTest(new SecurityUnitTestCase("runClientIDSetSteelPreconf"));
+      suite.addTest(new SecurityUnitTestCase("runClientIDSetAfterInvoke"));
+      // Autorization tests
+      suite.addTest(new SecurityUnitTestCase("runAuzValidTopicPublisher"));
+      suite.addTest(new SecurityUnitTestCase("runAuzInvalidTopicPublisher"));
+      suite.addTest(new SecurityUnitTestCase("runAuzValidTopicSubscriber"));
+      suite.addTest(new SecurityUnitTestCase("runAuzInvalidTopicSubscriber"));
+      suite.addTest(new SecurityUnitTestCase("runAuzValidQueueSender"));
+      suite.addTest(new SecurityUnitTestCase("runAuzInvalidQueueSender"));
+      suite.addTest(new SecurityUnitTestCase("runAuzValidQueueReceiver"));
+      suite.addTest(new SecurityUnitTestCase("runAuzInvalidQueueReceiver"));
+      suite.addTest(new SecurityUnitTestCase("runAuzValidTopicPublisherTransaction"));
+      suite.addTest(new SecurityUnitTestCase("runAuzInvalidTopicPublisherTransaction"));
+      suite.addTest(new SecurityUnitTestCase("runAuzValidTopicSubscriberTransaction"));
+      suite.addTest(new SecurityUnitTestCase("runAuzInvalidTopicSubscriberTransaction"));
+      suite.addTest(new SecurityUnitTestCase("runAuzValidQueueSenderTransaction"));
+      suite.addTest(new SecurityUnitTestCase("runAuzInvalidQueueSenderTransaction"));
+      suite.addTest(new SecurityUnitTestCase("runAuzValidQueueReceiverTransaction"));
+      suite.addTest(new SecurityUnitTestCase("runAuzInvalidQueueReceiverTransaction"));
+      suite.addTest(new SecurityUnitTestCase("runAuzValidQueueBrowser"));
+      suite.addTest(new SecurityUnitTestCase("runAuzInvalidQueueBrowser"));
+      suite.addTest(new SecurityUnitTestCase("runValidPreconfDurSub"));
+      suite.addTest(new SecurityUnitTestCase("runInvalidPreconfDurSub"));
+      suite.addTest(new SecurityUnitTestCase("runValidDynDurSub"));
+      suite.addTest(new SecurityUnitTestCase("runInvalidDynDurSub"));
+      //suite.addTest(new DurableSubscriberTest("testBadClient"));
+      // Create an initializer for the test suite
+      Test wrapper = new JBossTestSetup(suite)
+      {
+         protected void setUp() throws Exception
+         {
+            super.setUp();
+            ClassLoader loader = Thread.currentThread().getContextClassLoader();
+	    String resourceName = getJMSResourceRelativePathname("test-destinations-service.xml") ;
+
+            deploy (loader.getResource(resourceName).toString());
+         }
+         protected void tearDown() throws Exception
+         {
+            super.tearDown();
+            // Remove all the messages created during this test
+            //getServer().invoke(new ObjectName("jboss.mq.destination:service=Queue,name=testQueue"),
+            //      "removeAllMessages", new Object[0], new String[0]);
+	    // drainQueue() ;
+
+            ClassLoader loader = Thread.currentThread().getContextClassLoader();
+	    String resourceName = getJMSResourceRelativePathname("test-destinations-service.xml") ;
+
+            undeploy (loader.getResource(resourceName).toString());
+         }
+      };
+
+      return wrapper;
+   }
+
+   public static void main(String[] args)
+   {
+   }
+} // SecurityTest




More information about the jboss-cvs-commits mailing list