KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > harness > RunTest


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.harness.RunTest
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.derbyTesting.functionTests.harness;
23
24 import org.apache.derby.tools.sysinfo;
25 import org.apache.derby.tools.ij;
26 import org.apache.derby.iapi.reference.Attribute;
27
28 import java.io.File JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.InputStreamReader JavaDoc;
31 import java.io.ByteArrayInputStream JavaDoc;
32 import java.io.BufferedReader JavaDoc;
33 import java.io.FileReader JavaDoc;
34 import java.io.FileWriter JavaDoc;
35 import java.io.FileInputStream JavaDoc;
36 import java.io.FileOutputStream JavaDoc;
37 import java.io.BufferedOutputStream JavaDoc;
38 import java.io.BufferedWriter JavaDoc;
39 import java.io.FileWriter JavaDoc;
40 import java.io.OutputStreamWriter JavaDoc;
41 import java.io.PrintWriter JavaDoc;
42 import java.io.PrintStream JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.FileNotFoundException JavaDoc;
45 import java.lang.ClassNotFoundException JavaDoc;
46 import java.lang.ClassFormatError JavaDoc;
47 import java.lang.Thread JavaDoc;
48 import java.lang.reflect.Method JavaDoc;
49 import java.util.Enumeration JavaDoc;
50 import java.util.Locale JavaDoc;
51 import java.util.Vector JavaDoc;
52 import java.sql.Timestamp JavaDoc;
53 import java.sql.Connection JavaDoc;
54 import java.sql.SQLException JavaDoc;
55 import java.sql.DriverManager JavaDoc;
56 import java.util.Properties JavaDoc;
57 import java.util.StringTokenizer JavaDoc;
58 import java.net.URL JavaDoc;
59
60 import junit.framework.TestSuite;
61
62 public class RunTest
63 {
64
65     // For printing debug info
66
static boolean verbose=false;
67     // Under some circumstances, we may need to skip the test
68
static boolean skiptest = false;
69     static StringBuffer JavaDoc skiptestReason = new StringBuffer JavaDoc();
70     
71     //java requires / to look into jars, irrespective of OS
72
static final String JavaDoc testResourceHome = "/org/apache/derbyTesting/functionTests/";
73     
74     // Framework support
75
static String JavaDoc[] validFrameworks = {"embedded","",
76                        "DerbyNet","DerbyNetClient", "DB2jcc",
77                        "DB2app"};
78     static NetServer ns;
79     static boolean serverNeedsStopping = false; // used in controlling network server when useprocess=false:
80
static boolean jvmnet = false; // switch to see if we need have client & server in a different jvm
81
static String JavaDoc jvmnetjvm; // string for class name of server jvm if different from client jvm
82
static String JavaDoc driverName;
83     static String JavaDoc dbName;
84
85     // Test properties
86
static String JavaDoc jvmName = "currentjvm";
87     static String JavaDoc javaCmd;
88     static String JavaDoc javaVersion; // System.getProperty("java.version")
89
static String JavaDoc majorVersion;
90     static String JavaDoc minorVersion;
91     static int jccMajor;
92     static int jccMinor;
93     static int imajor;
94     static int iminor;
95     static boolean isjdk12test = false;
96     static String JavaDoc classpath = "";
97     static String JavaDoc classpathServer = "";
98     public static String JavaDoc framework = "embedded";
99     public static String JavaDoc J9_STATEMENTCACHESIZE = "20";
100
101     static String JavaDoc usesystem = "";
102     static String JavaDoc searchCP = "";
103     static boolean useCommonDB = false;
104     static boolean keepfiles = false;
105     static boolean useprocess = true;
106     static boolean systemdiff = false; // can set true if there is a system diff
107
static boolean upgradetest = false;
108     static boolean encryption = false; // requires jdk12ext plus encryptionProtocol
109
static boolean jdk12exttest = false; // requires jdk12ext
110
static boolean generateUTF8Out = false; // setting to create a utf8 encoded master file.
111
static String JavaDoc runningdir = ""; // where the tests are run and suppfiles placed
112
static String JavaDoc outputdir = ""; // user can specify as a property (optional)
113
static String JavaDoc canondir; // optional (to specify other than "master")
114
static String JavaDoc bootcp; // for j9 bootclasspath
115
static String JavaDoc canonpath; // special full path (will be platform dependent)
116
static String JavaDoc mtestdir = ""; // for MultiTest user must specify testdir
117
static String JavaDoc testSpecialProps = ""; // any special suite properties
118
static String JavaDoc testJavaFlags = ""; // special command line flags
119
static String JavaDoc jvmflags; // java special flags
120
static boolean reportstderr = true;
121     static int timeout = -1; // in case tests are hanging
122
public static String JavaDoc timeoutStr;
123     static String JavaDoc jarfile; // some tests have jar files (like upgrade)
124
static boolean skipsed = false;
125     static String JavaDoc commonDBHome = "testCSHome";
126     static boolean dbIsNew = true;
127     static String JavaDoc runwithjvm="true";
128     static boolean startServer=true; // should test harness start the server
129
static String JavaDoc hostName; // needs to be settable for ipv testing, localhost otherwise.)
130
static String JavaDoc testEncoding; // Encoding used for child jvm and to read the test output
131
static String JavaDoc upgradejarpath; // Encoding used for child jvm and to read the test output
132
static boolean replacePolicyFile=false; // property used to see if we need to replace the default policy file or append to it.
133

134     // Other test variables for directories, files, output
135
static String JavaDoc scriptName = ""; // testname as passed in
136
static String JavaDoc scriptFileName; // testname with extension
137
static String JavaDoc testDirName = ""; // test directory name
138
static String JavaDoc defaultPackageName = "/org/apache/derbyTesting/";
139         static String JavaDoc javaPath = "org.apache.derbyTesting."; // for java tests
140
static String JavaDoc testType; // sql, java, unit, etc.
141
static String JavaDoc testBase; // testname without extension
142
static String JavaDoc testOutName; // output name without path or extension (optional)
143
static String JavaDoc passFileName; // file listing passed tests
144
static String JavaDoc failFileName; // file listing failed tests
145
static String JavaDoc UTF8OutName; // file name for utf8 encoded out - not used for file comparison
146
static String JavaDoc tempMasterName; //file name for master, converted to local encoding, and for network server, corrected master
147
static File JavaDoc passFile;
148     static File JavaDoc failFile;
149     static String JavaDoc shutdownurl = "";
150     static boolean useOutput; // use output or assume .tmp file is produced?
151
static boolean outcopy; // copy support files to outDir rather than runDir
152
static String JavaDoc userdir; // current user directory
153
static char fileSep; // file separator for the system
154
static PrintWriter JavaDoc printWriter = null; // used to write test output to .tmp
155
static PrintWriter JavaDoc pwDiff = null; // for writing test output and info
156
static File JavaDoc script; // The file created for test files other than java tests
157
static File JavaDoc baseDir; // the DB base system dir
158
static boolean deleteBaseDir; // the DB base system dir
159
static File JavaDoc outDir; // test out dir
160
static File JavaDoc runDir; // where test is run and where support files are expected
161
static File JavaDoc canonDir; // allows setting master dir other than default
162
static File JavaDoc tmpOutFile; // tmp output file (before sed)
163
static File JavaDoc tempMasterFile; // master file copied into local encoding - with networkserver, also processed
164
static File JavaDoc stdOutFile; // for tests with useoutput false
165
static File JavaDoc finalOutFile; // final output file (after sed)
166
static File JavaDoc UTF8OutFile; // file name for out file copied into utf8 encoding
167
static File JavaDoc appPropFile; // testname_app.properties or default
168
static File JavaDoc clPropFile; // testname_derby.properties or default
169
static File JavaDoc diffFile; // To indicate diffs
170
static File JavaDoc tsuiteDir; // Final output dir for suite(s)
171
static File JavaDoc rsuiteDir; // Where to report .pass and .fail for suite(s)
172
static File JavaDoc extInDir; //Where all external test input files exist.
173
static File JavaDoc extOutDir; //Where all external test input files exist.
174
static File JavaDoc extInOutDir; //Where all external test input files exist.
175

176     // This test may be part of a suite
177
// (RunTest may need to know this is a suite to avoid dup output like sysinfo)
178
static String JavaDoc topsuitedir = ""; // in case of nested suites
179
static String JavaDoc topsuiteName = "";
180     static String JavaDoc topreportdir = "";
181     static String JavaDoc suiteName = "";
182     static boolean isSuiteRun = false;
183     static boolean lastTestFailed = false;
184
185     static boolean isI18N = false;
186     /** The value of derby.ui.codeset if it has been specified in the
187      * properties file. */

188     static String JavaDoc codeset = null;
189     static boolean junitXASingle = false;
190     
191     /**
192      * Run the test without a security manager. Hopefully
193      * should only be used in few cases. Though initially
194      * may be used to bypass problematic tests and get the
195      * remainder of the tests running with the security manager.
196      */

197     static boolean runWithoutSecurityManager;
198
199     static InputStream JavaDoc isSed = null; // For test_sed.properties // Cliff
200

201     public static void main(String JavaDoc[] args)
202         throws Exception JavaDoc
203     {
204         Locale.setDefault(Locale.US);
205         skiptestReason.setLength(0); // 0 out for useprocess
206
// Determine the test type
207
if (args.length == 0)
208         {
209             // No script name provided
210
System.out.println("no test name provided");
211             System.exit(1);
212         }
213         scriptName = args[0];
214
215         if (Boolean.getBoolean("listOnly"))
216         {
217             System.out.println("LISTONLY :" + scriptName);
218             return;
219         }
220
221         if ( (scriptName == null) || (scriptName.equals("")) )
222         {
223             System.out.println("Null or blank test script name.");
224             System.exit(1);
225         }
226         // If useprocess=false RunList calls this main method with 7 arguments...
227
if (args.length == 7)
228         {
229             defaultPackageName = args[1];
230             usesystem = args[2];
231             useprocess = false;
232             shutdownurl = args[4];
233             isSuiteRun = true;
234             suiteName = args[5];
235             //System.out.println("suiteName: " + suiteName);
236
framework=args[6];
237             // initializing startServer to true (which it would be if we'd
238
// run with useprocess=true) or network server will not get started
239
startServer=true;
240         }
241         
242         testType = scriptName.substring(scriptName.lastIndexOf(".") + 1);
243
244         verifyTestType();
245
246         // Get the properties for the test
247
Properties JavaDoc sp = System.getProperties();
248
249         // For useprocess=false, some system wide properties need to be reset
250
if (useprocess == false)
251         {
252             sp.put("useprocess", "false");
253             // Reset maximumDisplayWidth because some tests set this in app properties
254
// and when running in same process, can cause extra long lines and diffs
255
if ( sp.getProperty("maximumDisplayWidth") == null )
256                 sp.put("maximumDisplayWidth", "128");
257             if ( sp.getProperty("ij.defaultResourcePackage") != null )
258                 sp.put("ij.defaultResourcePackage", defaultPackageName);
259             System.setProperties(sp);
260         }
261         
262         JavaVersionHolder jvhs = getProperties(sp);
263         boolean isJDBC4 = jvhs.atLeast( 1, 6 );
264
265         // Setup the directories for the test and test output
266
setDirectories(scriptName,sp);
267
268         if (testDirName.startsWith("i18n")) {
269             isI18N=true;
270         }
271
272         // Check for properties files, including derby.properties
273
// and if needed, build the -p string to pass to the test
274
String JavaDoc propString = createPropString();
275         if ( (isSuiteRun == false) && (useprocess) )
276         {
277             SysInfoLog sysLog = new SysInfoLog();
278             sysLog.exec(jvmName, javaCmd, classpath, framework, pwDiff, useprocess);
279         }
280
281         String JavaDoc startTime = CurrentTime.getTime();
282         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
283         sb.append("*** Start: " + testBase + " jdk" + javaVersion + " ");
284         if ( (framework.length()>0) && (!framework.startsWith("embedded")) )
285             sb.append(framework + " ");
286         if ( (suiteName != null) && (suiteName.length()>0) )
287             sb.append(suiteName + " ");
288         sb.append(startTime + " ***");
289         System.out.println(sb.toString());
290         pwDiff.println(sb.toString());
291
292             // Run the Server if needed
293
if ((driverName != null) && (!skiptest) )
294         {
295             // before going further, get the policy file copied and if
296
// needed, modify it with the test's policy file
297
composePolicyFile();
298             String JavaDoc spacedJvmFlags = jvmflags;
299             // we now replace any '^' in jvmflags with ' '
300
if ((jvmflags != null) && (jvmflags.indexOf("^")>0))
301             {
302                 spacedJvmFlags = spaceJvmFlags(jvmflags);
303             }
304             
305             System.out.println("Initialize for framework: "+ framework );
306             if (jvmnet && framework.startsWith("DerbyNet"))
307             {
308                 // first check to see if properties were set to use a different jvm for server/client
309
String JavaDoc jvmnetjvm = System.getProperty("serverJvmName");
310                 if (jvmnetjvm == null)
311                 {
312                     // default to the latest one we know
313
jvmnetjvm = "j9_22";
314                 }
315
316                 ns = new NetServer(baseDir, jvmnetjvm, classpathServer, null,
317                                      spacedJvmFlags,framework, startServer);
318             }
319             else
320                 ns = new NetServer(baseDir, jvmName, classpathServer,
321                                      javaCmd, spacedJvmFlags,framework, startServer);
322
323             // With useprocess=true, we have a new dir for each test, and all files for
324
// the test, including a clean database, go in that directory. So, network server
325
// for each test runs in that dir, and stops when done. If the test's properties
326
// file has startServer=false the test will handle start/stop, otherwise the harness
327
// needs to start and stop the server.
328
// But with useprocess=false we're using the same directory and thus the same
329
// database, so there's little point in bouncing the server for each test in a suite,
330
// and it would slow the suite run.
331
// So, with useprocess=true, or if we're just running 1 test with useprocess=false,
332
// start network server and set serverNeedsStopping=true to have it stopped later;
333
// if useprocess=false and we're in a suite, start network server if it's not running
334
// and leave serverNeedsStopping=false, unless startServer=false, then,
335
// if network server is running, stop it.
336
if ((!useprocess) && (isSuiteRun))
337             {
338                 boolean started = false;
339                 try
340                 {
341                     started = ns.testNetworkServerConnection();
342                 }
343                 catch (Exception JavaDoc e) {} // ignore
344
if (!started && startServer)
345                     ns.start(); // start but don't stop, so not setting serverNeedsStopping
346
if (started && !startServer)
347                     ns.stop();
348             }
349             else
350             {
351                 ns.start();
352                 serverNeedsStopping = true;
353             }
354         }
355
356
357         
358         // If the test has a jar file (such as upgrade) unjar it
359
if (jarfile != null)
360         {
361             UnJar uj = new UnJar();
362             uj.unjar(jarfile, outDir.getCanonicalPath(), true);
363         }
364         
365         // Run the actual test (unless skiptest was set to true)
366
if (skiptest == false)
367         {
368             testRun(propString, sp);
369         }
370         else
371         {
372             if (skiptestReason.length() == 0)
373                 addSkiptestReason("Test skipped: skiptest set without setting skiptestReason, please fix RunTest.java...");
374             pwDiff.println(skiptestReason);
375             System.out.println(skiptestReason);
376             doCleanup(javaVersion);
377             return;
378         }
379             
380         // Stop the Network server if necessary
381
if (serverNeedsStopping)
382         {
383             ns.stop();
384         }
385
386         // Do "sed" to strip some unwanted stuff from the output file
387
// unless flag skipsed is set to true (for special cases)
388

389         String JavaDoc outName = finalOutFile.getPath();
390
391         if (skipsed)
392         {
393             tmpOutFile.renameTo(finalOutFile);
394         }
395         else
396         {
397                     try
398                     {
399                         Sed sed = new Sed();
400                         sed.exec(tmpOutFile,finalOutFile, isSed,
401                                         NetServer.isClientConnection(framework), isI18N, isJDBC4);
402             }
403             catch (ClassFormatError JavaDoc cfe)
404             {
405                 if (verbose) System.out.println("SED Error: " + cfe.getMessage());
406             }
407         }
408         // Now do a diff between the out and the master files
409
// Use the system's diff if systemdiff is true
410
String JavaDoc frameworkMaster = framework;
411         if (framework.startsWith("embedded"))
412             frameworkMaster = "";
413         FileCompare diff = new FileCompare();
414
415         if (verbose)
416         {
417             System.out.println(
418                 "About to execute: diff.exec(" +
419                 " outName = " + outName +
420                 ",outDir = " + outDir +
421                 ",pwDiff = " + pwDiff +
422                 ",testOutName = " + testOutName +
423                 ",frameworkMaster = " + frameworkMaster +
424                 ",jvmName = " + jvmName +
425                 ",iminor = " + iminor +
426                 ",useprocess = " + useprocess +
427                 ",systemdiff = " + systemdiff +
428                 ",canondir = " + canondir +
429                 ",canonpath = " + canonpath +
430                 ")\n");
431         }
432
433         boolean status;
434
435         // allow for server jvmName to be different from client jvmName
436
if (jvmnet)
437         {
438             // first check to see if properties were set to use a different jvm for server/client
439
if (jvmnetjvm == null)
440             {
441                 // default to the latest one we know
442
jvmnetjvm = "j9_22";
443             }
444             status = diff.exec(outName, outDir, pwDiff, testOutName,
445                 frameworkMaster, jvmName, iminor, useprocess, systemdiff, canondir,
446                 canonpath, jvmnetjvm);
447         }
448         else
449             status = diff.exec(outName, outDir, pwDiff, testOutName,
450                 frameworkMaster, jvmName, iminor, useprocess, systemdiff, canondir,
451                 canonpath, null);
452
453         if (status == true)
454         {
455             lastTestFailed = true;
456             pwDiff.println("Test Failed.");
457             System.out.println("Test Failed.");
458             keepfiles = true;
459             addToFailures(scriptName);
460             if (useCommonDB) {
461                 status = baseDir.delete();
462 //System.out.println("basedir delete status: " + status );
463
}
464         }
465         else
466         {
467             addToSuccesses(scriptName);
468             pwDiff.flush();
469         }
470
471         generateUTF8OutFile(finalOutFile);
472         
473         // Cleanup files
474
doCleanup(javaVersion);
475     }
476
477     private static void testRun(String JavaDoc propString, Properties JavaDoc sysProp)
478         throws FileNotFoundException JavaDoc, IOException JavaDoc, Exception JavaDoc
479     {
480         String JavaDoc systemHome = baseDir.getPath();
481         String JavaDoc scriptPath = null;
482         if (testType.startsWith("sql"))
483             scriptPath = script.getPath();
484                 
485         // cleanup for all tests that re-use standard testCSHome/wombat database
486
if (useCommonDB == true
487             && (usesystem == null || usesystem == "")
488             && (testType.equals("sql") || testType.equals("java")
489             || testType.equals("sql2"))) {
490             dbcleanup.doit(dbIsNew);
491         }
492     
493
494         // Create a process to execute the command unless useprocess is false
495
if ( useprocess )
496         {
497             // Build the test command
498
String JavaDoc[] testCmd =
499                 buildTestCommand(propString, systemHome, scriptPath);
500             execTestProcess(testCmd);
501         }
502         else
503         {
504             execTestNoProcess(sysProp, systemHome, propString, scriptPath);
505         }
506     }
507
508     /** This is the method which created directories and looks for script file,
509      * need to make OS specific paths here.
510      *
511      */

512     private static void setDirectories(String JavaDoc scriptName, Properties JavaDoc sp)
513         throws ClassNotFoundException JavaDoc, FileNotFoundException JavaDoc, IOException JavaDoc
514     {
515         // Get the current userdir
516
userdir = sp.getProperty("user.dir");
517         
518         // reset defaultPackageName (for useprocess=false)
519
if (useprocess == false)
520           defaultPackageName = "/org/apache/derbyTesting/";
521             
522         // reset defaultPackageName (for useCommonDB=true)
523
if (useCommonDB == true)
524         {
525             defaultPackageName = "/org/apache/derbyTesting/";
526         }
527             
528         // Set the resourceName from the default
529
// If not set by user, the default is used
530
String JavaDoc resourceName = defaultPackageName + "functionTests/tests/";
531         // scriptName could be of these two formats:
532
// testdir/test.testtype (testtype is sql, java, etc.)
533
// test.testtype (where the defaultPackageName includes the testdir)
534
int index = scriptName.lastIndexOf('/');
535         if (index == -1) // no test directory was specified
536
{
537             if ( (!testType.equals("sql")) && (!testType.equals("java")) && (!testType.equals("junit")))
538             {
539                 System.out.println("Test argument should be of the form: <dir>/<test>.<ext>");
540                 System.exit(1);
541             }
542             else
543             {
544                 scriptFileName = scriptName; // such as my.sql
545
resourceName += scriptName; // build the full resource name
546
}
547         }
548         else // the testdir was specified
549
{
550             testDirName = (index==0)?"":scriptName.substring(0,index);
551             //System.out.println("testDirName: " + testDirName);
552
scriptFileName = scriptName.substring(index+1, scriptName.length());
553             //System.out.println("scriptFileName: " + scriptFileName);
554
if (testType.equals("multi"))
555                 defaultPackageName = defaultPackageName + "functionTests/multi/" + testDirName + "/";
556             else
557                 defaultPackageName = defaultPackageName + "functionTests/tests/" + testDirName + "/";
558             //System.out.println("defaultPackage: " + defaultPackageName);
559
resourceName = defaultPackageName + scriptFileName;
560             //System.out.println("resource: " + resourceName);
561
}
562
563
564         // Get the test name without the extension
565
testBase = scriptFileName.substring(0, scriptFileName.lastIndexOf("."+testType));
566
567         if (testType.equals("java") || testType.equals("junit"))
568         {
569                     //get the javaPath
570
String JavaDoc tmp = defaultPackageName.replace('/', '.');
571                     int tl = tmp.length()-1;
572             javaPath = (tl==0)?"":tmp.substring(1, tl);
573         }
574
575         // Check for runDir
576
if ( (runningdir != null) && (runningdir.length()>0) )
577         {
578             if (File.separatorChar == '\\')
579             {
580                 //need to replace / in path with \ for windows
581
String JavaDoc runningdirWin = convertPathForWin(runningdir);
582                 runDir = new File JavaDoc((new File JavaDoc(runningdirWin)).getCanonicalPath());
583             }
584             else
585             {
586                 runDir = new File JavaDoc((new File JavaDoc(runningdir)).getCanonicalPath());
587             }
588         }
589
590         // Define the outDir if not already defined from properties
591
File JavaDoc tmpoutDir;
592         String JavaDoc userdirWin = null;
593             
594         if ( (outputdir == null) || (outputdir.length()==0) )
595         {
596             if (File.separatorChar == '\\')
597             {
598                 //need to replace / in path with \ for windows
599
userdirWin = convertPathForWin(userdir);
600                 tmpoutDir = new File JavaDoc((new File JavaDoc(userdirWin)).getCanonicalPath());
601             }
602             else
603             {
604                 tmpoutDir =
605                 new File JavaDoc((new File JavaDoc(userdir)).getCanonicalPath());
606             }
607         }
608         else
609         {
610             if (File.separatorChar == '\\')
611             {
612                 String JavaDoc outputdirWin = convertPathForWin(outputdir);
613                 tmpoutDir =
614                         new File JavaDoc((new File JavaDoc(outputdirWin)).getCanonicalPath());
615             }
616             else
617             {
618                 tmpoutDir =
619                         new File JavaDoc((new File JavaDoc(outputdir)).getCanonicalPath());
620             }
621         }
622
623         // If this is a suite run in a framework, outdir
624
// would already be defined to be a framework subdir
625
// But for RunTest, we must create the framework subdir
626
//if ( (!isSuiteRun) && (framework != null) && (framework.length()>0) )
627
if ( (!isSuiteRun) && (!framework.startsWith("embedded")) )
628         {
629             runDir = tmpoutDir;
630             outDir = new File JavaDoc(tmpoutDir, framework);
631             outDir.mkdir();
632         }
633         else // This is a Suite Run
634
{
635             outDir = tmpoutDir;
636             outDir.mkdir();
637             if ( (topsuitedir != null) && (topsuitedir.length()>0) )
638                     {
639                         if (File.separatorChar == '\\')
640                         {
641                             String JavaDoc topsuitedirWin = convertPathForWin(topsuitedir);
642                             tsuiteDir =
643                                 new File JavaDoc((new File JavaDoc(topsuitedirWin)).getCanonicalPath());
644                         }
645                         else
646                         {
647                             tsuiteDir = new File JavaDoc((new File JavaDoc(topsuitedir)).getCanonicalPath());
648                         }
649                     }
650             else
651                     {
652                 tsuiteDir = outDir;
653                     }
654             tsuiteDir.mkdir();
655             if ( (topreportdir != null) && (topreportdir.length()>0) )
656                     {
657                         if (File.separatorChar == '\\')
658                         {
659                             String JavaDoc topreportdirWin = convertPathForWin(topreportdir);
660                             rsuiteDir =
661                                 new File JavaDoc((new File JavaDoc(topreportdirWin)).getCanonicalPath());
662                         }
663                         else
664                         {
665                             rsuiteDir =
666                                 new File JavaDoc((new File JavaDoc(topreportdir)).getCanonicalPath());
667                         }
668                     }
669             else
670                     {
671                 rsuiteDir = outDir;
672                     }
673             rsuiteDir.mkdir();
674         }
675                 
676         fileSep = File.separatorChar;
677
678         // For multi tests, the user should have specified mtestdir (full path)
679
// unless this is a Suite, in which case outDir is used for mtestdir
680
if ( testType.equals("multi") )
681             if ( (mtestdir == null) || (mtestdir.length()==0) )
682                 // Use outDir for mtestdir
683
mtestdir = outDir.getPath();
684
685         // For certain test types, locate script file based on scriptName
686
// Then determine the actual test name and directory
687
if ( (!testType.equals("java")) &&
688              (!testType.equals("junit")) &&
689              (!testType.equals("unit")) &&
690              (!testType.equals("multi")) )
691         {
692             // NOTE: cannot use getResource because the urls returned
693
// are not the same between different java environments
694
InputStream JavaDoc is =
695                 loadTestResource("tests/" + testDirName + "/" + scriptFileName);
696
697             if (is == null)
698             {
699                 System.out.println("Could not locate " + scriptName);
700                 addToFailures(scriptName);
701                 throw new FileNotFoundException JavaDoc(resourceName);
702             }
703
704             // Read the test file and copy it to the outDir
705
// except for multi tests (for multi we just need to locate it)
706
BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is, "UTF-8"));
707             if (upgradetest)
708         
709                 //these calls to getCanonicalPath catch IOExceptions as a workaround to
710
//a bug in the EPOC jvm.
711
try { script = new File JavaDoc((new File JavaDoc(userdir, scriptFileName)).getCanonicalPath()); }
712                 catch (IOException JavaDoc e) {
713                     File JavaDoc f = new File JavaDoc(userdir, scriptFileName);
714                     FileWriter JavaDoc fw = new FileWriter JavaDoc(f);
715                     fw.close();
716                     script = new File JavaDoc(f.getCanonicalPath());
717                 }
718             // else is probably only multi test
719
else
720                 try { script = new File JavaDoc((new File JavaDoc(outDir, scriptFileName)).getCanonicalPath()); }
721                 catch (IOException JavaDoc e) {
722                     File JavaDoc f = new File JavaDoc(outDir, scriptFileName);
723                     FileWriter JavaDoc fw = new FileWriter JavaDoc(f);
724                     fw.close();
725                     script = new File JavaDoc(f.getCanonicalPath());
726                 }
727
728             PrintWriter JavaDoc pw = null;
729             pw = new PrintWriter JavaDoc( new BufferedWriter JavaDoc
730                 (new FileWriter JavaDoc(script.getPath()), 10000), true );
731                 
732             String JavaDoc str = "";
733             while ( (str = in.readLine()) != null )
734             {
735                 pw.println(str);
736             }
737             pw.close();
738             pw = null;
739             in = null;
740         }
741
742         // This is the base directory for creating a database (under the outDir)
743
baseDir = null;
744
745         if (useCommonDB == true)
746         {
747             if (File.separatorChar == '\\')
748             {
749                 String JavaDoc commonDBHomeWin = convertPathForWin(commonDBHome);
750                 baseDir = new File JavaDoc(userdirWin, commonDBHomeWin);
751             }
752             else
753             {
754             baseDir = new File JavaDoc(userdir, commonDBHome);
755             }
756         }
757         else if ( (!useprocess) && isSuiteRun && ((usesystem==null) || (usesystem.length()<=0)) )
758         {
759             String JavaDoc suite = (suiteName.substring(0,suiteName.indexOf(':')));
760             if (File.separatorChar == '\\')
761             {
762                 String JavaDoc useprWin = convertPathForWin(suite);
763                 baseDir = new File JavaDoc(outDir, useprWin);
764             }
765             else
766             {
767                 baseDir = new File JavaDoc(outDir, suite);
768             }
769         }
770         else if ( (usesystem != null) && (usesystem.length()>0) )
771         {
772             if (File.separatorChar == '\\')
773             {
774                 String JavaDoc usesystemWin = convertPathForWin(usesystem);
775                 if (upgradetest == true)
776                     baseDir = new File JavaDoc(userdirWin, usesystemWin);
777                 else
778                     baseDir = new File JavaDoc(outDir, usesystemWin);
779             }
780             else
781             {
782                 if (upgradetest == true)
783                     baseDir = new File JavaDoc(userdir, usesystem);
784                 else
785                     baseDir = new File JavaDoc(outDir, usesystem);
786             }
787         }
788         else
789         {
790             if (File.separatorChar == '\\')
791             {
792                 String JavaDoc testBaseWin = convertPathForWin(testBase);
793                 baseDir = new File JavaDoc(outDir, testBaseWin);
794             }
795             else
796             {
797                 baseDir = new File JavaDoc(outDir, testBase);
798             }
799         }
800
801         // clean up old db dirs
802
// (except for special cases such as nist, commonDB).
803
// In the case of useCommonDB == true, the baseDir (commonDBHome) only gets
804
// cleaned up if the last test was a failure. Further refinements may
805
// follow, since many test failures probably do not require such drastic
806
// action.
807
if (baseDir.exists())
808         {
809             if (useCommonDB == false || lastTestFailed == true) {
810                 cleanupBaseDir(baseDir);
811                 lastTestFailed = false;
812             }
813             else if (useCommonDB == true)
814                 dbIsNew = false; // dbcleanup may be needed
815
}
816         else {
817             
818             boolean created = baseDir.mkdir();
819             dbIsNew = true; // dbcleanup not needed on new database
820
}
821
822         // Determine if it is ok to delete base when done
823
if ( (usesystem == null) || (usesystem.length()<=0) )
824             deleteBaseDir = true; // ok to delete base when done
825
else
826             deleteBaseDir = false; // keep db dir for nist & puzzles in case of failures
827

828
829         // testOutName used to create the tmpOutFile
830
// this is probably always going to be testBase
831
if ( testOutName == null )
832         {
833             if (testType.equals("demo"))
834                 testOutName = testBase.substring(testBase.indexOf(".")+1);
835             else
836                 testOutName = testBase;
837         }
838
839         // Create a .tmp file for doing sed later to create testBase.out
840
tmpOutFile = new File JavaDoc(outDir, testOutName + ".tmp");
841         // Always create a.tmpmstr copy of the master file in local encoding.
842
// With network server, this gets adjusted for displaywidth
843
tempMasterName = testOutName+".tmpmstr";
844         UTF8OutName = testOutName+".utf8out";
845         // Define the .out file which will be created by massaging the tmp.out
846
finalOutFile = new File JavaDoc(outDir, testOutName + ".out");
847
848         // Define the .diff file which will contain diffs and other info
849
diffFile = new File JavaDoc(outDir, testOutName + ".diff");
850         stdOutFile = new File JavaDoc(outDir, testOutName + ".std");
851
852         // Define also the .pass and .fail files
853
if ( isSuiteRun )
854         {
855             String JavaDoc sname = suiteName.substring(0,suiteName.indexOf(":"));
856             //System.out.println("sname: " + sname);
857
//System.out.println("topsuiteName: " + topsuiteName);
858
passFileName = sname+".pass";
859             passFile = new File JavaDoc(rsuiteDir, passFileName);
860             failFileName = sname+".fail";
861             failFile = new File JavaDoc(rsuiteDir, failFileName);
862         }
863         else
864         {
865             passFileName=testBase+".pass";
866             passFile = new File JavaDoc(outDir, passFileName);
867             failFileName=testBase+".fail";
868             failFile = new File JavaDoc(outDir, failFileName);
869         }
870         //System.out.println("passFileName: " + passFileName);
871
boolean status = true;
872
873         // Delete any old .out or .tmp files
874
if (tmpOutFile.exists())
875             status = tmpOutFile.delete();
876         tempMasterFile = new File JavaDoc(outDir, tempMasterName);
877         if (tempMasterFile.exists())
878             status = tempMasterFile.delete();
879         if (finalOutFile.exists())
880             status = finalOutFile.delete();
881         if (diffFile.exists())
882             status = diffFile.delete();
883         if (stdOutFile.exists())
884             status = stdOutFile.delete();
885         UTF8OutFile = new File JavaDoc (outDir, UTF8OutName);
886         if (UTF8OutFile.exists())
887             status = UTF8OutFile.delete();
888
889         // Delete any old pass or fail files
890
if (!isSuiteRun)
891         {
892             if (failFile.exists())
893                 status = failFile.delete();
894             if (passFile.exists())
895                 status = passFile.delete();
896         }
897
898         if (status == false)
899             System.out.println("Unable to delete tmp, out and/or diff files to start");
900
901         // Create a PrintWriter for writing env and test info to the diff file
902
pwDiff = new PrintWriter JavaDoc
903             (new BufferedWriter JavaDoc(new FileWriter JavaDoc(diffFile.getPath()), 4096), true);
904     }
905
906     private static JavaVersionHolder getProperties(Properties JavaDoc sp)
907         throws Exception JavaDoc
908     {
909         // Get any properties specified on the command line
910

911         // before doing anything else, get jvmflags, evaluate any -D
912
// see if there is anything useful to the test harness in jvmflags
913
if ((jvmflags != null) && (jvmflags.length() > 0))
914         {
915             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(jvmflags,"^");
916             while (st.hasMoreTokens())
917             {
918                 String JavaDoc tmpstr = st.nextToken();
919                 if ((tmpstr.indexOf("=")> 0) && (tmpstr.startsWith("-D")))
920                 {
921                     // strip off the '-D'
922
String JavaDoc key = tmpstr.substring(2, tmpstr.indexOf("="));
923                     String JavaDoc value = tmpstr.substring((tmpstr.indexOf("=") +1), tmpstr.length());
924                     sp.put(key, value);
925                 }
926             }
927         }
928         
929         searchCP = sp.getProperty("ij.searchClassPath");
930         String JavaDoc frameworkp = sp.getProperty("framework");
931         if (frameworkp != null)
932             framework = frameworkp;
933         if (framework == null)
934             framework = "embedded";
935         if (!verifyFramework(framework))
936             framework = "";
937         else
938             driverName = NetServer.getDriverName(framework);
939         String JavaDoc junitXAProp = sp.getProperty ("derbyTesting.xa.single");
940         if (junitXAProp != null && junitXAProp.equals ("true")) {
941             junitXASingle = true;
942         }
943         hostName = sp.getProperty("hostName");
944         // force hostName to localhost if it is not set
945
if (hostName == null)
946            hostName="localhost";
947         
948         String JavaDoc generateUTF8OutProp = sp.getProperty("generateUTF8Out");
949         if (generateUTF8OutProp != null && generateUTF8OutProp.equals("true"))
950             generateUTF8Out = true;
951         
952         // Some tests will not work with some frameworks,
953
// so check suite exclude files for tests to be skipped
954
String JavaDoc skipFile = framework + ".exclude";
955         if (!framework.equals(""))
956         {
957             skiptest = (SkipTest.skipIt(skipFile, scriptName));
958             // in addition, check to see if the test should get skipped
959
// because it's not suitable for a remotely started server
960
if (!skiptest)
961             {
962                 if (!hostName.equals("localhost"))
963                 {
964                     skipFile = framework + "Remote.exclude";
965                     skiptest = (SkipTest.skipIt(skipFile, scriptName));
966                 }
967             }
968             if (skiptest) // if we're skipping...
969
addSkiptestReason("Test skipped: listed in " + skipFile +
970                      " file, skipping test: " + scriptName);
971         }
972         
973         jvmName = sp.getProperty("jvm");
974
975         //System.out.println("jvmName is: " + jvmName);
976
if ( (jvmName == null) || (jvmName.length()==0) || (jvmName.equals("jview")))
977         {
978             javaVersion = System.getProperty("java.version");
979             //System.out.println("javaVersion is: " + javaVersion);
980
}
981         else
982             javaVersion = jvmName;
983
984         //hang on a minute - if j9, we need to check further
985
String JavaDoc javavmVersion;
986         if (sp.getProperty("java.vm.name").equals("J9"))
987             javavmVersion = (sp.getProperty("java.vm.version"));
988         else
989             javavmVersion = javaVersion;
990
991
992         JavaVersionHolder jvh = new JavaVersionHolder(javavmVersion);
993         majorVersion = jvh.getMajorVersion();
994         minorVersion = jvh.getMinorVersion();
995         iminor = jvh.getMinorNumber();
996         imajor = jvh.getMajorNumber();
997
998         if ( (jvmName == null) || (!jvmName.equals("jview")) )
999         {
1000            if ( (iminor < 2) && (imajor < 2) )
1001                jvmName = "currentjvm";
1002            else
1003            {
1004                if (System.getProperty("java.vm.vendor").startsWith("IBM"))
1005                {
1006                    if (System.getProperty("java.vm.name").equals("J9"))
1007                    {
1008                        if (System.getProperty("com.ibm.oti.configuration").equals("foun10"))
1009                        {
1010                            jvmName = "j9_foundation";
1011                        }
1012                        else
1013                        {
1014                            // for reporting; first extend javaVersion
1015
javaVersion = javaVersion + " - " + majorVersion + "." + minorVersion;
1016                            // up to j9 2.1 (jdk 1.3.1 subset) the results are the same for all versions,
1017
// or we don't care about it anymore. Switch back to 1.3. (java.version) values.
1018
if ((imajor <= 2) && (iminor < 2))
1019                            {
1020                                majorVersion = "1";
1021                                minorVersion = "3";
1022                                imajor = 1;
1023                                iminor = 3;
1024                            }
1025                            jvmName = "j9_" + majorVersion + minorVersion;
1026                        }
1027                    }
1028                    else
1029                        jvmName = "ibm" + majorVersion + minorVersion;
1030                }
1031                else
1032            jvmName = "jdk" + majorVersion + minorVersion;
1033            }
1034        }
1035
1036        // create a JavaVersionHolder for the java.specification.version -
1037
// used to control Sed-ing for JDBC4 & up
1038
String JavaDoc specversion = (sp.getProperty("java.specification.version"));
1039        JavaVersionHolder jvhs = new JavaVersionHolder(specversion);
1040
1041        testEncoding = sp.getProperty("derbyTesting.encoding");
1042        upgradejarpath = sp.getProperty("derbyTesting.jar.path");
1043        if ((testEncoding != null) && (!jvmName.equals("jdk15")))
1044        {
1045            skiptest = true;
1046            addSkiptestReason("derbyTesting.encoding can only be used with jdk15, skipping test");
1047        }
1048        
1049        String JavaDoc replace_policy = sp.getProperty("derbyTesting.replacePolicyFile");
1050        if ((replace_policy != null) && (replace_policy.equals("true")))
1051            replacePolicyFile=true;
1052        else
1053            replacePolicyFile=false;
1054        
1055        javaCmd = sp.getProperty("javaCmd");
1056        bootcp = sp.getProperty("bootcp");
1057        jvmflags = sp.getProperty("jvmflags");
1058        testJavaFlags = sp.getProperty("testJavaFlags");
1059        classpath = sp.getProperty("classpath");
1060        //System.out.println("classpath set to: " + classpath);
1061
classpathServer = sp.getProperty("classpathServer");
1062        if ( (classpathServer == null) || (classpathServer.startsWith("${")) )
1063            classpathServer = classpath;
1064        //System.out.println("classpathServer set to: " + classpathServer);
1065
jarfile = sp.getProperty("jarfile");
1066        String JavaDoc upg = sp.getProperty("upgradetest");
1067        if (upg != null)
1068        {
1069            upg = upg.toLowerCase();
1070            if (upg.equals("true"))
1071                upgradetest = true;
1072        }
1073    
1074        if ( framework.equals("DerbyNet") && (! jvmName.equals("j9_foundation")))
1075        {
1076
1077            Class JavaDoc c = null;
1078            Method JavaDoc m = null;
1079            Object JavaDoc o = null;
1080            Integer JavaDoc i = null;
1081            try
1082            {
1083                c = Class.forName("com.ibm.db2.jcc.DB2Driver");
1084                o = c.newInstance();
1085                m = c.getMethod("getMajorVersion", null);
1086                i = (Integer JavaDoc)m.invoke(o, null);
1087                jccMajor = i.intValue();
1088                m = c.getMethod("getMinorVersion", null);
1089                i = (Integer JavaDoc)m.invoke(o, null);
1090                jccMinor = i.intValue();
1091            } catch (ClassNotFoundException JavaDoc e) {}
1092
1093            String JavaDoc excludeJcc = sp.getProperty("excludeJCC");
1094            try {
1095                RunList.checkClientExclusion(excludeJcc, "JCC", jccMajor, jccMinor, javaVersion);
1096            } catch (Exception JavaDoc e) {
1097                skiptest = true;
1098                addSkiptestReason(e.getMessage());
1099            }
1100        }
1101            
1102        String JavaDoc sysdiff = sp.getProperty("systemdiff");
1103        if (sysdiff != null)
1104        {
1105            sysdiff = sysdiff.toLowerCase();
1106            if (sysdiff.equals("true"))
1107                systemdiff = true;
1108        }
1109        String JavaDoc keep = sp.getProperty("keepfiles");
1110        if (keep != null)
1111        {
1112            keep = keep.toLowerCase();
1113            if (keep.equals("true"))
1114                keepfiles = true;
1115        }
1116        String JavaDoc encrypt = sp.getProperty("encryption");
1117        if ( (encrypt != null) && (encrypt.equalsIgnoreCase("true")) )
1118            encryption = true;
1119        String JavaDoc jdk12ext = sp.getProperty("jdk12exttest");
1120        if ( (jdk12ext != null) && (jdk12ext.equalsIgnoreCase("true")) )
1121            jdk12exttest = true; // applied to jdk12 or higher
1122
if ( encryption || jdk12exttest )
1123        {
1124            // Must be running jdk12 or higher and must have extensions
1125
if ( iminor < 2 ) // this is 1.1.x
1126
{
1127                skiptest = true;
1128                addSkiptestReason("Test skipped: encryption or jdk12exttest requires jdk12 or higher; this is jdk1"+iminor+", skipping test: " + scriptFileName);
1129            }
1130            else // now check for extensions
1131
{
1132                try
1133                {
1134                    Class JavaDoc jtaClass = Class.forName("javax.transaction.xa.Xid");
1135                }
1136                catch (ClassNotFoundException JavaDoc cnfe)
1137                {
1138                    // at least one of the extension classes was not found
1139
skiptest = true;
1140                    addSkiptestReason("Test skipped: javax.transaction.xa.Xid not found, skipping test: " + scriptFileName);
1141                }
1142                try
1143                {
1144                    Class JavaDoc jdbcClass = Class.forName("javax.sql.RowSet");
1145                }
1146                catch (ClassNotFoundException JavaDoc cnfe2)
1147                {
1148                    // at least one of the extension classes was not found
1149
skiptest = true;
1150                    addSkiptestReason("Test skipped: javax.sql.RowSet not found, skipping test: " + scriptFileName);
1151                }
1152            }
1153        }
1154        runningdir = sp.getProperty("rundir");
1155        if (runningdir == null)
1156            runningdir = "";
1157        outputdir = sp.getProperty("outputdir");
1158        if (outputdir == null)
1159            outputdir = "";
1160        
1161        canondir = sp.getProperty("canondir");
1162        canonpath = sp.getProperty("canonpath");
1163
1164        testOutName = sp.getProperty("testoutname");
1165        useOutput = new Boolean JavaDoc(sp.getProperty("useoutput","true")).booleanValue();
1166        outcopy = new Boolean JavaDoc(sp.getProperty("outcopy","false")).booleanValue();
1167        mtestdir = sp.getProperty("mtestdir"); // used by multi tests
1168
if (mtestdir == null)
1169            mtestdir = "";
1170            
1171        String JavaDoc usepr = sp.getProperty("useprocess");
1172        if (usepr != null)
1173        {
1174            usepr = usepr.toLowerCase();
1175            if (usepr.equals("false"))
1176                useprocess = false;
1177            else
1178                useprocess = true;
1179        }
1180        else
1181            useprocess = true;
1182        
1183        
1184        // if the hostName is something other than localhost, we must
1185
// be trying to connect to a remote server, and so,
1186
// startServer should be false.
1187
if (!hostName.equals("localhost"))
1188        {
1189                startServer=false;
1190        }
1191        
1192        String JavaDoc nosed = sp.getProperty("skipsed");
1193        if (nosed != null)
1194        {
1195            nosed = nosed.toLowerCase();
1196            if (nosed.equals("true"))
1197                skipsed = true;
1198        }
1199
1200            
1201        String JavaDoc dbug = sp.getProperty("verbose");
1202        if (dbug != null)
1203        {
1204            dbug = dbug.toLowerCase();
1205            if (dbug.equals("true"))
1206                verbose = true;
1207        }
1208        String JavaDoc rstderr = sp.getProperty("reportstderr");
1209        if (rstderr != null)
1210        {
1211            rstderr = rstderr.toLowerCase();
1212            if (rstderr.equals("false"))
1213                reportstderr = false;
1214        }
1215
1216        // default to -1 (no timeout) if no property is set
1217
if (timeoutStr == null)
1218        {
1219            timeoutStr = sp.getProperty("timeout", "-1");
1220            //System.out.println("+++setting timeoutStr to " + timeoutStr + " in RunTest::getProperties");
1221
}
1222        else
1223        {
1224            //System.out.println("+++timeoutStr was already " + timeoutStr + " in RunTest::getProperties");
1225
}
1226        
1227        try
1228        {
1229            timeout = Integer.parseInt(timeoutStr);
1230        }
1231        catch (NumberFormatException JavaDoc nfe)
1232        {
1233            timeout = -1;
1234        }
1235        
1236        //System.out.println("RunTest timeout is: " + timeout);
1237

1238        testSpecialProps = sp.getProperty("testSpecialProps");
1239        
1240        if (useprocess)
1241        {
1242            String JavaDoc defrespckg = sp.getProperty("ij.defaultResourcePackage");
1243            if (defrespckg != null) // if not set there is a default defined
1244
{
1245                defaultPackageName = defrespckg;
1246                if (!defaultPackageName.endsWith("/"))
1247                    defaultPackageName += "/";
1248            }
1249            usesystem = sp.getProperty("usesystem");
1250        }
1251
1252        // junit tests do not run successfully with j9 - except for j9_foundation
1253
// see DERBY-1785
1254
if ((jvmName.startsWith("j9")) && (!jvmName.equals("j9_foundation")) && testType.equals("junit"))
1255        {
1256            skiptest = true;
1257            addSkiptestReason("Test skipped: cannot run junit test with j9 jvms because of security manager issue in the jvm");
1258        }
1259            
1260
1261        // Some tests will not run well in a suite with use process false
1262
// with some frameworks, so skip
1263
if (!useprocess && !skiptest )
1264        {
1265            String JavaDoc tsuiteName = null;
1266            if (suiteName != null)
1267                tsuiteName = suiteName;
1268            else
1269                tsuiteName = sp.getProperty("suitename");
1270            if ( (tsuiteName != null) && (tsuiteName.length()>0) )
1271            {
1272                skipFile = framework + "Useprocess.exclude";
1273                if (!framework.equals(""))
1274                {
1275                    skiptest = (SkipTest.skipIt(skipFile, scriptName));
1276                    if (skiptest)
1277                    {
1278                        skiptest=true;
1279                        addSkiptestReason("Test " + scriptName + " skipped, " +
1280                           "listed in " + framework + "Useprocess.exclude file.");
1281                    }
1282                }
1283            }
1284        }
1285        
1286        if ( (useprocess) || (suiteName == null) )
1287        {
1288            if (useprocess)
1289                suiteName = sp.getProperty("suitename");
1290            if ( (suiteName != null) && (suiteName.length()>0) )
1291            {
1292                // This is a suite run
1293
isSuiteRun = true;
1294                if (useprocess)
1295                    // If a suite, it could be part of a top suite
1296
topsuiteName = sp.getProperty("topsuitename");
1297                topsuitedir = sp.getProperty("topsuitedir");
1298                topreportdir = sp.getProperty("topreportdir");
1299            }
1300        }
1301        String JavaDoc uscdb = sp.getProperty("useCommonDB");
1302        if (uscdb != null && uscdb.equals("true"))
1303            useCommonDB = true;
1304
1305        return jvhs;
1306    }
1307
1308    private static String JavaDoc createPropString()
1309        throws ClassNotFoundException JavaDoc, FileNotFoundException JavaDoc, IOException JavaDoc
1310    {
1311        // Check for existence of app properties and/or derby.properties files
1312
// Copy the derby.properties to the db base directory
1313
// Then create the -p string for the test
1314
String JavaDoc propString = "";
1315        
1316        // General purpose variables
1317
BufferedReader JavaDoc in = null;
1318        BufferedOutputStream JavaDoc bos = null;
1319        BufferedOutputStream JavaDoc bos2 = null;
1320        String JavaDoc str = "";
1321        
1322        // InputStreams for all possible properties files
1323
InputStream JavaDoc isCl = null; // For test_derby.properties
1324
InputStream JavaDoc isClDef = null; // For default_derby.properties
1325
InputStream JavaDoc isAp = null; // For test_app.properties
1326
InputStream JavaDoc isApDef = null; // For default_app.properties
1327

1328        // Resource names for locating the various properties files
1329
String JavaDoc clDefProp = "tests/" + testDirName + "/" + "default_derby.properties";
1330        String JavaDoc apDefProp = null;
1331        if (useCommonDB)
1332            apDefProp = "tests/" + testDirName + "/" + "commonDB_app.properties";
1333        else
1334            apDefProp = "tests/" + testDirName + "/" + "default_app.properties" ;
1335
1336        // Properties
1337
Properties JavaDoc clp = new Properties JavaDoc();
1338        Properties JavaDoc ap = new Properties JavaDoc();
1339
1340        // If there are special flags for ij or server, load these
1341
// into properties to be merged with app and/or derby props
1342
Properties JavaDoc ijProps = new Properties JavaDoc();
1343        Properties JavaDoc srvProps = new Properties JavaDoc();
1344        if ( (testSpecialProps != null) && (testSpecialProps.length()>0))
1345        {
1346            SpecialFlags.parse(testSpecialProps, ijProps, srvProps);
1347        }
1348
1349        /* If there are more than one derby.properties, the rule is to
1350           load either the test_derby.properties or the default one,
1351        */

1352        
1353        // Check for default_derby.properties
1354
isClDef = loadTestResource(clDefProp);
1355        // Check for test specific props
1356
isCl = loadTestResource("tests/" + testDirName + "/" + testBase + "_derby.properties");
1357//System.out.println("**************");
1358
//System.out.println("isCl = " + isCl);
1359
//System.out.println(defaultPackageName + testBase + "_derby.properties");
1360
//System.out.println("**************");
1361

1362        // Now load and merge the properties based on above rules
1363
if (isCl != null) // In case there exists a test_derby.properties
1364
{
1365            clp.load(isCl);
1366clp.list(System.out);
1367        }
1368        // Try the default_derby.properties instead
1369
else if (isClDef != null)
1370        {
1371            clp.load(isClDef);
1372        }
1373
1374        // j9 will run out of memory with the default cache size (100), so
1375
// forcing it lower unless set in _derby.properties file for a specific test
1376
if (jvmName.startsWith("j9"))
1377        {
1378            if (clp.getProperty("derby.language.statementCacheSize")==null)
1379                clp.put("derby.language.statementCacheSize", J9_STATEMENTCACHESIZE);
1380        }
1381
1382        // Now merge any special server props if they exist
1383
// But if clp is still empty, try using the special server props
1384
if ( clp.isEmpty() )
1385        {
1386            // Check for srvProps from testSpecialProps
1387
if ( !srvProps.isEmpty() )
1388                clp = srvProps;
1389        }
1390        else
1391        {
1392            // merge any special properties from testSpecialProps
1393
if ( !srvProps.isEmpty() )
1394            {
1395                for (Enumeration JavaDoc e = srvProps.propertyNames(); e.hasMoreElements();)
1396                {
1397                    String JavaDoc key = (String JavaDoc)e.nextElement();
1398                    String JavaDoc value = srvProps.getProperty(key);
1399                    if (key.equals("derby.debug.true")) // Add to existing prop
1400
{
1401                        String JavaDoc cval = clp.getProperty("derby.debug.true");
1402                        // If this property exists, edit to prepend the srvProp
1403
// but if the original property is null, just put the srvProp
1404
if (cval != null)
1405                        {
1406                            if (cval.length() != 0) {
1407                                // debug property exists, so edit it
1408
value = value + "," + cval;
1409                            } else {
1410                                // if new debug property is not null, but is zero length,
1411
// assume the intention was to override the debug property.
1412
value = "";
1413                            }
1414                        }
1415                    }
1416                    clp.put(key,value);
1417                }
1418            }
1419        }
1420
1421        if ( !clp.isEmpty() )
1422        {
1423            // Create and load the file
1424

1425            // This call to getCanonicalPath catches IOExceptions as a workaround to
1426
// a bug in the EPOC jvm.
1427
try { clPropFile = new File JavaDoc((new File JavaDoc(baseDir, "derby.properties")).getCanonicalPath()); }
1428            catch (IOException JavaDoc e) {
1429                File JavaDoc f = new File JavaDoc(baseDir, "derby.properties");
1430                FileWriter JavaDoc fw = new FileWriter JavaDoc(f);
1431                fw.close();
1432                clPropFile = new File JavaDoc(f.getCanonicalPath());
1433            }
1434
1435//System.out.println("clPropFile: " + clPropFile.getPath());
1436
bos = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(clPropFile));
1437            clp.store(bos, "Derby Properties");
1438            bos.close();
1439        }
1440
1441        // ---------------------------------
1442
// Check for existence of sed properties file (test_sed.properties)
1443
// See jdbc_sed.properties
1444
// Multiple patterns for DELETE: comma separated
1445
// delete=pattern1,pattern2,...,patternn
1446
// No commas can be allowed in the patterns.
1447
//
1448
// Multiple patterns for SUBSTITUTE: comma separated <pattern;substitute> pair
1449
// substitute=pattern1;substitute1,pattern2;substitute2,...,patternn;substituten
1450
// No commas or semicolons can be allowed in the patterns/subsitutes. //
1451

1452        if ( testType.equals("multi") )
1453            isSed = loadTestResource("multi/stress/" + testBase + "_sed.properties");
1454        else
1455            isSed = loadTestResource("tests/" + testDirName + "/" + testBase + "_sed.properties");
1456//System.out.println("**************");
1457
//System.out.println("isSed = " + isSed);
1458
//System.out.println(defaultPackageName + testBase + "_sed.properties");
1459
//System.out.println("**************");
1460
// ---------------------------------
1461

1462        // Check for existence of app properties file
1463
// If there is an test_app, use it to overwrite default_app
1464
// Then create the -p string for the test
1465
Properties JavaDoc dp = new Properties JavaDoc();
1466        String JavaDoc testPropName = null;
1467        String JavaDoc testPropSDName = null; // name for shutdown properties file if needed
1468
if (useCommonDB) testPropName = "CDB" + testBase + "_app.properties";
1469        else testPropName = testBase + "_app.properties";
1470
1471        // Check for default_app.properties
1472
isApDef = loadTestResource(apDefProp);
1473
1474        // Check for test_app.properties
1475

1476        if ( testType.equals("multi") )
1477            isAp = loadTestResource("multi/" + testDirName + "/" + testBase + "_app.properties");
1478        else
1479            isAp = loadTestResource("tests/" + testDirName + "/" + testBase + "_app.properties");
1480//System.out.println("**************");
1481
//System.out.println("isAp = " + isAp);
1482
//System.out.println(defaultPackageName + testBase + "_app.properties");
1483
//System.out.println("**************");
1484

1485
1486        // Try loading the ap and def properties if they exist
1487
// Merge only if the test's app properties has usedefaults property
1488
if ( isAp != null )
1489        {
1490            ap.load(isAp);
1491            // Check for a property usedefaults; if true merge in default props
1492
for (Enumeration JavaDoc e = ap.propertyNames(); e.hasMoreElements(); )
1493            {
1494                String JavaDoc key = (String JavaDoc)e.nextElement();
1495                String JavaDoc value = ap.getProperty(key);
1496                if ( (key.equals("usedefaults")) && (value.equals("true")) )
1497                {
1498                    // merge in the default properties
1499
if ( isApDef != null )
1500                    {
1501                        dp.load(isApDef);
1502                        mergeProps(ap, dp);
1503                        break;
1504                    }
1505                }
1506            }
1507        }
1508        else
1509        {
1510            // Just use the default props
1511
if ( isApDef != null )
1512                ap.load(isApDef);
1513        }
1514
1515        // If app props are still empty, check for any special testSpecialProps
1516
if ( ap.isEmpty() )
1517        {
1518            if ( !ijProps.isEmpty() )
1519                ap = ijProps;
1520        }
1521        else
1522        {
1523            // merge any special properties from testSpecialProps
1524
if ( !ijProps.isEmpty() )
1525            {
1526                for (Enumeration JavaDoc e = ijProps.propertyNames(); e.hasMoreElements();)
1527                {
1528                    String JavaDoc key = (String JavaDoc)e.nextElement();
1529                    String JavaDoc value = ijProps.getProperty(key);
1530                    ap.put(key,value);
1531                }
1532            }
1533        }
1534
1535        if ( !ap.isEmpty() )
1536        {
1537                // Create the file and load the properties
1538
// This call to getCanonicalPath catches IOExceptions as a workaround to
1539
// a bug in the EPOC jvm.
1540
try
1541            {
1542                appPropFile = new File JavaDoc((new File JavaDoc(baseDir, testPropName)).getCanonicalPath());
1543            }
1544            catch (IOException JavaDoc e)
1545            {
1546                File JavaDoc f = new File JavaDoc(baseDir, testPropName);
1547                FileWriter JavaDoc fw = new FileWriter JavaDoc(f);
1548                fw.close();
1549                appPropFile = new File JavaDoc(f.getCanonicalPath());
1550            }
1551
1552            // For IBM14 the console encoding is different from the platform
1553
// encoding on windows. We want it to be the same for our
1554
// test output like the other JDK's.
1555
//
1556
// For i18n test, we want UTF-8 encoding (DERBY-244).
1557
String JavaDoc conEnc = System.getProperty("console.encoding");
1558            String JavaDoc fileEnc = System.getProperty("file.encoding");
1559        
1560            if (ap.getProperty("derby.ui.codeset") != null) {
1561                // derby.ui.codeset is specified explicitly, don't override
1562
codeset = ap.getProperty("derby.ui.codeset");
1563            } else if (isI18N) {
1564                ap.put("derby.ui.codeset", "UTF-8");
1565            } else if ((conEnc != null) && (fileEnc != null ) &&
1566                conEnc.startsWith("Cp850"))
1567            {
1568                ap.put("derby.ui.codeset",fileEnc);
1569            }
1570
1571            if (verbose)
1572                System.out.println("console.encoding:" + conEnc +
1573                                   " file.encoding:" + fileEnc +
1574                               " derby.ui.codeset: " + ap.getProperty("derby.ui.codeset"));
1575            
1576            // If the initial connection is being specified as a DataSource
1577
// on the command line using -Dij.dataSource=<dsclassname>
1578
// then remove the ij.database property that comes from any
1579
// default_app or other properties file. This is because the
1580
// ij.database will override the ij.dataSource property.
1581
if (System.getProperty("ij.dataSource") != null)
1582            {
1583                ap.remove("ij.database");
1584                ap.remove("ij.protocol");
1585            }
1586        
1587//System.out.println("appPropFile: " + appPropFile.getPath());
1588
bos = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(appPropFile));
1589            ap.store(bos, "App Properties");
1590            bos.close();
1591            
1592            // Check now through jvmflags for insteresting properties
1593
// First grab jvmflags from _app.properties for the jvm process cannot
1594
// use it if just in the test's _app.properties file
1595
// note that it's already too late if useprocess is false
1596
String JavaDoc apppropsjvmflags = ap.getProperty("jvmflags");
1597            if (apppropsjvmflags != null)
1598            {
1599                if (jvmflags != null)
1600                    jvmflags = apppropsjvmflags + "^" + jvmflags;
1601                else
1602                    jvmflags = apppropsjvmflags;
1603            }
1604            // see if there is anything useful for the test harness in jvmflags
1605
// from commandline or suite properties
1606
if ((jvmflags != null) && (jvmflags.length() > 0))
1607            {
1608                StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(jvmflags,"^");
1609                while (st.hasMoreTokens())
1610                {
1611                    
1612                    String JavaDoc tmpstr = st.nextToken();
1613                    if ((tmpstr.indexOf("=")> 0) && (tmpstr.startsWith("-D")))
1614                    {
1615                        // start at position 2, i.e. strip off the "-D"
1616
String JavaDoc key = tmpstr.substring(2, tmpstr.indexOf("="));
1617                        String JavaDoc value = tmpstr.substring((tmpstr.indexOf("=")+1), tmpstr.length());
1618                        ap.put(key, value);
1619                    }
1620                }
1621            }
1622
1623            // Depending on the framework, the app prop file may need editing
1624
if ( (framework.length()>0) || (encryption) )
1625            {
1626                try
1627                {
1628                    if (!framework.equals("") &&
1629                        !framework.equals("embedded"))
1630                        frameworkProtocol(ap);
1631                    else if (encryption)
1632                        encryptionProtocol(ap);
1633                }
1634                catch(Exception JavaDoc e)
1635                {
1636                    System.out.println("Exception: " + e.getMessage());
1637                    e.printStackTrace();
1638                }
1639        
1640                // write the new properties to the appPropFile
1641
appPropFile = new File JavaDoc(baseDir, testBase + "_app.properties");
1642                try
1643                {
1644                    bos = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(appPropFile));
1645                    ap.store(bos, "Test Properties");
1646                    bos.close();
1647                }
1648                catch(IOException JavaDoc ioe)
1649                {
1650                    System.out.println("IOException creating prop file: " + ioe.getMessage());
1651                }
1652            }
1653        }
1654
1655        if ( (appPropFile != null) && (appPropFile.exists()) )
1656        {
1657            // Create the properties string for the test
1658
propString = appPropFile.getPath();
1659            
1660            // Check for shutdown url
1661
shutdownurl = ap.getProperty("shutdown");
1662            
1663            // Check for jdk12test set to true
1664
String JavaDoc jdk12test = ap.getProperty("jdk12test");
1665            //System.out.println("jdk12test: " + jdk12test);
1666
//System.out.println("jvmName: " + jvmName);
1667
if (jdk12test != null)
1668            {
1669                if (jdk12test.toLowerCase().equals("true"))
1670                {
1671                    isjdk12test = true;
1672                    //System.out.println("isjdk12test " + isjdk12test);
1673
if (jvmName.equals("currentjvm")) { // This is not at least jdk12
1674
skiptest = true;
1675                        addSkiptestReason("Test skipped: test needs jdk12, jvm is reporting currentjvm; skipping test: " + scriptFileName);
1676                    }
1677                }
1678            }
1679
1680            String JavaDoc srvJvm = System.getProperty("serverJvm");
1681            if (srvJvm !=null)
1682                jvmnet = true;
1683
1684            String JavaDoc excludeJcc = ap.getProperty("excludeJCC");
1685            if ( framework.equals("DerbyNet") )
1686            {
1687                try {
1688                    RunList.checkClientExclusion(excludeJcc, "JCC", jccMajor, jccMinor, javaVersion);
1689                } catch (Exception JavaDoc e) {
1690                    skiptest = true;
1691                    addSkiptestReason(e.getMessage());
1692                }
1693            }
1694        
1695            // for now we want just want to have a single property
1696
// for all j9 versions exception j9_foundation
1697
// which we map to the generic name foundation.
1698
String JavaDoc testJVM = jvmName;
1699            if (jvmName.startsWith("j9"))
1700            {
1701                if (jvmName.equals("j9_foundation"))
1702                    testJVM = "foundation";
1703                else
1704                    testJVM = "j9";
1705            }
1706            runwithjvm = ap.getProperty("runwith" + testJVM);
1707            if ((runwithjvm != null) && (runwithjvm.equalsIgnoreCase("false")))
1708            {
1709                skiptest = true;
1710                addSkiptestReason("Test skipped: test cannot run with jvm: " +
1711                                  jvmName + ". " + scriptFileName);
1712            }
1713            // startServer will determine whether the server will be started
1714
// for network server tests or that will be left to the test.
1715
String JavaDoc startServerProp = ap.getProperty("startServer");
1716            if (startServerProp != null &&
1717                startServerProp.equalsIgnoreCase("false"))
1718                startServer =false;
1719            
1720            //Check derbyTesting.encoding property
1721
if(testEncoding == null) {
1722                testEncoding = ap.getProperty("derbyTesting.encoding");
1723                // only bother if we have jdk15, otherwise we'll be skipping
1724
if ((jvmName.equals("jdk15")) && (testEncoding != null))
1725                {
1726                        jvmflags = (jvmflags==null?"":jvmflags+" ")
1727                                    + "-Dfile.encoding=" + testEncoding;
1728                        ap.put("file.encoding",testEncoding);
1729                }
1730            }
1731           
1732            if(!replacePolicyFile)
1733            {
1734                String JavaDoc replace_policy = ap.getProperty("derbyTesting.replacePolicyFile");
1735                if (replace_policy != null && replace_policy.equals("true"))
1736                    replacePolicyFile = true;
1737                else
1738                    replacePolicyFile = false;
1739
1740            }
1741
1742 
1743            if (NetServer.isJCCConnection(framework)
1744                    || "true".equalsIgnoreCase(ap.getProperty("noSecurityManager")))
1745                runWithoutSecurityManager = true;
1746            
1747        // Also check for supportfiles
1748
String JavaDoc suppFiles = ap.getProperty("supportfiles");
1749            boolean copySupportFiles = ((suppFiles != null) && (suppFiles.length()>0));
1750            boolean createExtDirs= new Boolean JavaDoc(ap.getProperty("useextdirs","false")).booleanValue();
1751            if (copySupportFiles || createExtDirs)
1752            {
1753                File JavaDoc copyOutDir = null;
1754
1755                if (testType.equals("sql2"))
1756                {
1757                    if ( (isSuiteRun) || (framework.length()>0) )
1758                        copyOutDir = outDir;
1759                    else if ( (runDir != null) && (runDir.exists()) )
1760                        copyOutDir = runDir;
1761                    else
1762                        copyOutDir = outDir;
1763                }
1764                else if ( testType.equals("multi") )
1765                {
1766                    if ( (isSuiteRun) || (mtestdir == null) || (mtestdir.length()==0) )
1767                    {
1768                        copyOutDir = outDir;
1769                    }
1770                    else
1771                    {
1772                        File JavaDoc multiDir = new File JavaDoc(mtestdir);
1773                        copyOutDir = multiDir;
1774                    }
1775                }
1776                else if ( outcopy == true )
1777                    copyOutDir = outDir;
1778                else if ( (runDir != null) && (runDir.exists()) )
1779                {
1780                    copyOutDir = runDir;
1781                }
1782                else
1783                    copyOutDir = outDir;
1784
1785                if(createExtDirs)
1786                {
1787                    extInDir = copyOutDir;
1788                    //create the support file directory for input files
1789
extInDir = new File JavaDoc(copyOutDir , "extin");
1790                    if(!extInDir.exists())
1791                        extInDir.mkdirs();
1792                    //create the support file directory for output files.
1793
extOutDir = new File JavaDoc(copyOutDir , "extout");
1794                    if(!extOutDir.exists())
1795                        extOutDir.mkdirs();
1796                    //create the support file directory for input output files.
1797
extInOutDir = new File JavaDoc(copyOutDir , "extinout");
1798                    if(!extInOutDir.exists())
1799                        extInOutDir.mkdirs();
1800                    copyOutDir = extInDir;
1801                }
1802                if(copySupportFiles)
1803                   CopySuppFiles.copyFiles(copyOutDir, suppFiles);
1804            }
1805            else
1806            {
1807                // for useprocess false, set ext* (back) to null, or it
1808
// later tries to delete files even though they don't exist
1809
extInDir = null;
1810                extOutDir = null;
1811                extInOutDir = null;
1812            }
1813        }
1814        return propString;
1815    }
1816
1817    public static String JavaDoc setTestJavaFlags(String JavaDoc tflags)
1818    {
1819        StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
1820        if (verbose) System.out.println("testJavaflags: " + tflags);
1821        String JavaDoc dintro = "-D";
1822        try
1823        {
1824            dintro = jvm.getJvm(jvmName).getDintro();
1825        }
1826        catch (Exception JavaDoc e)
1827        {
1828            System.out.println("Problem getting jvm "+jvmName+" Dintro: ");
1829            e.printStackTrace(System.out);
1830            System.exit(1);
1831        }
1832        // Parse because there could be a list of flags
1833
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(tflags,"^");
1834        while (st.hasMoreTokens())
1835        {
1836            String JavaDoc token = st.nextToken();
1837            if (! (token.startsWith("-"))) { sb.append(dintro); }
1838            sb.append(token);
1839            sb.append(" ");
1840        }
1841        if (verbose) System.out.println("setTestJavaFlags returning: " + sb.toString());
1842        return sb.toString();
1843    }
1844
1845    private static void loadProps(Properties JavaDoc p, File JavaDoc f) throws Exception JavaDoc
1846    {
1847        if (f.canRead())
1848        {
1849            FileInputStream JavaDoc fis = new FileInputStream JavaDoc(f);
1850            p.load(fis);
1851            fis.close();
1852        }
1853    }
1854
1855    private static void mergeProps(Properties JavaDoc to, Properties JavaDoc from)
1856    {
1857        for (Enumeration JavaDoc e = from.propertyNames(); e.hasMoreElements(); )
1858        {
1859            String JavaDoc key = (String JavaDoc)e.nextElement();
1860            to.put(key, from.getProperty(key));
1861        }
1862    }
1863
1864
1865
1866    private static void frameworkProtocol(Properties JavaDoc p) throws Exception JavaDoc
1867    {
1868
1869        if (p == null)
1870        {
1871            // No properties
1872
return;
1873        }
1874        for (Enumeration JavaDoc e = p.propertyNames(); e.hasMoreElements(); )
1875        {
1876            String JavaDoc key = (String JavaDoc)e.nextElement();
1877            String JavaDoc value = p.getProperty(key);
1878
1879            if (key.equals("driver") || key.equals("ij.driver") || key.equals("jdbc.drivers"))
1880            {
1881                p.put(key, driverName);
1882            }
1883            else if (key.startsWith("ij.protocol") || key.equals("protocol"))
1884            {
1885                value = NetServer.alterURL(framework,value);
1886                p.put(key, value);
1887            }
1888            else if (key.equals("database") || key.equals("ij.database") || key.startsWith("ij.connection") || key.equals("jdbc.url"))
1889            {
1890                dbName = value.substring(value.lastIndexOf(':') + 1 ,
1891                             value.length());
1892                value = NetServer.alterURL(framework,value);
1893                p.put(key, value);
1894            }
1895            else // for any other properties, just copy them
1896
p.put(key, value);
1897            
1898        }
1899
1900        
1901        // jcc default requires userid
1902
// derby client will default to "APP" so doesn't need harness
1903
// to set a user.
1904
if (NetServer.isJCCConnection(framework))
1905        {
1906            String JavaDoc user = System.getProperty("ij.user");
1907            if (user == null) user = "APP";
1908            p.put("ij.user",user);
1909        }
1910
1911        // both jcc and client require password for the moment
1912
if (NetServer.isClientConnection(framework))
1913        {
1914            String JavaDoc password = System.getProperty("ij.password");
1915            if (password == null) password = "APP";
1916            p.put("ij.password",password);
1917        }
1918        
1919        if (NetServer.isJCCConnection(framework))
1920        {
1921            // force messages to show
1922
p.put("ij.retrieveMessagesFromServerOnGetMessage","true");
1923        }
1924
1925        // If this is not a known protocol for ij we
1926
// need to set the driver
1927
if (driverName != null)
1928            p.put("ij.driver",driverName);
1929    }
1930    
1931
1932    private static void encryptionProtocol(Properties JavaDoc p) throws Exception JavaDoc
1933    {
1934        String JavaDoc encryptUrl = "dataEncryption=true;bootPassword=Thursday";
1935
1936        if (p == null)
1937        {
1938            // No properties
1939
return;
1940        }
1941
1942        // add encryption algorithm and provider to database creation URL
1943
String JavaDoc v = p.getProperty("testEncryptionAlgorithm");
1944        if (v != null)
1945            encryptUrl += ";" + Attribute.CRYPTO_ALGORITHM + "=" + v;
1946        v = p.getProperty("testEncryptionProvider");
1947        if (v != null)
1948            encryptUrl += ";" + Attribute.CRYPTO_PROVIDER + "=" + v;
1949
1950        for (Enumeration JavaDoc e = p.propertyNames(); e.hasMoreElements(); )
1951        {
1952            String JavaDoc key = (String JavaDoc)e.nextElement();
1953            String JavaDoc value = p.getProperty(key);
1954
1955            if (key.equals("database") || key.equals("ij.database") || key.startsWith("ij.connection") || key.equals("jdbc.url"))
1956            {
1957                // edit the url if necessary
1958
int index = value.indexOf(encryptUrl);
1959                if ( index == -1)
1960                {
1961                    value = value + ";" + encryptUrl;
1962                }
1963                p.put(key, value);
1964            }
1965            else if (key.equals("testEncryptionAlgorithm") || key.equals("testEncryptionProvider"))
1966                {} // ignore, do not copy
1967
else // for any other properties, just copy them
1968
p.put(key, value);
1969        }
1970    }
1971
1972    private static void cleanupBaseDir(File JavaDoc baseDir)
1973    {
1974        // Some tests rely on no cleanup being done on the baseDir
1975
boolean okToDelete = false;
1976
1977        if ( (usesystem == null) || (usesystem.length()==0) )
1978            okToDelete = true;
1979        else if (usesystem.equals("nist"))
1980        {
1981            if (testBase.equals("schema1"))
1982                okToDelete = true;
1983        }
1984        else if (usesystem.equals("puzzles"))
1985        {
1986            if (testBase.equals("puzzleschema"))
1987                okToDelete = true;
1988        }
1989
1990        if (useCommonDB) okToDelete = false;
1991
1992        if (okToDelete == true)
1993        {
1994           //System.out.println("Should be deleting the baseDir for a clean run");
1995
deleteFile(baseDir);
1996           if (baseDir.exists())
1997               System.out.println("baseDir did not get deleted which could cause test failures");
1998           else
1999               baseDir.mkdir();
2000        }
2001    }
2002
2003    private static void doCleanup(String JavaDoc javaVersion)
2004        throws IOException JavaDoc
2005    {
2006        boolean status = true;
2007        // The output files cannot be deleted if there
2008
// is still a reference to them -- even doing
2009
// this is not a guarantee that they will be deleted
2010
// It seems to depend on the Java environment
2011
//printWriter.close();
2012
//printWriter = null;
2013

2014        //Always cleanup the script files - except when keepfiles is true
2015
if ( !(script == null) && (script.exists()) && (!keepfiles) )
2016        {
2017            status = script.delete();
2018            //System.out.println("Status was: " + status);
2019
}
2020
2021        String JavaDoc endTime = CurrentTime.getTime();
2022        StringBuffer JavaDoc sbend = new StringBuffer JavaDoc();
2023        sbend.append("*** End: " + testBase + " jdk" + javaVersion + " ");
2024        if ( (framework.length()>0) && (!framework.startsWith("embedded")) )
2025            sbend.append(framework + " ");
2026        if ( (suiteName != null) && (suiteName.length()>0) )
2027            sbend.append(suiteName + " ");
2028        sbend.append(endTime + " ***");
2029        System.out.println(sbend.toString());
2030        pwDiff.println(sbend.toString());
2031        pwDiff.flush();
2032        pwDiff.close();
2033        pwDiff = null;
2034
2035        // This could be true if set by user or there were diffs
2036
if (keepfiles == false)
2037        {
2038            // Delete the out and diff files
2039
status = tmpOutFile.delete();
2040            if (status == false)
2041                tmpOutFile = null;
2042            status = finalOutFile.delete();
2043            if (skiptest == false)
2044                status = diffFile.delete();
2045            // delete the copied (and for Network Server, modified) master file
2046
tempMasterFile = new File JavaDoc(outDir, tempMasterName);
2047            status = tempMasterFile.delete();
2048            UTF8OutFile = new File JavaDoc(outDir, UTF8OutName);
2049            status = UTF8OutFile.delete();
2050            File JavaDoc defaultPolicyFile = new File JavaDoc(userdir, "derby_tests.policy");
2051            if (defaultPolicyFile.exists())
2052            status = defaultPolicyFile.delete();
2053            if (deleteBaseDir)
2054            {
2055                if (useCommonDB == false)
2056                {
2057                    //System.out.println("Should delete the baseDir: " + baseDir.getPath());
2058
deleteFile(baseDir);
2059                }
2060                else
2061                {
2062                    status = appPropFile.delete();
2063                    //no permission in Java to drop derby.log
2064
//File logfile = new File(baseDir, "derby.log");
2065
//System.out.println("delete derby.log ");
2066
//System.out.println(logfile.getPath());
2067
//status = logfile.delete();
2068
}
2069
2070                // delete the directories where external input/output files
2071
// were created
2072
if (extInDir!=null) deleteFile(extInDir);
2073                if (extOutDir!=null) deleteFile(extOutDir);
2074                if (extInOutDir!=null) deleteFile(extInOutDir);
2075            }
2076        }
2077        // reset for next test
2078
// the next line is a bug fix to get cleanup working correctly when
2079
// useprocess==false; without this the first failing test causes all
2080
// subsequent tests not to clean up, since keepfiles is static; a
2081
// more general solution is to make the variable non-static, and to
2082
// set the default in the initializer; I(john) have not done this
2083
// because the same reasoning probably applies to many of the static
2084
// variables, and the whole subject should probably be approached in
2085
// a coordinated way when someone has the time for it.
2086
keepfiles = false;
2087
2088    }
2089
2090    /*
2091     * For recursively deleting a directory
2092     *
2093     */

2094    public static void deleteFile(File JavaDoc f)
2095    {
2096        boolean status = true;
2097        if (f == null)
2098        {
2099            System.out.println(f.getName() + " is null");
2100            return;
2101        }
2102        if (!f.exists())
2103        {
2104            System.out.println(f.getName() + " does not exist; harness error");
2105            return;
2106        }
2107
2108        //System.out.println("Trying to delete: " + f.getPath());
2109
status = f.delete();
2110
2111        if (status == true)
2112            return;
2113        else
2114        {
2115            // Could not delete; this could be a non-empty directory
2116
if (!f.isDirectory())
2117            {
2118                System.out.println("Could not delete file " + f.getName() + ", going on");
2119                return;
2120            }
2121            //System.out.println("Recursively delete...");
2122
String JavaDoc[] files = f.list();
2123            for (int i = 0; i < files.length; i++)
2124            {
2125                File JavaDoc sub = new File JavaDoc(f.getPath(), files[i]);
2126                if (sub == null)
2127                    continue;
2128                //System.out.println("Try to delete: " + sub.getPath());
2129
status = sub.delete();
2130                if (status != true)
2131                {
2132                    // Could not delete; it may be a non-empty dir
2133
if ( sub.isDirectory() )
2134                    {
2135                        //System.out.println("Recursing again ... ");
2136
deleteFile(sub);
2137                        // It should be empty now; try again
2138
status = sub.delete();
2139                        //System.out.println("Recurse delete status: " + status);
2140
}
2141                    //else
2142
// The file delete failed
2143
//System.out.println("Failed to clean up file: " + sub.getPath());
2144
}
2145            }
2146        }
2147        //Should be able to delete the top directory now
2148
status = f.delete();
2149        if (status == false)
2150        {
2151            System.out.println("Warning: Cleanup failed on baseDir: " + f.getPath());
2152        }
2153    }
2154
2155    static void addToFailures(String JavaDoc testName) throws IOException JavaDoc
2156    {
2157        if (failFileName==null)
2158        {
2159            if (isSuiteRun) failFileName = suiteName+".fail";
2160            else failFileName=testBase+".fail";
2161        }
2162        addToListFile(failFileName,testName);
2163    }
2164
2165    static void addToSuccesses(String JavaDoc testName) throws IOException JavaDoc
2166    {
2167        if (passFileName==null)
2168        {
2169            if (isSuiteRun) passFileName = suiteName+".pass";
2170            else passFileName=testBase+".pass";
2171        }
2172        addToListFile(passFileName,testName);
2173    }
2174
2175    static void addToListFile(String JavaDoc fileName, String JavaDoc testName) throws IOException JavaDoc
2176    {
2177        File JavaDoc f;
2178        if (isSuiteRun)
2179            f = new File JavaDoc(rsuiteDir,fileName);
2180        else
2181            f = new File JavaDoc(outDir,fileName);
2182        PrintStream JavaDoc ps = null;
2183
2184        // This call to getCanonicalPath catches IOExceptions as a workaround to
2185
// a bug in the EPOC jvm.
2186
try { ps = new PrintStream JavaDoc( new FileOutputStream JavaDoc(f.getCanonicalPath(),true) ); }
2187        catch (IOException JavaDoc e) {
2188            FileWriter JavaDoc fw = new FileWriter JavaDoc(f);
2189            fw.close();
2190            ps = new PrintStream JavaDoc( new FileOutputStream JavaDoc(f.getCanonicalPath(),true) );
2191        }
2192
2193        ps.println(testName);
2194        ps.flush();
2195        ps.close();
2196    }
2197
2198    static void appendStderr(BufferedOutputStream JavaDoc bos, InputStream JavaDoc is)
2199        throws IOException JavaDoc
2200    {
2201        PrintWriter JavaDoc tmpPw = new PrintWriter JavaDoc(bos);
2202        // reader for stderr
2203
BufferedReader JavaDoc errReader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is, "UTF-8"));
2204        String JavaDoc s = null;
2205        int lines = 0;
2206        while ((s = errReader.readLine()) != null)
2207        {
2208           tmpPw.println(s);
2209        }
2210        errReader.close();
2211        tmpPw.flush();
2212    }
2213
2214    static void verifyTestType()
2215        throws ClassNotFoundException JavaDoc, FileNotFoundException JavaDoc, IOException JavaDoc
2216    {
2217        //java requires '/' as separator to look into jar, irrespective of OS
2218
InputStream JavaDoc is =
2219            loadTestResource("harness/testtypes.properties");
2220        Properties JavaDoc p = new Properties JavaDoc();
2221        p.load(is);
2222        String JavaDoc testtypes = p.getProperty("testtypes");
2223        StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(testtypes,",");
2224        String JavaDoc ttype = "";
2225        while (st.hasMoreTokens())
2226        {
2227            ttype = st.nextToken();
2228            if ( testType.equals(ttype) )
2229                return;
2230        }
2231        // Not a known test type
2232
System.out.println("Unknown test type: " + testType);
2233        System.exit(1);
2234    }
2235
2236    public static void
2237    addStandardTestJvmProps(Vector JavaDoc testJvmProps,String JavaDoc derbySystemHome,
2238                            String JavaDoc userDirName, jvm jvm)
2239    {
2240        if (derbySystemHome==null || derbySystemHome.length() == 0)
2241            derbySystemHome = userDirName;
2242        testJvmProps.addElement("derby.system.home=" + derbySystemHome);
2243        testJvmProps.addElement("derby.infolog.append=true ");
2244        // Why is this being done here
2245
//if (jvm != null)
2246
//testJvmProps.addElement("jvm="+jvm.getName());
2247
}
2248    
2249    private static String JavaDoc[] buildTestCommand(String JavaDoc propString,
2250        String JavaDoc systemHome, String JavaDoc scriptPath)
2251        throws FileNotFoundException JavaDoc, IOException JavaDoc, Exception JavaDoc
2252    {
2253        composePolicyFile();
2254        
2255        //System.out.println("testType: " + testType);
2256
String JavaDoc ij = "";
2257        // Create the test command line
2258
if (testType.equals("sql"))
2259            ij = "ij";
2260        jvm jvm = null; // to quiet compiler
2261
jvm = jvm.getJvm(jvmName);
2262        if (javaCmd != null)
2263            jvm.setJavaCmd(javaCmd);
2264
2265        if ( (classpath != null) && (classpath.length()>0) )
2266            jvm.setClasspath(classpath);
2267
2268        Vector JavaDoc jvmProps = new Vector JavaDoc();
2269        if ( testType.equals("java") || testType.equals("demo") )
2270            addStandardTestJvmProps(jvmProps,systemHome,
2271                outDir.getCanonicalPath(),null);
2272        else if ( (runDir != null) && (runDir.exists()) )
2273            addStandardTestJvmProps(jvmProps,systemHome,
2274                runDir.getCanonicalPath(),jvm);
2275        else
2276            addStandardTestJvmProps(jvmProps,systemHome,
2277                outDir.getCanonicalPath(),jvm);
2278        
2279        if ( (testJavaFlags != null) && (testJavaFlags.length()>0) )
2280        {
2281        String JavaDoc parsedFlags = setTestJavaFlags(testJavaFlags);
2282            StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(parsedFlags," ");
2283            while (st.hasMoreTokens())
2284            {
2285                jvmflags = (jvmflags==null?"":jvmflags) + " " + st.nextToken();
2286            }
2287        }
2288        
2289        if ( ij.startsWith("ij") )
2290            jvmProps.addElement("ij.defaultResourcePackage=" +
2291                defaultPackageName);
2292        
2293        if ( (framework != null) )
2294        {
2295            jvmProps.addElement("framework=" + framework);
2296            if ((hostName != null) && (!hostName.equals("localhost")))
2297                    jvmProps.addElement("hostName=" + hostName);
2298        }
2299        
2300        if (junitXASingle)
2301            jvmProps.addElement ("derbyTesting.xa.single=true");
2302
2303        // if we're not jdk15, don't, we'll skip
2304
if ((testEncoding != null) && (jvmName.equals("jdk15")))
2305        {
2306            jvmProps.addElement("derbyTesting.encoding=" + testEncoding);
2307            jvmProps.addElement("file.encoding=" + testEncoding);
2308            jvmflags = (jvmflags==null?"":jvmflags+" ")
2309                         + "-Dfile.encoding=" + testEncoding;
2310        } else if (isI18N) {
2311            // The I18N tests should be run with UTF-8 encoding to avoid
2312
// problems with characters that cannot be represented in the
2313
// default encoding (DERBY-244).
2314
jvmProps.addElement("file.encoding=UTF-8");
2315            jvmflags = (jvmflags==null ? "" : jvmflags + " ") +
2316                        "-Dfile.encoding=UTF-8";
2317        }
2318        
2319        if (upgradejarpath != null)
2320            jvmProps.addElement("derbyTesting.jar.path=" + upgradejarpath);
2321            
2322        if ( (jvmflags != null) && (jvmflags.length()>0) )
2323        {
2324            // We now replace any '^' in jvmflags with ' '
2325
if (jvmflags.indexOf("^")>0)
2326            {
2327                jvmflags = spaceJvmFlags(jvmflags);
2328            }
2329            jvm.setFlags(jvmflags);
2330        }
2331        
2332        
2333        if (testType.equals("multi"))
2334        {
2335            if ( (jvmflags != null) && (jvmflags.indexOf("mx") == -1) && (jvmflags.indexOf("Xmx") == -1))
2336                jvm.setMx(64*1024*1024); // -mx64m
2337

2338            // MultiTest is special case, so pass on properties
2339
// related to encryption to MultiTest
2340
jvmProps.addElement("encryption="+encryption);
2341            Properties JavaDoc props = new Properties JavaDoc();
2342            // parse and get only the special properties that are needed for the url
2343
SpecialFlags.parse(testSpecialProps, props, new Properties JavaDoc());
2344            String JavaDoc encryptionAlgorithm = props.getProperty("testEncryptionAlgorithm");
2345            if(encryptionAlgorithm != null)
2346                jvmProps.addElement("encryptionAlgorithm=\""+ Attribute.CRYPTO_ALGORITHM
2347                        +"="+encryptionAlgorithm+"\"");
2348        }
2349        jvm.setD(jvmProps);
2350       
2351        // set security properties
2352
if (!runWithoutSecurityManager)
2353            jvm.setSecurityProps();
2354        else
2355            System.out.println("-- SecurityManager not installed --");
2356            
2357        Vector JavaDoc v = jvm.getCommandLine();
2358        if ( ij.startsWith("ij") )
2359        {
2360            // As of cn1411-20030930 IBM jvm the system takes the default
2361
// console encoding which in the US, on windows, is Cp437.
2362
// Sun jvms, however, always force a console encoding of 1252.
2363
// To get the same result for ibm141 & jdk14*, the harness needs to
2364
// force the console encoding to Cp1252 for ij tests - unless
2365
// we're on non-ascii systems.
2366
String JavaDoc isNotAscii = System.getProperty("platform.notASCII");
2367            if (isI18N) {
2368                // DERBY-244: Use UTF-8 console encoding for the i18n tests to
2369
// avoid MalformedInputException with the IBM jvm.
2370
v.addElement("-Dconsole.encoding=UTF-8");
2371            } else if ((isNotAscii == null) || (isNotAscii.equals("false"))) {
2372                v.addElement("-Dconsole.encoding=Cp1252" );
2373            }
2374            v.addElement("org.apache.derby.tools." + ij);
2375            if (ij.equals("ij"))
2376            {
2377                //TODO: is there a setting/property we could check after which
2378
// we can use v.addElement("-fr"); (read from the classpath?)
2379
// then we can also use v.addElement(scriptFile);
2380
v.addElement("-f");
2381                v.addElement(outDir.toString() + File.separatorChar + scriptFileName);
2382            }
2383            v.addElement("-p");
2384            v.addElement(propString);
2385        }
2386        else if ( testType.equals("java") )
2387        {
2388            if (javaPath.length() > 0)
2389                v.addElement(javaPath + "." + testBase);
2390            else
2391                v.addElement(testBase);
2392            if (propString.length() > 0)
2393            {
2394                v.addElement("-p");
2395                v.addElement(propString);
2396            }
2397        }
2398        else if (testType.equals("unit"))
2399        {
2400            v.addElement("org.apache.derbyTesting.unitTests.harness.UnitTestMain");
2401            v.addElement("-p");
2402            v.addElement(propString);
2403        }
2404        else if (testType.equals("junit"))
2405        {
2406            v.addElement("junit.textui.TestRunner");
2407            if (javaPath.length() > 0) {
2408                v.addElement(javaPath + "." + testBase);
2409            } else {
2410                v.addElement(testBase);
2411            }
2412        }
2413        else if ( testType.equals("multi") )
2414        {
2415    System.out.println("scriptiflename is: " + scriptFileName);
2416            v.addElement("org.apache.derbyTesting.functionTests.harness.MultiTest");
2417            v.addElement(scriptFileName);
2418            v.addElement("-i");
2419            v.addElement(mtestdir);
2420            v.addElement("-o");
2421            v.addElement(outDir.getPath());
2422            v.addElement("-p");
2423            v.addElement(propString);
2424        }
2425
2426        // Now convert the vector into a string array
2427
String JavaDoc[] sCmd = new String JavaDoc[v.size()];
2428        for (int i = 0; i < v.size(); i++)
2429        {
2430            sCmd[i] = (String JavaDoc)v.elementAt(i);
2431        }
2432
2433        return sCmd;
2434    }
2435
2436    public static String JavaDoc spaceJvmFlags(String JavaDoc caretedJvmFlags)
2437    {
2438        String JavaDoc spacedJvmFlags = "";
2439        // there must at least be one
2440
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(jvmflags,"^");
2441        while (st.hasMoreTokens())
2442        {
2443            spacedJvmFlags += st.nextToken() + " ";
2444        }
2445        spacedJvmFlags = spacedJvmFlags.substring(0,spacedJvmFlags.length() -1);
2446        return spacedJvmFlags;
2447    }
2448    
2449    public static void composePolicyFile() throws ClassNotFoundException JavaDoc
2450    {
2451        try{
2452            //DERBY-892: allow for test- and suite-specific policy additions
2453

2454            // this is the default policy file
2455
String JavaDoc default_policy = "util/derby_tests.policy";
2456            
2457            // if the property replacePolicyFile is set (in the
2458
// test specific _app.properties file, or at the command line)
2459
// do not use the default policy file at all, otherwise, append
2460
if (!replacePolicyFile)
2461            {
2462                File JavaDoc userDirHandle = new File JavaDoc(userdir);
2463                CopySuppFiles.copyFiles(userDirHandle, default_policy);
2464            }
2465            // see if there is a suite specific policy file and append or replace
2466
if ((isSuiteRun) && (suiteName!=null))
2467            {
2468                InputStream JavaDoc newpolicy = loadTestResource("suites/" +
2469                    suiteName.substring(0,suiteName.indexOf(':')) +
2470                    ".policy");
2471                writePolicyFile(newpolicy);
2472            }
2473
2474            // if we are running with useprocess=false, we need some special
2475
// properties (setSecurityManager, setIO)
2476
if (!useprocess)
2477            {
2478                InputStream JavaDoc newpolicy = loadTestResource("util/" + "useprocessfalse.policy");
2479                writePolicyFile(newpolicy);
2480            }
2481            
2482            // now get the test specific policy file and append or replace
2483
InputStream JavaDoc newpolicy =
2484                loadTestResource("tests/" + testDirName + "/" + testBase + ".policy");
2485            writePolicyFile(newpolicy);
2486        } catch (IOException JavaDoc ie) {
2487            System.out.println("Exception trying to create policy file: ");
2488            ie.printStackTrace();
2489        }
2490    }
2491
2492    public static void writePolicyFile(InputStream JavaDoc newpolicy)
2493    {
2494        try{
2495            if (newpolicy != null)
2496            {
2497                File JavaDoc oldpolicy = new File JavaDoc(runDir,"derby_tests.policy");
2498                if (verbose && oldpolicy.exists()) System.out.println("Appending to derby_tests.policy");
2499                BufferedReader JavaDoc policyadd = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(newpolicy, "UTF-8"));
2500                FileWriter JavaDoc policyfw = new FileWriter JavaDoc(oldpolicy.getPath(), true);
2501                PrintWriter JavaDoc policypw = new PrintWriter JavaDoc( new BufferedWriter JavaDoc(policyfw, 10000), true );
2502                String JavaDoc str = "";
2503                while ( (str = policyadd.readLine()) != null ) { policypw.println(str); }
2504                policypw.close();
2505                policyadd.close();
2506                policypw= null;
2507                newpolicy = null;
2508            }
2509        } catch (IOException JavaDoc ie) {
2510            System.out.println("Exception trying to create policy file: ");
2511            ie.printStackTrace();
2512        }
2513    }
2514
2515    private static void execTestProcess(String JavaDoc[] testCmd)
2516        throws Exception JavaDoc
2517    {
2518        // Execute the process and handle the results
2519
Process JavaDoc pr = null;
2520        try
2521        {
2522            // We need the process inputstream and errorstream
2523
ProcessStreamResult prout = null;
2524            ProcessStreamResult prerr = null;
2525            FileOutputStream JavaDoc fos = null;
2526            BufferedOutputStream JavaDoc bos = null;
2527            
2528            StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
2529            
2530            for (int i = 0; i < testCmd.length; i++)
2531            {
2532                sb.append(testCmd[i] + " ");
2533            }
2534            if (verbose) System.out.println(sb.toString());
2535            pr = Runtime.getRuntime().exec(testCmd);
2536
2537            String JavaDoc inEncoding = null;
2538            String JavaDoc outEncoding = null;
2539            if (isI18N) {
2540                // DERBY-244: I18N tests are run with -Dfile.encoding=UTF-8, so
2541
// we need to specify encoding to the streams as well.
2542
inEncoding = "UTF-8";
2543                outEncoding = inEncoding;
2544            }
2545
2546            if (codeset != null) {
2547                // The test explicitly specified a codeset, use that codeset
2548
// when reading the test output.
2549
inEncoding = codeset;
2550            }
2551
2552            if (testEncoding != null) {
2553                inEncoding = testEncoding;
2554            }
2555
2556            if (useOutput)
2557            {
2558                fos = new FileOutputStream JavaDoc(tmpOutFile);
2559            }
2560            else
2561            {
2562                fos = new FileOutputStream JavaDoc(stdOutFile);
2563            }
2564            bos = new BufferedOutputStream JavaDoc(fos, 1024);
2565            prout =
2566                new ProcessStreamResult(pr.getInputStream(), bos,
2567                                        timeoutStr,
2568                                        inEncoding, outEncoding);
2569            prerr =
2570                new ProcessStreamResult(pr.getErrorStream(), bos,
2571                                        timeoutStr,
2572                                        inEncoding, outEncoding);
2573    
2574            if (framework != null && ! framework.equals(""))
2575                if (verbose) System.out.println("The test should be running...");
2576
2577            if (timeout != -1) {
2578                TimedProcess tp = new TimedProcess(pr);
2579                tp.waitFor(timeout*60);
2580                pr = null;
2581            }
2582
2583            // determine if the process is done or was interrupted
2584
boolean outInterrupt = prout.Wait();
2585            boolean errInterrupt = prerr.Wait();
2586                
2587            if ( (errInterrupt) || (outInterrupt) )
2588            {
2589                pwDiff.println("The test timed out...");
2590                System.out.println("Test timed out...");
2591                pr.destroy();
2592                pr = null;
2593            }
2594
2595            fos.close();
2596            bos.close();
2597            //printWriter.flush();
2598
//printWriter.close();
2599
if (pr != null)
2600            {
2601                pr.destroy();
2602                pr = null;
2603            }
2604        }
2605        catch(IOException JavaDoc ioe)
2606        {
2607            System.out.println("IOException: " + ioe.getMessage());
2608        }
2609        catch(Throwable JavaDoc t)
2610        {
2611            pwDiff.println("Process exception: " + t);
2612            System.out.println("Process exception: " + t.getMessage());
2613            t.printStackTrace();
2614            if (pr != null)
2615            {
2616                pr.destroy();
2617                pr = null;
2618            }
2619        }
2620    }
2621    
2622    private static void execTestNoProcess(Properties JavaDoc sysProp,
2623        String JavaDoc systemHome, String JavaDoc propString, String JavaDoc scriptPath)
2624        throws Exception JavaDoc
2625    {
2626        // For platforms where executing a process is failing
2627
Properties JavaDoc ptmp = System.getProperties();
2628        ptmp.put("derby.system.home", systemHome);
2629        ptmp.put("derby.infolog.append", "true");
2630        // for framework tests, we may need to pick up the hostName
2631
// passed on on command line to individual tests...
2632
if (framework.startsWith("DerbyNet"))
2633            ptmp.put("hostName=", hostName);
2634        System.setProperties(ptmp);
2635
2636        String JavaDoc pathStr = "";
2637
2638        //these calls to getCanonicalPath catch IOExceptions as a workaround to
2639
//a bug in the EPOC jvm.
2640
try
2641        {
2642            pathStr = tmpOutFile.getCanonicalPath().replace(File.separatorChar,fileSep);
2643        }
2644        catch (IOException JavaDoc e)
2645        {
2646            FileWriter JavaDoc fw = new FileWriter JavaDoc(tmpOutFile);
2647            fw.close();
2648            pathStr = tmpOutFile.getCanonicalPath().replace(File.separatorChar,fileSep);
2649        }
2650        
2651
2652        PrintStream JavaDoc ps = new PrintStream JavaDoc(new FileOutputStream JavaDoc(pathStr), true);
2653        
2654        // Install a security manager within this JVM for this test.
2655
composePolicyFile();
2656        boolean installedSecurityManager = installSecurityManager();
2657        if (testType.equals("sql"))
2658        {
2659            String JavaDoc[] ijarg = new String JavaDoc[3];
2660            ijarg[0] = "-p";
2661            ijarg[1] = propString;
2662            ijarg[2] = scriptPath;
2663            // direct the standard output and error to the print stream for the .tmp file
2664
PrintStream JavaDoc stdout = System.out;
2665            PrintStream JavaDoc stderr = System.err;
2666            System.setOut(ps);
2667            System.setErr(ps);
2668            RunIJ ij = new RunIJ(ijarg);
2669            Thread JavaDoc ijThread = new Thread JavaDoc(ij);
2670            try
2671            {
2672                ijThread.start();
2673                if (timeout < 0)
2674                {
2675                    ijThread.join();
2676                }
2677                else
2678                {
2679                    ijThread.join(timeout * 60 * 1000);
2680                }
2681            }
2682            catch (Exception JavaDoc e)
2683            {
2684                System.out.println("Aiiie! Got some kind of exception " + e);
2685            }
2686
2687            // Now make sure a shutdown is complete if necessary
2688
if (shutdownurl != null)
2689            {
2690                String JavaDoc[] sdargs = new String JavaDoc[2];
2691                sdargs[0] = systemHome;
2692                sdargs[1] = shutdownurl;
2693                shutdown.main(sdargs);
2694            }
2695            // Reset ij.defaultResourcePackage
2696
ptmp = System.getProperties();
2697                        ptmp.put("ij.defaultResourcePackage", "/org/apache/derbyTesting/");
2698            ptmp.put("usesystem", "");
2699            System.setProperties(ptmp);
2700            // Reset System.out and System.err
2701
System.setOut(stdout);
2702            System.setErr(stderr);
2703        }
2704        else if (testType.equals("java"))
2705        {
2706        if (javaPath == null)
2707                javaPath = "org.apache.derbyTesting.functionTests.tests." + testDirName;
2708        
2709            String JavaDoc[] args = new String JavaDoc[2];
2710            args[0] = "-p";
2711            args[1] = propString;
2712            Class JavaDoc[] classArray = new Class JavaDoc[1];
2713            classArray[0] = args.getClass();
2714            String JavaDoc testName = javaPath + "." + testBase;
2715            Class JavaDoc JavaTest = Class.forName(testName);
2716            PrintStream JavaDoc stdout = System.out;
2717            PrintStream JavaDoc stderr = System.err;
2718            System.setOut(ps);
2719            System.setErr(ps);
2720            // Get the tests's main method and invoke it
2721
Method JavaDoc testMain = JavaTest.getMethod("main", classArray);
2722            Object JavaDoc[] argObj = new Object JavaDoc[1];
2723            argObj[0] = args;
2724            RunClass testObject = new RunClass(testMain, argObj);
2725            Thread JavaDoc testThread = new Thread JavaDoc(testObject);
2726            try
2727            {
2728                testThread.start();
2729                if (timeout < 0)
2730                {
2731                    testThread.join();
2732                }
2733                else
2734                {
2735                    testThread.join(timeout * 60 * 1000);
2736                }
2737            }
2738            catch(Exception JavaDoc e)
2739            {
2740                System.out.println("Exception upon invoking test..." + e);
2741                e.printStackTrace();
2742            }
2743
2744            try
2745            {
2746                java.sql.DriverManager.getConnection("jdbc:derby:;shutdown=true");
2747            }
2748            catch (java.sql.SQLException JavaDoc e)
2749            {
2750                // ignore the errors, they are expected.
2751
}
2752            // Reset System.out and System.err
2753
System.setOut(stdout);
2754            System.setErr(stderr);
2755        }
2756        else if (testType.equals("multi"))
2757        {
2758    System.out.println("scriptiflename is: later " + scriptFileName);
2759            // multi tests will now run with useprocess=false;
2760
// however, probably because they use Threads, if run
2761
// within another test suite, the next suite will not run
2762
// And using a Thread.join() to start the tests doesn't resolve
2763
// this. So this support is here simply to allow running
2764
// something like stressmulti just by itself for debugging
2765
//javaPath = "org.apache.derbyTesting.functionTests.harness.MultiTest";
2766
String JavaDoc[] args = new String JavaDoc[5];
2767            args[0] = scriptFileName;
2768            args[1] = "-i";
2769            args[2] = mtestdir;
2770            args[3] = "-o";
2771            args[4] = outDir.getPath();
2772            System.out.println("Try running MultiTest.main");
2773            for (int i = 0; i < args.length; i++)
2774                System.out.println("args: " + args[i]);
2775            System.exit(1);
2776            org.apache.derbyTesting.functionTests.harness.MultiTest.main(args);
2777        }
2778        else if (testType.equals("unit"))
2779        {
2780            // direct the standard output and error to the print stream for the .tmp file
2781
PrintStream JavaDoc stdout = System.out;
2782            PrintStream JavaDoc stderr = System.err;
2783            System.setOut(ps);
2784            System.setErr(ps);
2785            System.out.println("Unit tests not implemented yet with useprocess=false");
2786            // Reset System.out and System.err
2787
System.setOut(stdout);
2788            System.setErr(stderr);
2789            // repeat to stdout...
2790
System.out.println("Unit tests not implemented yet with useprocess=false");
2791            //System.exit(1);
2792
/*
2793            String[] args = new String[2];
2794            args[0] = "-p";
2795            args[1] = propString;
2796            org.apache.derbyTesting.unitTests.harness.UnitTestMain.main(args);
2797            */

2798        }
2799        else if (testType.equals("junit"))
2800        {
2801            PrintStream JavaDoc stdout = System.out;
2802            PrintStream JavaDoc stderr = System.err;
2803            System.setOut(ps);
2804            System.setErr(ps);
2805            if (javaPath == null) {
2806                javaPath = "org.apache.derbyTesting.functionTests.tests." + testDirName;
2807            }
2808            String JavaDoc testName = javaPath + "." + testBase;
2809            
2810            // NOTE: This is most likely a temporary solution that will be
2811
// replaced once we get our own TestRunner.
2812

2813            // Cannot use junit.textui.TestRunner.main() since it will exit
2814
// the JVM when done.
2815

2816            // Extract/create a TestSuite object, which is either
2817
// a) retreived from a static suite() method in the test class
2818
//
2819
// or, if a) fails
2820
//
2821
// b) containing all methods starting with "test" in the junit
2822
// test class.
2823
// This corresponds to what the junit.textui.TestRunner would
2824
// do if invoked in a separate process (useprocess=true).
2825

2826            // Load the test class
2827
Class JavaDoc testClass = Class.forName(testName);
2828            
2829            TestSuite junitTestSuite = null;
2830            
2831            try{
2832                // Get the static suite() method if it exists.
2833
Method JavaDoc suiteMethod = testClass.getMethod("suite", null);
2834                // Get the TestSuite object returned by the suite() method
2835
// by invoking it.
2836
// Method is static, hence param1 is null
2837
// Method has no formal parameters, hence param2 is null
2838
junitTestSuite = (TestSuite) suiteMethod.invoke(null, null);
2839            } catch(Exception JavaDoc ex){
2840                // Not able to access static suite() method (with no params)
2841
// returning a TestSuite object.
2842
// Use JUnit to create a TestSuite with all methods in the test
2843
// class starting with "test"
2844
junitTestSuite = new TestSuite(testClass);
2845            }
2846            
2847            if(junitTestSuite != null){
2848                // Now run the test suite
2849
junit.textui.TestRunner.run(junitTestSuite);
2850            }
2851            else{
2852                System.out.println("Not able to extract JUnit TestSuite from " +
2853                        "test class " + testName);
2854            }
2855        
2856            // Reset System.out and System.err
2857
System.setOut(stdout);
2858            System.setErr(stderr);
2859        }
2860        ps.close();
2861         if (installedSecurityManager)
2862        {
2863            System.setSecurityManager(null);
2864            
2865        }
2866    }
2867
2868    static void addSkiptestReason(String JavaDoc reason) {
2869        if (skiptestReason.length() > 0)
2870            skiptestReason.append(System.getProperty("line.separator","\n"));
2871        skiptestReason.append(reason);
2872    }
2873
2874    static boolean verifyFramework (String JavaDoc framework) {
2875        String JavaDoc validFmString = "";
2876        for (int i = 0 ; i < validFrameworks.length; i++)
2877        {
2878            validFmString += " " + validFrameworks[i];
2879            if (validFrameworks[i].equals(framework))
2880                return true;
2881        }
2882        System.err.println("Invalid framework: " + framework);
2883    
2884        System.err.println("Valid frameworks are: " +
2885               validFmString);
2886        return false;
2887    
2888    }
2889
2890    /*
2891     * method for loading a resource relative to testResourceHome.
2892     * @param loc location of file relative to testResourceHome (e.g. master/cast.out)
2893     * @return InputStream for the resource
2894     */

2895    public static InputStream JavaDoc loadTestResource(String JavaDoc loc) {
2896        return new Object JavaDoc().getClass().getResourceAsStream(testResourceHome + loc);
2897    }
2898    
2899    /*
2900     * This method replaces the / in the path string with \ for windows
2901     * @param path the path string to convert
2902     * @return String the new path
2903     */

2904    private static String JavaDoc convertPathForWin(String JavaDoc path)
2905    {
2906        String JavaDoc tmp = "a" + path +"a";
2907        int i = tmp.indexOf('/');
2908        while (i != -1)
2909        {
2910            //replace the \\ with .
2911
tmp = tmp.substring(0, i) + "\\\\" + tmp.substring(i+1);
2912            i = tmp.indexOf('/');
2913        }
2914        //lets remove the a we added
2915
tmp = tmp.substring(1, tmp.length()-1);
2916        //System.out.println(tmp);
2917

2918        return tmp;
2919    }
2920    
2921    /**
2922     * Install the default security manager in this JVM for this
2923     * test, used when useprocess is false.
2924     * @return
2925     * @throws ClassNotFoundException
2926     * @throws IOException
2927     */

2928    private static boolean installSecurityManager() throws ClassNotFoundException JavaDoc, IOException JavaDoc
2929    {
2930        // SecurityManager not currently work with j9 and useProcess=false
2931
// need to disable to allow tests to run.
2932
if (jvmName.startsWith("j9"))
2933            return false;
2934        
2935        boolean installedSecurityManager = false;
2936        // Set up the SecurityManager in this JVM for this test.
2937
boolean haveSecurityManagerAlready = System.getSecurityManager() != null;
2938        if (runWithoutSecurityManager)
2939        {
2940            // Test doesn't run with a SecurityManager,
2941
// print a warning if one is there already.
2942
if (haveSecurityManagerAlready)
2943                System.out.println(
2944                        "noSecurityManager=true,useProcess=false but SecurityManager installed by previous test");
2945            else
2946                System.out.println("-- SecurityManager not installed --");
2947        }
2948        else if (!haveSecurityManagerAlready)
2949        {
2950            // Get the set of -D options that would be needed
2951
// for a spawned VM and convert them to system properties.
2952
Vector JavaDoc propList = jvm.getSecurityProps(null);
2953            for (Enumeration JavaDoc e = propList.elements(); e.hasMoreElements();)
2954            {
2955                String JavaDoc dashDOpt = (String JavaDoc) e.nextElement();
2956                if ("java.security.manager".equals(dashDOpt))
2957                    continue;
2958                
2959                int eq = dashDOpt.indexOf("=");
2960                String JavaDoc key = dashDOpt.substring(0, eq);
2961                String JavaDoc value = dashDOpt.substring(eq + 1);
2962                
2963                System.setProperty(key, value);
2964                
2965            }
2966            System.setSecurityManager(new SecurityManager JavaDoc());
2967            installedSecurityManager = true;
2968        }
2969        
2970        return installedSecurityManager;
2971    }
2972    
2973    // copy the .out file in utf8 format.
2974
// This can then be used as a new master in the source.
2975
// It is assumed that if one runs with this property, keepfiles should be true.
2976
private static void generateUTF8OutFile(File JavaDoc FinalOutFile) throws IOException JavaDoc
2977    {
2978        if (generateUTF8Out)
2979        {
2980            keepfiles=true;
2981            File JavaDoc UTF8OutFile = new File JavaDoc(UTF8OutName);
2982            
2983            // start reading the .out file back in, using default encoding
2984
BufferedReader JavaDoc inFile = new BufferedReader JavaDoc(new FileReader JavaDoc(FinalOutFile));
2985            FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(UTF8OutFile);
2986            BufferedWriter JavaDoc bw = new BufferedWriter JavaDoc (new OutputStreamWriter JavaDoc(fos, "UTF-8"));
2987            int c;
2988            while ((c = inFile.read()) != -1)
2989                bw.write(c);
2990            bw.flush();
2991            bw.close();
2992            fos.close();
2993        }
2994    }
2995
2996}
2997
2998
2999
Popular Tags