Return-Path: Delivered-To: apmail-activemq-commits-archive@www.apache.org Received: (qmail 91932 invoked from network); 15 Jun 2009 15:20:13 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 15 Jun 2009 15:20:13 -0000 Received: (qmail 61116 invoked by uid 500); 15 Jun 2009 15:20:25 -0000 Delivered-To: apmail-activemq-commits-archive@activemq.apache.org Received: (qmail 61089 invoked by uid 500); 15 Jun 2009 15:20:25 -0000 Mailing-List: contact commits-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@activemq.apache.org Delivered-To: mailing list commits@activemq.apache.org Received: (qmail 61080 invoked by uid 99); 15 Jun 2009 15:20:25 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 15 Jun 2009 15:20:25 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 15 Jun 2009 15:20:13 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id BA2202388874; Mon, 15 Jun 2009 15:19:51 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r784806 - in /activemq/sandbox/activemq-flow: activemq-all/src/test/java/org/apache/activemq/legacy/ activemq-all/src/test/java/org/apache/activemq/legacy/broker/ activemq-all/src/test/java/org/apache/activemq/legacy/transport/discovery/ ac... Date: Mon, 15 Jun 2009 15:19:50 -0000 To: commits@activemq.apache.org From: chirino@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090615151951.BA2202388874@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: chirino Date: Mon Jun 15 15:19:48 2009 New Revision: 784806 URL: http://svn.apache.org/viewvc?rev=784806&view=rev Log: moved the handy AutoFailTestSupport and CombinationTestSupport classes to the util package. Added: activemq/sandbox/activemq-flow/activemq-util/src/test/java/org/apache/activemq/apollo/ activemq/sandbox/activemq-flow/activemq-util/src/test/java/org/apache/activemq/apollo/AutoFailTestSupport.java (with props) activemq/sandbox/activemq-flow/activemq-util/src/test/java/org/apache/activemq/apollo/CombinationTestSupport.java (with props) Removed: activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/AutoFailTestSupport.java activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/CombinationTestSupport.java Modified: activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/ActiveMQConnectionFactoryTest.java activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/ActiveMQXAConnectionFactoryTest.java activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/EmbeddedBrokerTestSupport.java activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/JmsMultipleClientsTestSupport.java activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/JmsRollbackRedeliveryTest.java activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/JmsTestSupport.java activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/MessageGroupTest.java activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/broker/BrokerTestSupport.java activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/transport/discovery/DiscoveryTransportNoBrokerTest.java activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/transport/tcp/InactivityMonitorTest.java activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/transport/tcp/WireformatNegociationTest.java activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/usecases/ExpiredMessagesTest.java Modified: activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/ActiveMQConnectionFactoryTest.java URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/ActiveMQConnectionFactoryTest.java?rev=784806&r1=784805&r2=784806&view=diff ============================================================================== --- activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/ActiveMQConnectionFactoryTest.java (original) +++ activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/ActiveMQConnectionFactoryTest.java Mon Jun 15 15:19:48 2009 @@ -30,6 +30,7 @@ import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.ActiveMQMessageConsumer; +import org.apache.activemq.apollo.CombinationTestSupport; import org.apache.activemq.legacy.broker.BrokerRegistry; import org.apache.activemq.legacy.broker.BrokerService; import org.apache.activemq.legacy.broker.TransportConnector; Modified: activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/ActiveMQXAConnectionFactoryTest.java URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/ActiveMQXAConnectionFactoryTest.java?rev=784806&r1=784805&r2=784806&view=diff ============================================================================== --- activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/ActiveMQXAConnectionFactoryTest.java (original) +++ activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/ActiveMQXAConnectionFactoryTest.java Mon Jun 15 15:19:48 2009 @@ -40,6 +40,7 @@ import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.ActiveMQXAConnectionFactory; +import org.apache.activemq.apollo.CombinationTestSupport; import org.apache.activemq.command.ActiveMQQueue; import org.apache.activemq.command.ActiveMQTextMessage; import org.apache.activemq.legacy.broker.BrokerRegistry; Modified: activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/EmbeddedBrokerTestSupport.java URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/EmbeddedBrokerTestSupport.java?rev=784806&r1=784805&r2=784806&view=diff ============================================================================== --- activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/EmbeddedBrokerTestSupport.java (original) +++ activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/EmbeddedBrokerTestSupport.java Mon Jun 15 15:19:48 2009 @@ -23,6 +23,7 @@ import junit.framework.TestCase; import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.apollo.CombinationTestSupport; import org.apache.activemq.command.ActiveMQQueue; import org.apache.activemq.command.ActiveMQTopic; import org.apache.activemq.legacy.broker.BrokerService; Modified: activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/JmsMultipleClientsTestSupport.java URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/JmsMultipleClientsTestSupport.java?rev=784806&r1=784805&r2=784806&view=diff ============================================================================== --- activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/JmsMultipleClientsTestSupport.java (original) +++ activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/JmsMultipleClientsTestSupport.java Mon Jun 15 15:19:48 2009 @@ -38,6 +38,7 @@ import javax.jms.TopicSubscriber; import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.apollo.CombinationTestSupport; import org.apache.activemq.command.ActiveMQDestination; import org.apache.activemq.command.ActiveMQQueue; import org.apache.activemq.command.ActiveMQTopic; Modified: activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/JmsRollbackRedeliveryTest.java URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/JmsRollbackRedeliveryTest.java?rev=784806&r1=784805&r2=784806&view=diff ============================================================================== --- activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/JmsRollbackRedeliveryTest.java (original) +++ activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/JmsRollbackRedeliveryTest.java Mon Jun 15 15:19:48 2009 @@ -32,6 +32,7 @@ import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.RedeliveryPolicy; +import org.apache.activemq.apollo.AutoFailTestSupport; import org.apache.activemq.legacy.broker.BrokerService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; Modified: activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/JmsTestSupport.java URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/JmsTestSupport.java?rev=784806&r1=784805&r2=784806&view=diff ============================================================================== --- activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/JmsTestSupport.java (original) +++ activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/JmsTestSupport.java Mon Jun 15 15:19:48 2009 @@ -35,6 +35,7 @@ import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.apollo.CombinationTestSupport; import org.apache.activemq.command.ActiveMQDestination; import org.apache.activemq.legacy.broker.BrokerFactory; import org.apache.activemq.legacy.broker.BrokerService; Modified: activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/MessageGroupTest.java URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/MessageGroupTest.java?rev=784806&r1=784805&r2=784806&view=diff ============================================================================== --- activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/MessageGroupTest.java (original) +++ activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/MessageGroupTest.java Mon Jun 15 15:19:48 2009 @@ -7,6 +7,7 @@ import javax.jms.Session; import javax.jms.TextMessage; +import org.apache.activemq.apollo.CombinationTestSupport; import org.apache.activemq.command.ActiveMQDestination; import org.apache.activemq.command.ActiveMQQueue; import org.apache.commons.logging.Log; Modified: activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/broker/BrokerTestSupport.java URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/broker/BrokerTestSupport.java?rev=784806&r1=784805&r2=784806&view=diff ============================================================================== --- activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/broker/BrokerTestSupport.java (original) +++ activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/broker/BrokerTestSupport.java Mon Jun 15 15:19:48 2009 @@ -28,7 +28,6 @@ import javax.jms.DeliveryMode; import javax.jms.MessageNotWriteableException; -import org.apache.activemq.legacy.CombinationTestSupport; import org.apache.activemq.legacy.broker.BrokerFactory; import org.apache.activemq.legacy.broker.BrokerService; import org.apache.activemq.legacy.broker.region.RegionBroker; @@ -38,6 +37,7 @@ import org.apache.activemq.legacy.broker.region.policy.RoundRobinDispatchPolicy; import org.apache.activemq.legacy.store.PersistenceAdapter; import org.apache.activemq.legacy.usage.SystemUsage; +import org.apache.activemq.apollo.CombinationTestSupport; import org.apache.activemq.command.ActiveMQDestination; import org.apache.activemq.command.ActiveMQTextMessage; import org.apache.activemq.command.ConnectionId; Modified: activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/transport/discovery/DiscoveryTransportNoBrokerTest.java URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/transport/discovery/DiscoveryTransportNoBrokerTest.java?rev=784806&r1=784805&r2=784806&view=diff ============================================================================== --- activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/transport/discovery/DiscoveryTransportNoBrokerTest.java (original) +++ activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/transport/discovery/DiscoveryTransportNoBrokerTest.java Mon Jun 15 15:19:48 2009 @@ -20,7 +20,7 @@ import javax.jms.JMSException; import org.apache.activemq.ActiveMQConnectionFactory; -import org.apache.activemq.legacy.CombinationTestSupport; +import org.apache.activemq.apollo.CombinationTestSupport; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; Modified: activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/transport/tcp/InactivityMonitorTest.java URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/transport/tcp/InactivityMonitorTest.java?rev=784806&r1=784805&r2=784806&view=diff ============================================================================== --- activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/transport/tcp/InactivityMonitorTest.java (original) +++ activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/transport/tcp/InactivityMonitorTest.java Mon Jun 15 15:19:48 2009 @@ -24,8 +24,8 @@ import javax.net.SocketFactory; +import org.apache.activemq.apollo.CombinationTestSupport; import org.apache.activemq.command.WireFormatInfo; -import org.apache.activemq.legacy.CombinationTestSupport; import org.apache.activemq.openwire.OpenWireFormat; import org.apache.activemq.transport.Transport; import org.apache.activemq.transport.TransportAcceptListener; Modified: activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/transport/tcp/WireformatNegociationTest.java URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/transport/tcp/WireformatNegociationTest.java?rev=784806&r1=784805&r2=784806&view=diff ============================================================================== --- activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/transport/tcp/WireformatNegociationTest.java (original) +++ activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/transport/tcp/WireformatNegociationTest.java Mon Jun 15 15:19:48 2009 @@ -24,9 +24,9 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; +import org.apache.activemq.apollo.CombinationTestSupport; import org.apache.activemq.command.CommandTypes; import org.apache.activemq.command.WireFormatInfo; -import org.apache.activemq.legacy.CombinationTestSupport; import org.apache.activemq.transport.Transport; import org.apache.activemq.transport.TransportAcceptListener; import org.apache.activemq.transport.TransportFactory; Modified: activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/usecases/ExpiredMessagesTest.java URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/usecases/ExpiredMessagesTest.java?rev=784806&r1=784805&r2=784806&view=diff ============================================================================== --- activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/usecases/ExpiredMessagesTest.java (original) +++ activemq/sandbox/activemq-flow/activemq-all/src/test/java/org/apache/activemq/legacy/usecases/ExpiredMessagesTest.java Mon Jun 15 15:19:48 2009 @@ -27,11 +27,11 @@ import junit.framework.Test; import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.apollo.CombinationTestSupport; import org.apache.activemq.broker.jmx.DestinationViewMBean; import org.apache.activemq.command.ActiveMQDestination; import org.apache.activemq.command.ActiveMQQueue; import org.apache.activemq.command.ActiveMQTopic; -import org.apache.activemq.legacy.CombinationTestSupport; import org.apache.activemq.legacy.broker.BrokerService; Added: activemq/sandbox/activemq-flow/activemq-util/src/test/java/org/apache/activemq/apollo/AutoFailTestSupport.java URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-util/src/test/java/org/apache/activemq/apollo/AutoFailTestSupport.java?rev=784806&view=auto ============================================================================== --- activemq/sandbox/activemq-flow/activemq-util/src/test/java/org/apache/activemq/apollo/AutoFailTestSupport.java (added) +++ activemq/sandbox/activemq-flow/activemq-util/src/test/java/org/apache/activemq/apollo/AutoFailTestSupport.java Mon Jun 15 15:19:48 2009 @@ -0,0 +1,141 @@ +/** + * 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.apollo; + +import java.util.concurrent.atomic.AtomicBoolean; + +import junit.framework.TestCase; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * Enforces a test case to run for only an allotted time to prevent them from + * hanging and breaking the whole testing. + * + * @version $Revision: 1.0 $ + */ + +public abstract class AutoFailTestSupport extends TestCase { + public static final int EXIT_SUCCESS = 0; + public static final int EXIT_ERROR = 1; + private static final Log LOG = LogFactory.getLog(AutoFailTestSupport.class); + + private long maxTestTime = 5 * 60 * 1000; // 5 mins by default + private Thread autoFailThread; + + private boolean verbose = true; + private boolean useAutoFail; // Disable auto fail by default + private AtomicBoolean isTestSuccess; + + protected void setUp() throws Exception { + // Runs the auto fail thread before performing any setup + if (isAutoFail()) { + startAutoFailThread(); + } + super.setUp(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + + // Stops the auto fail thread only after performing any clean up + stopAutoFailThread(); + } + + /** + * Manually start the auto fail thread. To start it automatically, just set + * the auto fail to true before calling any setup methods. As a rule, this + * method is used only when you are not sure, if the setUp and tearDown + * method is propagated correctly. + */ + public void startAutoFailThread() { + setAutoFail(true); + isTestSuccess = new AtomicBoolean(false); + autoFailThread = new Thread(new Runnable() { + public void run() { + try { + // Wait for test to finish succesfully + Thread.sleep(getMaxTestTime()); + } catch (InterruptedException e) { + // This usually means the test was successful + } finally { + // Check if the test was able to tear down succesfully, + // which usually means, it has finished its run. + if (!isTestSuccess.get()) { + LOG.error("Test case has exceeded the maximum allotted time to run of: " + getMaxTestTime() + " ms."); + LOG.fatal("Test case has exceeded the maximum allotted time to run of: " + getMaxTestTime() + " ms."); + System.exit(EXIT_ERROR); + } + } + } + }, "AutoFailThread"); + + if (verbose) { + LOG.info("Starting auto fail thread..."); + } + + LOG.info("Starting auto fail thread..."); + autoFailThread.start(); + } + + /** + * Manually stops the auto fail thread. As a rule, this method is used only + * when you are not sure, if the setUp and tearDown method is propagated + * correctly. + */ + public void stopAutoFailThread() { + if (isAutoFail() && autoFailThread != null && autoFailThread.isAlive()) { + isTestSuccess.set(true); + + if (verbose) { + LOG.info("Stopping auto fail thread..."); + } + + LOG.info("Stopping auto fail thread..."); + autoFailThread.interrupt(); + } + } + + /** + * Sets the auto fail value. As a rule, this should be used only before any + * setup methods is called to automatically enable the auto fail thread in + * the setup method of the test case. + * + * @param val + */ + public void setAutoFail(boolean val) { + this.useAutoFail = val; + } + + public boolean isAutoFail() { + return this.useAutoFail; + } + + /** + * The assigned value will only be reflected when the auto fail thread has + * started its run. Value is in milliseconds. + * + * @param val + */ + public void setMaxTestTime(long val) { + this.maxTestTime = val; + } + + public long getMaxTestTime() { + return this.maxTestTime; + } +} Propchange: activemq/sandbox/activemq-flow/activemq-util/src/test/java/org/apache/activemq/apollo/AutoFailTestSupport.java ------------------------------------------------------------------------------ svn:eol-style = native Added: activemq/sandbox/activemq-flow/activemq-util/src/test/java/org/apache/activemq/apollo/CombinationTestSupport.java URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-util/src/test/java/org/apache/activemq/apollo/CombinationTestSupport.java?rev=784806&view=auto ============================================================================== --- activemq/sandbox/activemq-flow/activemq-util/src/test/java/org/apache/activemq/apollo/CombinationTestSupport.java (added) +++ activemq/sandbox/activemq-flow/activemq-util/src/test/java/org/apache/activemq/apollo/CombinationTestSupport.java Mon Jun 15 15:19:48 2009 @@ -0,0 +1,231 @@ +/** + * 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.apollo; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * Poor mans way of getting JUnit to run a test case through a few different + * combinations of options. Usage: If you have a test case called testFoo what + * you want to run through a few combinations, of of values for the attributes + * age and color, you would something like: + * public void initCombosForTestFoo() { + * addCombinationValues( "age", new Object[]{ new Integer(21), new Integer(30) } ); + * addCombinationValues( "color", new Object[]{"blue", "green"} ); + * } + * + * The testFoo test case would be run for each possible combination of age and + * color that you setup in the initCombosForTestFoo method. Before each + * combination is run, the age and color fields of the test class are set to one + * of the values defined. This is done before the normal setUp method is called. + * If you want the test combinations to show up as separate test runs in the + * JUnit reports, add a suite method to your test case similar to: + * public static Test suite() { + * return suite(FooTest.class); + * } + * + * + * @version $Revision: 1.5 $ + */ +public abstract class CombinationTestSupport extends AutoFailTestSupport { + + private static final Log LOG = LogFactory.getLog(CombinationTestSupport.class); + + private HashMap comboOptions = new HashMap(); + private boolean combosEvaluated; + private Map options; + + static class ComboOption { + final String attribute; + final LinkedHashSet values = new LinkedHashSet(); + + public ComboOption(String attribute, Collection options) { + this.attribute = attribute; + this.values.addAll(options); + } + } + + public void addCombinationValues(String attribute, Object[] options) { + ComboOption co = this.comboOptions.get(attribute); + if (co == null) { + this.comboOptions.put(attribute, new ComboOption(attribute, Arrays.asList(options))); + } else { + co.values.addAll(Arrays.asList(options)); + } + } + + public void runBare() throws Throwable { + if (combosEvaluated) { + super.runBare(); + } else { + CombinationTestSupport[] combinations = getCombinations(); + for (int i = 0; i < combinations.length; i++) { + CombinationTestSupport test = combinations[i]; + if (getName() == null || getName().equals(test.getName())) { + test.runBare(); + } + } + } + } + + private void setOptions(Map options) throws NoSuchFieldException, IllegalAccessException { + this.options = options; + for (Iterator iterator = options.keySet().iterator(); iterator.hasNext();) { + String attribute = (String)iterator.next(); + Object value = options.get(attribute); + try { + Field field = getClass().getField(attribute); + field.set(this, value); + } catch (Throwable e) { + LOG.info("Could not set field '" + attribute + "' to value '" + value + "', make sure the field exists and is public."); + } + } + } + + private CombinationTestSupport[] getCombinations() { + try { + Method method = getClass().getMethod("initCombos", null); + method.invoke(this, null); + } catch (Throwable e) { + } + + String name = getName().split(" ")[0]; + String comboSetupMethodName = "initCombosFor" + Character.toUpperCase(name.charAt(0)) + name.substring(1); + try { + Method method = getClass().getMethod(comboSetupMethodName, null); + method.invoke(this, null); + } catch (Throwable e) { + } + + try { + ArrayList> expandedOptions = new ArrayList>(); + expandCombinations(new ArrayList(comboOptions.values()), expandedOptions); + + if (expandedOptions.isEmpty()) { + combosEvaluated = true; + return new CombinationTestSupport[] {this}; + } else { + + ArrayList result = new ArrayList(); + // Run the test case for each possible combination + for (Iterator> iter = expandedOptions.iterator(); iter.hasNext();) { + CombinationTestSupport combo = (CombinationTestSupport)TestSuite.createTest(getClass(), name); + combo.combosEvaluated = true; + combo.setOptions(iter.next()); + result.add(combo); + } + + CombinationTestSupport rc[] = new CombinationTestSupport[result.size()]; + result.toArray(rc); + return rc; + } + } catch (Throwable e) { + combosEvaluated = true; + return new CombinationTestSupport[] {this}; + } + + } + + private void expandCombinations(List optionsLeft, List> expandedCombos) { + if (!optionsLeft.isEmpty()) { + HashMap map; + if (comboOptions.size() == optionsLeft.size()) { + map = new HashMap(); + expandedCombos.add(map); + } else { + map = expandedCombos.get(expandedCombos.size() - 1); + } + + LinkedList l = new LinkedList(optionsLeft); + ComboOption comboOption = l.removeLast(); + int i = 0; + for (Iterator iter = comboOption.values.iterator(); iter.hasNext();) { + Object value = iter.next(); + if (i != 0) { + map = new HashMap(map); + expandedCombos.add(map); + } + map.put(comboOption.attribute, value); + expandCombinations(l, expandedCombos); + i++; + } + } + } + + public static Test suite(Class clazz) { + TestSuite suite = new TestSuite(); + + ArrayList names = new ArrayList(); + Method[] methods = clazz.getMethods(); + for (int i = 0; i < methods.length; i++) { + String name = methods[i].getName(); + if (names.contains(name) || !isPublicTestMethod(methods[i])) { + continue; + } + names.add(name); + Test test = TestSuite.createTest(clazz, name); + if (test instanceof CombinationTestSupport) { + CombinationTestSupport[] combinations = ((CombinationTestSupport)test).getCombinations(); + for (int j = 0; j < combinations.length; j++) { + suite.addTest(combinations[j]); + } + } else { + suite.addTest(test); + } + } + return suite; + } + + private static boolean isPublicTestMethod(Method m) { + return isTestMethod(m) && Modifier.isPublic(m.getModifiers()); + } + + private static boolean isTestMethod(Method m) { + String name = m.getName(); + Class[] parameters = m.getParameterTypes(); + Class returnType = m.getReturnType(); + return parameters.length == 0 && name.startsWith("test") && returnType.equals(Void.TYPE); + } + + public String getName() { + return getName(false); + } + + public String getName(boolean original) { + if (options != null && !original) { + return super.getName() + " " + options; + } + return super.getName(); + } +} Propchange: activemq/sandbox/activemq-flow/activemq-util/src/test/java/org/apache/activemq/apollo/CombinationTestSupport.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: activemq/sandbox/activemq-flow/activemq-util/src/test/java/org/apache/activemq/apollo/CombinationTestSupport.java ------------------------------------------------------------------------------ svn:executable = *