You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by de...@apache.org on 2011/07/25 16:10:04 UTC

svn commit: r1150708 - in /activemq/trunk/activemq-console/src: main/java/org/apache/activemq/console/command/ main/java/org/apache/activemq/console/filter/ main/java/org/apache/activemq/console/util/ test/java/org/apache/activemq/console/command/

Author: dejanb
Date: Mon Jul 25 14:10:02 2011
New Revision: 1150708

URL: http://svn.apache.org/viewvc?rev=1150708&view=rev
Log:
https://issues.apache.org/jira/browse/AMQ-3411 - user/pass for consolne commands

Added:
    activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/DefaultPasswordFactory.java
    activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/PasswordFactory.java
    activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/DummyConnectionFactory.java
    activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/InvalidConnectionFactory.java
    activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/LowercasingPasswordFactory.java
    activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/TestAMQ3411.java
      - copied, changed from r1150666, activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/TestAMQ3410.java
Modified:
    activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/AbstractAmqCommand.java
    activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/AmqBrowseCommand.java
    activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/filter/AmqMessagesQueryFilter.java
    activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/util/AmqMessagesUtil.java
    activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/TestAMQ3410.java
    activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/TestPurgeCommand.java

Modified: activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/AbstractAmqCommand.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/AbstractAmqCommand.java?rev=1150708&r1=1150707&r2=1150708&view=diff
==============================================================================
--- activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/AbstractAmqCommand.java (original)
+++ activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/AbstractAmqCommand.java Mon Jul 25 14:10:02 2011
@@ -33,7 +33,11 @@ public abstract class AbstractAmqCommand
     private URI brokerUrl;
     private ConnectionFactory factory;
     private String factoryClassString;
+    private String username;
+    private String password;
+    private PasswordFactory passwordFactory;
     private final List<Connection> connections = new ArrayList<Connection>();
+    private String passwordFactoryClassString;
 
     /**
      * Establishes a connection to the remote broker specified by the broker
@@ -43,17 +47,7 @@ public abstract class AbstractAmqCommand
      * @throws JMSException
      */
     protected Connection createConnection() throws JMSException {
-        if (getBrokerUrl() == null) {
-            context
-                .printException(new IllegalStateException("You must specify a broker "
-                                                          + "URL to connect to using the --amqurl option."));
-            return null;
-        }
-
-        Connection conn = getFactory().createConnection();
-        connections.add(conn);
-
-        return conn;
+        return createConnection(getUsername(), getPassword());
     }
 
     /**
@@ -73,7 +67,14 @@ public abstract class AbstractAmqCommand
             return null;
         }
 
-        Connection conn = getFactory().createConnection(username, password);
+        ConnectionFactory factory = getConnectionFactory();
+        Connection conn;
+
+        if (null == username && null == password)
+            conn = factory.createConnection();
+        else
+            conn = factory.createConnection(username, password);
+
         connections.add(conn);
         conn.start();
 
@@ -130,6 +131,12 @@ public abstract class AbstractAmqCommand
             }
         } else if (token.equals("--factory")) {
             factoryClassString = (String) tokens.remove(0);
+        } else if (token.equals("--passwordFactory")) {
+            passwordFactoryClassString = (String) tokens.remove(0);
+        } else if (token.equals("--password")) {
+            password = (String) tokens.remove(0);
+        } else if (token.equals("--user")) {
+            username = (String) tokens.remove(0);
         } else {
             // Let the super class handle the option
             super.handleOption(token, tokens);
@@ -164,48 +171,104 @@ public abstract class AbstractAmqCommand
         return brokerUrl;
     }
 
-	/**
-	 * @return the factory
-	 */
-	@SuppressWarnings("unchecked")
-    public ConnectionFactory getFactory() {
+    /**
+     * @return the factory
+     */
+    @SuppressWarnings("unchecked")
+    public ConnectionFactory getConnectionFactory() {
         if (factory == null && factoryClassString != null) {
             try {
                 Class klass = Class.forName(factoryClassString);
-                if (klass.isInstance(ConnectionFactory.class)) {
-                    Class<ConnectionFactory> factoryClass = (Class<ConnectionFactory>) klass;
-                    factory = factoryClass.getConstructor(URI.class)
-                            .newInstance(getBrokerUrl());
+
+                if (getUsername() != null || getPassword() != null) {
+                    factory = (ConnectionFactory) klass.getConstructor(
+                            String.class, String.class, URI.class).newInstance(
+                            getUsername(), getPassword(), getBrokerUrl());
+                } else {
+                    factory = (ConnectionFactory) klass.getConstructor(
+                            URI.class).newInstance(getBrokerUrl());
                 }
-            } catch (IllegalArgumentException e) {
-                e.printStackTrace();
-            } catch (SecurityException e) {
-                e.printStackTrace();
-            } catch (InstantiationException e) {
-                e.printStackTrace();
-            } catch (IllegalAccessException e) {
-                e.printStackTrace();
-            } catch (InvocationTargetException e) {
-                e.printStackTrace();
-            } catch (NoSuchMethodException e) {
-                e.printStackTrace();
-            } catch (ClassNotFoundException e) {
-                e.printStackTrace();
+            } catch (Exception e) {
+                throw new RuntimeException(e);
             }
         }
 
-        // Preserve the fallback case, if someone did specify a bad class, let them realize when things don't work.
         if (factory == null) {
-            factory = new ActiveMQConnectionFactory(getBrokerUrl());
+            if (getUsername() != null || getPassword() != null) {
+                factory = new ActiveMQConnectionFactory(getUsername(),
+                        getPassword(), getBrokerUrl());
+            } else {
+                factory = new ActiveMQConnectionFactory(getBrokerUrl());
+            }
         }
 
         return factory;
     }
 
-	/**
-	 * @param factory the factory to set
-	 */
-	public void setFactory(ConnectionFactory factory) {
-		this.factory = factory;
-	}
+    /**
+     * @return the username
+     */
+    public String getUsername() {
+        return username;
+    }
+
+    /**
+     * @param factory the factory to set
+     */
+    public void setFactory(ConnectionFactory factory) {
+        this.factory = factory;
+    }
+
+    /**
+     * @param username the username to set
+     */
+    public void setUsername(String username) {
+        this.username = username;
+    }
+
+    /**
+     * @return the password
+     */
+    public String getPassword() {
+        if (null == password)
+            return null;
+
+        return getPasswordFactory().getPassword(password);
+    }
+
+    /**
+     * @param password the password to set
+     */
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    /**
+     * @return the passwordFactory
+     */
+    @SuppressWarnings("unchecked")
+    public PasswordFactory getPasswordFactory() {
+        if (passwordFactory == null && passwordFactoryClassString != null) {
+            try {
+                Class klass = Class.forName(passwordFactoryClassString);
+                passwordFactory = (PasswordFactory) klass.newInstance();
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }
+
+        // Preserve the fallback case, if someone did specify a bad class, let them realize when things don't work.
+        if (passwordFactory == null) {
+            passwordFactory = DefaultPasswordFactory.factory;
+        }
+
+        return passwordFactory;
+    }
+
+    /**
+     * @param passwordFactory the passwordFactory to set
+     */
+    public void setPasswordFactory(PasswordFactory passwordFactory) {
+        this.passwordFactory = passwordFactory;
+    }
 }

Modified: activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/AmqBrowseCommand.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/AmqBrowseCommand.java?rev=1150708&r1=1150707&r2=1150708&view=diff
==============================================================================
--- activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/AmqBrowseCommand.java (original)
+++ activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/AmqBrowseCommand.java Mon Jul 25 14:10:02 2011
@@ -114,11 +114,11 @@ public class AmqBrowseCommand extends Ab
                 }
 
                 // Query for the messages to view
-                List addMsgs = AmqMessagesUtil.getMessages(getBrokerUrl(), dest, queryAddObjects);
+                List addMsgs = AmqMessagesUtil.getMessages(getConnectionFactory(), dest, queryAddObjects);
 
                 // Query for the messages to remove from view
                 if (querySubObjects.size() > 0) {
-                    List subMsgs = AmqMessagesUtil.getMessages(getBrokerUrl(), dest, querySubObjects);
+                    List subMsgs = AmqMessagesUtil.getMessages(getConnectionFactory(), dest, querySubObjects);
                     addMsgs.removeAll(subMsgs);
                 }
 

Added: activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/DefaultPasswordFactory.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/DefaultPasswordFactory.java?rev=1150708&view=auto
==============================================================================
--- activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/DefaultPasswordFactory.java (added)
+++ activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/DefaultPasswordFactory.java Mon Jul 25 14:10:02 2011
@@ -0,0 +1,29 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq.console.command;
+
+/**
+ * This is a simple dummy implementation that can be used for people who aren't in need of a keystore.
+ */
+public class DefaultPasswordFactory implements PasswordFactory{
+	// everyone can share this, since it has no state at all.
+	public static PasswordFactory factory = new DefaultPasswordFactory();
+	
+	public String getPassword(String password) {
+		return password;
+	}
+}

Added: activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/PasswordFactory.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/PasswordFactory.java?rev=1150708&view=auto
==============================================================================
--- activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/PasswordFactory.java (added)
+++ activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/PasswordFactory.java Mon Jul 25 14:10:02 2011
@@ -0,0 +1,28 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq.console.command;
+
+/**
+ * This interface is used to allow people to provide a mechanism to override where the password comes from.
+ * Implementors of this interface will typically use the specified password to look up the real password in a
+ * keystore of some sort.
+ * @author areese@yahoo-inc.com
+ *
+ */
+public interface PasswordFactory {
+	String getPassword(String password);
+}

Modified: activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/filter/AmqMessagesQueryFilter.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/filter/AmqMessagesQueryFilter.java?rev=1150708&r1=1150707&r2=1150708&view=diff
==============================================================================
--- activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/filter/AmqMessagesQueryFilter.java (original)
+++ activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/filter/AmqMessagesQueryFilter.java Mon Jul 25 14:10:02 2011
@@ -22,6 +22,7 @@ import java.util.Iterator;
 import java.util.List;
 
 import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
 import javax.jms.Destination;
 import javax.jms.JMSException;
 import javax.jms.QueueBrowser;
@@ -36,10 +37,12 @@ public class AmqMessagesQueryFilter exte
     private URI brokerUrl;
     private Destination destination;
 
+    private ConnectionFactory connectionFactory;
+
     /**
      * Create a JMS message query filter
-     * 
-     * @param brokerUrl - broker url to connect to
+     *
+     * @param brokerUrl   - broker url to connect to
      * @param destination - JMS destination to query
      */
     public AmqMessagesQueryFilter(URI brokerUrl, Destination destination) {
@@ -49,8 +52,20 @@ public class AmqMessagesQueryFilter exte
     }
 
     /**
+     * Create a JMS message query filter
+     *
+     * @param brokerUrl   - broker url to connect to
+     * @param destination - JMS destination to query
+     */
+    public AmqMessagesQueryFilter(ConnectionFactory connectionFactory, Destination destination) {
+        super(null);
+        this.destination = destination;
+        this.connectionFactory = connectionFactory;
+    }
+
+    /**
      * Queries the specified destination using the message selector format query
-     * 
+     *
      * @param queries - message selector queries
      * @return list messages that matches the selector
      * @throws Exception
@@ -59,7 +74,7 @@ public class AmqMessagesQueryFilter exte
         String selector = "";
 
         // Convert to message selector
-        for (Iterator i = queries.iterator(); i.hasNext();) {
+        for (Iterator i = queries.iterator(); i.hasNext(); ) {
             selector = selector + "(" + i.next().toString() + ") AND ";
         }
 
@@ -69,22 +84,22 @@ public class AmqMessagesQueryFilter exte
         }
 
         if (destination instanceof ActiveMQQueue) {
-            return queryMessages((ActiveMQQueue)destination, selector);
+            return queryMessages((ActiveMQQueue) destination, selector);
         } else {
-            return queryMessages((ActiveMQTopic)destination, selector);
+            return queryMessages((ActiveMQTopic) destination, selector);
         }
     }
 
     /**
      * Query the messages of a queue destination using a queue browser
-     * 
-     * @param queue - queue destination
+     *
+     * @param queue    - queue destination
      * @param selector - message selector
      * @return list of messages that matches the selector
      * @throws Exception
      */
     protected List queryMessages(ActiveMQQueue queue, String selector) throws Exception {
-        Connection conn = createConnection(getBrokerUrl());
+        Connection conn = createConnection();
 
         Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
         QueueBrowser browser = sess.createBrowser(queue, selector);
@@ -98,8 +113,8 @@ public class AmqMessagesQueryFilter exte
 
     /**
      * Query the messages of a topic destination using a message consumer
-     * 
-     * @param topic - topic destination
+     *
+     * @param topic    - topic destination
      * @param selector - message selector
      * @return list of messages that matches the selector
      * @throws Exception
@@ -114,20 +129,39 @@ public class AmqMessagesQueryFilter exte
 
     /**
      * Create and start a JMS connection
-     * 
+     *
      * @param brokerUrl - broker url to connect to.
      * @return JMS connection
      * @throws JMSException
+     * @deprecated Use createConnection() instead, and pass the url to the ConnectionFactory when it's created.
      */
+    @Deprecated
     protected Connection createConnection(URI brokerUrl) throws JMSException {
-        Connection conn = (new ActiveMQConnectionFactory(brokerUrl)).createConnection();
+        // maintain old behaviour, when called this way.
+        connectionFactory = (new ActiveMQConnectionFactory(brokerUrl));
+        return createConnection();
+    }
+
+    /**
+     * Create and start a JMS connection
+     *
+     * @return JMS connection
+     * @throws JMSException
+     */
+    protected Connection createConnection() throws JMSException {
+        // maintain old behaviour, when called either way.
+        if (null == connectionFactory)
+            connectionFactory = (new ActiveMQConnectionFactory(getBrokerUrl()));
+
+        Connection conn = connectionFactory.createConnection();
         conn.start();
         return conn;
     }
 
+
     /**
      * Get the broker url being used.
-     * 
+     *
      * @return broker url
      */
     public URI getBrokerUrl() {
@@ -136,7 +170,7 @@ public class AmqMessagesQueryFilter exte
 
     /**
      * Set the broker url to use.
-     * 
+     *
      * @param brokerUrl - broker url
      */
     public void setBrokerUrl(URI brokerUrl) {
@@ -145,7 +179,7 @@ public class AmqMessagesQueryFilter exte
 
     /**
      * Get the destination being used.
-     * 
+     *
      * @return - JMS destination
      */
     public Destination getDestination() {
@@ -154,7 +188,7 @@ public class AmqMessagesQueryFilter exte
 
     /**
      * Set the destination to use.
-     * 
+     *
      * @param destination - JMS destination
      */
     public void setDestination(Destination destination) {

Modified: activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/util/AmqMessagesUtil.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/util/AmqMessagesUtil.java?rev=1150708&r1=1150707&r2=1150708&view=diff
==============================================================================
--- activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/util/AmqMessagesUtil.java (original)
+++ activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/util/AmqMessagesUtil.java Mon Jul 25 14:10:02 2011
@@ -20,6 +20,7 @@ import java.net.URI;
 import java.util.List;
 import java.util.Set;
 
+import javax.jms.ConnectionFactory;
 import javax.jms.Destination;
 
 import org.apache.activemq.console.filter.AmqMessagesQueryFilter;
@@ -47,10 +48,18 @@ public final class AmqMessagesUtil {
         return createMessageQueryFilter(brokerUrl, dest).query(selector);
     }
 
+    public static List getMessages(ConnectionFactory connectionFactory, Destination dest, String selector) throws Exception {
+        return createMessageQueryFilter(connectionFactory, dest).query(selector);
+    }
+
     public static List getMessages(URI brokerUrl, Destination dest, List selectors) throws Exception {
         return createMessageQueryFilter(brokerUrl, dest).query(selectors);
     }
 
+    public static List getMessages(ConnectionFactory connectionFactory, Destination dest, List selectors) throws Exception {
+        return createMessageQueryFilter(connectionFactory, dest).query(selectors);
+    }
+
     public static List filterMessagesView(List messages, Set groupViews, Set attributeViews) throws Exception {
         return (new PropertiesViewFilter(attributeViews, new GroupPropertiesViewFilter(groupViews, new MapTransformFilter(new StubQueryFilter(messages))))).query("");
     }
@@ -58,4 +67,8 @@ public final class AmqMessagesUtil {
     public static QueryFilter createMessageQueryFilter(URI brokerUrl, Destination dest) {
         return new WildcardToMsgSelectorTransformFilter(new AmqMessagesQueryFilter(brokerUrl, dest));
     }
+
+    public static QueryFilter createMessageQueryFilter(ConnectionFactory connectionFactory, Destination dest) {
+        return new WildcardToMsgSelectorTransformFilter(new AmqMessagesQueryFilter(connectionFactory, dest));
+    }
 }

Added: activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/DummyConnectionFactory.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/DummyConnectionFactory.java?rev=1150708&view=auto
==============================================================================
--- activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/DummyConnectionFactory.java (added)
+++ activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/DummyConnectionFactory.java Mon Jul 25 14:10:02 2011
@@ -0,0 +1,28 @@
+package org.apache.activemq.console.command;
+
+import java.net.URI;
+
+import org.apache.activemq.ActiveMQConnectionFactory;
+
+public class DummyConnectionFactory extends ActiveMQConnectionFactory {
+	public DummyConnectionFactory() {
+		super();
+	}
+
+	public DummyConnectionFactory(String userName, String password, String brokerURL) {
+		super(userName, password, brokerURL);
+	}
+
+	public DummyConnectionFactory(String userName, String password, URI brokerURL) {
+		super(userName, password, brokerURL);
+	}
+
+	public DummyConnectionFactory(String brokerURL) {
+		super(brokerURL);
+	}
+
+	public DummyConnectionFactory(URI brokerURL) {
+		super(brokerURL);
+	}
+
+}

Added: activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/InvalidConnectionFactory.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/InvalidConnectionFactory.java?rev=1150708&view=auto
==============================================================================
--- activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/InvalidConnectionFactory.java (added)
+++ activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/InvalidConnectionFactory.java Mon Jul 25 14:10:02 2011
@@ -0,0 +1,7 @@
+package org.apache.activemq.console.command;
+
+import org.apache.activemq.ActiveMQConnectionFactory;
+
+public class InvalidConnectionFactory extends ActiveMQConnectionFactory {
+
+}

Added: activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/LowercasingPasswordFactory.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/LowercasingPasswordFactory.java?rev=1150708&view=auto
==============================================================================
--- activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/LowercasingPasswordFactory.java (added)
+++ activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/LowercasingPasswordFactory.java Mon Jul 25 14:10:02 2011
@@ -0,0 +1,9 @@
+package org.apache.activemq.console.command;
+
+public class LowercasingPasswordFactory implements PasswordFactory {
+	@Override
+	public String getPassword(String password) {
+		return password.toLowerCase();
+	}
+
+};

Modified: activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/TestAMQ3410.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/TestAMQ3410.java?rev=1150708&r1=1150707&r2=1150708&view=diff
==============================================================================
--- activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/TestAMQ3410.java (original)
+++ activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/TestAMQ3410.java Mon Jul 25 14:10:02 2011
@@ -16,7 +16,6 @@
  */
 package org.apache.activemq.console.command;
 
-import java.net.URI;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -34,40 +33,15 @@ import org.springframework.context.suppo
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 public class TestAMQ3410 extends TestCase {
-	public static class InvalidFactory extends ActiveMQConnectionFactory {
-	}
-
-	public static class NotAFactory {
-	}
-
-	public static class DummyFactory extends ActiveMQConnectionFactory {
-		public DummyFactory() {
-			super();
-		}
-
-		public DummyFactory(String userName, String password, String brokerURL) {
-			super(userName, password, brokerURL);
-		}
-
-		public DummyFactory(String userName, String password, URI brokerURL) {
-			super(userName, password, brokerURL);
-		}
-
-		public DummyFactory(String brokerURL) {
-			super(brokerURL);
-		}
-
-		public DummyFactory(URI brokerURL) {
-			super(brokerURL);
-		}
-
-	};
-
+	@SuppressWarnings("unused")
 	private static final Logger LOG = LoggerFactory
 			.getLogger(TestPurgeCommand.class);
+	private static final Collection<String> DEFAULT_OPTIONS = Arrays
+			.asList(new String[] { "--amqurl", "tcp://localhost:61616", });
+
 	private static final Collection<String> DEFAULT_TOKENS = Arrays
-			.asList(new String[] { "--amqurl", "tcp://localhost:61616",
-					"FOO.QUEUE" });
+			.asList(new String[] { "FOO.QUEUE" });
+
 	protected AbstractApplicationContext context;
 
 	protected void setUp() throws Exception {
@@ -98,9 +72,12 @@ public class TestAMQ3410 extends TestCas
 		command.setCommandContext(context);
 
 		List<String> tokens = new ArrayList<String>();
+		tokens.addAll(DEFAULT_OPTIONS);
 		tokens.addAll(DEFAULT_TOKENS);
+
 		command.execute(tokens);
-		assertNotNull(command.getFactory());
+		assertNotNull(command.getConnectionFactory());
+		assertTrue(command.getConnectionFactory() instanceof ActiveMQConnectionFactory);
 	}
 
 	public void testFactorySet() throws Exception {
@@ -112,12 +89,17 @@ public class TestAMQ3410 extends TestCas
 		command.setCommandContext(context);
 
 		List<String> tokens = new ArrayList<String>();
-		tokens.addAll(DEFAULT_TOKENS);
+		tokens.addAll(DEFAULT_OPTIONS);
 		tokens.add("--factory");
-		tokens
-				.add("org.apache.activemq.console.command.TestAMQ3410.DummyFactory");
+		tokens.add(DummyConnectionFactory.class.getCanonicalName());
+		tokens.addAll(DEFAULT_TOKENS);
+
 		command.execute(tokens);
-		assertNotNull(command.getFactory());
+
+		assertNotNull(command.getConnectionFactory());
+		assertTrue("wrong instance returned: "
+				+ command.getConnectionFactory().getClass().getName(), command
+				.getConnectionFactory() instanceof DummyConnectionFactory);
 	}
 
 	public void testFactorySetWrong1() throws Exception {
@@ -129,12 +111,22 @@ public class TestAMQ3410 extends TestCas
 		command.setCommandContext(context);
 
 		List<String> tokens = new ArrayList<String>();
-		tokens.addAll(DEFAULT_TOKENS);
+		tokens.addAll(DEFAULT_OPTIONS);
 		tokens.add("--factory");
 		tokens
 				.add("org.apache.activemq.console.command.TestAMQ3410.DoesntExistFactory");
+		tokens.addAll(DEFAULT_TOKENS);
+
+		try {
 		command.execute(tokens);
-		assertNotNull(command.getFactory());
+		} catch (Throwable cause) {
+			while (null != cause) {
+				if (cause instanceof java.lang.ClassNotFoundException)
+					return;
+				cause = cause.getCause();
+	}
+		}
+		assertFalse("No exception caught", true);
 	}
 
 	public void testFactorySetWrong2() throws Exception {
@@ -146,12 +138,49 @@ public class TestAMQ3410 extends TestCas
 		command.setCommandContext(context);
 
 		List<String> tokens = new ArrayList<String>();
+		tokens.addAll(DEFAULT_OPTIONS);
+		tokens.add("--factory");
+		tokens.add(InvalidConnectionFactory.class.getCanonicalName());
 		tokens.addAll(DEFAULT_TOKENS);
+
+		try {
+			command.execute(tokens);
+		} catch (Throwable e) {
+			Throwable cause = e;
+			while (null != cause) {
+				if (cause instanceof java.lang.NoSuchMethodException)
+					return;
+				cause = cause.getCause();
+			}
+			assertFalse(e.toString(), true);
+		}
+		assertFalse("No exception caught", true);
+	}
+
+	public void testFactorySetWrong3() throws Exception {
+		AmqBrowseCommand command = new AmqBrowseCommand();
+		CommandContext context = new CommandContext();
+
+		context.setFormatter(new CommandShellOutputFormatter(System.out));
+
+		command.setCommandContext(context);
+
+		List<String> tokens = new ArrayList<String>();
+		tokens.addAll(DEFAULT_OPTIONS);
 		tokens.add("--factory");
-		tokens
-				.add("org.apache.activemq.console.command.TestAMQ3410.InvalidFactory");
+		tokens.add("java.lang.Object");
+		tokens.addAll(DEFAULT_TOKENS);
+
+		try {
 		command.execute(tokens);
-		assertNotNull(command.getFactory());
+		} catch (Throwable cause) {
+			while (null != cause) {
+				if (cause instanceof java.lang.NoSuchMethodException)
+					return;
+				cause = cause.getCause();
+	}
+		}
+		assertFalse(true);
 	}
 
 }

Copied: activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/TestAMQ3411.java (from r1150666, activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/TestAMQ3410.java)
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/TestAMQ3411.java?p2=activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/TestAMQ3411.java&p1=activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/TestAMQ3410.java&r1=1150666&r2=1150708&rev=1150708&view=diff
==============================================================================
--- activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/TestAMQ3410.java (original)
+++ activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/TestAMQ3411.java Mon Jul 25 14:10:02 2011
@@ -16,7 +16,6 @@
  */
 package org.apache.activemq.console.command;
 
-import java.net.URI;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -24,7 +23,6 @@ import java.util.List;
 
 import junit.framework.TestCase;
 
-import org.apache.activemq.ActiveMQConnectionFactory;
 import org.apache.activemq.broker.BrokerService;
 import org.apache.activemq.console.CommandContext;
 import org.apache.activemq.console.formatter.CommandShellOutputFormatter;
@@ -33,42 +31,17 @@ import org.slf4j.LoggerFactory;
 import org.springframework.context.support.AbstractApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
-public class TestAMQ3410 extends TestCase {
-	public static class InvalidFactory extends ActiveMQConnectionFactory {
-	}
-
-	public static class NotAFactory {
-	}
-
-	public static class DummyFactory extends ActiveMQConnectionFactory {
-		public DummyFactory() {
-			super();
-		}
-
-		public DummyFactory(String userName, String password, String brokerURL) {
-			super(userName, password, brokerURL);
-		}
-
-		public DummyFactory(String userName, String password, URI brokerURL) {
-			super(userName, password, brokerURL);
-		}
-
-		public DummyFactory(String brokerURL) {
-			super(brokerURL);
-		}
-
-		public DummyFactory(URI brokerURL) {
-			super(brokerURL);
-		}
-
-	};
-
+public class TestAMQ3411 extends TestCase {
+	@SuppressWarnings("unused")
 	private static final Logger LOG = LoggerFactory
 			.getLogger(TestPurgeCommand.class);
+	private static final Collection<String> DEFAULT_OPTIONS = Arrays
+			.asList(new String[] { "--amqurl", "tcp://localhost:61616", });
+
 	private static final Collection<String> DEFAULT_TOKENS = Arrays
-			.asList(new String[] { "--amqurl", "tcp://localhost:61616",
-					"FOO.QUEUE" });
+			.asList(new String[] { "FOO.QUEUE" });
 	protected AbstractApplicationContext context;
+	protected static final String origPassword = "ABCDEFG";
 
 	protected void setUp() throws Exception {
 		super.setUp();
@@ -98,9 +71,42 @@ public class TestAMQ3410 extends TestCas
 		command.setCommandContext(context);
 
 		List<String> tokens = new ArrayList<String>();
+		tokens.addAll(DEFAULT_OPTIONS);
+		tokens.addAll(DEFAULT_TOKENS);
+
+		command.execute(tokens);
+
+		assertNotNull(command.getPasswordFactory());
+		assertTrue(command.getPasswordFactory() instanceof DefaultPasswordFactory);
+		assertNull(command.getPassword());
+	}
+
+	public void testUsernamePasswordSet() throws Exception {
+		AmqBrowseCommand command = new AmqBrowseCommand();
+		CommandContext context = new CommandContext();
+
+		String username = "user";
+		String password = "password";
+
+		context.setFormatter(new CommandShellOutputFormatter(System.out));
+
+		command.setCommandContext(context);
+
+		List<String> tokens = new ArrayList<String>();
+		tokens.addAll(DEFAULT_OPTIONS);
+		tokens.add("--password");
+		tokens.add(password);
+
+		tokens.add("--user");
+		tokens.add(username);
 		tokens.addAll(DEFAULT_TOKENS);
+
 		command.execute(tokens);
-		assertNotNull(command.getFactory());
+
+		assertNotNull(command.getPasswordFactory());
+		assertTrue(command.getPasswordFactory() instanceof DefaultPasswordFactory);
+		assertEquals(password, command.getPassword());
+		assertEquals(username, command.getUsername());
 	}
 
 	public void testFactorySet() throws Exception {
@@ -112,12 +118,19 @@ public class TestAMQ3410 extends TestCas
 		command.setCommandContext(context);
 
 		List<String> tokens = new ArrayList<String>();
+		tokens.addAll(DEFAULT_OPTIONS);
+		tokens.add("--passwordFactory");
+		tokens.add(LowercasingPasswordFactory.class.getCanonicalName());
+		tokens.add("--password");
+		tokens.add(origPassword);
 		tokens.addAll(DEFAULT_TOKENS);
-		tokens.add("--factory");
-		tokens
-				.add("org.apache.activemq.console.command.TestAMQ3410.DummyFactory");
+
 		command.execute(tokens);
-		assertNotNull(command.getFactory());
+		assertNotNull(command.getPasswordFactory());
+		assertTrue(command.getPasswordFactory() instanceof LowercasingPasswordFactory);
+
+		// validate that the factory is indeed being used for the password.
+		assertEquals(origPassword.toLowerCase(), command.getPassword());
 	}
 
 	public void testFactorySetWrong1() throws Exception {
@@ -129,12 +142,27 @@ public class TestAMQ3410 extends TestCas
 		command.setCommandContext(context);
 
 		List<String> tokens = new ArrayList<String>();
-		tokens.addAll(DEFAULT_TOKENS);
-		tokens.add("--factory");
+		tokens.addAll(DEFAULT_OPTIONS);
+		tokens.add("--passwordFactory");
 		tokens
-				.add("org.apache.activemq.console.command.TestAMQ3410.DoesntExistFactory");
-		command.execute(tokens);
-		assertNotNull(command.getFactory());
+				.add("org.apache.activemq.console.command.TestAMQ3411.DoesntExistFactory");
+		tokens.add("--password");
+		tokens.add(origPassword);
+
+		tokens.addAll(DEFAULT_TOKENS);
+
+		try {
+			command.execute(tokens);
+		} catch (Throwable e) {
+			Throwable cause = e;
+			while (null != cause) {
+				if (cause instanceof java.lang.ClassNotFoundException)
+					return;
+				cause = cause.getCause();
+			}
+			assertFalse(e.toString(), true);
+		}
+		assertFalse("No exception caught", true);
 	}
 
 	public void testFactorySetWrong2() throws Exception {
@@ -146,12 +174,24 @@ public class TestAMQ3410 extends TestCas
 		command.setCommandContext(context);
 
 		List<String> tokens = new ArrayList<String>();
+		tokens.addAll(DEFAULT_OPTIONS);
+		tokens.add("--passwordFactory");
+		tokens.add("java.lang.Object");
+		tokens.add("--password");
+		tokens.add(origPassword);
 		tokens.addAll(DEFAULT_TOKENS);
-		tokens.add("--factory");
-		tokens
-				.add("org.apache.activemq.console.command.TestAMQ3410.InvalidFactory");
-		command.execute(tokens);
-		assertNotNull(command.getFactory());
-	}
 
+		try {
+			command.execute(tokens);
+		} catch (Throwable e) {
+			Throwable cause = e;
+			while (null != cause) {
+				if (cause instanceof java.lang.ClassCastException)
+					return;
+				cause = cause.getCause();
+			}
+			assertFalse(e.toString(), true);
+		}
+		assertFalse("No exception caught", true);
+	}
 }

Modified: activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/TestPurgeCommand.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/TestPurgeCommand.java?rev=1150708&r1=1150707&r2=1150708&view=diff
==============================================================================
--- activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/TestPurgeCommand.java (original)
+++ activemq/trunk/activemq-console/src/test/java/org/apache/activemq/console/command/TestPurgeCommand.java Mon Jul 25 14:10:02 2011
@@ -246,8 +246,6 @@ public class TestPurgeCommand extends Te
 
 			List<String> tokens = new ArrayList<String>();
 			tokens.add("--msgsel");
-//			String[] extras = MSG_SEL_WITH_PROPERTY.split(" ");
-//			tokens.addAll(Arrays.asList(extras));
 			tokens.add(MSG_SEL_WITH_PROPERTY);
 			
 			addMessages();