KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > tools > ij > mtTestCase


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

21
22 package org.apache.derby.impl.tools.ij;
23
24 import java.util.Hashtable JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.lang.Math JavaDoc;
27 import java.io.FileNotFoundException JavaDoc;
28 import java.io.BufferedInputStream JavaDoc;
29
30 import java.io.FileInputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32
33 import org.apache.derby.iapi.tools.i18n.*;
34
35 /**
36  */

37 public class mtTestCase
38 {
39     public String JavaDoc name = null;
40     public String JavaDoc file = null;
41     public String JavaDoc propFile = null;
42     public float weight = (float).5;
43     public Hashtable JavaDoc ignoreErrors = null;
44     public String JavaDoc description = null;
45
46
47     private int iterations;
48     private int attempts;
49
50     public void mtTestCase()
51     { };
52
53     public void setName(String JavaDoc name)
54     {
55         this.name = name;
56     }
57     public String JavaDoc getName()
58     {
59         return name;
60     }
61
62     public void setFile(String JavaDoc name)
63     {
64         this.file = name;
65     }
66
67     public void setInputDir(String JavaDoc dir)
68     {
69         file = dir + "/" + file;
70     }
71
72     public String JavaDoc getFile()
73     {
74         return file;
75     }
76     
77     public void setPropFile(String JavaDoc name)
78     {
79         this.propFile = name;
80     }
81
82     public String JavaDoc getPropFile()
83     {
84         return propFile;
85     }
86
87     public void setWeight(int weight)
88     {
89         this.weight = (float)(weight/100.0);
90     }
91     
92     public void setIgnoreErrors(Hashtable JavaDoc t)
93     {
94         this.ignoreErrors = t;
95     }
96     
97     public void setDescription(String JavaDoc description)
98     {
99         this.description = description;
100     }
101
102     /**
103     ** Initialize the test case. See initialize(String)
104     */

105     public synchronized BufferedInputStream JavaDoc initialize()
106             throws FileNotFoundException JavaDoc, IOException JavaDoc
107     {
108         return initialize(null);
109     }
110
111     /**
112     ** Initizalize the test case. Loads up the properties
113     ** file and sets the input stream. Used to set up
114     ** prior to running the thread.
115     */

116     public synchronized BufferedInputStream JavaDoc initialize(String JavaDoc inputDir)
117             throws FileNotFoundException JavaDoc, IOException JavaDoc
118     {
119         String JavaDoc filePath;
120         BufferedInputStream JavaDoc inStream = null;
121
122         // load up properties
123
if (propFile != null)
124         {
125             BufferedInputStream JavaDoc propStream;
126             Properties JavaDoc p;
127             String JavaDoc propPath = (inputDir == null) ?
128                         propFile :
129                 (inputDir + "/" + propFile);
130             
131             try
132             {
133                 propStream = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(propPath));
134             } catch (FileNotFoundException JavaDoc e)
135             {
136                 System.out.println(name+": unable to find properties file "+propPath);
137                 throw e;
138             }
139
140             p = System.getProperties();
141             p.load(propStream);
142             // for network server need to alter url
143
String JavaDoc framework = p.getProperty("framework");
144             
145             if (framework != null)
146             {
147                 String JavaDoc newURLPrefix = null;
148                 framework = framework.toUpperCase(java.util.Locale.ENGLISH);
149                 if (framework.equals("DB2JNET") || framework.equals("DERBYNET"))
150                     newURLPrefix= "jdbc:derby:net://localhost:1527/";
151                 else if (framework.equals("DERBYNETCLIENT"))
152                     newURLPrefix= "jdbc:derby://localhost:1527/";
153                 if (newURLPrefix != null)
154                 {
155                     updateURLProperties(p,newURLPrefix);
156                     p.setProperty("ij.user","APP");
157                     p.setProperty("ij.password","PWD");
158                 }
159             }
160             // this is a special case for the MultiTest.
161
// check and alter url if there are any encryption related
162
// properties that need to be set on the url
163
if (("true").equalsIgnoreCase(p.getProperty("encryption")))
164             {
165                String JavaDoc encryptUrl = "dataEncryption=true;bootPassword=Thursday";
166                String JavaDoc dbUrl = p.getProperty("ij.database");
167                String JavaDoc encryptionAlgorithm = p.getProperty("encryptionAlgorithm");
168                if (encryptionAlgorithm != null)
169                {
170                    p.setProperty(
171                        "ij.database",
172                        dbUrl + ";" + encryptUrl + ";" + encryptionAlgorithm);
173                }
174                else
175                {
176                    p.setProperty("ij.database",dbUrl + ";"+encryptUrl);
177                }
178             }
179             
180             // If the initial connection is being specified as a DataSource
181
// on the command line using -Dij.dataSource=<dsclassname>
182
// then remove the ij.database and ij.protocol property.
183
// This is because the ij.database and ij.protocol
184
// will override the ij.dataSource property.
185
if (System.getProperty("ij.dataSource") != null)
186             {
187                 p.remove("ij.database");
188                 p.remove("ij.protocol");
189             }
190             
191             System.setProperties(p);
192         }
193         // set input stream
194
filePath = (inputDir == null) ?
195                         file : (inputDir + "/" + file);
196
197         try
198         {
199             inStream = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(filePath),
200                             utilMain.BUFFEREDFILESIZE);
201         } catch (FileNotFoundException JavaDoc e)
202         {
203             System.out.println("unable to find properties file "+filePath);
204             throw e;
205         }
206         return inStream;
207     }
208
209     /**
210     ** Attempt to grab this test case.
211     ** Uses random number and the weight of this
212     ** case to determine if the grab was successful.
213     **
214     ** @return true/false
215     */

216     public synchronized boolean grab()
217     {
218         attempts++;
219         if (java.lang.Math.random() < weight)
220         {
221             iterations++;
222             return true;
223         }
224         else
225         {
226             return false;
227         }
228     }
229
230     /**
231     ** Run the test case. Invokes IJ to do our
232     ** dirty work.
233     */

234     public void runMe(LocalizedOutput log, LocalizedOutput out, BufferedInputStream JavaDoc infile)
235     {
236         utilMain utilInstance;
237         LocalizedInput is;
238         is = LocalizedResource.getInstance().getNewInput(infile);
239
240         LocalizedInput [] in = { is };
241     
242         out.println("--------------"+file+"-----------------");
243         utilInstance = new utilMain(1, out, ignoreErrors);
244         utilInstance.initFromEnvironment();
245         utilInstance.setMtUse(true);
246         utilInstance.go(in, out, (java.util.Properties JavaDoc) null);
247         log.flush();
248         out.flush();
249     }
250
251     public void updateURLProperties(Properties JavaDoc p, String JavaDoc newURLPrefix)
252     {
253         String JavaDoc[] propsToUpdate = {"ij.database", "ij.protocol",
254                                   "database"};
255         for (int i = 0; i < propsToUpdate.length; i++)
256         {
257             String JavaDoc key = propsToUpdate[i];
258             String JavaDoc val = p.getProperty(key);
259             if (val != null)
260                 p.setProperty(key,alterURL(val,newURLPrefix));
261         }
262     }
263
264
265     public String JavaDoc alterURL(String JavaDoc url, String JavaDoc newURLPrefix)
266     {
267         String JavaDoc urlPrefix = "jdbc:derby:";
268     
269         if (url.startsWith(newURLPrefix))
270             return url;
271
272         // If we don't have a URL prefix for this framework
273
// just return
274
if (newURLPrefix == null)
275             return url;
276     
277         if (url.equals(urlPrefix)) // Replace embedded
278
return newURLPrefix;
279
280         if (url.startsWith(urlPrefix))
281         {
282             // replace jdbc:derby: with our url:
283
url = newURLPrefix +
284                 url.substring(urlPrefix.length());
285
286         }
287         else
288         {
289             if (! (url.startsWith("jdbc:")))
290         {
291             url = newURLPrefix + url;
292         }
293         }
294         //System.out.println("New url:" +url);
295
return url;
296     }
297   
298
299 // NOTE: tried invoking ij directly, but had some problems,
300
// so stick with calling utilMain().
301
// /**
302
// ** Run the test case. Invokes IJ to do our
303
// ** dirty work.
304
// */
305
// public void runMe(AppStreamWriter log, AppStreamWriter out, BufferedInputStream infile)
306
// {
307
// ASCII_UCodeESC_CharStream charStream;
308
// ijTokenManager ijTokMgr;
309
// ij ijParser;
310
//
311
//
312
// out.println("--------------"+file+"-----------------");
313
// charStream = new ASCII_UCodeESC_CharStream(in, 1, 1);
314
// ijTokMgr = new ijTokenManager(charStream);
315
// ijParser = new ij(ijTokMgr, System.out, this);
316
// log.flush();
317
// out.flush();
318
// }
319

320     /**
321     ** Name says it all
322     */

323     public String JavaDoc toString()
324     {
325         return "name: "+name+
326                 "\n\tfile: "+file+
327                 "\n\tproperties: "+propFile+
328                 "\n\tweight: "+weight+
329                 "\n\tignoreErrors: "+ignoreErrors+
330                 "\n\tdescription: "+description;
331     }
332
333     
334 }
335
Popular Tags