jdk/test/java/rmi/testlibrary/TestLibrary.java
author mfang
Wed, 17 Aug 2011 14:18:26 -0700
changeset 10294 8fcdae2a7ec7
parent 5506 202f599c92aa
child 13256 5886d7607acd
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @author Adrian Colley
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Laird Dornin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @author Ann Wollrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * The rmi library directory contains a set of simple utiltity classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * for use in rmi regression tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * NOTE: The JavaTest group has recommended that regression tests do
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * not make use of packages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.rmi.activation.Activatable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.rmi.activation.ActivationID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.rmi.NoSuchObjectException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.rmi.registry.Registry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.rmi.Remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.rmi.server.UnicastRemoteObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Class of utility/library methods (i.e. procedures) that assist with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * the writing and maintainance of rmi regression tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public class TestLibrary {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /** standard test port number for registry */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public final static int REGISTRY_PORT = 2006;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /** port for rmid necessary: not used to actually start rmid */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public final static int RMID_PORT = 1098;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static void mesg(Object mesg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        System.err.println("TEST_LIBRARY: " + mesg.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Routines that enable rmi tests to fail in a uniformly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * informative fashion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static void bomb(String message, Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        String testFailed = "TEST FAILED: ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if ((message == null) && (e == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            testFailed += " No relevant information";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } else if (e == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            testFailed += message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        System.err.println(testFailed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            System.err.println("Test failed with: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                               e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            e.printStackTrace(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        throw new TestFailedException(testFailed, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public static void bomb(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        bomb(message, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public static void bomb(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        bomb(null, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Property accessors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static boolean getBoolean(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return (new Boolean(getProperty(name, "false")).booleanValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static Integer getInteger(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        int val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        Integer value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        String propVal = getProperty(name, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (propVal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            value = new Integer(Integer.parseInt(propVal));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public static String getProperty(String property, String defaultVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        final String prop = property;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        final String def = defaultVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return ((String) java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            (new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    return System.getProperty(prop, def);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Property mutators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public static void setBoolean(String property, boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        setProperty(property, (new Boolean(value)).toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public static void setInteger(String property, int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        setProperty(property, Integer.toString(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public static void setProperty(String property, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        final String prop = property;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        final String val = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            (new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    System.setProperty(prop, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Routines to print out a test's properties environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public static void printEnvironment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        printEnvironment(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public static void printEnvironment(PrintStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        out.println("-------------------Test environment----------" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    "---------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        for(Enumeration keys = System.getProperties().keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            keys.hasMoreElements();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            String property = (String) keys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            out.println(property + " = " + getProperty(property, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        out.println("---------------------------------------------" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    "---------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Routine that "works-around" a limitation in jtreg.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Currently it is not possible for a test to specify that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * test harness should build a given source file and install the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * resulting class in a location that is not accessible from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * test's classpath.  This method enables a test to move a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * compiled test class file from the test's class directory into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * given "codebase" directory.  As a result the test can only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * access the class file for <code>className</code>if the test loads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * it from a classloader (e.g. RMIClassLoader).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Tests that use this routine must have the following permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * granted to them:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *   getProperty user.dir
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *   getProperty etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public static URL installClassInCodebase(String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                             String codebase)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        throws MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return installClassInCodebase(className, codebase, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public static URL installClassInCodebase(String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                             String codebase,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                             boolean delete)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        throws MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
         * NOTES/LIMITATIONS: The class must not be in a named package,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
         * and the codebase must be a relative path (it's created relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
         * to the working directory).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        String classFileName = className + ".class";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
         * Specify the file to contain the class definition.  Make sure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         * that the codebase directory exists (underneath the working
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         * directory).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        File dstDir = (new File(getProperty("user.dir", "."), codebase));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (!dstDir.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (!dstDir.mkdir()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    "could not create codebase directory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        File dstFile = new File(dstDir, classFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
         * Obtain the URL for the codebase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        URL codebaseURL = dstDir.toURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
         * Specify where we will copy the class definition from, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
         * necessary.  After the test is built, the class file can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
         * found in the "test.classes" directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        File srcDir = new File(getProperty("test.classes", "."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        File srcFile = new File(srcDir, classFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        mesg(srcFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        mesg(dstFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
         * If the class definition is not already located at the codebase,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
         * copy it there from the test build area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (!dstFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (!srcFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    "could not find class file to install in codebase " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    "(try rebuilding the test): " + srcFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                copyFile(srcFile, dstFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    "could not install class file in codebase");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            mesg("Installed class \"" + className +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                "\" in codebase " + codebaseURL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
         * After the class definition is successfully installed at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
         * codebase, delete it from the test's CLASSPATH, so that it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
         * not be found there first before the codebase is searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (srcFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            if (delete && !srcFile.delete()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    "could not delete duplicate class file in CLASSPATH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return codebaseURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public static void copyFile(File srcFile, File dstFile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        FileInputStream src = new FileInputStream(srcFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        FileOutputStream dst = new FileOutputStream(dstFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        byte[] buf = new byte[32768];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            int count = src.read(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            if (count < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            dst.write(buf, 0, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        dst.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        src.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /** routine to unexport an object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public static void unexport(Remote obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (obj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                mesg("unexporting object...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                UnicastRemoteObject.unexportObject(obj, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            } catch (NoSuchObjectException munch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                e.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Allow test framework to control the security manager set in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * each test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @param managerClassName The class name of the security manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *                         to be instantiated and set if no security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *                         manager has already been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public static void suggestSecurityManager(String managerClassName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        SecurityManager manager = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (System.getSecurityManager() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                if (managerClassName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    managerClassName = TestParams.defaultSecurityManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                manager = ((SecurityManager) Class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                           forName(managerClassName).newInstance());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            } catch (ClassNotFoundException cnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                bomb("Security manager could not be found: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                     managerClassName, cnfe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                bomb("Error creating security manager. ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            System.setSecurityManager(manager);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Method to capture the stack trace of an exception and return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * as a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    public String stackTraceToString(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        PrintStream ps = new PrintStream(bos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        e.printStackTrace(ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return bos.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /** extra properties */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    private static Properties props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Returns extra test properties. Looks for the file "../../test.props"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * and reads it in as a Properties file. Assuming the working directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * is "<path>/JTwork/scratch", this will find "<path>/test.props".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private static synchronized Properties getExtraProperties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (props != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            return props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        File f = new File(".." + File.separator + ".." + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                          "test.props");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (!f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            FileInputStream in = new FileInputStream(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                props.load(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            throw new RuntimeException("extra property setup failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Returns an extra test property. Looks for the file "../../test.props"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * and reads it in as a Properties file. Assuming the working directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * is "<path>/JTwork/scratch", this will find "<path>/test.props".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * If the property isn't found, defaultVal is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    public static String getExtraProperty(String property, String defaultVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return getExtraProperties().getProperty(property, defaultVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
}