KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > tools > testlet > AbstractTestLet


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.tools.testlet;
26
27 import java.util.Hashtable JavaDoc;
28
29 import junit.framework.TestCase;
30
31 /**
32  * This class defines a AbstractTestLet. A testlet is meant to be a portion of
33  * testing. This should be included in a scenario based on a template.
34  * <ul>
35  * <li><tt>Template</tt>: Starts the test configuration, including
36  * controller, virtual database and backends.</li>
37  * <li><tt>TestLet</tt>: the stand alone part of the test. It needs a
38  * connection and test parameters</li>
39  * <li><tt>Scenario</tt>: it coordinates the template with the testlet.
40  * </ul>
41  *
42  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
43  * @version 1.0
44  */

45 public abstract class AbstractTestLet extends TestCase
46 {
47   /** <tt>TABLE_NAME</tt> configuration parameter */
48   public static final String JavaDoc TABLE_NAME = "TABLE_NAME";
49   /** <tt>COLUMN_NAME</tt> configuration parameter */
50   public static final String JavaDoc COLUMN_NAME = "COLUMN_NAME";
51   /** <tt>UPDATED_COLUMN_VALUE</tt> configuration parameter */
52   public static final String JavaDoc UPDATED_COLUMN_VALUE = "UPDATED_COLUMN_VALUE";
53   /** <tt>SELECTED_COLUMNS</tt> configuration parameter */
54   public static final String JavaDoc SELECTED_COLUMNS = "SELECTED_COLUMNS";
55   /** <tt>USE_PREPARED_STATEMENT</tt> configuration parameter */
56   public static final String JavaDoc USE_PREPARED_STATEMENT = "USE_PREPARED_STATEMENT";
57   /** <tt>VIRTUAL_DATABASE</tt> configuration parameter */
58   public static final String JavaDoc VIRTUAL_DATABASE = "VIRTUAL_DATABASE";
59   /** <tt>IGNORE_CASE</tt> configuration parameter */
60   public static final String JavaDoc IGNORE_CASE = "IGNORE_CASE";
61   /** <tt>TABLE_METADATA_COLUMNS</tt> configuration parameter */
62   public static final String JavaDoc TABLE_METADATA_COLUMNS = "TABLE_METADATA_COLUMNS";
63   /** <tt>USE_TRANSACTIONS</tt> configuration parameter */
64   public static final String JavaDoc USE_TRANSACTIONS = "USE_TRANSACTIONS";
65   /** <tt>USE_CJDBC_CLASS</tt> configuration parameter */
66   public static final String JavaDoc USE_CJDBC_CLASS = "USE_CJDBC_CLASS";
67   /** <tt>FILE_NAME</tt> configuration parameter */
68   public static final String JavaDoc FILE_NAME = "FILE_NAME";
69   /** <tt>LIST_FAILOVER_BACKENDS</tt> configuration parameter */
70   public static final String JavaDoc LIST_FAILOVER_BACKENDS = "LIST_FAILOVER_BACKENDS";
71   /** <tt>ITERATION</tt> configuration parameter */
72   public static final String JavaDoc ITERATION = "ITERATION";
73   /** <tt>PROCEDURE_NAME</tt> configuration parameter */
74   public static final String JavaDoc PROCEDURE_NAME = "PROCEDURE";
75   /** <tt>USE_UPDATE_STATEMENT</tt> configuration parameter */
76   public static final String JavaDoc USE_UPDATE_STATEMENT = "USE_UPDATE_STATEMENT";
77   /** <tt>NUMBER_OF_UPDATES</tt> configuration parameter */
78   public static final String JavaDoc NUMBER_OF_UPDATES = "NUMBER_OF_UPDATES";
79   /** <tt>USE_OPTIMIZED_STATEMENT</tt> configuration parameter */
80   public static final String JavaDoc USE_OPTIMIZED_STATEMENT = "USE_OPTIMIZED_STATEMENT";
81   /** <tt>MACRO_NAME</tt> configuration parameter */
82   public static final String JavaDoc MACRO_NAME = "MACRO_NAME";
83
84   protected Hashtable JavaDoc config;
85   private long initialMemoryUsage;
86   private long initialTime;
87
88   /**
89    * Creates a new <code>AbstractTestLet</code> object
90    */

91   public AbstractTestLet()
92   {
93     config = new Hashtable JavaDoc();
94     System.gc();
95     initialMemoryUsage = checkMemoryUsage();
96     initialTime = System.currentTimeMillis();
97   }
98
99   /**
100    * Execute the content of the test. This method can call JUnit's assert
101    * methods and can therefore validate or invalidate the whole test.
102    *
103    * @throws Exception if fails
104    */

105   public abstract void execute() throws Exception JavaDoc;
106
107   /**
108    * Collect the current memory usage
109    *
110    * @return a value in Kilobytes
111    */

112   public long checkMemoryUsage()
113   {
114     long total = Runtime.getRuntime().totalMemory();
115     long before = Runtime.getRuntime().freeMemory();
116     return (total - before) / 1024;
117   }
118
119   /**
120    * This calls the <code>execute</code> method multiple times. The batch
121    * method is limited to change values on a single entry of the configuration
122    * of this testlet.
123    *
124    * @param batchCategory the configuration value description to change
125    * @param batchValues the different value the parameter will take
126    * @throws Exception if any fail
127    */

128   public void executeBatch(String JavaDoc batchCategory, Object JavaDoc[] batchValues)
129       throws Exception JavaDoc
130   {
131     for (int i = 0; i < batchValues.length; i++)
132     {
133       set(batchCategory, batchValues[i]);
134       execute();
135     }
136   }
137
138   /**
139    * Execute the same let with the same parameters a couple of times
140    *
141    * @param numberOfTimes the number of times to repeat the test
142    */

143   public void executeBatch(int numberOfTimes) throws Exception JavaDoc
144   {
145     for (int i = 0; i < numberOfTimes; i++)
146       execute();
147   }
148
149   /**
150    * Configure the test with new values. The values are specific to each test
151    * let. If this method is not called before <code>execute</code>, the test
152    * will use predefined test properties
153    *
154    * @param properties set of properties to use for this test
155    * @throws Exception if fails
156    */

157   public void configure(Hashtable JavaDoc properties) throws Exception JavaDoc
158   {
159     properties.putAll(properties);
160   }
161
162   /**
163    * get the boolean value of a configuration value
164    *
165    * @param key key name of the value
166    * @return <code>boolean</code> primitive type
167    */

168   public boolean getConfigBoolean(String JavaDoc key)
169   {
170     return Boolean.valueOf((String JavaDoc) config.get(key)).booleanValue();
171   }
172
173   /**
174    * Get the current test configuration
175    *
176    * @return <code>Hashtable</code> with all the defined properties
177    */

178   public Hashtable JavaDoc getConfig()
179   {
180     return config;
181   }
182
183   /**
184    * Sets the hashtable of properties for this test
185    *
186    * @param config <code>Hashtable</code> with all the defined properties
187    */

188   public void setConfig(Hashtable JavaDoc config)
189   {
190     this.config = config;
191   }
192
193   /**
194    * Sets a single test parameter
195    *
196    * @param key the key to define
197    * @param value the value of the key
198    */

199   public void set(String JavaDoc key, Object JavaDoc value)
200   {
201     config.put(key, value);
202   }
203
204   /**
205    * Shortcut to know if we should use prepare statement in this let
206    *
207    * @return true or false
208    */

209   public boolean usePreparedStatement()
210   {
211     return Boolean.valueOf((String JavaDoc) config.get(USE_PREPARED_STATEMENT))
212         .booleanValue();
213   }
214
215   /**
216    * Should we ignore case related problems in this let
217    *
218    * @return true or false
219    */

220   public boolean ignoreCase()
221   {
222     return Boolean.valueOf((String JavaDoc) config.get(IGNORE_CASE)).booleanValue();
223   }
224
225   /**
226    * Should we use transactions in this let
227    *
228    * @return true or false
229    */

230   public boolean useTransaction()
231   {
232     return Boolean.valueOf((String JavaDoc) config.get(USE_TRANSACTIONS))
233         .booleanValue();
234   }
235
236   /**
237    * Should we use CJDBC class while we can use generic ones in this let This is
238    * useful for blobs and clobs
239    *
240    * @return true or false
241    */

242   public boolean useCJDBCClass()
243   {
244     return Boolean.valueOf((String JavaDoc) config.get(USE_CJDBC_CLASS)).booleanValue();
245   }
246
247   /**
248    * Returns the initialMemoryUsage value.
249    *
250    * @return Returns the initialMemoryUsage.
251    */

252   public long getInitialMemoryUsage()
253   {
254     return initialMemoryUsage;
255   }
256
257   /**
258    * Gather the total memory usage since the let was instanciated
259    *
260    * @return a long value of kilobytes of memory used
261    */

262   public long getTotalMemoryUsage()
263   {
264     System.gc();
265     return checkMemoryUsage() - initialMemoryUsage;
266   }
267
268   /**
269    * Get the total time usage in seconds
270    *
271    * @return a long value of seconds
272    */

273   public long getTotalTimeUsage()
274   {
275     return (System.currentTimeMillis() - initialTime) / 1000;
276   }
277 }
Popular Tags