1 18 package org.apache.activemq.systest; 19 20 import org.springframework.context.ApplicationContext; 21 22 import javax.jms.Destination ; 23 24 import java.lang.reflect.Constructor ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import junit.framework.TestCase; 31 import junit.framework.TestSuite; 32 33 38 public class ScenarioTestSuite extends TestCase { 39 40 private List brokers = new ArrayList (); 41 private boolean cacheBrokers; 42 43 public static TestSuite createSuite(ApplicationContext clientContext, ApplicationContext brokerContext, Class [] scenarios, int destinationType) throws Exception { 44 TestSuite suite = new TestSuite(); 45 46 ScenarioTestSuite test = new ScenarioTestSuite(); 47 test.appendTestCases(suite, clientContext, brokerContext, scenarios, destinationType); 48 return suite; 49 } 50 51 public void appendTestCases(TestSuite suite, ApplicationContext clientContext, ApplicationContext brokerContext, Class [] scenarios, int destinationType) throws Exception { 52 for (int i = 0; i < scenarios.length; i++) { 53 Class scenario = scenarios[i]; 54 appendTestCase(suite, clientContext, brokerContext, scenario, destinationType); 55 } 56 } 57 58 public void appendTestCase(TestSuite suite, ApplicationContext clientContext, ApplicationContext brokerContext, Class scenario, int destinationType) throws Exception { 59 62 Constructor [] constructors = scenario.getConstructors(); 63 for (int i = 0; i < constructors.length; i++) { 64 Constructor constructor = constructors[i]; 65 appendTestCase(suite, clientContext, brokerContext, scenario, constructor, destinationType); 66 } 67 } 68 69 public void appendTestCase(TestSuite suite, ApplicationContext clientContext, ApplicationContext brokerContext, Class scenario, Constructor constructor, 70 int destinationType) throws Exception { 71 Class [] parameterTypes = constructor.getParameterTypes(); 73 int size = parameterTypes.length; 74 String [][] namesForParameters = new String [size][]; 75 for (int i = 0; i < size; i++) { 76 Class parameterType = parameterTypes[i]; 77 String [] names = clientContext.getBeanNamesForType(parameterType); 78 if (names == null || names.length == 0) { 79 if (parameterType.equals(BrokerAgent.class)) { 80 names = new String [1]; 81 } 82 else { 83 fail("No bean instances available in the ApplicationContext for type: " + parameterType.getName()); 84 } 85 } 86 namesForParameters[i] = names; 87 } 88 89 int[] counters = new int[size]; 91 boolean completed = false; 92 while (!completed) { 93 Object [] parameters = new Object [size]; 94 StringBuffer buffer = new StringBuffer (scenario.getName()); 95 int brokerCounter = 1; 96 for (int i = 0; i < size; i++) { 97 String beanName = namesForParameters[i][counters[i]]; 98 if (beanName != null) { 99 parameters[i] = clientContext.getBean(beanName); 100 buffer.append("."); 101 buffer.append(beanName); 102 } 103 else { 104 parameters[i] = getBrokerAgent(brokerContext, brokerCounter++); 105 } 106 } 107 108 String destinationName = buffer.toString(); 109 addTestsFor(suite, clientContext, brokerContext, constructor, parameters, destinationName, destinationType); 110 111 int pivot = 0; 113 while (++counters[pivot] >= namesForParameters[pivot].length) { 114 counters[pivot] = 0; 115 pivot++; 116 if (pivot >= size) { 117 completed = true; 118 break; 119 } 120 } 121 } 122 } 123 124 protected BrokerAgent getBrokerAgent(ApplicationContext brokerContext, int brokerCounter) { 125 if (cacheBrokers) { 126 BrokerAgent broker = null; 127 int index = brokerCounter - 1; 128 129 if (brokers.size() >= brokerCounter) { 131 broker = (BrokerAgent) brokers.get(index); 132 } 133 if (broker == null) { 134 broker = (BrokerAgent) brokerContext.getBean("broker" + brokerCounter); 135 brokers.add(index, broker); 136 } 137 return broker; 138 } 139 else { 140 return (BrokerAgent) brokerContext.getBean("broker" + brokerCounter); 141 } 142 } 143 144 protected void addTestsFor(TestSuite suite, ApplicationContext clientContext, ApplicationContext brokerContext, Constructor constructor, 145 Object [] parameters, String destinationName, int destinationType) throws Exception { 146 Collection values = clientContext.getBeansOfType(DestinationFactory.class).values(); 147 assertTrue("we should at least one DestinationFactory in the ApplicationContext", values.size() > 0); 148 149 for (Iterator iter = values.iterator(); iter.hasNext();) { 150 DestinationFactory destinationFactory = (DestinationFactory) iter.next(); 151 152 Object instance = constructor.newInstance(parameters); 153 assertTrue("Instance is not a Scenario: " + instance, instance instanceof Scenario); 154 Scenario scenarioInstance = (Scenario) instance; 155 156 String testName = destinationDescription(destinationType) + "." + destinationName; 157 Destination destination = destinationFactory.createDestination(testName, destinationType); 158 scenarioInstance.setDestination(destination); 159 160 suite.addTest(new ScenarioTestCase(scenarioInstance, testName)); 161 } 162 } 163 164 public static String destinationDescription(int destinationType) { 165 switch (destinationType) { 166 case DestinationFactory.QUEUE: 167 return "queue"; 168 169 case DestinationFactory.TOPIC: 170 return "topic"; 171 172 default: 173 return "Unknown"; 174 } 175 } 176 } 177 | Popular Tags |