jdk/test/java/lang/instrument/ATransformerManagementTestCase.java
author dcubed
Mon, 24 Mar 2008 15:14:31 -0700
changeset 273 3d51c181a2e5
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names Summary: Attribute values should be extracted without leading or trailing whitespace. Reviewed-by: ohair, sspitsyn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.lang.instrument.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.ProtectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Simple tests for the TransformerManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public abstract class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
ATransformerManagementTestCase
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    extends AInstrumentationTestCase
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static final String redefinedClassName = "DummyClass";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    protected int   kModSamples = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public final    ClassFileTransformer[] kTransformerSamples = new ClassFileTransformer[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            new MyClassFileTransformer(         Integer.toString(0)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            new MyClassFileTransformer( Integer.toString(1)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            new MyClassFileTransformer(         Integer.toString(2)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            new MyClassFileTransformer( Integer.toString(3)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            new MyClassFileTransformer(         Integer.toString(4)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            new MyClassFileTransformer( Integer.toString(5)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            new MyClassFileTransformer(         Integer.toString(6)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            new MyClassFileTransformer( Integer.toString(7)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            new MyClassFileTransformer(         Integer.toString(8)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            new MyClassFileTransformer( Integer.toString(9)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            new MyClassFileTransformer(         Integer.toString(10)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            new MyClassFileTransformer( Integer.toString(11)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            new MyClassFileTransformer( Integer.toString(12)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            new MyClassFileTransformer( Integer.toString(13)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            new MyClassFileTransformer(         Integer.toString(14)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            new MyClassFileTransformer( Integer.toString(15)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            new MyClassFileTransformer(         Integer.toString(16)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            new MyClassFileTransformer(         Integer.toString(17)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            new MyClassFileTransformer( Integer.toString(18)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private ArrayList           fTransformers;       // The list of transformers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private int                 fTransformerIndex;   // The number of transformers encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private String              fDelayedFailure;     // Set non-null if failed in transformer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Constructor for ATransformerManagementTestCase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public ATransformerManagementTestCase(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Returns one of the sample transformers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @return a random transformer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    protected ClassFileTransformer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    getRandomTransformer()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        int randIndex = (int)Math.floor(Math.random() * kTransformerSamples.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        verbosePrint("Choosing random transformer #" + randIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return kTransformerSamples[randIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Method addTransformerToManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param transformer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    protected void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    addTransformerToManager(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        Instrumentation         manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        ClassFileTransformer    transformer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (transformer != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            fTransformers.add(transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        manager.addTransformer(transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        verbosePrint("Added transformer " + transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Remove transformer from manager and list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param transformer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    protected void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    removeTransformerFromManager(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        Instrumentation manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        ClassFileTransformer transformer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        assertTrue("Transformer not found in manager ("+transformer+")", manager.removeTransformer(transformer));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (transformer != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            fTransformers.remove(transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        verbosePrint("Removed transformer " + transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Decrements the transformer index as well as removes transformer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param fInst         manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param transformer       transformer to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param decrementIndex    the tranformer index gets out of sync with transformers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *                          that are removed from the manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    protected void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    removeTransformerFromManager(   Instrumentation manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                    ClassFileTransformer transformer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                    boolean decrementIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        removeTransformerFromManager(manager, transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (decrementIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            fTransformerIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            verbosePrint("removeTransformerFromManager fTransformerIndex decremented to: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                         fTransformerIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * verify transformer by asserting that the number of transforms that occured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * is the same as the number of valid transformers added to the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    protected void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    verifyTransformers(Instrumentation manager)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        File f = new File(System.getProperty("test.classes", "."), redefinedClassName + ".class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        System.out.println("Reading test class from " + f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            InputStream redefineStream = new FileInputStream(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            byte[] bytes = NamedBuffer.loadBufferFromStream(redefineStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            ClassDefinition cd = new ClassDefinition(DummyClass.class, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            fInst.redefineClasses(new ClassDefinition[]{ cd });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            verbosePrint("verifyTransformers redefined " + redefinedClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        catch (IOException e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            fail("Could not load the class: " + redefinedClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        catch (ClassNotFoundException e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            fail("Could not find the class: " + redefinedClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        catch (UnmodifiableClassException e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            fail("Could not modify the class: " + redefinedClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        // Report any delayed failures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        assertTrue(fDelayedFailure, fDelayedFailure == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        assertEquals("The number of transformers that were run does not match the expected number added to manager",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                        fTransformers.size(), fTransformerIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Asserts that the transformer being checked by the manager is the correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * one (as far as order goes) and updates the number of transformers that have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * been called.  Note that since this is being called inside of a transformer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * a standard assert (which throws an exception) cannot be used since it would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * simply cancel the transformation and otherwise be ignored.  Instead, note
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * the failure for delayed processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param ClassFileTransformer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    checkInTransformer(ClassFileTransformer transformer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        verbosePrint("checkInTransformer: " + transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (fDelayedFailure == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if (fTransformers.size() <= fTransformerIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                String msg = "The number of transformers that have checked in (" +(fTransformerIndex+1) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    ") is greater number of tranformers created ("+fTransformers.size()+")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                fDelayedFailure = msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                System.err.println("Delayed failure: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                verbosePrint("Delayed failure: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            if (!fTransformers.get(fTransformerIndex).equals(transformer))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                String msg = "Transformer " + fTransformers.get(fTransformerIndex) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    " should be the same as " + transformer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                fDelayedFailure = msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                System.err.println("Delayed failure: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                verbosePrint("Delayed failure: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            fTransformerIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            verbosePrint("fTransformerIndex incremented to: " + fTransformerIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Create a new manager, a new transformer list, and initializes the number of transformers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * indexed to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    protected void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    setUp()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        super.setUp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        fTransformers = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        fTransformerIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        fDelayedFailure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        verbosePrint("setUp completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Sets the manager and transformers to null so that setUp needs to update.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    protected void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    tearDown()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        verbosePrint("tearDown beginning");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        fTransformers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        super.tearDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *  Simple transformer that registers when it transforms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public class MyClassFileTransformer extends SimpleIdentityTransformer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        private final String fID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        public MyClassFileTransformer(String id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            fID = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            return MyClassFileTransformer.this.getClass().getName() + fID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        public byte[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        transform(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            Class<?> classBeingRedefined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            ProtectionDomain    protectionDomain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            byte[] classfileBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            // The transform testing is triggered by redefine, ignore others
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if (classBeingRedefined != null) checkInTransformer(MyClassFileTransformer.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            return super.transform(    loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                        className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                                        classBeingRedefined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                        protectionDomain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                        classfileBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Class loader that does nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public class MyClassLoader extends ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         * Constructor for MyClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        public MyClassLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public String debug_byteArrayToString(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (b == null) return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        buf.append("byte[");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        buf.append(b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        buf.append("] (");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        for (int i = 0; i < b.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            buf.append(b[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            buf.append(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        buf.deleteCharAt(buf.length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        buf.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
}