--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.tools.ij.mtTestCase\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.tools.ij;\r
+\r
+import java.util.Hashtable;\r
+import java.util.Properties;\r
+import java.lang.Math;\r
+import java.io.FileNotFoundException;\r
+import java.io.BufferedInputStream;\r
+\r
+import java.io.FileInputStream;\r
+import java.io.IOException;\r
+\r
+import org.apache.derby.iapi.tools.i18n.*;\r
+\r
+/**\r
+ */\r
+public class mtTestCase\r
+{\r
+ public String name = null;\r
+ public String file = null;\r
+ public String propFile = null;\r
+ public float weight = (float).5;\r
+ public Hashtable ignoreErrors = null;\r
+ public String description = null;\r
+\r
+\r
+ private int iterations;\r
+ private int attempts;\r
+\r
+ public void mtTestCase()\r
+ { };\r
+\r
+ public void setName(String name)\r
+ {\r
+ this.name = name;\r
+ }\r
+ public String getName()\r
+ {\r
+ return name;\r
+ }\r
+\r
+ public void setFile(String name)\r
+ {\r
+ this.file = name;\r
+ }\r
+\r
+ public void setInputDir(String dir)\r
+ {\r
+ file = dir + "/" + file;\r
+ }\r
+\r
+ public String getFile()\r
+ {\r
+ return file;\r
+ }\r
+ \r
+ public void setPropFile(String name)\r
+ {\r
+ this.propFile = name;\r
+ }\r
+\r
+ public String getPropFile()\r
+ {\r
+ return propFile;\r
+ }\r
+\r
+ public void setWeight(int weight)\r
+ {\r
+ this.weight = (float)(weight/100.0);\r
+ }\r
+ \r
+ public void setIgnoreErrors(Hashtable t)\r
+ {\r
+ this.ignoreErrors = t;\r
+ }\r
+ \r
+ public void setDescription(String description)\r
+ {\r
+ this.description = description;\r
+ }\r
+\r
+ /**\r
+ ** Initialize the test case. See initialize(String)\r
+ */\r
+ public synchronized BufferedInputStream initialize() \r
+ throws FileNotFoundException, IOException\r
+ {\r
+ return initialize(null);\r
+ }\r
+\r
+ /**\r
+ ** Initizalize the test case. Loads up the properties\r
+ ** file and sets the input stream. Used to set up\r
+ ** prior to running the thread.\r
+ */\r
+ public synchronized BufferedInputStream initialize(String inputDir) \r
+ throws FileNotFoundException, IOException\r
+ {\r
+ String filePath; \r
+ BufferedInputStream inStream = null;\r
+\r
+ // load up properties\r
+ if (propFile != null)\r
+ { \r
+ BufferedInputStream propStream;\r
+ Properties p;\r
+ String propPath = (inputDir == null) ?\r
+ propFile : \r
+ (inputDir + "/" + propFile);\r
+ \r
+ try \r
+ {\r
+ propStream = new BufferedInputStream(new FileInputStream(propPath));\r
+ } catch (FileNotFoundException e)\r
+ {\r
+ System.out.println(name+": unable to find properties file "+propPath);\r
+ throw e;\r
+ }\r
+\r
+ p = System.getProperties();\r
+ p.load(propStream);\r
+ // for network server need to alter url\r
+ String framework = p.getProperty("framework");\r
+ \r
+ if (framework != null)\r
+ {\r
+ String newURLPrefix = null;\r
+ framework = framework.toUpperCase(java.util.Locale.ENGLISH);\r
+ if (framework.equals("DB2JNET") || framework.equals("DERBYNET"))\r
+ newURLPrefix= "jdbc:derby:net://localhost:1527/";\r
+ else if (framework.equals("DERBYNETCLIENT"))\r
+ newURLPrefix= "jdbc:derby://localhost:1527/";\r
+ if (newURLPrefix != null)\r
+ {\r
+ updateURLProperties(p,newURLPrefix);\r
+ p.setProperty("ij.user","APP");\r
+ p.setProperty("ij.password","PWD");\r
+ }\r
+ }\r
+ // this is a special case for the MultiTest.\r
+ // check and alter url if there are any encryption related \r
+ // properties that need to be set on the url \r
+ if (("true").equalsIgnoreCase(p.getProperty("encryption"))) \r
+ {\r
+ String encryptUrl = "dataEncryption=true;bootPassword=Thursday";\r
+ String dbUrl = p.getProperty("ij.database");\r
+ String encryptionAlgorithm = p.getProperty("encryptionAlgorithm");\r
+ if (encryptionAlgorithm != null)\r
+ {\r
+ p.setProperty(\r
+ "ij.database",\r
+ dbUrl + ";" + encryptUrl + ";" + encryptionAlgorithm);\r
+ }\r
+ else\r
+ {\r
+ p.setProperty("ij.database",dbUrl + ";"+encryptUrl);\r
+ }\r
+ }\r
+ \r
+ // If the initial connection is being specified as a DataSource\r
+ // on the command line using -Dij.dataSource=<dsclassname>\r
+ // then remove the ij.database and ij.protocol property.\r
+ // This is because the ij.database and ij.protocol \r
+ // will override the ij.dataSource property.\r
+ if (System.getProperty("ij.dataSource") != null)\r
+ {\r
+ p.remove("ij.database");\r
+ p.remove("ij.protocol");\r
+ }\r
+ \r
+ System.setProperties(p);\r
+ }\r
+ // set input stream\r
+ filePath = (inputDir == null) ?\r
+ file : (inputDir + "/" + file);\r
+\r
+ try \r
+ {\r
+ inStream = new BufferedInputStream(new FileInputStream(filePath), \r
+ utilMain.BUFFEREDFILESIZE); \r
+ } catch (FileNotFoundException e)\r
+ {\r
+ System.out.println("unable to find properties file "+filePath);\r
+ throw e;\r
+ }\r
+ return inStream;\r
+ }\r
+\r
+ /**\r
+ ** Attempt to grab this test case. \r
+ ** Uses random number and the weight of this\r
+ ** case to determine if the grab was successful.\r
+ ** \r
+ ** @return true/false\r
+ */\r
+ public synchronized boolean grab()\r
+ {\r
+ attempts++;\r
+ if (java.lang.Math.random() < weight)\r
+ {\r
+ iterations++;\r
+ return true;\r
+ }\r
+ else\r
+ {\r
+ return false;\r
+ }\r
+ }\r
+\r
+ /**\r
+ ** Run the test case. Invokes IJ to do our\r
+ ** dirty work.\r
+ */\r
+ public void runMe(LocalizedOutput log, LocalizedOutput out, BufferedInputStream infile)\r
+ {\r
+ utilMain utilInstance;\r
+ LocalizedInput is;\r
+ is = LocalizedResource.getInstance().getNewInput(infile);\r
+\r
+ LocalizedInput [] in = { is };\r
+ \r
+ out.println("--------------"+file+"-----------------");\r
+ utilInstance = new utilMain(1, out, ignoreErrors);\r
+ utilInstance.initFromEnvironment();\r
+ utilInstance.setMtUse(true);\r
+ utilInstance.go(in, out, (java.util.Properties) null);\r
+ log.flush();\r
+ out.flush();\r
+ }\r
+\r
+ public void updateURLProperties(Properties p, String newURLPrefix)\r
+ {\r
+ String[] propsToUpdate = {"ij.database", "ij.protocol",\r
+ "database"};\r
+ for (int i = 0; i < propsToUpdate.length; i++)\r
+ {\r
+ String key = propsToUpdate[i];\r
+ String val = p.getProperty(key);\r
+ if (val != null)\r
+ p.setProperty(key,alterURL(val,newURLPrefix));\r
+ }\r
+ }\r
+\r
+\r
+ public String alterURL(String url, String newURLPrefix)\r
+ {\r
+ String urlPrefix = "jdbc:derby:";\r
+ \r
+ if (url.startsWith(newURLPrefix))\r
+ return url;\r
+\r
+ // If we don't have a URL prefix for this framework\r
+ // just return\r
+ if (newURLPrefix == null)\r
+ return url;\r
+ \r
+ if (url.equals(urlPrefix)) // Replace embedded\r
+ return newURLPrefix;\r
+\r
+ if (url.startsWith(urlPrefix))\r
+ {\r
+ // replace jdbc:derby: with our url:\r
+ url = newURLPrefix +\r
+ url.substring(urlPrefix.length());\r
+\r
+ }\r
+ else\r
+ {\r
+ if (! (url.startsWith("jdbc:")))\r
+ {\r
+ url = newURLPrefix + url;\r
+ }\r
+ }\r
+ //System.out.println("New url:" +url);\r
+ return url;\r
+ }\r
+ \r
+\r
+// NOTE: tried invoking ij directly, but had some problems,\r
+// so stick with calling utilMain(). \r
+// /**\r
+// ** Run the test case. Invokes IJ to do our\r
+// ** dirty work.\r
+// */\r
+// public void runMe(AppStreamWriter log, AppStreamWriter out, BufferedInputStream infile)\r
+// {\r
+// ASCII_UCodeESC_CharStream charStream;\r
+// ijTokenManager ijTokMgr;\r
+// ij ijParser;\r
+// \r
+// \r
+// out.println("--------------"+file+"-----------------");\r
+// charStream = new ASCII_UCodeESC_CharStream(in, 1, 1);\r
+// ijTokMgr = new ijTokenManager(charStream);\r
+// ijParser = new ij(ijTokMgr, System.out, this);\r
+// log.flush();\r
+// out.flush();\r
+// }\r
+\r
+ /**\r
+ ** Name says it all\r
+ */\r
+ public String toString()\r
+ {\r
+ return "name: "+name+\r
+ "\n\tfile: "+file+\r
+ "\n\tproperties: "+propFile+\r
+ "\n\tweight: "+weight+\r
+ "\n\tignoreErrors: "+ignoreErrors+\r
+ "\n\tdescription: "+description;\r
+ }\r
+\r
+ \r
+}\r