View Javadoc

1   /*
2    * Copyright (c) 2004, RV Test Team
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * Redistributions of source code must retain the above copyright notice, this
9    * list of conditions and the following disclaimer.
10   *
11   * Redistributions in binary form must reproduce the above copyright notice,
12   * this list of conditions and the following disclaimer in the documentation
13   * and/or other materials provided with the distribution.
14   *
15   * Neither the name of the "RV Test Team" nor the names of its contributors may
16   * be used to endorse or promote products derived from this software without
17   * specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   */
32  /*
33   * Copyright (c) 2002, Reuters
34   * All rights reserved.
35   *
36   * Redistribution and use in source and binary forms, with or without
37   * modification, are permitted provided that the following conditions are met:
38   *
39   * Redistributions of source code must retain the above copyright notice, this
40   * list of conditions and the following disclaimer.
41   * Redistributions in binary form must reproduce the above copyright notice,
42   * this list of conditions and the following disclaimer in the documentation
43   * and/or other materials provided with the distribution.
44   * Neither the name of the Reuters nor the names of its contributors may be
45   * used to endorse or promote products derived from this software without
46   * specific prior written permission.
47   *
48   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
49   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
52   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
58   * THE POSSIBILITY OF SUCH DAMAGE.
59   *
60   */
61  package com.reuters.msgtest.load.value;
62  
63  import com.reuters.msgtest.load.LoadTest;
64  import java.util.ArrayList;
65  import java.util.StringTokenizer;
66  
67  
68  /***
69   * Selects values from a given delimlist based on a
70   * selection criteria (sequential, random, etc). This
71   * implementation is for a "," delimeted strings.
72   * TODO: generalize this.
73   *
74   * @author Cavit Aydin
75   * @version @VSS_FLAG_VERSION@
76   */
77  public class DelimListValueGenerator implements ValueGenerator {
78      private ArrayList values = new ArrayList();
79      private int currentIndex = 0;
80      private String currentValue;
81      private String selectionCriteria = ValueGenerator.SEQUENTIAL;
82  
83      /***
84       * Construct a DelimListValueGenerator from a Valueset.
85       *
86       * @param pValueSet the valueset object constructed from the
87       *                  xml config file.
88       */
89      public DelimListValueGenerator(ValueSet pValueSet) {
90          this(pValueSet.getValues(), pValueSet.getSelection());
91      }
92  
93      /***
94       * Construct a DelimListValueGenerator from a delimeted
95       * string and a criteria.
96       *
97       * @param pDelimList the comma delimeted string
98       * @param pSelection the selection criteria
99       */
100     public DelimListValueGenerator(String pDelimList, String pSelection) {
101         StringTokenizer st = new StringTokenizer(pDelimList, ",");
102 
103         while (st.hasMoreTokens()) {
104             values.add(st.nextToken());
105         }
106 
107         selectionCriteria = pSelection;
108         currentValue = (String) values.get(0);
109     }
110 
111     /***
112      * @see ValueGenerator#getCurrentValue
113      */
114     public String getCurrentValue() {
115         return currentValue;
116     }
117 
118     /***
119      * @see ValueGenerator#getCurrentValue
120      */
121     public String getCurrentValue(String pSubstr) {
122         return currentValue;
123     }
124 
125     /***
126      * @see ValueGenerator#changeValue
127      */
128     public boolean changeValue() {
129         if (selectionCriteria.equals(ValueGenerator.RANDOM)) {
130             int randomIndex = LoadTest.RANDOM.nextInt(values.size());
131             currentValue = values.get(randomIndex).toString();
132         } else {
133             // sequential
134             boolean endLoop = (currentIndex == (values.size() - 1));
135 
136             currentIndex = ++currentIndex % values.size();
137             currentValue = values.get(currentIndex).toString();
138 
139             return endLoop;
140         }
141 
142         return false;
143     }
144 }