KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > control > RandomOrderController


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/control/RandomOrderController.java,v 1.4 2004/02/19 00:04:35 sebb Exp $
2
/*
3  * Copyright 2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.jmeter.control;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import junit.framework.TestSuite;
25
26 import org.apache.jmeter.junit.JMeterTestCase;
27 import org.apache.jmeter.junit.stubs.TestSampler;
28 import org.apache.jmeter.testelement.TestElement;
29
30 /**
31  * A controller that runs its children each at most once, but in a random order.
32  *
33  * @author Mike Verdone
34  * @version $Revision: 1.4 $ updated on $Date: 2004/02/19 00:04:35 $
35  */

36 public class RandomOrderController
37     extends GenericController
38     implements Serializable JavaDoc
39 {
40     /**
41      * Create a new RandomOrderController.
42      */

43     public RandomOrderController()
44     {
45     }
46
47     /**
48      * @see GenericController#initialize()
49      */

50     public void initialize()
51     {
52         super.initialize();
53         this.reorder();
54     }
55
56     /**
57      * @see GenericController#reInitialize()
58      */

59     public void reInitialize()
60     {
61         super.reInitialize();
62         this.reorder();
63     }
64     
65     /**
66      * Replace the subControllersAndSamplers list with a reordered ArrayList.
67      */

68     private void reorder()
69     {
70         int numElements = this.subControllersAndSamplers.size();
71         
72         // Create a new list containing numElements null elements.
73
List JavaDoc reordered = new ArrayList JavaDoc(this.subControllersAndSamplers.size());
74         for (int i = 0; i < numElements; i++)
75         {
76             reordered.add(null);
77         }
78
79         // Insert the subControllersAndSamplers into random list positions.
80
for (Iterator JavaDoc i = this.subControllersAndSamplers.iterator();
81              i.hasNext(); )
82         {
83             int idx = (int)Math.floor(Math.random() * reordered.size());
84             while (true)
85             {
86                 if (idx == numElements)
87                 {
88                     idx = 0;
89                 }
90                 if (reordered.get(idx) == null)
91                 {
92                     reordered.set(idx, i.next());
93                     break;
94                 }
95                 idx++;
96             }
97         }
98         
99         // Replace subControllersAndSamplers with reordered copy.
100
this.subControllersAndSamplers = reordered;
101     }
102
103     public static class Test extends JMeterTestCase
104     {
105
106         public Test(String JavaDoc name)
107         {
108             super(name);
109         }
110         
111         public void testRandomOrder()
112         {
113             testLog.debug("Testing RandomOrderController");
114             RandomOrderController roc = new RandomOrderController();
115             roc.addTestElement(new TestSampler("zero"));
116             roc.addTestElement(new TestSampler("one"));
117             roc.addTestElement(new TestSampler("two"));
118             roc.addTestElement(new TestSampler("three"));
119             TestElement sampler = null;
120             List JavaDoc usedSamplers = new ArrayList JavaDoc();
121             roc.initialize();
122             while ((sampler = roc.next()) != null)
123             {
124                 String JavaDoc samplerName = sampler.getPropertyAsString(TestSampler.NAME);
125                 if (usedSamplers.contains(samplerName))
126                 {
127                     assertTrue("Duplicate sampler returned from next()", false);
128                 }
129                 usedSamplers.add(samplerName);
130             }
131             assertTrue("All samplers were returned",
132                 usedSamplers.size() == 4);
133         }
134         
135         public void testRandomOrderNoElements()
136         {
137             RandomOrderController roc = new RandomOrderController();
138             roc.initialize();
139             assertTrue(roc.next() == null);
140         }
141
142         public void testRandomOrderOneElement()
143         {
144             RandomOrderController roc = new RandomOrderController();
145             roc.addTestElement(new TestSampler("zero"));
146             TestElement sampler = null;
147             List JavaDoc usedSamplers = new ArrayList JavaDoc();
148             roc.initialize();
149             while ((sampler = roc.next()) != null)
150             {
151                 String JavaDoc samplerName = sampler.getPropertyAsString(TestSampler.NAME);
152                 if (usedSamplers.contains(samplerName))
153                 {
154                     assertTrue("Duplicate sampler returned from next()", false);
155                 }
156                 usedSamplers.add(samplerName);
157             }
158             assertTrue("All samplers were returned",
159                 usedSamplers.size() == 1);
160         }
161     }
162
163     public static void main(String JavaDoc args[])
164     {
165         junit.textui.TestRunner.run(suite());
166     }
167
168     public static TestSuite suite()
169     {
170         TestSuite suite = new TestSuite();
171         suite.addTest(new Test("testRandomOrderController"));
172         return suite;
173     }
174
175 }
176
Popular Tags