Source Code Cross Referenced for RunTest.java in  » Database-DBMS » db-derby-10.2 » org » apache » derbyTesting » functionTests » harness » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database DBMS » db derby 10.2 » org.apache.derbyTesting.functionTests.harness 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:
0003:           Derby - Class org.apache.derbyTesting.functionTests.harness.RunTest
0004:
0005:           Licensed to the Apache Software Foundation (ASF) under one or more
0006:           contributor license agreements.  See the NOTICE file distributed with
0007:           this work for additional information regarding copyright ownership.
0008:           The ASF licenses this file to You under the Apache License, Version 2.0
0009:           (the "License"); you may not use this file except in compliance with
0010:           the License.  You may obtain a copy of the License at
0011:
0012:              http://www.apache.org/licenses/LICENSE-2.0
0013:
0014:           Unless required by applicable law or agreed to in writing, software
0015:           distributed under the License is distributed on an "AS IS" BASIS,
0016:           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0017:           See the License for the specific language governing permissions and
0018:           limitations under the License.
0019:
0020:         */
0021:
0022:        package org.apache.derbyTesting.functionTests.harness;
0023:
0024:        import org.apache.derby.tools.sysinfo;
0025:        import org.apache.derby.tools.ij;
0026:        import org.apache.derby.iapi.reference.Attribute;
0027:
0028:        import java.io.File;
0029:        import java.io.InputStream;
0030:        import java.io.InputStreamReader;
0031:        import java.io.ByteArrayInputStream;
0032:        import java.io.BufferedReader;
0033:        import java.io.FileReader;
0034:        import java.io.FileWriter;
0035:        import java.io.FileInputStream;
0036:        import java.io.FileOutputStream;
0037:        import java.io.BufferedOutputStream;
0038:        import java.io.BufferedWriter;
0039:        import java.io.FileWriter;
0040:        import java.io.OutputStreamWriter;
0041:        import java.io.PrintWriter;
0042:        import java.io.PrintStream;
0043:        import java.io.IOException;
0044:        import java.io.FileNotFoundException;
0045:        import java.lang.ClassNotFoundException;
0046:        import java.lang.ClassFormatError;
0047:        import java.lang.Thread;
0048:        import java.lang.reflect.Method;
0049:        import java.util.Enumeration;
0050:        import java.util.Locale;
0051:        import java.util.Vector;
0052:        import java.sql.Timestamp;
0053:        import java.sql.Connection;
0054:        import java.sql.SQLException;
0055:        import java.sql.DriverManager;
0056:        import java.util.Properties;
0057:        import java.util.StringTokenizer;
0058:        import java.net.URL;
0059:
0060:        import junit.framework.TestSuite;
0061:
0062:        public class RunTest {
0063:
0064:            // For printing debug info
0065:            static boolean verbose = false;
0066:            // Under some circumstances, we may need to skip the test
0067:            static boolean skiptest = false;
0068:            static StringBuffer skiptestReason = new StringBuffer();
0069:
0070:            //java requires / to look into jars, irrespective of OS
0071:            static final String testResourceHome = "/org/apache/derbyTesting/functionTests/";
0072:
0073:            // Framework support
0074:            static String[] validFrameworks = { "embedded", "", "DerbyNet",
0075:                    "DerbyNetClient", "DB2jcc", "DB2app" };
0076:            static NetServer ns;
0077:            static boolean serverNeedsStopping = false; // used in controlling network server when useprocess=false:
0078:            static boolean jvmnet = false; // switch to see if we need have client & server in a different jvm
0079:            static String jvmnetjvm; // string for class name of server jvm if different from client jvm
0080:            static String driverName;
0081:            static String dbName;
0082:
0083:            // Test properties
0084:            static String jvmName = "currentjvm";
0085:            static String javaCmd;
0086:            static String javaVersion; // System.getProperty("java.version")
0087:            static String majorVersion;
0088:            static String minorVersion;
0089:            static int jccMajor;
0090:            static int jccMinor;
0091:            static int imajor;
0092:            static int iminor;
0093:            static boolean isjdk12test = false;
0094:            static String classpath = "";
0095:            static String classpathServer = "";
0096:            public static String framework = "embedded";
0097:            public static String J9_STATEMENTCACHESIZE = "20";
0098:
0099:            static String usesystem = "";
0100:            static String searchCP = "";
0101:            static boolean useCommonDB = false;
0102:            static boolean keepfiles = false;
0103:            static boolean useprocess = true;
0104:            static boolean systemdiff = false; // can set true if there is a system diff
0105:            static boolean upgradetest = false;
0106:            static boolean encryption = false; // requires jdk12ext plus encryptionProtocol
0107:            static boolean jdk12exttest = false; // requires jdk12ext
0108:            static boolean generateUTF8Out = false; // setting to create a utf8 encoded master file.
0109:            static String runningdir = ""; // where the tests are run and suppfiles placed
0110:            static String outputdir = ""; // user can specify as a property (optional)
0111:            static String canondir; // optional (to specify other than "master")
0112:            static String bootcp; // for j9 bootclasspath
0113:            static String canonpath; // special full path (will be platform dependent)
0114:            static String mtestdir = ""; // for MultiTest user must specify testdir
0115:            static String testSpecialProps = ""; // any special suite properties
0116:            static String testJavaFlags = ""; // special command line flags
0117:            static String jvmflags; // java special flags
0118:            static boolean reportstderr = true;
0119:            static int timeout = -1; // in case tests are hanging
0120:            public static String timeoutStr;
0121:            static String jarfile; // some tests have jar files (like upgrade)
0122:            static boolean skipsed = false;
0123:            static String commonDBHome = "testCSHome";
0124:            static boolean dbIsNew = true;
0125:            static String runwithjvm = "true";
0126:            static boolean startServer = true; // should test harness start the server
0127:            static String hostName; // needs to be settable for ipv testing, localhost otherwise.)
0128:            static String testEncoding; // Encoding used for child jvm and to read the test output
0129:            static String upgradejarpath; // Encoding used for child jvm and to read the test output
0130:            static boolean replacePolicyFile = false; // property used to see if we need to replace the default policy file or append to it.
0131:
0132:            // Other test variables for directories, files, output
0133:            static String scriptName = ""; // testname as passed in
0134:            static String scriptFileName; // testname with extension
0135:            static String testDirName = ""; // test directory name
0136:            static String defaultPackageName = "/org/apache/derbyTesting/";
0137:            static String javaPath = "org.apache.derbyTesting."; // for java tests
0138:            static String testType; // sql, java, unit, etc.
0139:            static String testBase; // testname without extension
0140:            static String testOutName; // output name without path or extension (optional)
0141:            static String passFileName; // file listing passed tests
0142:            static String failFileName; // file listing failed tests
0143:            static String UTF8OutName; // file name for utf8 encoded out - not used for file comparison
0144:            static String tempMasterName; //file name for master, converted to local encoding, and for network server, corrected master
0145:            static File passFile;
0146:            static File failFile;
0147:            static String shutdownurl = "";
0148:            static boolean useOutput; // use output or assume .tmp file is produced?
0149:            static boolean outcopy; // copy support files to outDir rather than runDir
0150:            static String userdir; // current user directory
0151:            static char fileSep; // file separator for the system
0152:            static PrintWriter printWriter = null; // used to write test output to .tmp
0153:            static PrintWriter pwDiff = null; // for writing test output and info
0154:            static File script; // The file created for test files other than java tests
0155:            static File baseDir; // the DB base system dir
0156:            static boolean deleteBaseDir; // the DB base system dir
0157:            static File outDir; // test out dir
0158:            static File runDir; // where test is run and where support files are expected
0159:            static File canonDir; // allows setting master dir other than default
0160:            static File tmpOutFile; // tmp output file (before sed)
0161:            static File tempMasterFile; // master file copied into local encoding - with networkserver, also processed
0162:            static File stdOutFile; // for tests with useoutput false
0163:            static File finalOutFile; // final output file (after sed)
0164:            static File UTF8OutFile; // file name for out file copied into utf8 encoding
0165:            static File appPropFile; // testname_app.properties or default
0166:            static File clPropFile; // testname_derby.properties or default
0167:            static File diffFile; // To indicate diffs
0168:            static File tsuiteDir; // Final output dir for suite(s)
0169:            static File rsuiteDir; // Where to report .pass and .fail for suite(s)
0170:            static File extInDir; //Where all external test input files exist.
0171:            static File extOutDir; //Where all external test input files exist.
0172:            static File extInOutDir; //Where all external test input files exist.
0173:
0174:            // This test may be part of a suite
0175:            // (RunTest may need to know this is a suite to avoid dup output like sysinfo)
0176:            static String topsuitedir = ""; // in case of nested suites
0177:            static String topsuiteName = "";
0178:            static String topreportdir = "";
0179:            static String suiteName = "";
0180:            static boolean isSuiteRun = false;
0181:            static boolean lastTestFailed = false;
0182:
0183:            static boolean isI18N = false;
0184:            /** The value of derby.ui.codeset if it has been specified in the
0185:             * properties file. */
0186:            static String codeset = null;
0187:            static boolean junitXASingle = false;
0188:
0189:            /**
0190:             * Run the test without a security manager. Hopefully
0191:             * should only be used in few cases. Though initially
0192:             * may be used to bypass problematic tests and get the
0193:             * remainder of the tests running with the security manager.
0194:             */
0195:            static boolean runWithoutSecurityManager;
0196:
0197:            static InputStream isSed = null; // For test_sed.properties // Cliff
0198:
0199:            public static void main(String[] args) throws Exception {
0200:                Locale.setDefault(Locale.US);
0201:                skiptestReason.setLength(0); // 0 out for useprocess
0202:                // Determine the test type
0203:                if (args.length == 0) {
0204:                    // No script name provided
0205:                    System.out.println("no test name provided");
0206:                    System.exit(1);
0207:                }
0208:                scriptName = args[0];
0209:
0210:                if (Boolean.getBoolean("listOnly")) {
0211:                    System.out.println("LISTONLY :" + scriptName);
0212:                    return;
0213:                }
0214:
0215:                if ((scriptName == null) || (scriptName.equals(""))) {
0216:                    System.out.println("Null or blank test script name.");
0217:                    System.exit(1);
0218:                }
0219:                // If useprocess=false RunList calls this main method with 7 arguments...
0220:                if (args.length == 7) {
0221:                    defaultPackageName = args[1];
0222:                    usesystem = args[2];
0223:                    useprocess = false;
0224:                    shutdownurl = args[4];
0225:                    isSuiteRun = true;
0226:                    suiteName = args[5];
0227:                    //System.out.println("suiteName: " + suiteName);
0228:                    framework = args[6];
0229:                    // initializing startServer to true (which it would be if we'd
0230:                    // run with useprocess=true) or network server will not get started
0231:                    startServer = true;
0232:                }
0233:
0234:                testType = scriptName
0235:                        .substring(scriptName.lastIndexOf(".") + 1);
0236:
0237:                verifyTestType();
0238:
0239:                // Get the properties for the test
0240:                Properties sp = System.getProperties();
0241:
0242:                // For useprocess=false, some system wide properties need to be reset
0243:                if (useprocess == false) {
0244:                    sp.put("useprocess", "false");
0245:                    // Reset maximumDisplayWidth because some tests set this in app properties
0246:                    // and when running in same process, can cause extra long lines and diffs
0247:                    if (sp.getProperty("maximumDisplayWidth") == null)
0248:                        sp.put("maximumDisplayWidth", "128");
0249:                    if (sp.getProperty("ij.defaultResourcePackage") != null)
0250:                        sp.put("ij.defaultResourcePackage", defaultPackageName);
0251:                    System.setProperties(sp);
0252:                }
0253:
0254:                JavaVersionHolder jvhs = getProperties(sp);
0255:                boolean isJDBC4 = jvhs.atLeast(1, 6);
0256:
0257:                // Setup the directories for the test and test output
0258:                setDirectories(scriptName, sp);
0259:
0260:                if (testDirName.startsWith("i18n")) {
0261:                    isI18N = true;
0262:                }
0263:
0264:                // Check for properties files, including derby.properties
0265:                // and if needed, build the -p string to pass to the test
0266:                String propString = createPropString();
0267:                if ((isSuiteRun == false) && (useprocess)) {
0268:                    SysInfoLog sysLog = new SysInfoLog();
0269:                    sysLog.exec(jvmName, javaCmd, classpath, framework, pwDiff,
0270:                            useprocess);
0271:                }
0272:
0273:                String startTime = CurrentTime.getTime();
0274:                StringBuffer sb = new StringBuffer();
0275:                sb
0276:                        .append("*** Start: " + testBase + " jdk" + javaVersion
0277:                                + " ");
0278:                if ((framework.length() > 0)
0279:                        && (!framework.startsWith("embedded")))
0280:                    sb.append(framework + " ");
0281:                if ((suiteName != null) && (suiteName.length() > 0))
0282:                    sb.append(suiteName + " ");
0283:                sb.append(startTime + " ***");
0284:                System.out.println(sb.toString());
0285:                pwDiff.println(sb.toString());
0286:
0287:                // Run the Server if needed
0288:                if ((driverName != null) && (!skiptest)) {
0289:                    // before going further, get the policy file copied and if
0290:                    // needed, modify it with the test's policy file
0291:                    composePolicyFile();
0292:                    String spacedJvmFlags = jvmflags;
0293:                    // we now replace any '^' in jvmflags with ' '
0294:                    if ((jvmflags != null) && (jvmflags.indexOf("^") > 0)) {
0295:                        spacedJvmFlags = spaceJvmFlags(jvmflags);
0296:                    }
0297:
0298:                    System.out
0299:                            .println("Initialize for framework: " + framework);
0300:                    if (jvmnet && framework.startsWith("DerbyNet")) {
0301:                        // first check to see if properties were set to use a different jvm for server/client
0302:                        String jvmnetjvm = System.getProperty("serverJvmName");
0303:                        if (jvmnetjvm == null) {
0304:                            // default to the latest one we know 
0305:                            jvmnetjvm = "j9_22";
0306:                        }
0307:
0308:                        ns = new NetServer(baseDir, jvmnetjvm, classpathServer,
0309:                                null, spacedJvmFlags, framework, startServer);
0310:                    } else
0311:                        ns = new NetServer(baseDir, jvmName, classpathServer,
0312:                                javaCmd, spacedJvmFlags, framework, startServer);
0313:
0314:                    //  With useprocess=true, we have a new dir for each test, and all files for
0315:                    // the test, including a clean database, go in that directory. So, network server
0316:                    // for each test runs in that dir, and stops when done. If the test's properties
0317:                    // file has startServer=false the test will handle start/stop, otherwise the harness
0318:                    // needs to start and stop the server.
0319:                    //  But with useprocess=false we're using the same directory and thus the same
0320:                    // database, so there's little point in bouncing the server for each test in a suite,
0321:                    // and it would slow the suite run.
0322:                    //  So, with useprocess=true, or if we're just running 1 test with useprocess=false,
0323:                    // start network server and set serverNeedsStopping=true to have it stopped later; 
0324:                    // if useprocess=false and we're in a suite, start network server if it's not running 
0325:                    // and leave serverNeedsStopping=false, unless startServer=false, then, 
0326:                    // if network server is running, stop it.
0327:                    if ((!useprocess) && (isSuiteRun)) {
0328:                        boolean started = false;
0329:                        try {
0330:                            started = ns.testNetworkServerConnection();
0331:                        } catch (Exception e) {
0332:                        } // ignore
0333:                        if (!started && startServer)
0334:                            ns.start(); // start but don't stop, so not setting serverNeedsStopping
0335:                        if (started && !startServer)
0336:                            ns.stop();
0337:                    } else {
0338:                        ns.start();
0339:                        serverNeedsStopping = true;
0340:                    }
0341:                }
0342:
0343:                // If the test has a jar file (such as upgrade) unjar it
0344:                if (jarfile != null) {
0345:                    UnJar uj = new UnJar();
0346:                    uj.unjar(jarfile, outDir.getCanonicalPath(), true);
0347:                }
0348:
0349:                // Run the actual test (unless skiptest was set to true)
0350:                if (skiptest == false) {
0351:                    testRun(propString, sp);
0352:                } else {
0353:                    if (skiptestReason.length() == 0)
0354:                        addSkiptestReason("Test skipped: skiptest set without setting skiptestReason, please fix RunTest.java...");
0355:                    pwDiff.println(skiptestReason);
0356:                    System.out.println(skiptestReason);
0357:                    doCleanup(javaVersion);
0358:                    return;
0359:                }
0360:
0361:                // Stop the Network server if necessary
0362:                if (serverNeedsStopping) {
0363:                    ns.stop();
0364:                }
0365:
0366:                // Do "sed" to strip some unwanted stuff from the output file
0367:                // unless flag skipsed is set to true (for special cases)
0368:
0369:                String outName = finalOutFile.getPath();
0370:
0371:                if (skipsed) {
0372:                    tmpOutFile.renameTo(finalOutFile);
0373:                } else {
0374:                    try {
0375:                        Sed sed = new Sed();
0376:                        sed
0377:                                .exec(
0378:                                        tmpOutFile,
0379:                                        finalOutFile,
0380:                                        isSed,
0381:                                        NetServer.isClientConnection(framework),
0382:                                        isI18N, isJDBC4);
0383:                    } catch (ClassFormatError cfe) {
0384:                        if (verbose)
0385:                            System.out
0386:                                    .println("SED Error: " + cfe.getMessage());
0387:                    }
0388:                }
0389:                // Now do a diff between the out and the master files
0390:                // Use the system's diff if systemdiff is true
0391:                String frameworkMaster = framework;
0392:                if (framework.startsWith("embedded"))
0393:                    frameworkMaster = "";
0394:                FileCompare diff = new FileCompare();
0395:
0396:                if (verbose) {
0397:                    System.out.println("About to execute: diff.exec("
0398:                            + " outName = "
0399:                            + outName
0400:                            + ",outDir = "
0401:                            + outDir
0402:                            + ",pwDiff = "
0403:                            + pwDiff
0404:                            + ",testOutName = "
0405:                            + testOutName
0406:                            + ",frameworkMaster = "
0407:                            + frameworkMaster
0408:                            + ",jvmName = "
0409:                            + jvmName
0410:                            + ",iminor = "
0411:                            + iminor
0412:                            + ",useprocess = "
0413:                            + useprocess
0414:                            + ",systemdiff = "
0415:                            + systemdiff
0416:                            + ",canondir = "
0417:                            + canondir
0418:                            + ",canonpath = " + canonpath + ")\n");
0419:                }
0420:
0421:                boolean status;
0422:
0423:                // allow for server jvmName to be different from client jvmName
0424:                if (jvmnet) {
0425:                    // first check to see if properties were set to use a different jvm for server/client
0426:                    if (jvmnetjvm == null) {
0427:                        // default to the latest one we know 
0428:                        jvmnetjvm = "j9_22";
0429:                    }
0430:                    status = diff.exec(outName, outDir, pwDiff, testOutName,
0431:                            frameworkMaster, jvmName, iminor, useprocess,
0432:                            systemdiff, canondir, canonpath, jvmnetjvm);
0433:                } else
0434:                    status = diff.exec(outName, outDir, pwDiff, testOutName,
0435:                            frameworkMaster, jvmName, iminor, useprocess,
0436:                            systemdiff, canondir, canonpath, null);
0437:
0438:                if (status == true) {
0439:                    lastTestFailed = true;
0440:                    pwDiff.println("Test Failed.");
0441:                    System.out.println("Test Failed.");
0442:                    keepfiles = true;
0443:                    addToFailures(scriptName);
0444:                    if (useCommonDB) {
0445:                        status = baseDir.delete();
0446:                        //System.out.println("basedir delete status: " + status );
0447:                    }
0448:                } else {
0449:                    addToSuccesses(scriptName);
0450:                    pwDiff.flush();
0451:                }
0452:
0453:                generateUTF8OutFile(finalOutFile);
0454:
0455:                // Cleanup files
0456:                doCleanup(javaVersion);
0457:            }
0458:
0459:            private static void testRun(String propString, Properties sysProp)
0460:                    throws FileNotFoundException, IOException, Exception {
0461:                String systemHome = baseDir.getPath();
0462:                String scriptPath = null;
0463:                if (testType.startsWith("sql"))
0464:                    scriptPath = script.getPath();
0465:
0466:                // cleanup for all tests that re-use standard testCSHome/wombat database
0467:                if (useCommonDB == true
0468:                        && (usesystem == null || usesystem == "")
0469:                        && (testType.equals("sql") || testType.equals("java") || testType
0470:                                .equals("sql2"))) {
0471:                    dbcleanup.doit(dbIsNew);
0472:                }
0473:
0474:                // Create a process to execute the command unless useprocess is false
0475:                if (useprocess) {
0476:                    // Build the test command
0477:                    String[] testCmd = buildTestCommand(propString, systemHome,
0478:                            scriptPath);
0479:                    execTestProcess(testCmd);
0480:                } else {
0481:                    execTestNoProcess(sysProp, systemHome, propString,
0482:                            scriptPath);
0483:                }
0484:            }
0485:
0486:            /** This is the method which created directories and looks for script file,
0487:             * need to make OS specific paths here.
0488:             *
0489:             */
0490:            private static void setDirectories(String scriptName, Properties sp)
0491:                    throws ClassNotFoundException, FileNotFoundException,
0492:                    IOException {
0493:                // Get the current userdir
0494:                userdir = sp.getProperty("user.dir");
0495:
0496:                // reset defaultPackageName (for useprocess=false)
0497:                if (useprocess == false)
0498:                    defaultPackageName = "/org/apache/derbyTesting/";
0499:
0500:                // reset defaultPackageName (for useCommonDB=true)
0501:                if (useCommonDB == true) {
0502:                    defaultPackageName = "/org/apache/derbyTesting/";
0503:                }
0504:
0505:                // Set the resourceName from the default
0506:                // If not set by user, the default is used
0507:                String resourceName = defaultPackageName
0508:                        + "functionTests/tests/";
0509:                // scriptName could be of these two formats:
0510:                // testdir/test.testtype (testtype is sql, java, etc.)
0511:                // test.testtype (where the defaultPackageName includes the testdir)
0512:                int index = scriptName.lastIndexOf('/');
0513:                if (index == -1) // no test directory was specified
0514:                {
0515:                    if ((!testType.equals("sql")) && (!testType.equals("java"))
0516:                            && (!testType.equals("junit"))) {
0517:                        System.out
0518:                                .println("Test argument should be of the form: <dir>/<test>.<ext>");
0519:                        System.exit(1);
0520:                    } else {
0521:                        scriptFileName = scriptName; // such as my.sql
0522:                        resourceName += scriptName; // build the full resource name
0523:                    }
0524:                } else // the testdir was specified 
0525:                {
0526:                    testDirName = (index == 0) ? "" : scriptName.substring(0,
0527:                            index);
0528:                    //System.out.println("testDirName: " + testDirName);
0529:                    scriptFileName = scriptName.substring(index + 1, scriptName
0530:                            .length());
0531:                    //System.out.println("scriptFileName: " + scriptFileName);
0532:                    if (testType.equals("multi"))
0533:                        defaultPackageName = defaultPackageName
0534:                                + "functionTests/multi/" + testDirName + "/";
0535:                    else
0536:                        defaultPackageName = defaultPackageName
0537:                                + "functionTests/tests/" + testDirName + "/";
0538:                    //System.out.println("defaultPackage: " + defaultPackageName);
0539:                    resourceName = defaultPackageName + scriptFileName;
0540:                    //System.out.println("resource: " + resourceName);
0541:                }
0542:
0543:                // Get the test name without the extension
0544:                testBase = scriptFileName.substring(0, scriptFileName
0545:                        .lastIndexOf("." + testType));
0546:
0547:                if (testType.equals("java") || testType.equals("junit")) {
0548:                    //get the javaPath
0549:                    String tmp = defaultPackageName.replace('/', '.');
0550:                    int tl = tmp.length() - 1;
0551:                    javaPath = (tl == 0) ? "" : tmp.substring(1, tl);
0552:                }
0553:
0554:                // Check for runDir
0555:                if ((runningdir != null) && (runningdir.length() > 0)) {
0556:                    if (File.separatorChar == '\\') {
0557:                        //need to replace / in path with \ for windows
0558:                        String runningdirWin = convertPathForWin(runningdir);
0559:                        runDir = new File((new File(runningdirWin))
0560:                                .getCanonicalPath());
0561:                    } else {
0562:                        runDir = new File((new File(runningdir))
0563:                                .getCanonicalPath());
0564:                    }
0565:                }
0566:
0567:                // Define the outDir if not already defined from properties
0568:                File tmpoutDir;
0569:                String userdirWin = null;
0570:
0571:                if ((outputdir == null) || (outputdir.length() == 0)) {
0572:                    if (File.separatorChar == '\\') {
0573:                        //need to replace / in path with \ for windows
0574:                        userdirWin = convertPathForWin(userdir);
0575:                        tmpoutDir = new File((new File(userdirWin))
0576:                                .getCanonicalPath());
0577:                    } else {
0578:                        tmpoutDir = new File((new File(userdir))
0579:                                .getCanonicalPath());
0580:                    }
0581:                } else {
0582:                    if (File.separatorChar == '\\') {
0583:                        String outputdirWin = convertPathForWin(outputdir);
0584:                        tmpoutDir = new File((new File(outputdirWin))
0585:                                .getCanonicalPath());
0586:                    } else {
0587:                        tmpoutDir = new File((new File(outputdir))
0588:                                .getCanonicalPath());
0589:                    }
0590:                }
0591:
0592:                // If this is a suite run in a framework, outdir
0593:                // would already be defined to be a framework subdir
0594:                // But for RunTest, we must create the framework subdir
0595:                //if ( (!isSuiteRun) && (framework != null) && (framework.length()>0) )
0596:                if ((!isSuiteRun) && (!framework.startsWith("embedded"))) {
0597:                    runDir = tmpoutDir;
0598:                    outDir = new File(tmpoutDir, framework);
0599:                    outDir.mkdir();
0600:                } else // This is a Suite Run
0601:                {
0602:                    outDir = tmpoutDir;
0603:                    outDir.mkdir();
0604:                    if ((topsuitedir != null) && (topsuitedir.length() > 0)) {
0605:                        if (File.separatorChar == '\\') {
0606:                            String topsuitedirWin = convertPathForWin(topsuitedir);
0607:                            tsuiteDir = new File((new File(topsuitedirWin))
0608:                                    .getCanonicalPath());
0609:                        } else {
0610:                            tsuiteDir = new File((new File(topsuitedir))
0611:                                    .getCanonicalPath());
0612:                        }
0613:                    } else {
0614:                        tsuiteDir = outDir;
0615:                    }
0616:                    tsuiteDir.mkdir();
0617:                    if ((topreportdir != null) && (topreportdir.length() > 0)) {
0618:                        if (File.separatorChar == '\\') {
0619:                            String topreportdirWin = convertPathForWin(topreportdir);
0620:                            rsuiteDir = new File((new File(topreportdirWin))
0621:                                    .getCanonicalPath());
0622:                        } else {
0623:                            rsuiteDir = new File((new File(topreportdir))
0624:                                    .getCanonicalPath());
0625:                        }
0626:                    } else {
0627:                        rsuiteDir = outDir;
0628:                    }
0629:                    rsuiteDir.mkdir();
0630:                }
0631:
0632:                fileSep = File.separatorChar;
0633:
0634:                // For multi tests, the user should have specified mtestdir (full path)
0635:                // unless this is a Suite, in which case outDir is used for mtestdir
0636:                if (testType.equals("multi"))
0637:                    if ((mtestdir == null) || (mtestdir.length() == 0))
0638:                        // Use outDir for mtestdir
0639:                        mtestdir = outDir.getPath();
0640:
0641:                // For certain test types, locate script file based on scriptName
0642:                // Then determine the actual test name and directory
0643:                if ((!testType.equals("java")) && (!testType.equals("junit"))
0644:                        && (!testType.equals("unit"))
0645:                        && (!testType.equals("multi"))) {
0646:                    // NOTE: cannot use getResource because the urls returned
0647:                    // are not the same between different java environments
0648:                    InputStream is = loadTestResource("tests/" + testDirName
0649:                            + "/" + scriptFileName);
0650:
0651:                    if (is == null) {
0652:                        System.out.println("Could not locate " + scriptName);
0653:                        addToFailures(scriptName);
0654:                        throw new FileNotFoundException(resourceName);
0655:                    }
0656:
0657:                    // Read the test file and copy it to the outDir
0658:                    // except for multi tests (for multi we just need to locate it)
0659:                    BufferedReader in = new BufferedReader(
0660:                            new InputStreamReader(is, "UTF-8"));
0661:                    if (upgradetest)
0662:
0663:                        //these calls to getCanonicalPath catch IOExceptions as a workaround to
0664:                        //a bug in the EPOC jvm. 
0665:                        try {
0666:                            script = new File(
0667:                                    (new File(userdir, scriptFileName))
0668:                                            .getCanonicalPath());
0669:                        } catch (IOException e) {
0670:                            File f = new File(userdir, scriptFileName);
0671:                            FileWriter fw = new FileWriter(f);
0672:                            fw.close();
0673:                            script = new File(f.getCanonicalPath());
0674:                        }
0675:                    // else is probably only multi test
0676:                    else
0677:                        try {
0678:                            script = new File(
0679:                                    (new File(outDir, scriptFileName))
0680:                                            .getCanonicalPath());
0681:                        } catch (IOException e) {
0682:                            File f = new File(outDir, scriptFileName);
0683:                            FileWriter fw = new FileWriter(f);
0684:                            fw.close();
0685:                            script = new File(f.getCanonicalPath());
0686:                        }
0687:
0688:                    PrintWriter pw = null;
0689:                    pw = new PrintWriter(new BufferedWriter(new FileWriter(
0690:                            script.getPath()), 10000), true);
0691:
0692:                    String str = "";
0693:                    while ((str = in.readLine()) != null) {
0694:                        pw.println(str);
0695:                    }
0696:                    pw.close();
0697:                    pw = null;
0698:                    in = null;
0699:                }
0700:
0701:                // This is the base directory for creating a database (under the outDir)
0702:                baseDir = null;
0703:
0704:                if (useCommonDB == true) {
0705:                    if (File.separatorChar == '\\') {
0706:                        String commonDBHomeWin = convertPathForWin(commonDBHome);
0707:                        baseDir = new File(userdirWin, commonDBHomeWin);
0708:                    } else {
0709:                        baseDir = new File(userdir, commonDBHome);
0710:                    }
0711:                } else if ((!useprocess) && isSuiteRun
0712:                        && ((usesystem == null) || (usesystem.length() <= 0))) {
0713:                    String suite = (suiteName.substring(0, suiteName
0714:                            .indexOf(':')));
0715:                    if (File.separatorChar == '\\') {
0716:                        String useprWin = convertPathForWin(suite);
0717:                        baseDir = new File(outDir, useprWin);
0718:                    } else {
0719:                        baseDir = new File(outDir, suite);
0720:                    }
0721:                } else if ((usesystem != null) && (usesystem.length() > 0)) {
0722:                    if (File.separatorChar == '\\') {
0723:                        String usesystemWin = convertPathForWin(usesystem);
0724:                        if (upgradetest == true)
0725:                            baseDir = new File(userdirWin, usesystemWin);
0726:                        else
0727:                            baseDir = new File(outDir, usesystemWin);
0728:                    } else {
0729:                        if (upgradetest == true)
0730:                            baseDir = new File(userdir, usesystem);
0731:                        else
0732:                            baseDir = new File(outDir, usesystem);
0733:                    }
0734:                } else {
0735:                    if (File.separatorChar == '\\') {
0736:                        String testBaseWin = convertPathForWin(testBase);
0737:                        baseDir = new File(outDir, testBaseWin);
0738:                    } else {
0739:                        baseDir = new File(outDir, testBase);
0740:                    }
0741:                }
0742:
0743:                // clean up old db dirs
0744:                // (except for special cases such as nist, commonDB).
0745:                // In the case of useCommonDB == true, the baseDir (commonDBHome) only gets
0746:                // cleaned up if the last test was a failure.  Further refinements may
0747:                // follow, since many test failures probably do not require such drastic
0748:                // action.
0749:                if (baseDir.exists()) {
0750:                    if (useCommonDB == false || lastTestFailed == true) {
0751:                        cleanupBaseDir(baseDir);
0752:                        lastTestFailed = false;
0753:                    } else if (useCommonDB == true)
0754:                        dbIsNew = false; // dbcleanup may be needed
0755:                } else {
0756:
0757:                    boolean created = baseDir.mkdir();
0758:                    dbIsNew = true; // dbcleanup not needed on new database
0759:                }
0760:
0761:                // Determine if it is ok to delete base when done
0762:                if ((usesystem == null) || (usesystem.length() <= 0))
0763:                    deleteBaseDir = true; // ok to delete base when done
0764:                else
0765:                    deleteBaseDir = false; // keep db dir for nist & puzzles in case of failures
0766:
0767:                // testOutName used to create the tmpOutFile
0768:                // this is probably always going to be testBase
0769:                if (testOutName == null) {
0770:                    if (testType.equals("demo"))
0771:                        testOutName = testBase
0772:                                .substring(testBase.indexOf(".") + 1);
0773:                    else
0774:                        testOutName = testBase;
0775:                }
0776:
0777:                // Create a .tmp file for doing sed later to create testBase.out
0778:                tmpOutFile = new File(outDir, testOutName + ".tmp");
0779:                // Always create a.tmpmstr copy of the master file in local encoding.
0780:                // With network server, this gets adjusted for displaywidth 
0781:                tempMasterName = testOutName + ".tmpmstr";
0782:                UTF8OutName = testOutName + ".utf8out";
0783:                // Define the .out file which will be created by massaging the tmp.out
0784:                finalOutFile = new File(outDir, testOutName + ".out");
0785:
0786:                // Define the .diff file which will contain diffs and other info
0787:                diffFile = new File(outDir, testOutName + ".diff");
0788:                stdOutFile = new File(outDir, testOutName + ".std");
0789:
0790:                // Define also the .pass and .fail files        
0791:                if (isSuiteRun) {
0792:                    String sname = suiteName.substring(0, suiteName
0793:                            .indexOf(":"));
0794:                    //System.out.println("sname: " + sname);
0795:                    //System.out.println("topsuiteName: " + topsuiteName);
0796:                    passFileName = sname + ".pass";
0797:                    passFile = new File(rsuiteDir, passFileName);
0798:                    failFileName = sname + ".fail";
0799:                    failFile = new File(rsuiteDir, failFileName);
0800:                } else {
0801:                    passFileName = testBase + ".pass";
0802:                    passFile = new File(outDir, passFileName);
0803:                    failFileName = testBase + ".fail";
0804:                    failFile = new File(outDir, failFileName);
0805:                }
0806:                //System.out.println("passFileName: " + passFileName);
0807:                boolean status = true;
0808:
0809:                // Delete any old .out or .tmp files
0810:                if (tmpOutFile.exists())
0811:                    status = tmpOutFile.delete();
0812:                tempMasterFile = new File(outDir, tempMasterName);
0813:                if (tempMasterFile.exists())
0814:                    status = tempMasterFile.delete();
0815:                if (finalOutFile.exists())
0816:                    status = finalOutFile.delete();
0817:                if (diffFile.exists())
0818:                    status = diffFile.delete();
0819:                if (stdOutFile.exists())
0820:                    status = stdOutFile.delete();
0821:                UTF8OutFile = new File(outDir, UTF8OutName);
0822:                if (UTF8OutFile.exists())
0823:                    status = UTF8OutFile.delete();
0824:
0825:                // Delete any old pass or fail files
0826:                if (!isSuiteRun) {
0827:                    if (failFile.exists())
0828:                        status = failFile.delete();
0829:                    if (passFile.exists())
0830:                        status = passFile.delete();
0831:                }
0832:
0833:                if (status == false)
0834:                    System.out
0835:                            .println("Unable to delete tmp, out and/or diff files to start");
0836:
0837:                // Create a PrintWriter for writing env and test info to the diff file
0838:                pwDiff = new PrintWriter(new BufferedWriter(new FileWriter(
0839:                        diffFile.getPath()), 4096), true);
0840:            }
0841:
0842:            private static JavaVersionHolder getProperties(Properties sp)
0843:                    throws Exception {
0844:                // Get any properties specified on the command line
0845:
0846:                // before doing anything else, get jvmflags, evaluate any -D 
0847:                // see if there is anything useful to the test harness in jvmflags
0848:                if ((jvmflags != null) && (jvmflags.length() > 0)) {
0849:                    StringTokenizer st = new StringTokenizer(jvmflags, "^");
0850:                    while (st.hasMoreTokens()) {
0851:                        String tmpstr = st.nextToken();
0852:                        if ((tmpstr.indexOf("=") > 0)
0853:                                && (tmpstr.startsWith("-D"))) {
0854:                            // strip off the '-D'
0855:                            String key = tmpstr.substring(2, tmpstr
0856:                                    .indexOf("="));
0857:                            String value = tmpstr.substring((tmpstr
0858:                                    .indexOf("=") + 1), tmpstr.length());
0859:                            sp.put(key, value);
0860:                        }
0861:                    }
0862:                }
0863:
0864:                searchCP = sp.getProperty("ij.searchClassPath");
0865:                String frameworkp = sp.getProperty("framework");
0866:                if (frameworkp != null)
0867:                    framework = frameworkp;
0868:                if (framework == null)
0869:                    framework = "embedded";
0870:                if (!verifyFramework(framework))
0871:                    framework = "";
0872:                else
0873:                    driverName = NetServer.getDriverName(framework);
0874:                String junitXAProp = sp.getProperty("derbyTesting.xa.single");
0875:                if (junitXAProp != null && junitXAProp.equals("true")) {
0876:                    junitXASingle = true;
0877:                }
0878:                hostName = sp.getProperty("hostName");
0879:                // force hostName to localhost if it is not set
0880:                if (hostName == null)
0881:                    hostName = "localhost";
0882:
0883:                String generateUTF8OutProp = sp.getProperty("generateUTF8Out");
0884:                if (generateUTF8OutProp != null
0885:                        && generateUTF8OutProp.equals("true"))
0886:                    generateUTF8Out = true;
0887:
0888:                // Some tests will not work with some frameworks,
0889:                // so check suite exclude files for tests to be skipped
0890:                String skipFile = framework + ".exclude";
0891:                if (!framework.equals("")) {
0892:                    skiptest = (SkipTest.skipIt(skipFile, scriptName));
0893:                    // in addition, check to see if the test should get skipped 
0894:                    // because it's not suitable for a remotely started server
0895:                    if (!skiptest) {
0896:                        if (!hostName.equals("localhost")) {
0897:                            skipFile = framework + "Remote.exclude";
0898:                            skiptest = (SkipTest.skipIt(skipFile, scriptName));
0899:                        }
0900:                    }
0901:                    if (skiptest) // if we're skipping...
0902:                        addSkiptestReason("Test skipped: listed in " + skipFile
0903:                                + " file, skipping test: " + scriptName);
0904:                }
0905:
0906:                jvmName = sp.getProperty("jvm");
0907:
0908:                //System.out.println("jvmName is: " + jvmName);
0909:                if ((jvmName == null) || (jvmName.length() == 0)
0910:                        || (jvmName.equals("jview"))) {
0911:                    javaVersion = System.getProperty("java.version");
0912:                    //System.out.println("javaVersion is: " + javaVersion);
0913:                } else
0914:                    javaVersion = jvmName;
0915:
0916:                //hang on a minute - if j9, we need to check further
0917:                String javavmVersion;
0918:                if (sp.getProperty("java.vm.name").equals("J9"))
0919:                    javavmVersion = (sp.getProperty("java.vm.version"));
0920:                else
0921:                    javavmVersion = javaVersion;
0922:
0923:                JavaVersionHolder jvh = new JavaVersionHolder(javavmVersion);
0924:                majorVersion = jvh.getMajorVersion();
0925:                minorVersion = jvh.getMinorVersion();
0926:                iminor = jvh.getMinorNumber();
0927:                imajor = jvh.getMajorNumber();
0928:
0929:                if ((jvmName == null) || (!jvmName.equals("jview"))) {
0930:                    if ((iminor < 2) && (imajor < 2))
0931:                        jvmName = "currentjvm";
0932:                    else {
0933:                        if (System.getProperty("java.vm.vendor").startsWith(
0934:                                "IBM")) {
0935:                            if (System.getProperty("java.vm.name").equals("J9")) {
0936:                                if (System.getProperty(
0937:                                        "com.ibm.oti.configuration").equals(
0938:                                        "foun10")) {
0939:                                    jvmName = "j9_foundation";
0940:                                } else {
0941:                                    // for reporting; first extend javaVersion
0942:                                    javaVersion = javaVersion + " - "
0943:                                            + majorVersion + "." + minorVersion;
0944:                                    // up to j9 2.1 (jdk 1.3.1 subset) the results are the same for all versions,
0945:                                    // or we don't care about it anymore. Switch back to 1.3. (java.version) values.
0946:                                    if ((imajor <= 2) && (iminor < 2)) {
0947:                                        majorVersion = "1";
0948:                                        minorVersion = "3";
0949:                                        imajor = 1;
0950:                                        iminor = 3;
0951:                                    }
0952:                                    jvmName = "j9_" + majorVersion
0953:                                            + minorVersion;
0954:                                }
0955:                            } else
0956:                                jvmName = "ibm" + majorVersion + minorVersion;
0957:                        } else
0958:                            jvmName = "jdk" + majorVersion + minorVersion;
0959:                    }
0960:                }
0961:
0962:                // create a JavaVersionHolder for the java.specification.version - 
0963:                // used to control Sed-ing for JDBC4 & up
0964:                String specversion = (sp
0965:                        .getProperty("java.specification.version"));
0966:                JavaVersionHolder jvhs = new JavaVersionHolder(specversion);
0967:
0968:                testEncoding = sp.getProperty("derbyTesting.encoding");
0969:                upgradejarpath = sp.getProperty("derbyTesting.jar.path");
0970:                if ((testEncoding != null) && (!jvmName.equals("jdk15"))) {
0971:                    skiptest = true;
0972:                    addSkiptestReason("derbyTesting.encoding can only be used with jdk15, skipping test");
0973:                }
0974:
0975:                String replace_policy = sp
0976:                        .getProperty("derbyTesting.replacePolicyFile");
0977:                if ((replace_policy != null) && (replace_policy.equals("true")))
0978:                    replacePolicyFile = true;
0979:                else
0980:                    replacePolicyFile = false;
0981:
0982:                javaCmd = sp.getProperty("javaCmd");
0983:                bootcp = sp.getProperty("bootcp");
0984:                jvmflags = sp.getProperty("jvmflags");
0985:                testJavaFlags = sp.getProperty("testJavaFlags");
0986:                classpath = sp.getProperty("classpath");
0987:                //System.out.println("classpath set to: " + classpath);
0988:                classpathServer = sp.getProperty("classpathServer");
0989:                if ((classpathServer == null)
0990:                        || (classpathServer.startsWith("${")))
0991:                    classpathServer = classpath;
0992:                //System.out.println("classpathServer set to: " + classpathServer);
0993:                jarfile = sp.getProperty("jarfile");
0994:                String upg = sp.getProperty("upgradetest");
0995:                if (upg != null) {
0996:                    upg = upg.toLowerCase();
0997:                    if (upg.equals("true"))
0998:                        upgradetest = true;
0999:                }
1000:
1001:                if (framework.equals("DerbyNet")
1002:                        && (!jvmName.equals("j9_foundation"))) {
1003:
1004:                    Class c = null;
1005:                    Method m = null;
1006:                    Object o = null;
1007:                    Integer i = null;
1008:                    try {
1009:                        c = Class.forName("com.ibm.db2.jcc.DB2Driver");
1010:                        o = c.newInstance();
1011:                        m = c.getMethod("getMajorVersion", null);
1012:                        i = (Integer) m.invoke(o, null);
1013:                        jccMajor = i.intValue();
1014:                        m = c.getMethod("getMinorVersion", null);
1015:                        i = (Integer) m.invoke(o, null);
1016:                        jccMinor = i.intValue();
1017:                    } catch (ClassNotFoundException e) {
1018:                    }
1019:
1020:                    String excludeJcc = sp.getProperty("excludeJCC");
1021:                    try {
1022:                        RunList.checkClientExclusion(excludeJcc, "JCC",
1023:                                jccMajor, jccMinor, javaVersion);
1024:                    } catch (Exception e) {
1025:                        skiptest = true;
1026:                        addSkiptestReason(e.getMessage());
1027:                    }
1028:                }
1029:
1030:                String sysdiff = sp.getProperty("systemdiff");
1031:                if (sysdiff != null) {
1032:                    sysdiff = sysdiff.toLowerCase();
1033:                    if (sysdiff.equals("true"))
1034:                        systemdiff = true;
1035:                }
1036:                String keep = sp.getProperty("keepfiles");
1037:                if (keep != null) {
1038:                    keep = keep.toLowerCase();
1039:                    if (keep.equals("true"))
1040:                        keepfiles = true;
1041:                }
1042:                String encrypt = sp.getProperty("encryption");
1043:                if ((encrypt != null) && (encrypt.equalsIgnoreCase("true")))
1044:                    encryption = true;
1045:                String jdk12ext = sp.getProperty("jdk12exttest");
1046:                if ((jdk12ext != null) && (jdk12ext.equalsIgnoreCase("true")))
1047:                    jdk12exttest = true; // applied to jdk12 or higher
1048:                if (encryption || jdk12exttest) {
1049:                    // Must be running jdk12 or higher and must have extensions
1050:                    if (iminor < 2) // this is 1.1.x
1051:                    {
1052:                        skiptest = true;
1053:                        addSkiptestReason("Test skipped: encryption or jdk12exttest requires jdk12 or higher; this is jdk1"
1054:                                + iminor + ", skipping test: " + scriptFileName);
1055:                    } else // now check for extensions
1056:                    {
1057:                        try {
1058:                            Class jtaClass = Class
1059:                                    .forName("javax.transaction.xa.Xid");
1060:                        } catch (ClassNotFoundException cnfe) {
1061:                            // at least one of the extension classes was not found
1062:                            skiptest = true;
1063:                            addSkiptestReason("Test skipped: javax.transaction.xa.Xid not found, skipping test: "
1064:                                    + scriptFileName);
1065:                        }
1066:                        try {
1067:                            Class jdbcClass = Class.forName("javax.sql.RowSet");
1068:                        } catch (ClassNotFoundException cnfe2) {
1069:                            // at least one of the extension classes was not found
1070:                            skiptest = true;
1071:                            addSkiptestReason("Test skipped: javax.sql.RowSet not found, skipping test: "
1072:                                    + scriptFileName);
1073:                        }
1074:                    }
1075:                }
1076:                runningdir = sp.getProperty("rundir");
1077:                if (runningdir == null)
1078:                    runningdir = "";
1079:                outputdir = sp.getProperty("outputdir");
1080:                if (outputdir == null)
1081:                    outputdir = "";
1082:
1083:                canondir = sp.getProperty("canondir");
1084:                canonpath = sp.getProperty("canonpath");
1085:
1086:                testOutName = sp.getProperty("testoutname");
1087:                useOutput = new Boolean(sp.getProperty("useoutput", "true"))
1088:                        .booleanValue();
1089:                outcopy = new Boolean(sp.getProperty("outcopy", "false"))
1090:                        .booleanValue();
1091:                mtestdir = sp.getProperty("mtestdir"); // used by multi tests
1092:                if (mtestdir == null)
1093:                    mtestdir = "";
1094:
1095:                String usepr = sp.getProperty("useprocess");
1096:                if (usepr != null) {
1097:                    usepr = usepr.toLowerCase();
1098:                    if (usepr.equals("false"))
1099:                        useprocess = false;
1100:                    else
1101:                        useprocess = true;
1102:                } else
1103:                    useprocess = true;
1104:
1105:                // if the hostName is something other than localhost, we must
1106:                // be trying to connect to a remote server, and so, 
1107:                // startServer should be false.
1108:                if (!hostName.equals("localhost")) {
1109:                    startServer = false;
1110:                }
1111:
1112:                String nosed = sp.getProperty("skipsed");
1113:                if (nosed != null) {
1114:                    nosed = nosed.toLowerCase();
1115:                    if (nosed.equals("true"))
1116:                        skipsed = true;
1117:                }
1118:
1119:                String dbug = sp.getProperty("verbose");
1120:                if (dbug != null) {
1121:                    dbug = dbug.toLowerCase();
1122:                    if (dbug.equals("true"))
1123:                        verbose = true;
1124:                }
1125:                String rstderr = sp.getProperty("reportstderr");
1126:                if (rstderr != null) {
1127:                    rstderr = rstderr.toLowerCase();
1128:                    if (rstderr.equals("false"))
1129:                        reportstderr = false;
1130:                }
1131:
1132:                // default to -1 (no timeout) if no property is set
1133:                if (timeoutStr == null) {
1134:                    timeoutStr = sp.getProperty("timeout", "-1");
1135:                    //System.out.println("+++setting timeoutStr to " + timeoutStr + " in RunTest::getProperties");
1136:                } else {
1137:                    //System.out.println("+++timeoutStr was already " + timeoutStr + " in RunTest::getProperties");
1138:                }
1139:
1140:                try {
1141:                    timeout = Integer.parseInt(timeoutStr);
1142:                } catch (NumberFormatException nfe) {
1143:                    timeout = -1;
1144:                }
1145:
1146:                //System.out.println("RunTest timeout is: " + timeout);
1147:
1148:                testSpecialProps = sp.getProperty("testSpecialProps");
1149:
1150:                if (useprocess) {
1151:                    String defrespckg = sp
1152:                            .getProperty("ij.defaultResourcePackage");
1153:                    if (defrespckg != null) // if not set there is a default defined
1154:                    {
1155:                        defaultPackageName = defrespckg;
1156:                        if (!defaultPackageName.endsWith("/"))
1157:                            defaultPackageName += "/";
1158:                    }
1159:                    usesystem = sp.getProperty("usesystem");
1160:                }
1161:
1162:                // junit tests do not run successfully with j9 - except for j9_foundation 
1163:                // see DERBY-1785
1164:                if ((jvmName.startsWith("j9"))
1165:                        && (!jvmName.equals("j9_foundation"))
1166:                        && testType.equals("junit")) {
1167:                    skiptest = true;
1168:                    addSkiptestReason("Test skipped: cannot run junit test with j9 jvms because of security manager issue in the jvm");
1169:                }
1170:
1171:                // Some tests will not run well in a suite with use process false 
1172:                // with some frameworks, so skip
1173:                if (!useprocess && !skiptest) {
1174:                    String tsuiteName = null;
1175:                    if (suiteName != null)
1176:                        tsuiteName = suiteName;
1177:                    else
1178:                        tsuiteName = sp.getProperty("suitename");
1179:                    if ((tsuiteName != null) && (tsuiteName.length() > 0)) {
1180:                        skipFile = framework + "Useprocess.exclude";
1181:                        if (!framework.equals("")) {
1182:                            skiptest = (SkipTest.skipIt(skipFile, scriptName));
1183:                            if (skiptest) {
1184:                                skiptest = true;
1185:                                addSkiptestReason("Test " + scriptName
1186:                                        + " skipped, " + "listed in "
1187:                                        + framework
1188:                                        + "Useprocess.exclude file.");
1189:                            }
1190:                        }
1191:                    }
1192:                }
1193:
1194:                if ((useprocess) || (suiteName == null)) {
1195:                    if (useprocess)
1196:                        suiteName = sp.getProperty("suitename");
1197:                    if ((suiteName != null) && (suiteName.length() > 0)) {
1198:                        // This is a suite run
1199:                        isSuiteRun = true;
1200:                        if (useprocess)
1201:                            // If a suite, it could be part of a top suite
1202:                            topsuiteName = sp.getProperty("topsuitename");
1203:                        topsuitedir = sp.getProperty("topsuitedir");
1204:                        topreportdir = sp.getProperty("topreportdir");
1205:                    }
1206:                }
1207:                String uscdb = sp.getProperty("useCommonDB");
1208:                if (uscdb != null && uscdb.equals("true"))
1209:                    useCommonDB = true;
1210:
1211:                return jvhs;
1212:            }
1213:
1214:            private static String createPropString()
1215:                    throws ClassNotFoundException, FileNotFoundException,
1216:                    IOException {
1217:                // Check for existence of app properties and/or derby.properties files
1218:                // Copy the derby.properties to the db base directory
1219:                // Then create the -p string for the test
1220:                String propString = "";
1221:
1222:                // General purpose variables
1223:                BufferedReader in = null;
1224:                BufferedOutputStream bos = null;
1225:                BufferedOutputStream bos2 = null;
1226:                String str = "";
1227:
1228:                // InputStreams for all possible properties files
1229:                InputStream isCl = null; // For test_derby.properties
1230:                InputStream isClDef = null; // For default_derby.properties
1231:                InputStream isAp = null; // For test_app.properties
1232:                InputStream isApDef = null; // For default_app.properties
1233:
1234:                // Resource names for locating the various properties files
1235:                String clDefProp = "tests/" + testDirName + "/"
1236:                        + "default_derby.properties";
1237:                String apDefProp = null;
1238:                if (useCommonDB)
1239:                    apDefProp = "tests/" + testDirName + "/"
1240:                            + "commonDB_app.properties";
1241:                else
1242:                    apDefProp = "tests/" + testDirName + "/"
1243:                            + "default_app.properties";
1244:
1245:                // Properties
1246:                Properties clp = new Properties();
1247:                Properties ap = new Properties();
1248:
1249:                // If there are special flags for ij or server, load these
1250:                // into properties to be merged with app and/or derby props
1251:                Properties ijProps = new Properties();
1252:                Properties srvProps = new Properties();
1253:                if ((testSpecialProps != null)
1254:                        && (testSpecialProps.length() > 0)) {
1255:                    SpecialFlags.parse(testSpecialProps, ijProps, srvProps);
1256:                }
1257:
1258:                /* If there are more than one derby.properties, the rule is to
1259:                   load either the test_derby.properties or the default one,
1260:                 */
1261:
1262:                // Check for default_derby.properties
1263:                isClDef = loadTestResource(clDefProp);
1264:                // Check for test specific props 
1265:                isCl = loadTestResource("tests/" + testDirName + "/" + testBase
1266:                        + "_derby.properties");
1267:                //System.out.println("**************");
1268:                //System.out.println("isCl = " + isCl);
1269:                //System.out.println(defaultPackageName + testBase + "_derby.properties");
1270:                //System.out.println("**************");
1271:
1272:                // Now load and merge the properties based on above rules
1273:                if (isCl != null) // In case there exists a test_derby.properties
1274:                {
1275:                    clp.load(isCl);
1276:                    clp.list(System.out);
1277:                }
1278:                // Try the default_derby.properties instead
1279:                else if (isClDef != null) {
1280:                    clp.load(isClDef);
1281:                }
1282:
1283:                // j9 will run out of memory with the default cache size (100), so
1284:                // forcing it lower unless set in _derby.properties file for a specific test
1285:                if (jvmName.startsWith("j9")) {
1286:                    if (clp.getProperty("derby.language.statementCacheSize") == null)
1287:                        clp.put("derby.language.statementCacheSize",
1288:                                J9_STATEMENTCACHESIZE);
1289:                }
1290:
1291:                // Now merge any special server props if they exist
1292:                // But if clp is still empty, try using the special server props
1293:                if (clp.isEmpty()) {
1294:                    // Check for srvProps from testSpecialProps
1295:                    if (!srvProps.isEmpty())
1296:                        clp = srvProps;
1297:                } else {
1298:                    // merge any special properties from testSpecialProps
1299:                    if (!srvProps.isEmpty()) {
1300:                        for (Enumeration e = srvProps.propertyNames(); e
1301:                                .hasMoreElements();) {
1302:                            String key = (String) e.nextElement();
1303:                            String value = srvProps.getProperty(key);
1304:                            if (key.equals("derby.debug.true")) // Add to existing prop
1305:                            {
1306:                                String cval = clp
1307:                                        .getProperty("derby.debug.true");
1308:                                // If this property exists, edit to prepend the srvProp
1309:                                // but if the original property is null, just put the srvProp
1310:                                if (cval != null) {
1311:                                    if (cval.length() != 0) {
1312:                                        // debug property exists, so edit it
1313:                                        value = value + "," + cval;
1314:                                    } else {
1315:                                        // if new debug property is not null, but is zero length, 
1316:                                        // assume the intention was to override the debug property.
1317:                                        value = "";
1318:                                    }
1319:                                }
1320:                            }
1321:                            clp.put(key, value);
1322:                        }
1323:                    }
1324:                }
1325:
1326:                if (!clp.isEmpty()) {
1327:                    // Create and load the file
1328:
1329:                    // This call to getCanonicalPath catches IOExceptions as a workaround to
1330:                    // a bug in the EPOC jvm. 
1331:                    try {
1332:                        clPropFile = new File((new File(baseDir,
1333:                                "derby.properties")).getCanonicalPath());
1334:                    } catch (IOException e) {
1335:                        File f = new File(baseDir, "derby.properties");
1336:                        FileWriter fw = new FileWriter(f);
1337:                        fw.close();
1338:                        clPropFile = new File(f.getCanonicalPath());
1339:                    }
1340:
1341:                    //System.out.println("clPropFile: " + clPropFile.getPath());
1342:                    bos = new BufferedOutputStream(new FileOutputStream(
1343:                            clPropFile));
1344:                    clp.store(bos, "Derby Properties");
1345:                    bos.close();
1346:                }
1347:
1348:                // --------------------------------- 
1349:                // Check for existence of sed properties file (test_sed.properties)
1350:                // See jdbc_sed.properties
1351:                //  Multiple patterns for DELETE: comma separated
1352:                //    delete=pattern1,pattern2,...,patternn
1353:                //  No commas can be allowed in the patterns.
1354:                // 
1355:                //  Multiple patterns for SUBSTITUTE: comma separated <pattern;substitute> pair
1356:                //    substitute=pattern1;substitute1,pattern2;substitute2,...,patternn;substituten
1357:                //  No commas or semicolons can be allowed in the patterns/subsitutes.  //
1358:
1359:                if (testType.equals("multi"))
1360:                    isSed = loadTestResource("multi/stress/" + testBase
1361:                            + "_sed.properties");
1362:                else
1363:                    isSed = loadTestResource("tests/" + testDirName + "/"
1364:                            + testBase + "_sed.properties");
1365:                //System.out.println("**************");
1366:                //System.out.println("isSed = " + isSed);
1367:                //System.out.println(defaultPackageName + testBase + "_sed.properties");
1368:                //System.out.println("**************");
1369:                // --------------------------------- 
1370:
1371:                // Check for existence of app properties file
1372:                // If there is an test_app, use it to overwrite default_app
1373:                // Then create the -p string for the test
1374:                Properties dp = new Properties();
1375:                String testPropName = null;
1376:                String testPropSDName = null; // name for shutdown properties file if needed
1377:                if (useCommonDB)
1378:                    testPropName = "CDB" + testBase + "_app.properties";
1379:                else
1380:                    testPropName = testBase + "_app.properties";
1381:
1382:                // Check for default_app.properties
1383:                isApDef = loadTestResource(apDefProp);
1384:
1385:                // Check for test_app.properties
1386:
1387:                if (testType.equals("multi"))
1388:                    isAp = loadTestResource("multi/" + testDirName + "/"
1389:                            + testBase + "_app.properties");
1390:                else
1391:                    isAp = loadTestResource("tests/" + testDirName + "/"
1392:                            + testBase + "_app.properties");
1393:                //System.out.println("**************");
1394:                //System.out.println("isAp = " + isAp);
1395:                //System.out.println(defaultPackageName + testBase + "_app.properties");
1396:                //System.out.println("**************");
1397:
1398:                // Try loading the ap and def properties if they exist
1399:                // Merge only if the test's app properties has usedefaults property
1400:                if (isAp != null) {
1401:                    ap.load(isAp);
1402:                    // Check for a property usedefaults; if true merge in default props
1403:                    for (Enumeration e = ap.propertyNames(); e
1404:                            .hasMoreElements();) {
1405:                        String key = (String) e.nextElement();
1406:                        String value = ap.getProperty(key);
1407:                        if ((key.equals("usedefaults"))
1408:                                && (value.equals("true"))) {
1409:                            // merge in the default properties
1410:                            if (isApDef != null) {
1411:                                dp.load(isApDef);
1412:                                mergeProps(ap, dp);
1413:                                break;
1414:                            }
1415:                        }
1416:                    }
1417:                } else {
1418:                    // Just use the default props
1419:                    if (isApDef != null)
1420:                        ap.load(isApDef);
1421:                }
1422:
1423:                // If app props are still empty, check for any special testSpecialProps
1424:                if (ap.isEmpty()) {
1425:                    if (!ijProps.isEmpty())
1426:                        ap = ijProps;
1427:                } else {
1428:                    // merge any special properties from testSpecialProps
1429:                    if (!ijProps.isEmpty()) {
1430:                        for (Enumeration e = ijProps.propertyNames(); e
1431:                                .hasMoreElements();) {
1432:                            String key = (String) e.nextElement();
1433:                            String value = ijProps.getProperty(key);
1434:                            ap.put(key, value);
1435:                        }
1436:                    }
1437:                }
1438:
1439:                if (!ap.isEmpty()) {
1440:                    // Create the file and load the properties
1441:                    // This call to getCanonicalPath catches IOExceptions as a workaround to
1442:                    // a bug in the EPOC jvm. 
1443:                    try {
1444:                        appPropFile = new File(
1445:                                (new File(baseDir, testPropName))
1446:                                        .getCanonicalPath());
1447:                    } catch (IOException e) {
1448:                        File f = new File(baseDir, testPropName);
1449:                        FileWriter fw = new FileWriter(f);
1450:                        fw.close();
1451:                        appPropFile = new File(f.getCanonicalPath());
1452:                    }
1453:
1454:                    // For IBM14 the console encoding is different from the platform
1455:                    // encoding on windows.  We want it to be the same for our
1456:                    // test output like the other JDK's.
1457:                    //
1458:                    // For i18n test, we want UTF-8 encoding (DERBY-244).
1459:                    String conEnc = System.getProperty("console.encoding");
1460:                    String fileEnc = System.getProperty("file.encoding");
1461:
1462:                    if (ap.getProperty("derby.ui.codeset") != null) {
1463:                        // derby.ui.codeset is specified explicitly, don't override
1464:                        codeset = ap.getProperty("derby.ui.codeset");
1465:                    } else if (isI18N) {
1466:                        ap.put("derby.ui.codeset", "UTF-8");
1467:                    } else if ((conEnc != null) && (fileEnc != null)
1468:                            && conEnc.startsWith("Cp850")) {
1469:                        ap.put("derby.ui.codeset", fileEnc);
1470:                    }
1471:
1472:                    if (verbose)
1473:                        System.out.println("console.encoding:" + conEnc
1474:                                + " file.encoding:" + fileEnc
1475:                                + " derby.ui.codeset: "
1476:                                + ap.getProperty("derby.ui.codeset"));
1477:
1478:                    // If the initial connection is being specified as a DataSource
1479:                    // on the command line using -Dij.dataSource=<dsclassname>
1480:                    // then remove the ij.database property that comes from any
1481:                    // default_app or other properties file. This is because the
1482:                    // ij.database will override the ij.dataSource property.
1483:                    if (System.getProperty("ij.dataSource") != null) {
1484:                        ap.remove("ij.database");
1485:                        ap.remove("ij.protocol");
1486:                    }
1487:
1488:                    //System.out.println("appPropFile: " + appPropFile.getPath());
1489:                    bos = new BufferedOutputStream(new FileOutputStream(
1490:                            appPropFile));
1491:                    ap.store(bos, "App Properties");
1492:                    bos.close();
1493:
1494:                    // Check now through jvmflags for insteresting properties
1495:                    // First grab jvmflags from _app.properties for the jvm process cannot
1496:                    // use it if just in the test's _app.properties file
1497:                    // note that it's already too late if useprocess is false
1498:                    String apppropsjvmflags = ap.getProperty("jvmflags");
1499:                    if (apppropsjvmflags != null) {
1500:                        if (jvmflags != null)
1501:                            jvmflags = apppropsjvmflags + "^" + jvmflags;
1502:                        else
1503:                            jvmflags = apppropsjvmflags;
1504:                    }
1505:                    // see if there is anything useful for the test harness in jvmflags
1506:                    // from commandline or suite properties
1507:                    if ((jvmflags != null) && (jvmflags.length() > 0)) {
1508:                        StringTokenizer st = new StringTokenizer(jvmflags, "^");
1509:                        while (st.hasMoreTokens()) {
1510:
1511:                            String tmpstr = st.nextToken();
1512:                            if ((tmpstr.indexOf("=") > 0)
1513:                                    && (tmpstr.startsWith("-D"))) {
1514:                                // start at position 2, i.e. strip off the "-D"
1515:                                String key = tmpstr.substring(2, tmpstr
1516:                                        .indexOf("="));
1517:                                String value = tmpstr.substring((tmpstr
1518:                                        .indexOf("=") + 1), tmpstr.length());
1519:                                ap.put(key, value);
1520:                            }
1521:                        }
1522:                    }
1523:
1524:                    // Depending on the framework, the app prop file may need editing
1525:                    if ((framework.length() > 0) || (encryption)) {
1526:                        try {
1527:                            if (!framework.equals("")
1528:                                    && !framework.equals("embedded"))
1529:                                frameworkProtocol(ap);
1530:                            else if (encryption)
1531:                                encryptionProtocol(ap);
1532:                        } catch (Exception e) {
1533:                            System.out.println("Exception: " + e.getMessage());
1534:                            e.printStackTrace();
1535:                        }
1536:
1537:                        // write the new properties to the appPropFile
1538:                        appPropFile = new File(baseDir, testBase
1539:                                + "_app.properties");
1540:                        try {
1541:                            bos = new BufferedOutputStream(
1542:                                    new FileOutputStream(appPropFile));
1543:                            ap.store(bos, "Test Properties");
1544:                            bos.close();
1545:                        } catch (IOException ioe) {
1546:                            System.out
1547:                                    .println("IOException creating prop file: "
1548:                                            + ioe.getMessage());
1549:                        }
1550:                    }
1551:                }
1552:
1553:                if ((appPropFile != null) && (appPropFile.exists())) {
1554:                    // Create the properties string for the test
1555:                    propString = appPropFile.getPath();
1556:
1557:                    // Check for shutdown url
1558:                    shutdownurl = ap.getProperty("shutdown");
1559:
1560:                    // Check for jdk12test set to true
1561:                    String jdk12test = ap.getProperty("jdk12test");
1562:                    //System.out.println("jdk12test: " + jdk12test);
1563:                    //System.out.println("jvmName: " + jvmName);
1564:                    if (jdk12test != null) {
1565:                        if (jdk12test.toLowerCase().equals("true")) {
1566:                            isjdk12test = true;
1567:                            //System.out.println("isjdk12test " + isjdk12test);
1568:                            if (jvmName.equals("currentjvm")) { // This is not at least jdk12
1569:                                skiptest = true;
1570:                                addSkiptestReason("Test skipped: test needs jdk12, jvm is reporting currentjvm; skipping test: "
1571:                                        + scriptFileName);
1572:                            }
1573:                        }
1574:                    }
1575:
1576:                    String srvJvm = System.getProperty("serverJvm");
1577:                    if (srvJvm != null)
1578:                        jvmnet = true;
1579:
1580:                    String excludeJcc = ap.getProperty("excludeJCC");
1581:                    if (framework.equals("DerbyNet")) {
1582:                        try {
1583:                            RunList.checkClientExclusion(excludeJcc, "JCC",
1584:                                    jccMajor, jccMinor, javaVersion);
1585:                        } catch (Exception e) {
1586:                            skiptest = true;
1587:                            addSkiptestReason(e.getMessage());
1588:                        }
1589:                    }
1590:
1591:                    // for now we want just want to have a single property
1592:                    // for all j9 versions exception j9_foundation
1593:                    // which we map to the generic name foundation.
1594:                    String testJVM = jvmName;
1595:                    if (jvmName.startsWith("j9")) {
1596:                        if (jvmName.equals("j9_foundation"))
1597:                            testJVM = "foundation";
1598:                        else
1599:                            testJVM = "j9";
1600:                    }
1601:                    runwithjvm = ap.getProperty("runwith" + testJVM);
1602:                    if ((runwithjvm != null)
1603:                            && (runwithjvm.equalsIgnoreCase("false"))) {
1604:                        skiptest = true;
1605:                        addSkiptestReason("Test skipped: test cannot run with jvm: "
1606:                                + jvmName + ".  " + scriptFileName);
1607:                    }
1608:                    // startServer will determine whether the server will be started 
1609:                    // for network server tests or that will be left to the test.
1610:                    String startServerProp = ap.getProperty("startServer");
1611:                    if (startServerProp != null
1612:                            && startServerProp.equalsIgnoreCase("false"))
1613:                        startServer = false;
1614:
1615:                    //Check derbyTesting.encoding property
1616:                    if (testEncoding == null) {
1617:                        testEncoding = ap.getProperty("derbyTesting.encoding");
1618:                        // only bother if we have jdk15, otherwise we'll be skipping
1619:                        if ((jvmName.equals("jdk15")) && (testEncoding != null)) {
1620:                            jvmflags = (jvmflags == null ? "" : jvmflags + " ")
1621:                                    + "-Dfile.encoding=" + testEncoding;
1622:                            ap.put("file.encoding", testEncoding);
1623:                        }
1624:                    }
1625:
1626:                    if (!replacePolicyFile) {
1627:                        String replace_policy = ap
1628:                                .getProperty("derbyTesting.replacePolicyFile");
1629:                        if (replace_policy != null
1630:                                && replace_policy.equals("true"))
1631:                            replacePolicyFile = true;
1632:                        else
1633:                            replacePolicyFile = false;
1634:
1635:                    }
1636:
1637:                    if (NetServer.isJCCConnection(framework)
1638:                            || "true".equalsIgnoreCase(ap
1639:                                    .getProperty("noSecurityManager")))
1640:                        runWithoutSecurityManager = true;
1641:
1642:                    // Also check for supportfiles
1643:                    String suppFiles = ap.getProperty("supportfiles");
1644:                    boolean copySupportFiles = ((suppFiles != null) && (suppFiles
1645:                            .length() > 0));
1646:                    boolean createExtDirs = new Boolean(ap.getProperty(
1647:                            "useextdirs", "false")).booleanValue();
1648:                    if (copySupportFiles || createExtDirs) {
1649:                        File copyOutDir = null;
1650:
1651:                        if (testType.equals("sql2")) {
1652:                            if ((isSuiteRun) || (framework.length() > 0))
1653:                                copyOutDir = outDir;
1654:                            else if ((runDir != null) && (runDir.exists()))
1655:                                copyOutDir = runDir;
1656:                            else
1657:                                copyOutDir = outDir;
1658:                        } else if (testType.equals("multi")) {
1659:                            if ((isSuiteRun) || (mtestdir == null)
1660:                                    || (mtestdir.length() == 0)) {
1661:                                copyOutDir = outDir;
1662:                            } else {
1663:                                File multiDir = new File(mtestdir);
1664:                                copyOutDir = multiDir;
1665:                            }
1666:                        } else if (outcopy == true)
1667:                            copyOutDir = outDir;
1668:                        else if ((runDir != null) && (runDir.exists())) {
1669:                            copyOutDir = runDir;
1670:                        } else
1671:                            copyOutDir = outDir;
1672:
1673:                        if (createExtDirs) {
1674:                            extInDir = copyOutDir;
1675:                            //create the support file directory for input files
1676:                            extInDir = new File(copyOutDir, "extin");
1677:                            if (!extInDir.exists())
1678:                                extInDir.mkdirs();
1679:                            //create the support file directory for output files.
1680:                            extOutDir = new File(copyOutDir, "extout");
1681:                            if (!extOutDir.exists())
1682:                                extOutDir.mkdirs();
1683:                            //create the support file directory for input output files.
1684:                            extInOutDir = new File(copyOutDir, "extinout");
1685:                            if (!extInOutDir.exists())
1686:                                extInOutDir.mkdirs();
1687:                            copyOutDir = extInDir;
1688:                        }
1689:                        if (copySupportFiles)
1690:                            CopySuppFiles.copyFiles(copyOutDir, suppFiles);
1691:                    } else {
1692:                        // for useprocess false, set ext* (back) to null, or it
1693:                        // later tries to delete files even though they don't exist
1694:                        extInDir = null;
1695:                        extOutDir = null;
1696:                        extInOutDir = null;
1697:                    }
1698:                }
1699:                return propString;
1700:            }
1701:
1702:            public static String setTestJavaFlags(String tflags) {
1703:                StringBuffer sb = new StringBuffer();
1704:                if (verbose)
1705:                    System.out.println("testJavaflags: " + tflags);
1706:                String dintro = "-D";
1707:                try {
1708:                    dintro = jvm.getJvm(jvmName).getDintro();
1709:                } catch (Exception e) {
1710:                    System.out.println("Problem getting jvm " + jvmName
1711:                            + " Dintro: ");
1712:                    e.printStackTrace(System.out);
1713:                    System.exit(1);
1714:                }
1715:                // Parse because there could be a list of flags
1716:                StringTokenizer st = new StringTokenizer(tflags, "^");
1717:                while (st.hasMoreTokens()) {
1718:                    String token = st.nextToken();
1719:                    if (!(token.startsWith("-"))) {
1720:                        sb.append(dintro);
1721:                    }
1722:                    sb.append(token);
1723:                    sb.append(" ");
1724:                }
1725:                if (verbose)
1726:                    System.out.println("setTestJavaFlags returning: "
1727:                            + sb.toString());
1728:                return sb.toString();
1729:            }
1730:
1731:            private static void loadProps(Properties p, File f)
1732:                    throws Exception {
1733:                if (f.canRead()) {
1734:                    FileInputStream fis = new FileInputStream(f);
1735:                    p.load(fis);
1736:                    fis.close();
1737:                }
1738:            }
1739:
1740:            private static void mergeProps(Properties to, Properties from) {
1741:                for (Enumeration e = from.propertyNames(); e.hasMoreElements();) {
1742:                    String key = (String) e.nextElement();
1743:                    to.put(key, from.getProperty(key));
1744:                }
1745:            }
1746:
1747:            private static void frameworkProtocol(Properties p)
1748:                    throws Exception {
1749:
1750:                if (p == null) {
1751:                    // No properties
1752:                    return;
1753:                }
1754:                for (Enumeration e = p.propertyNames(); e.hasMoreElements();) {
1755:                    String key = (String) e.nextElement();
1756:                    String value = p.getProperty(key);
1757:
1758:                    if (key.equals("driver") || key.equals("ij.driver")
1759:                            || key.equals("jdbc.drivers")) {
1760:                        p.put(key, driverName);
1761:                    } else if (key.startsWith("ij.protocol")
1762:                            || key.equals("protocol")) {
1763:                        value = NetServer.alterURL(framework, value);
1764:                        p.put(key, value);
1765:                    } else if (key.equals("database")
1766:                            || key.equals("ij.database")
1767:                            || key.startsWith("ij.connection")
1768:                            || key.equals("jdbc.url")) {
1769:                        dbName = value.substring(value.lastIndexOf(':') + 1,
1770:                                value.length());
1771:                        value = NetServer.alterURL(framework, value);
1772:                        p.put(key, value);
1773:                    } else
1774:                        // for any other properties, just copy them
1775:                        p.put(key, value);
1776:
1777:                }
1778:
1779:                // jcc default requires userid
1780:                // derby client will default to "APP" so doesn't need harness
1781:                // to set a user.
1782:                if (NetServer.isJCCConnection(framework)) {
1783:                    String user = System.getProperty("ij.user");
1784:                    if (user == null)
1785:                        user = "APP";
1786:                    p.put("ij.user", user);
1787:                }
1788:
1789:                // both jcc and client require password for the moment
1790:                if (NetServer.isClientConnection(framework)) {
1791:                    String password = System.getProperty("ij.password");
1792:                    if (password == null)
1793:                        password = "APP";
1794:                    p.put("ij.password", password);
1795:                }
1796:
1797:                if (NetServer.isJCCConnection(framework)) {
1798:                    // force messages to show
1799:                    p.put("ij.retrieveMessagesFromServerOnGetMessage", "true");
1800:                }
1801:
1802:                // If this is not a known protocol for ij we
1803:                // need to set the driver
1804:                if (driverName != null)
1805:                    p.put("ij.driver", driverName);
1806:            }
1807:
1808:            private static void encryptionProtocol(Properties p)
1809:                    throws Exception {
1810:                String encryptUrl = "dataEncryption=true;bootPassword=Thursday";
1811:
1812:                if (p == null) {
1813:                    // No properties
1814:                    return;
1815:                }
1816:
1817:                // add encryption algorithm and provider to database creation URL
1818:                String v = p.getProperty("testEncryptionAlgorithm");
1819:                if (v != null)
1820:                    encryptUrl += ";" + Attribute.CRYPTO_ALGORITHM + "=" + v;
1821:                v = p.getProperty("testEncryptionProvider");
1822:                if (v != null)
1823:                    encryptUrl += ";" + Attribute.CRYPTO_PROVIDER + "=" + v;
1824:
1825:                for (Enumeration e = p.propertyNames(); e.hasMoreElements();) {
1826:                    String key = (String) e.nextElement();
1827:                    String value = p.getProperty(key);
1828:
1829:                    if (key.equals("database") || key.equals("ij.database")
1830:                            || key.startsWith("ij.connection")
1831:                            || key.equals("jdbc.url")) {
1832:                        // edit the url if necessary
1833:                        int index = value.indexOf(encryptUrl);
1834:                        if (index == -1) {
1835:                            value = value + ";" + encryptUrl;
1836:                        }
1837:                        p.put(key, value);
1838:                    } else if (key.equals("testEncryptionAlgorithm")
1839:                            || key.equals("testEncryptionProvider")) {
1840:                    } // ignore, do not copy
1841:                    else
1842:                        // for any other properties, just copy them
1843:                        p.put(key, value);
1844:                }
1845:            }
1846:
1847:            private static void cleanupBaseDir(File baseDir) {
1848:                // Some tests rely on no cleanup being done on the baseDir
1849:                boolean okToDelete = false;
1850:
1851:                if ((usesystem == null) || (usesystem.length() == 0))
1852:                    okToDelete = true;
1853:                else if (usesystem.equals("nist")) {
1854:                    if (testBase.equals("schema1"))
1855:                        okToDelete = true;
1856:                } else if (usesystem.equals("puzzles")) {
1857:                    if (testBase.equals("puzzleschema"))
1858:                        okToDelete = true;
1859:                }
1860:
1861:                if (useCommonDB)
1862:                    okToDelete = false;
1863:
1864:                if (okToDelete == true) {
1865:                    //System.out.println("Should be deleting the baseDir for a clean run");
1866:                    deleteFile(baseDir);
1867:                    if (baseDir.exists())
1868:                        System.out
1869:                                .println("baseDir did not get deleted which could cause test failures");
1870:                    else
1871:                        baseDir.mkdir();
1872:                }
1873:            }
1874:
1875:            private static void doCleanup(String javaVersion)
1876:                    throws IOException {
1877:                boolean status = true;
1878:                // The output files cannot be deleted if there
1879:                // is still a reference to them -- even doing
1880:                // this is not a guarantee that they will be deleted
1881:                // It seems to depend on the Java environment
1882:                //printWriter.close();
1883:                //printWriter = null;
1884:
1885:                //Always cleanup the script files - except when keepfiles is true
1886:                if (!(script == null) && (script.exists()) && (!keepfiles)) {
1887:                    status = script.delete();
1888:                    //System.out.println("Status was: " + status);
1889:                }
1890:
1891:                String endTime = CurrentTime.getTime();
1892:                StringBuffer sbend = new StringBuffer();
1893:                sbend.append("*** End:   " + testBase + " jdk" + javaVersion
1894:                        + " ");
1895:                if ((framework.length() > 0)
1896:                        && (!framework.startsWith("embedded")))
1897:                    sbend.append(framework + " ");
1898:                if ((suiteName != null) && (suiteName.length() > 0))
1899:                    sbend.append(suiteName + " ");
1900:                sbend.append(endTime + " ***");
1901:                System.out.println(sbend.toString());
1902:                pwDiff.println(sbend.toString());
1903:                pwDiff.flush();
1904:                pwDiff.close();
1905:                pwDiff = null;
1906:
1907:                // This could be true if set by user or there were diffs
1908:                if (keepfiles == false) {
1909:                    // Delete the out and diff files
1910:                    status = tmpOutFile.delete();
1911:                    if (status == false)
1912:                        tmpOutFile = null;
1913:                    status = finalOutFile.delete();
1914:                    if (skiptest == false)
1915:                        status = diffFile.delete();
1916:                    // delete the copied (and for Network Server, modified) master file 
1917:                    tempMasterFile = new File(outDir, tempMasterName);
1918:                    status = tempMasterFile.delete();
1919:                    UTF8OutFile = new File(outDir, UTF8OutName);
1920:                    status = UTF8OutFile.delete();
1921:                    File defaultPolicyFile = new File(userdir,
1922:                            "derby_tests.policy");
1923:                    if (defaultPolicyFile.exists())
1924:                        status = defaultPolicyFile.delete();
1925:                    if (deleteBaseDir) {
1926:                        if (useCommonDB == false) {
1927:                            //System.out.println("Should delete the baseDir: " + baseDir.getPath());
1928:                            deleteFile(baseDir);
1929:                        } else {
1930:                            status = appPropFile.delete();
1931:                            //no permission in Java to drop derby.log
1932:                            //File logfile = new File(baseDir, "derby.log");
1933:                            //System.out.println("delete derby.log ");
1934:                            //System.out.println(logfile.getPath());
1935:                            //status = logfile.delete();
1936:                        }
1937:
1938:                        // delete the directories where external input/output files 
1939:                        // were created
1940:                        if (extInDir != null)
1941:                            deleteFile(extInDir);
1942:                        if (extOutDir != null)
1943:                            deleteFile(extOutDir);
1944:                        if (extInOutDir != null)
1945:                            deleteFile(extInOutDir);
1946:                    }
1947:                }
1948:                // reset for next test
1949:                // the next line is a bug fix to get cleanup working correctly when
1950:                // useprocess==false;  without this the first failing test causes all
1951:                // subsequent tests not to clean up, since keepfiles is static; a
1952:                // more general solution is to make the variable non-static, and to
1953:                // set the default in the initializer; I(john) have not done this
1954:                // because the same reasoning probably applies to many of the static
1955:                // variables, and the whole subject should probably be approached in
1956:                // a coordinated way when someone has the time for it.
1957:                keepfiles = false;
1958:
1959:            }
1960:
1961:            /*
1962:             * For recursively deleting a directory
1963:             *
1964:             */
1965:            public static void deleteFile(File f) {
1966:                boolean status = true;
1967:                if (f == null) {
1968:                    System.out.println(f.getName() + " is null");
1969:                    return;
1970:                }
1971:                if (!f.exists()) {
1972:                    System.out.println(f.getName()
1973:                            + " does not exist; harness error");
1974:                    return;
1975:                }
1976:
1977:                //System.out.println("Trying to delete: " + f.getPath());
1978:                status = f.delete();
1979:
1980:                if (status == true)
1981:                    return;
1982:                else {
1983:                    // Could not delete; this could be a non-empty directory
1984:                    if (!f.isDirectory()) {
1985:                        System.out.println("Could not delete file "
1986:                                + f.getName() + ", going on");
1987:                        return;
1988:                    }
1989:                    //System.out.println("Recursively delete...");
1990:                    String[] files = f.list();
1991:                    for (int i = 0; i < files.length; i++) {
1992:                        File sub = new File(f.getPath(), files[i]);
1993:                        if (sub == null)
1994:                            continue;
1995:                        //System.out.println("Try to delete: " + sub.getPath());
1996:                        status = sub.delete();
1997:                        if (status != true) {
1998:                            // Could not delete; it may be a non-empty dir
1999:                            if (sub.isDirectory()) {
2000:                                //System.out.println("Recursing again ... ");
2001:                                deleteFile(sub);
2002:                                // It should be empty now; try again
2003:                                status = sub.delete();
2004:                                //System.out.println("Recurse delete status: " + status);
2005:                            }
2006:                            //else
2007:                            // The file delete failed
2008:                            //System.out.println("Failed to clean up file: " + sub.getPath());
2009:                        }
2010:                    }
2011:                }
2012:                //Should be able to delete the top directory now
2013:                status = f.delete();
2014:                if (status == false) {
2015:                    System.out.println("Warning: Cleanup failed on baseDir: "
2016:                            + f.getPath());
2017:                }
2018:            }
2019:
2020:            static void addToFailures(String testName) throws IOException {
2021:                if (failFileName == null) {
2022:                    if (isSuiteRun)
2023:                        failFileName = suiteName + ".fail";
2024:                    else
2025:                        failFileName = testBase + ".fail";
2026:                }
2027:                addToListFile(failFileName, testName);
2028:            }
2029:
2030:            static void addToSuccesses(String testName) throws IOException {
2031:                if (passFileName == null) {
2032:                    if (isSuiteRun)
2033:                        passFileName = suiteName + ".pass";
2034:                    else
2035:                        passFileName = testBase + ".pass";
2036:                }
2037:                addToListFile(passFileName, testName);
2038:            }
2039:
2040:            static void addToListFile(String fileName, String testName)
2041:                    throws IOException {
2042:                File f;
2043:                if (isSuiteRun)
2044:                    f = new File(rsuiteDir, fileName);
2045:                else
2046:                    f = new File(outDir, fileName);
2047:                PrintStream ps = null;
2048:
2049:                // This call to getCanonicalPath catches IOExceptions as a workaround to
2050:                // a bug in the EPOC jvm. 
2051:                try {
2052:                    ps = new PrintStream(new FileOutputStream(f
2053:                            .getCanonicalPath(), true));
2054:                } catch (IOException e) {
2055:                    FileWriter fw = new FileWriter(f);
2056:                    fw.close();
2057:                    ps = new PrintStream(new FileOutputStream(f
2058:                            .getCanonicalPath(), true));
2059:                }
2060:
2061:                ps.println(testName);
2062:                ps.flush();
2063:                ps.close();
2064:            }
2065:
2066:            static void appendStderr(BufferedOutputStream bos, InputStream is)
2067:                    throws IOException {
2068:                PrintWriter tmpPw = new PrintWriter(bos);
2069:                // reader for stderr
2070:                BufferedReader errReader = new BufferedReader(
2071:                        new InputStreamReader(is, "UTF-8"));
2072:                String s = null;
2073:                int lines = 0;
2074:                while ((s = errReader.readLine()) != null) {
2075:                    tmpPw.println(s);
2076:                }
2077:                errReader.close();
2078:                tmpPw.flush();
2079:            }
2080:
2081:            static void verifyTestType() throws ClassNotFoundException,
2082:                    FileNotFoundException, IOException {
2083:                //java requires '/' as separator to look into jar, irrespective of OS
2084:                InputStream is = loadTestResource("harness/testtypes.properties");
2085:                Properties p = new Properties();
2086:                p.load(is);
2087:                String testtypes = p.getProperty("testtypes");
2088:                StringTokenizer st = new StringTokenizer(testtypes, ",");
2089:                String ttype = "";
2090:                while (st.hasMoreTokens()) {
2091:                    ttype = st.nextToken();
2092:                    if (testType.equals(ttype))
2093:                        return;
2094:                }
2095:                // Not a known test type
2096:                System.out.println("Unknown test type: " + testType);
2097:                System.exit(1);
2098:            }
2099:
2100:            public static void addStandardTestJvmProps(Vector testJvmProps,
2101:                    String derbySystemHome, String userDirName, jvm jvm) {
2102:                if (derbySystemHome == null || derbySystemHome.length() == 0)
2103:                    derbySystemHome = userDirName;
2104:                testJvmProps.addElement("derby.system.home=" + derbySystemHome);
2105:                testJvmProps.addElement("derby.infolog.append=true ");
2106:                // Why is this being done here
2107:                //if (jvm != null)
2108:                //testJvmProps.addElement("jvm="+jvm.getName());
2109:            }
2110:
2111:            private static String[] buildTestCommand(String propString,
2112:                    String systemHome, String scriptPath)
2113:                    throws FileNotFoundException, IOException, Exception {
2114:                composePolicyFile();
2115:
2116:                //System.out.println("testType: " + testType);
2117:                String ij = "";
2118:                // Create the test command line
2119:                if (testType.equals("sql"))
2120:                    ij = "ij";
2121:                jvm jvm = null; // to quiet compiler
2122:                jvm = jvm.getJvm(jvmName);
2123:                if (javaCmd != null)
2124:                    jvm.setJavaCmd(javaCmd);
2125:
2126:                if ((classpath != null) && (classpath.length() > 0))
2127:                    jvm.setClasspath(classpath);
2128:
2129:                Vector jvmProps = new Vector();
2130:                if (testType.equals("java") || testType.equals("demo"))
2131:                    addStandardTestJvmProps(jvmProps, systemHome, outDir
2132:                            .getCanonicalPath(), null);
2133:                else if ((runDir != null) && (runDir.exists()))
2134:                    addStandardTestJvmProps(jvmProps, systemHome, runDir
2135:                            .getCanonicalPath(), jvm);
2136:                else
2137:                    addStandardTestJvmProps(jvmProps, systemHome, outDir
2138:                            .getCanonicalPath(), jvm);
2139:
2140:                if ((testJavaFlags != null) && (testJavaFlags.length() > 0)) {
2141:                    String parsedFlags = setTestJavaFlags(testJavaFlags);
2142:                    StringTokenizer st = new StringTokenizer(parsedFlags, " ");
2143:                    while (st.hasMoreTokens()) {
2144:                        jvmflags = (jvmflags == null ? "" : jvmflags) + " "
2145:                                + st.nextToken();
2146:                    }
2147:                }
2148:
2149:                if (ij.startsWith("ij"))
2150:                    jvmProps.addElement("ij.defaultResourcePackage="
2151:                            + defaultPackageName);
2152:
2153:                if ((framework != null)) {
2154:                    jvmProps.addElement("framework=" + framework);
2155:                    if ((hostName != null) && (!hostName.equals("localhost")))
2156:                        jvmProps.addElement("hostName=" + hostName);
2157:                }
2158:
2159:                if (junitXASingle)
2160:                    jvmProps.addElement("derbyTesting.xa.single=true");
2161:
2162:                // if we're not jdk15, don't, we'll skip
2163:                if ((testEncoding != null) && (jvmName.equals("jdk15"))) {
2164:                    jvmProps
2165:                            .addElement("derbyTesting.encoding=" + testEncoding);
2166:                    jvmProps.addElement("file.encoding=" + testEncoding);
2167:                    jvmflags = (jvmflags == null ? "" : jvmflags + " ")
2168:                            + "-Dfile.encoding=" + testEncoding;
2169:                } else if (isI18N) {
2170:                    // The I18N tests should be run with UTF-8 encoding to avoid
2171:                    // problems with characters that cannot be represented in the
2172:                    // default encoding (DERBY-244).
2173:                    jvmProps.addElement("file.encoding=UTF-8");
2174:                    jvmflags = (jvmflags == null ? "" : jvmflags + " ")
2175:                            + "-Dfile.encoding=UTF-8";
2176:                }
2177:
2178:                if (upgradejarpath != null)
2179:                    jvmProps.addElement("derbyTesting.jar.path="
2180:                            + upgradejarpath);
2181:
2182:                if ((jvmflags != null) && (jvmflags.length() > 0)) {
2183:                    // We now replace any '^' in jvmflags with ' '
2184:                    if (jvmflags.indexOf("^") > 0) {
2185:                        jvmflags = spaceJvmFlags(jvmflags);
2186:                    }
2187:                    jvm.setFlags(jvmflags);
2188:                }
2189:
2190:                if (testType.equals("multi")) {
2191:                    if ((jvmflags != null) && (jvmflags.indexOf("mx") == -1)
2192:                            && (jvmflags.indexOf("Xmx") == -1))
2193:                        jvm.setMx(64 * 1024 * 1024); // -mx64m
2194:
2195:                    // MultiTest is special case, so pass on properties
2196:                    // related to encryption to MultiTest
2197:                    jvmProps.addElement("encryption=" + encryption);
2198:                    Properties props = new Properties();
2199:                    // parse and get only the special properties that are needed for the url 
2200:                    SpecialFlags.parse(testSpecialProps, props,
2201:                            new Properties());
2202:                    String encryptionAlgorithm = props
2203:                            .getProperty("testEncryptionAlgorithm");
2204:                    if (encryptionAlgorithm != null)
2205:                        jvmProps.addElement("encryptionAlgorithm=\""
2206:                                + Attribute.CRYPTO_ALGORITHM + "="
2207:                                + encryptionAlgorithm + "\"");
2208:                }
2209:                jvm.setD(jvmProps);
2210:
2211:                // set security properties
2212:                if (!runWithoutSecurityManager)
2213:                    jvm.setSecurityProps();
2214:                else
2215:                    System.out.println("-- SecurityManager not installed --");
2216:
2217:                Vector v = jvm.getCommandLine();
2218:                if (ij.startsWith("ij")) {
2219:                    // As of cn1411-20030930 IBM jvm the system takes the default
2220:                    // console encoding which in the US, on windows, is Cp437.
2221:                    // Sun jvms, however, always force a console encoding of 1252.
2222:                    // To get the same result for ibm141 & jdk14*, the harness needs to
2223:                    // force the console encoding to Cp1252 for ij tests - unless 
2224:                    // we're on non-ascii systems.
2225:                    String isNotAscii = System.getProperty("platform.notASCII");
2226:                    if (isI18N) {
2227:                        // DERBY-244: Use UTF-8 console encoding for the i18n tests to
2228:                        // avoid MalformedInputException with the IBM jvm.
2229:                        v.addElement("-Dconsole.encoding=UTF-8");
2230:                    } else if ((isNotAscii == null)
2231:                            || (isNotAscii.equals("false"))) {
2232:                        v.addElement("-Dconsole.encoding=Cp1252");
2233:                    }
2234:                    v.addElement("org.apache.derby.tools." + ij);
2235:                    if (ij.equals("ij")) {
2236:                        //TODO: is there a setting/property we could check after which
2237:                        // we can use v.addElement("-fr"); (read from the classpath?)
2238:                        // then we can also use v.addElement(scriptFile);
2239:                        v.addElement("-f");
2240:                        v.addElement(outDir.toString() + File.separatorChar
2241:                                + scriptFileName);
2242:                    }
2243:                    v.addElement("-p");
2244:                    v.addElement(propString);
2245:                } else if (testType.equals("java")) {
2246:                    if (javaPath.length() > 0)
2247:                        v.addElement(javaPath + "." + testBase);
2248:                    else
2249:                        v.addElement(testBase);
2250:                    if (propString.length() > 0) {
2251:                        v.addElement("-p");
2252:                        v.addElement(propString);
2253:                    }
2254:                } else if (testType.equals("unit")) {
2255:                    v
2256:                            .addElement("org.apache.derbyTesting.unitTests.harness.UnitTestMain");
2257:                    v.addElement("-p");
2258:                    v.addElement(propString);
2259:                } else if (testType.equals("junit")) {
2260:                    v.addElement("junit.textui.TestRunner");
2261:                    if (javaPath.length() > 0) {
2262:                        v.addElement(javaPath + "." + testBase);
2263:                    } else {
2264:                        v.addElement(testBase);
2265:                    }
2266:                } else if (testType.equals("multi")) {
2267:                    System.out.println("scriptiflename is: " + scriptFileName);
2268:                    v
2269:                            .addElement("org.apache.derbyTesting.functionTests.harness.MultiTest");
2270:                    v.addElement(scriptFileName);
2271:                    v.addElement("-i");
2272:                    v.addElement(mtestdir);
2273:                    v.addElement("-o");
2274:                    v.addElement(outDir.getPath());
2275:                    v.addElement("-p");
2276:                    v.addElement(propString);
2277:                }
2278:
2279:                // Now convert the vector into a string array
2280:                String[] sCmd = new String[v.size()];
2281:                for (int i = 0; i < v.size(); i++) {
2282:                    sCmd[i] = (String) v.elementAt(i);
2283:                }
2284:
2285:                return sCmd;
2286:            }
2287:
2288:            public static String spaceJvmFlags(String caretedJvmFlags) {
2289:                String spacedJvmFlags = "";
2290:                // there must at least be one
2291:                StringTokenizer st = new StringTokenizer(jvmflags, "^");
2292:                while (st.hasMoreTokens()) {
2293:                    spacedJvmFlags += st.nextToken() + " ";
2294:                }
2295:                spacedJvmFlags = spacedJvmFlags.substring(0, spacedJvmFlags
2296:                        .length() - 1);
2297:                return spacedJvmFlags;
2298:            }
2299:
2300:            public static void composePolicyFile()
2301:                    throws ClassNotFoundException {
2302:                try {
2303:                    //DERBY-892: allow for test- and suite-specific policy additions
2304:
2305:                    // this is the default policy file
2306:                    String default_policy = "util/derby_tests.policy";
2307:
2308:                    // if the property replacePolicyFile is set (in the 
2309:                    // test specific _app.properties file, or at the command line)
2310:                    // do not use the default policy file at all, otherwise, append
2311:                    if (!replacePolicyFile) {
2312:                        File userDirHandle = new File(userdir);
2313:                        CopySuppFiles.copyFiles(userDirHandle, default_policy);
2314:                    }
2315:                    // see if there is a suite specific policy file and append or replace
2316:                    if ((isSuiteRun) && (suiteName != null)) {
2317:                        InputStream newpolicy = loadTestResource("suites/"
2318:                                + suiteName
2319:                                        .substring(0, suiteName.indexOf(':'))
2320:                                + ".policy");
2321:                        writePolicyFile(newpolicy);
2322:                    }
2323:
2324:                    // if we are running with useprocess=false, we need some special
2325:                    // properties (setSecurityManager, setIO)
2326:                    if (!useprocess) {
2327:                        InputStream newpolicy = loadTestResource("util/"
2328:                                + "useprocessfalse.policy");
2329:                        writePolicyFile(newpolicy);
2330:                    }
2331:
2332:                    // now get the test specific policy file and append or replace
2333:                    InputStream newpolicy = loadTestResource("tests/"
2334:                            + testDirName + "/" + testBase + ".policy");
2335:                    writePolicyFile(newpolicy);
2336:                } catch (IOException ie) {
2337:                    System.out
2338:                            .println("Exception trying to create policy file: ");
2339:                    ie.printStackTrace();
2340:                }
2341:            }
2342:
2343:            public static void writePolicyFile(InputStream newpolicy) {
2344:                try {
2345:                    if (newpolicy != null) {
2346:                        File oldpolicy = new File(runDir, "derby_tests.policy");
2347:                        if (verbose && oldpolicy.exists())
2348:                            System.out
2349:                                    .println("Appending to derby_tests.policy");
2350:                        BufferedReader policyadd = new BufferedReader(
2351:                                new InputStreamReader(newpolicy, "UTF-8"));
2352:                        FileWriter policyfw = new FileWriter(oldpolicy
2353:                                .getPath(), true);
2354:                        PrintWriter policypw = new PrintWriter(
2355:                                new BufferedWriter(policyfw, 10000), true);
2356:                        String str = "";
2357:                        while ((str = policyadd.readLine()) != null) {
2358:                            policypw.println(str);
2359:                        }
2360:                        policypw.close();
2361:                        policyadd.close();
2362:                        policypw = null;
2363:                        newpolicy = null;
2364:                    }
2365:                } catch (IOException ie) {
2366:                    System.out
2367:                            .println("Exception trying to create policy file: ");
2368:                    ie.printStackTrace();
2369:                }
2370:            }
2371:
2372:            private static void execTestProcess(String[] testCmd)
2373:                    throws Exception {
2374:                // Execute the process and handle the results
2375:                Process pr = null;
2376:                try {
2377:                    // We need the process inputstream and errorstream
2378:                    ProcessStreamResult prout = null;
2379:                    ProcessStreamResult prerr = null;
2380:                    FileOutputStream fos = null;
2381:                    BufferedOutputStream bos = null;
2382:
2383:                    StringBuffer sb = new StringBuffer();
2384:
2385:                    for (int i = 0; i < testCmd.length; i++) {
2386:                        sb.append(testCmd[i] + " ");
2387:                    }
2388:                    if (verbose)
2389:                        System.out.println(sb.toString());
2390:                    pr = Runtime.getRuntime().exec(testCmd);
2391:
2392:                    String inEncoding = null;
2393:                    String outEncoding = null;
2394:                    if (isI18N) {
2395:                        // DERBY-244: I18N tests are run with -Dfile.encoding=UTF-8, so
2396:                        // we need to specify encoding to the streams as well.
2397:                        inEncoding = "UTF-8";
2398:                        outEncoding = inEncoding;
2399:                    }
2400:
2401:                    if (codeset != null) {
2402:                        // The test explicitly specified a codeset, use that codeset
2403:                        // when reading the test output.
2404:                        inEncoding = codeset;
2405:                    }
2406:
2407:                    if (testEncoding != null) {
2408:                        inEncoding = testEncoding;
2409:                    }
2410:
2411:                    if (useOutput) {
2412:                        fos = new FileOutputStream(tmpOutFile);
2413:                    } else {
2414:                        fos = new FileOutputStream(stdOutFile);
2415:                    }
2416:                    bos = new BufferedOutputStream(fos, 1024);
2417:                    prout = new ProcessStreamResult(pr.getInputStream(), bos,
2418:                            timeoutStr, inEncoding, outEncoding);
2419:                    prerr = new ProcessStreamResult(pr.getErrorStream(), bos,
2420:                            timeoutStr, inEncoding, outEncoding);
2421:
2422:                    if (framework != null && !framework.equals(""))
2423:                        if (verbose)
2424:                            System.out.println("The test should be running...");
2425:
2426:                    if (timeout != -1) {
2427:                        TimedProcess tp = new TimedProcess(pr);
2428:                        tp.waitFor(timeout * 60);
2429:                        pr = null;
2430:                    }
2431:
2432:                    // determine if the process is done or was interrupted
2433:                    boolean outInterrupt = prout.Wait();
2434:                    boolean errInterrupt = prerr.Wait();
2435:
2436:                    if ((errInterrupt) || (outInterrupt)) {
2437:                        pwDiff.println("The test timed out...");
2438:                        System.out.println("Test timed out...");
2439:                        pr.destroy();
2440:                        pr = null;
2441:                    }
2442:
2443:                    fos.close();
2444:                    bos.close();
2445:                    //printWriter.flush();
2446:                    //printWriter.close();
2447:                    if (pr != null) {
2448:                        pr.destroy();
2449:                        pr = null;
2450:                    }
2451:                } catch (IOException ioe) {
2452:                    System.out.println("IOException: " + ioe.getMessage());
2453:                } catch (Throwable t) {
2454:                    pwDiff.println("Process exception: " + t);
2455:                    System.out.println("Process exception: " + t.getMessage());
2456:                    t.printStackTrace();
2457:                    if (pr != null) {
2458:                        pr.destroy();
2459:                        pr = null;
2460:                    }
2461:                }
2462:            }
2463:
2464:            private static void execTestNoProcess(Properties sysProp,
2465:                    String systemHome, String propString, String scriptPath)
2466:                    throws Exception {
2467:                // For platforms where executing a process is failing
2468:                Properties ptmp = System.getProperties();
2469:                ptmp.put("derby.system.home", systemHome);
2470:                ptmp.put("derby.infolog.append", "true");
2471:                // for framework tests, we may need to pick up the hostName 
2472:                // passed on on command line to individual tests...
2473:                if (framework.startsWith("DerbyNet"))
2474:                    ptmp.put("hostName=", hostName);
2475:                System.setProperties(ptmp);
2476:
2477:                String pathStr = "";
2478:
2479:                //these calls to getCanonicalPath catch IOExceptions as a workaround to
2480:                //a bug in the EPOC jvm. 
2481:                try {
2482:                    pathStr = tmpOutFile.getCanonicalPath().replace(
2483:                            File.separatorChar, fileSep);
2484:                } catch (IOException e) {
2485:                    FileWriter fw = new FileWriter(tmpOutFile);
2486:                    fw.close();
2487:                    pathStr = tmpOutFile.getCanonicalPath().replace(
2488:                            File.separatorChar, fileSep);
2489:                }
2490:
2491:                PrintStream ps = new PrintStream(new FileOutputStream(pathStr),
2492:                        true);
2493:
2494:                // Install a security manager within this JVM for this test.
2495:                composePolicyFile();
2496:                boolean installedSecurityManager = installSecurityManager();
2497:                if (testType.equals("sql")) {
2498:                    String[] ijarg = new String[3];
2499:                    ijarg[0] = "-p";
2500:                    ijarg[1] = propString;
2501:                    ijarg[2] = scriptPath;
2502:                    // direct the standard output and error to the print stream for the .tmp file
2503:                    PrintStream stdout = System.out;
2504:                    PrintStream stderr = System.err;
2505:                    System.setOut(ps);
2506:                    System.setErr(ps);
2507:                    RunIJ ij = new RunIJ(ijarg);
2508:                    Thread ijThread = new Thread(ij);
2509:                    try {
2510:                        ijThread.start();
2511:                        if (timeout < 0) {
2512:                            ijThread.join();
2513:                        } else {
2514:                            ijThread.join(timeout * 60 * 1000);
2515:                        }
2516:                    } catch (Exception e) {
2517:                        System.out.println("Aiiie! Got some kind of exception "
2518:                                + e);
2519:                    }
2520:
2521:                    // Now make sure a shutdown is complete if necessary
2522:                    if (shutdownurl != null) {
2523:                        String[] sdargs = new String[2];
2524:                        sdargs[0] = systemHome;
2525:                        sdargs[1] = shutdownurl;
2526:                        shutdown.main(sdargs);
2527:                    }
2528:                    // Reset ij.defaultResourcePackage
2529:                    ptmp = System.getProperties();
2530:                    ptmp.put("ij.defaultResourcePackage",
2531:                            "/org/apache/derbyTesting/");
2532:                    ptmp.put("usesystem", "");
2533:                    System.setProperties(ptmp);
2534:                    // Reset System.out and System.err
2535:                    System.setOut(stdout);
2536:                    System.setErr(stderr);
2537:                } else if (testType.equals("java")) {
2538:                    if (javaPath == null)
2539:                        javaPath = "org.apache.derbyTesting.functionTests.tests."
2540:                                + testDirName;
2541:
2542:                    String[] args = new String[2];
2543:                    args[0] = "-p";
2544:                    args[1] = propString;
2545:                    Class[] classArray = new Class[1];
2546:                    classArray[0] = args.getClass();
2547:                    String testName = javaPath + "." + testBase;
2548:                    Class JavaTest = Class.forName(testName);
2549:                    PrintStream stdout = System.out;
2550:                    PrintStream stderr = System.err;
2551:                    System.setOut(ps);
2552:                    System.setErr(ps);
2553:                    // Get the tests's main method and invoke it
2554:                    Method testMain = JavaTest.getMethod("main", classArray);
2555:                    Object[] argObj = new Object[1];
2556:                    argObj[0] = args;
2557:                    RunClass testObject = new RunClass(testMain, argObj);
2558:                    Thread testThread = new Thread(testObject);
2559:                    try {
2560:                        testThread.start();
2561:                        if (timeout < 0) {
2562:                            testThread.join();
2563:                        } else {
2564:                            testThread.join(timeout * 60 * 1000);
2565:                        }
2566:                    } catch (Exception e) {
2567:                        System.out.println("Exception upon invoking test..."
2568:                                + e);
2569:                        e.printStackTrace();
2570:                    }
2571:
2572:                    try {
2573:                        java.sql.DriverManager
2574:                                .getConnection("jdbc:derby:;shutdown=true");
2575:                    } catch (java.sql.SQLException e) {
2576:                        // ignore the errors, they are expected.
2577:                    }
2578:                    // Reset System.out and System.err
2579:                    System.setOut(stdout);
2580:                    System.setErr(stderr);
2581:                } else if (testType.equals("multi")) {
2582:                    System.out.println("scriptiflename is: later "
2583:                            + scriptFileName);
2584:                    // multi tests will now run with useprocess=false;
2585:                    // however, probably because they use Threads, if run 
2586:                    // within another test suite, the next suite will not run
2587:                    // And using a Thread.join() to start the tests doesn't resolve
2588:                    // this. So this support is here simply to allow running
2589:                    // something like stressmulti just by itself for debugging
2590:                    //javaPath = "org.apache.derbyTesting.functionTests.harness.MultiTest";
2591:                    String[] args = new String[5];
2592:                    args[0] = scriptFileName;
2593:                    args[1] = "-i";
2594:                    args[2] = mtestdir;
2595:                    args[3] = "-o";
2596:                    args[4] = outDir.getPath();
2597:                    System.out.println("Try running MultiTest.main");
2598:                    for (int i = 0; i < args.length; i++)
2599:                        System.out.println("args: " + args[i]);
2600:                    System.exit(1);
2601:                    org.apache.derbyTesting.functionTests.harness.MultiTest
2602:                            .main(args);
2603:                } else if (testType.equals("unit")) {
2604:                    // direct the standard output and error to the print stream for the .tmp file
2605:                    PrintStream stdout = System.out;
2606:                    PrintStream stderr = System.err;
2607:                    System.setOut(ps);
2608:                    System.setErr(ps);
2609:                    System.out
2610:                            .println("Unit tests not implemented yet with useprocess=false");
2611:                    // Reset System.out and System.err
2612:                    System.setOut(stdout);
2613:                    System.setErr(stderr);
2614:                    // repeat to stdout...
2615:                    System.out
2616:                            .println("Unit tests not implemented yet with useprocess=false");
2617:                    //System.exit(1);
2618:                    /*
2619:                    String[] args = new String[2];
2620:                    args[0] = "-p";
2621:                    args[1] = propString;
2622:                    org.apache.derbyTesting.unitTests.harness.UnitTestMain.main(args);
2623:                     */
2624:                } else if (testType.equals("junit")) {
2625:                    PrintStream stdout = System.out;
2626:                    PrintStream stderr = System.err;
2627:                    System.setOut(ps);
2628:                    System.setErr(ps);
2629:                    if (javaPath == null) {
2630:                        javaPath = "org.apache.derbyTesting.functionTests.tests."
2631:                                + testDirName;
2632:                    }
2633:                    String testName = javaPath + "." + testBase;
2634:
2635:                    // NOTE: This is most likely a temporary solution that will be 
2636:                    //       replaced once we get our own TestRunner.
2637:
2638:                    // Cannot use junit.textui.TestRunner.main() since it will exit 
2639:                    // the JVM when done.
2640:
2641:                    // Extract/create a TestSuite object, which is either
2642:                    //      a) retreived from a static suite() method in the test class
2643:                    //
2644:                    // or, if a) fails
2645:                    //
2646:                    //      b) containing all methods starting with "test" in the junit 
2647:                    //         test class.
2648:                    // This corresponds to what the junit.textui.TestRunner would
2649:                    // do if invoked in a separate process (useprocess=true).
2650:
2651:                    // Load the test class
2652:                    Class testClass = Class.forName(testName);
2653:
2654:                    TestSuite junitTestSuite = null;
2655:
2656:                    try {
2657:                        // Get the static suite() method if it exists.
2658:                        Method suiteMethod = testClass.getMethod("suite", null);
2659:                        // Get the TestSuite object returned by the suite() method
2660:                        // by invoking it.
2661:                        // Method is static, hence param1 is null
2662:                        // Method has no formal parameters, hence param2 is null
2663:                        junitTestSuite = (TestSuite) suiteMethod.invoke(null,
2664:                                null);
2665:                    } catch (Exception ex) {
2666:                        // Not able to access static suite() method (with no params)
2667:                        // returning a TestSuite object.
2668:                        // Use JUnit to create a TestSuite with all methods in the test
2669:                        // class starting with "test"
2670:                        junitTestSuite = new TestSuite(testClass);
2671:                    }
2672:
2673:                    if (junitTestSuite != null) {
2674:                        // Now run the test suite
2675:                        junit.textui.TestRunner.run(junitTestSuite);
2676:                    } else {
2677:                        System.out
2678:                                .println("Not able to extract JUnit TestSuite from "
2679:                                        + "test class " + testName);
2680:                    }
2681:
2682:                    // Reset System.out and System.err
2683:                    System.setOut(stdout);
2684:                    System.setErr(stderr);
2685:                }
2686:                ps.close();
2687:                if (installedSecurityManager) {
2688:                    System.setSecurityManager(null);
2689:
2690:                }
2691:            }
2692:
2693:            static void addSkiptestReason(String reason) {
2694:                if (skiptestReason.length() > 0)
2695:                    skiptestReason.append(System.getProperty("line.separator",
2696:                            "\n"));
2697:                skiptestReason.append(reason);
2698:            }
2699:
2700:            static boolean verifyFramework(String framework) {
2701:                String validFmString = "";
2702:                for (int i = 0; i < validFrameworks.length; i++) {
2703:                    validFmString += " " + validFrameworks[i];
2704:                    if (validFrameworks[i].equals(framework))
2705:                        return true;
2706:                }
2707:                System.err.println("Invalid framework: " + framework);
2708:
2709:                System.err.println("Valid frameworks are: " + validFmString);
2710:                return false;
2711:
2712:            }
2713:
2714:            /*
2715:             * method for loading a resource relative to testResourceHome.
2716:             * @param loc location of file relative to testResourceHome (e.g. master/cast.out)
2717:             * @return InputStream for the resource
2718:             */
2719:            public static InputStream loadTestResource(String loc) {
2720:                return new Object().getClass().getResourceAsStream(
2721:                        testResourceHome + loc);
2722:            }
2723:
2724:            /*
2725:             * This method replaces the / in the path string with \ for windows
2726:             * @param path the path string to convert
2727:             * @return String the new path
2728:             */
2729:            private static String convertPathForWin(String path) {
2730:                String tmp = "a" + path + "a";
2731:                int i = tmp.indexOf('/');
2732:                while (i != -1) {
2733:                    //replace the \\ with .
2734:                    tmp = tmp.substring(0, i) + "\\\\" + tmp.substring(i + 1);
2735:                    i = tmp.indexOf('/');
2736:                }
2737:                //lets remove the a we added
2738:                tmp = tmp.substring(1, tmp.length() - 1);
2739:                //System.out.println(tmp);
2740:
2741:                return tmp;
2742:            }
2743:
2744:            /**
2745:             * Install the default security manager in this JVM for this
2746:             * test, used when useprocess is false.
2747:             * @return
2748:             * @throws ClassNotFoundException
2749:             * @throws IOException
2750:             */
2751:            private static boolean installSecurityManager()
2752:                    throws ClassNotFoundException, IOException {
2753:                // SecurityManager not currently work with j9 and useProcess=false
2754:                // need to disable to allow tests to run.
2755:                if (jvmName.startsWith("j9"))
2756:                    return false;
2757:
2758:                boolean installedSecurityManager = false;
2759:                // Set up the SecurityManager in this JVM for this test.
2760:                boolean haveSecurityManagerAlready = System
2761:                        .getSecurityManager() != null;
2762:                if (runWithoutSecurityManager) {
2763:                    // Test doesn't run with a SecurityManager,
2764:                    // print a warning if one is there already.
2765:                    if (haveSecurityManagerAlready)
2766:                        System.out
2767:                                .println("noSecurityManager=true,useProcess=false but SecurityManager installed by previous test");
2768:                    else
2769:                        System.out
2770:                                .println("-- SecurityManager not installed --");
2771:                } else if (!haveSecurityManagerAlready) {
2772:                    // Get the set of -D options that would be needed
2773:                    // for a spawned VM and convert them to system properties.
2774:                    Vector propList = jvm.getSecurityProps(null);
2775:                    for (Enumeration e = propList.elements(); e
2776:                            .hasMoreElements();) {
2777:                        String dashDOpt = (String) e.nextElement();
2778:                        if ("java.security.manager".equals(dashDOpt))
2779:                            continue;
2780:
2781:                        int eq = dashDOpt.indexOf("=");
2782:                        String key = dashDOpt.substring(0, eq);
2783:                        String value = dashDOpt.substring(eq + 1);
2784:
2785:                        System.setProperty(key, value);
2786:
2787:                    }
2788:                    System.setSecurityManager(new SecurityManager());
2789:                    installedSecurityManager = true;
2790:                }
2791:
2792:                return installedSecurityManager;
2793:            }
2794:
2795:            // copy the .out file in utf8 format. 
2796:            // This can then be used as a new master in the source.
2797:            // It is assumed that if one runs with this property, keepfiles should be true.
2798:            private static void generateUTF8OutFile(File FinalOutFile)
2799:                    throws IOException {
2800:                if (generateUTF8Out) {
2801:                    keepfiles = true;
2802:                    File UTF8OutFile = new File(UTF8OutName);
2803:
2804:                    // start reading the .out file back in, using default encoding
2805:                    BufferedReader inFile = new BufferedReader(new FileReader(
2806:                            FinalOutFile));
2807:                    FileOutputStream fos = new FileOutputStream(UTF8OutFile);
2808:                    BufferedWriter bw = new BufferedWriter(
2809:                            new OutputStreamWriter(fos, "UTF-8"));
2810:                    int c;
2811:                    while ((c = inFile.read()) != -1)
2812:                        bw.write(c);
2813:                    bw.flush();
2814:                    bw.close();
2815:                    fos.close();
2816:                }
2817:            }
2818:
2819:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.