001: /*
002:
003: Derby - Class org.apache.derby.impl.tools.ij.mtTestCase
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.tools.ij;
023:
024: import java.util.Hashtable;
025: import java.util.Properties;
026: import java.lang.Math;
027: import java.io.FileNotFoundException;
028: import java.io.BufferedInputStream;
029:
030: import java.io.FileInputStream;
031: import java.io.IOException;
032:
033: import org.apache.derby.iapi.tools.i18n.*;
034:
035: /**
036: */
037: public class mtTestCase {
038: public String name = null;
039: public String file = null;
040: public String propFile = null;
041: public float weight = (float) .5;
042: public Hashtable ignoreErrors = null;
043: public String description = null;
044:
045: private int iterations;
046: private int attempts;
047:
048: public void mtTestCase() {
049: };
050:
051: public void setName(String name) {
052: this .name = name;
053: }
054:
055: public String getName() {
056: return name;
057: }
058:
059: public void setFile(String name) {
060: this .file = name;
061: }
062:
063: public void setInputDir(String dir) {
064: file = dir + "/" + file;
065: }
066:
067: public String getFile() {
068: return file;
069: }
070:
071: public void setPropFile(String name) {
072: this .propFile = name;
073: }
074:
075: public String getPropFile() {
076: return propFile;
077: }
078:
079: public void setWeight(int weight) {
080: this .weight = (float) (weight / 100.0);
081: }
082:
083: public void setIgnoreErrors(Hashtable t) {
084: this .ignoreErrors = t;
085: }
086:
087: public void setDescription(String description) {
088: this .description = description;
089: }
090:
091: /**
092: ** Initialize the test case. See initialize(String)
093: */
094: public synchronized BufferedInputStream initialize()
095: throws FileNotFoundException, IOException {
096: return initialize(null);
097: }
098:
099: /**
100: ** Initizalize the test case. Loads up the properties
101: ** file and sets the input stream. Used to set up
102: ** prior to running the thread.
103: */
104: public synchronized BufferedInputStream initialize(String inputDir)
105: throws FileNotFoundException, IOException {
106: String filePath;
107: BufferedInputStream inStream = null;
108:
109: // load up properties
110: if (propFile != null) {
111: BufferedInputStream propStream;
112: Properties p;
113: String propPath = (inputDir == null) ? propFile : (inputDir
114: + "/" + propFile);
115:
116: try {
117: propStream = new BufferedInputStream(
118: new FileInputStream(propPath));
119: } catch (FileNotFoundException e) {
120: System.out.println(name
121: + ": unable to find properties file "
122: + propPath);
123: throw e;
124: }
125:
126: p = System.getProperties();
127: p.load(propStream);
128: // for network server need to alter url
129: String framework = p.getProperty("framework");
130:
131: if (framework != null) {
132: String newURLPrefix = null;
133: framework = framework
134: .toUpperCase(java.util.Locale.ENGLISH);
135: if (framework.equals("DB2JNET")
136: || framework.equals("DERBYNET"))
137: newURLPrefix = "jdbc:derby:net://localhost:1527/";
138: else if (framework.equals("DERBYNETCLIENT"))
139: newURLPrefix = "jdbc:derby://localhost:1527/";
140: if (newURLPrefix != null) {
141: updateURLProperties(p, newURLPrefix);
142: p.setProperty("ij.user", "APP");
143: p.setProperty("ij.password", "PWD");
144: }
145: }
146: // this is a special case for the MultiTest.
147: // check and alter url if there are any encryption related
148: // properties that need to be set on the url
149: if (("true").equalsIgnoreCase(p.getProperty("encryption"))) {
150: String encryptUrl = "dataEncryption=true;bootPassword=Thursday";
151: String dbUrl = p.getProperty("ij.database");
152: String encryptionAlgorithm = p
153: .getProperty("encryptionAlgorithm");
154: if (encryptionAlgorithm != null) {
155: p.setProperty("ij.database", dbUrl + ";"
156: + encryptUrl + ";" + encryptionAlgorithm);
157: } else {
158: p.setProperty("ij.database", dbUrl + ";"
159: + encryptUrl);
160: }
161: }
162:
163: // If the initial connection is being specified as a DataSource
164: // on the command line using -Dij.dataSource=<dsclassname>
165: // then remove the ij.database and ij.protocol property.
166: // This is because the ij.database and ij.protocol
167: // will override the ij.dataSource property.
168: if (System.getProperty("ij.dataSource") != null) {
169: p.remove("ij.database");
170: p.remove("ij.protocol");
171: }
172:
173: System.setProperties(p);
174: }
175: // set input stream
176: filePath = (inputDir == null) ? file : (inputDir + "/" + file);
177:
178: try {
179: inStream = new BufferedInputStream(new FileInputStream(
180: filePath), utilMain.BUFFEREDFILESIZE);
181: } catch (FileNotFoundException e) {
182: System.out.println("unable to find properties file "
183: + filePath);
184: throw e;
185: }
186: return inStream;
187: }
188:
189: /**
190: ** Attempt to grab this test case.
191: ** Uses random number and the weight of this
192: ** case to determine if the grab was successful.
193: **
194: ** @return true/false
195: */
196: public synchronized boolean grab() {
197: attempts++;
198: if (java.lang.Math.random() < weight) {
199: iterations++;
200: return true;
201: } else {
202: return false;
203: }
204: }
205:
206: /**
207: ** Run the test case. Invokes IJ to do our
208: ** dirty work.
209: */
210: public void runMe(LocalizedOutput log, LocalizedOutput out,
211: BufferedInputStream infile) {
212: utilMain utilInstance;
213: LocalizedInput is;
214: is = LocalizedResource.getInstance().getNewInput(infile);
215:
216: LocalizedInput[] in = { is };
217:
218: out.println("--------------" + file + "-----------------");
219: utilInstance = new utilMain(1, out, ignoreErrors);
220: utilInstance.initFromEnvironment();
221: utilInstance.setMtUse(true);
222: utilInstance.go(in, out, (java.util.Properties) null);
223: log.flush();
224: out.flush();
225: }
226:
227: public void updateURLProperties(Properties p, String newURLPrefix) {
228: String[] propsToUpdate = { "ij.database", "ij.protocol",
229: "database" };
230: for (int i = 0; i < propsToUpdate.length; i++) {
231: String key = propsToUpdate[i];
232: String val = p.getProperty(key);
233: if (val != null)
234: p.setProperty(key, alterURL(val, newURLPrefix));
235: }
236: }
237:
238: public String alterURL(String url, String newURLPrefix) {
239: String urlPrefix = "jdbc:derby:";
240:
241: if (url.startsWith(newURLPrefix))
242: return url;
243:
244: // If we don't have a URL prefix for this framework
245: // just return
246: if (newURLPrefix == null)
247: return url;
248:
249: if (url.equals(urlPrefix)) // Replace embedded
250: return newURLPrefix;
251:
252: if (url.startsWith(urlPrefix)) {
253: // replace jdbc:derby: with our url:
254: url = newURLPrefix + url.substring(urlPrefix.length());
255:
256: } else {
257: if (!(url.startsWith("jdbc:"))) {
258: url = newURLPrefix + url;
259: }
260: }
261: //System.out.println("New url:" +url);
262: return url;
263: }
264:
265: // NOTE: tried invoking ij directly, but had some problems,
266: // so stick with calling utilMain().
267: // /**
268: // ** Run the test case. Invokes IJ to do our
269: // ** dirty work.
270: // */
271: // public void runMe(AppStreamWriter log, AppStreamWriter out, BufferedInputStream infile)
272: // {
273: // ASCII_UCodeESC_CharStream charStream;
274: // ijTokenManager ijTokMgr;
275: // ij ijParser;
276: //
277: //
278: // out.println("--------------"+file+"-----------------");
279: // charStream = new ASCII_UCodeESC_CharStream(in, 1, 1);
280: // ijTokMgr = new ijTokenManager(charStream);
281: // ijParser = new ij(ijTokMgr, System.out, this);
282: // log.flush();
283: // out.flush();
284: // }
285:
286: /**
287: ** Name says it all
288: */
289: public String toString() {
290: return "name: " + name + "\n\tfile: " + file
291: + "\n\tproperties: " + propFile + "\n\tweight: "
292: + weight + "\n\tignoreErrors: " + ignoreErrors
293: + "\n\tdescription: " + description;
294: }
295:
296: }
|