KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > systest > ScenarioTestSuite


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.systest;
19
20 import org.springframework.context.ApplicationContext;
21
22 import javax.jms.Destination JavaDoc;
23
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import junit.framework.TestCase;
31 import junit.framework.TestSuite;
32
33 /**
34  * A helper class for creating a test suite
35  *
36  * @version $Revision: 1.1 $
37  */

38 public class ScenarioTestSuite extends TestCase {
39
40     private List JavaDoc brokers = new ArrayList JavaDoc();
41     private boolean cacheBrokers;
42
43     public static TestSuite createSuite(ApplicationContext clientContext, ApplicationContext brokerContext, Class JavaDoc[] scenarios, int destinationType) throws Exception JavaDoc {
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 JavaDoc[] scenarios, int destinationType) throws Exception JavaDoc {
52         for (int i = 0; i < scenarios.length; i++) {
53             Class JavaDoc scenario = scenarios[i];
54             appendTestCase(suite, clientContext, brokerContext, scenario, destinationType);
55         }
56     }
57
58     public void appendTestCase(TestSuite suite, ApplicationContext clientContext, ApplicationContext brokerContext, Class JavaDoc scenario, int destinationType) throws Exception JavaDoc {
59         // lets figure out how to create the scenario from all the options
60
// available
61

62         Constructor JavaDoc[] constructors = scenario.getConstructors();
63         for (int i = 0; i < constructors.length; i++) {
64             Constructor JavaDoc 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 JavaDoc scenario, Constructor JavaDoc constructor,
70             int destinationType) throws Exception JavaDoc {
71         // lets configure the test case
72
Class JavaDoc[] parameterTypes = constructor.getParameterTypes();
73         int size = parameterTypes.length;
74         String JavaDoc[][] namesForParameters = new String JavaDoc[size][];
75         for (int i = 0; i < size; i++) {
76             Class JavaDoc parameterType = parameterTypes[i];
77             String JavaDoc[] names = clientContext.getBeanNamesForType(parameterType);
78             if (names == null || names.length == 0) {
79                 if (parameterType.equals(BrokerAgent.class)) {
80                     names = new String JavaDoc[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         // lets try out each permutation of configuration
90
int[] counters = new int[size];
91         boolean completed = false;
92         while (!completed) {
93             Object JavaDoc[] parameters = new Object JavaDoc[size];
94             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(scenario.getName());
95             int brokerCounter = 1;
96             for (int i = 0; i < size; i++) {
97                 String JavaDoc 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 JavaDoc destinationName = buffer.toString();
109             addTestsFor(suite, clientContext, brokerContext, constructor, parameters, destinationName, destinationType);
110
111             // now lets count though the options
112
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             // lets reuse broker instances across test cases
130
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 JavaDoc constructor,
145             Object JavaDoc[] parameters, String JavaDoc destinationName, int destinationType) throws Exception JavaDoc {
146         Collection JavaDoc values = clientContext.getBeansOfType(DestinationFactory.class).values();
147         assertTrue("we should at least one DestinationFactory in the ApplicationContext", values.size() > 0);
148
149         for (Iterator JavaDoc iter = values.iterator(); iter.hasNext();) {
150             DestinationFactory destinationFactory = (DestinationFactory) iter.next();
151
152             Object JavaDoc instance = constructor.newInstance(parameters);
153             assertTrue("Instance is not a Scenario: " + instance, instance instanceof Scenario);
154             Scenario scenarioInstance = (Scenario) instance;
155
156             String JavaDoc testName = destinationDescription(destinationType) + "." + destinationName;
157             Destination JavaDoc destination = destinationFactory.createDestination(testName, destinationType);
158             scenarioInstance.setDestination(destination);
159
160             suite.addTest(new ScenarioTestCase(scenarioInstance, testName));
161         }
162     }
163
164     public static String JavaDoc 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