jdk/test/java/lang/instrument/ATransformerManagementTestCase.java
author jbachorik
Tue, 21 Jan 2014 13:04:55 +0100
changeset 22353 d09e3ff5fd63
parent 13576 850377563282
child 36511 9d0388c6b336
permissions -rw-r--r--
8032377: test/java/lang/management/ThreadMXBean/ThreadBlockedCount.java still fails intermittently Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
13576
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2012, 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
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
    {
13576
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   102
        addTransformerToManager(manager, transformer, false);
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   103
    }
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   104
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   105
    /**
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   106
     * Method addTransformerToManager.
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   107
     * @param manager
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   108
     * @param transformer
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   109
     * @param canRetransform
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   110
     */
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   111
    protected void
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   112
    addTransformerToManager(
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   113
        Instrumentation         manager,
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   114
        ClassFileTransformer    transformer,
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   115
        boolean                 canRetransform)
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   116
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (transformer != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            fTransformers.add(transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
13576
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   121
        manager.addTransformer(transformer, canRetransform);
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   122
        verbosePrint("Added transformer " + transformer
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   123
            + " with canRetransform=" + canRetransform);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Remove transformer from manager and list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param transformer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    protected void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    removeTransformerFromManager(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        Instrumentation manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        ClassFileTransformer transformer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        assertTrue("Transformer not found in manager ("+transformer+")", manager.removeTransformer(transformer));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (transformer != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            fTransformers.remove(transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        verbosePrint("Removed transformer " + transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Decrements the transformer index as well as removes transformer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param fInst         manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param transformer       transformer to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param decrementIndex    the tranformer index gets out of sync with transformers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *                          that are removed from the manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    protected void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    removeTransformerFromManager(   Instrumentation manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                                    ClassFileTransformer transformer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                    boolean decrementIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        removeTransformerFromManager(manager, transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (decrementIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            fTransformerIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            verbosePrint("removeTransformerFromManager fTransformerIndex decremented to: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                         fTransformerIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * verify transformer by asserting that the number of transforms that occured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * is the same as the number of valid transformers added to the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @param manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    protected void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    verifyTransformers(Instrumentation manager)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        File f = new File(System.getProperty("test.classes", "."), redefinedClassName + ".class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        System.out.println("Reading test class from " + f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            InputStream redefineStream = new FileInputStream(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            byte[] bytes = NamedBuffer.loadBufferFromStream(redefineStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            ClassDefinition cd = new ClassDefinition(DummyClass.class, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            fInst.redefineClasses(new ClassDefinition[]{ cd });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            verbosePrint("verifyTransformers redefined " + redefinedClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        catch (IOException e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            fail("Could not load the class: " + redefinedClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        catch (ClassNotFoundException e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            fail("Could not find the class: " + redefinedClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        catch (UnmodifiableClassException e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            fail("Could not modify the class: " + redefinedClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        // Report any delayed failures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        assertTrue(fDelayedFailure, fDelayedFailure == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        assertEquals("The number of transformers that were run does not match the expected number added to manager",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        fTransformers.size(), fTransformerIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Asserts that the transformer being checked by the manager is the correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * one (as far as order goes) and updates the number of transformers that have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * been called.  Note that since this is being called inside of a transformer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * a standard assert (which throws an exception) cannot be used since it would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * simply cancel the transformation and otherwise be ignored.  Instead, note
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * the failure for delayed processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param ClassFileTransformer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    checkInTransformer(ClassFileTransformer transformer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        verbosePrint("checkInTransformer: " + transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (fDelayedFailure == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (fTransformers.size() <= fTransformerIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                String msg = "The number of transformers that have checked in (" +(fTransformerIndex+1) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    ") is greater number of tranformers created ("+fTransformers.size()+")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                fDelayedFailure = msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                System.err.println("Delayed failure: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                verbosePrint("Delayed failure: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            if (!fTransformers.get(fTransformerIndex).equals(transformer))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                String msg = "Transformer " + fTransformers.get(fTransformerIndex) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    " should be the same as " + transformer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                fDelayedFailure = msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                System.err.println("Delayed failure: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                verbosePrint("Delayed failure: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            fTransformerIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            verbosePrint("fTransformerIndex incremented to: " + fTransformerIndex);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Create a new manager, a new transformer list, and initializes the number of transformers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * indexed to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    protected void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    setUp()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        super.setUp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        fTransformers = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        fTransformerIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        fDelayedFailure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        verbosePrint("setUp completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Sets the manager and transformers to null so that setUp needs to update.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    protected void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    tearDown()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        verbosePrint("tearDown beginning");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        fTransformers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        super.tearDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *  Simple transformer that registers when it transforms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public class MyClassFileTransformer extends SimpleIdentityTransformer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        private final String fID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        public MyClassFileTransformer(String id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            fID = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return MyClassFileTransformer.this.getClass().getName() + fID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        public byte[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        transform(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            Class<?> classBeingRedefined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            ProtectionDomain    protectionDomain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            byte[] classfileBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // The transform testing is triggered by redefine, ignore others
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if (classBeingRedefined != null) checkInTransformer(MyClassFileTransformer.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return super.transform(    loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                        className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                        classBeingRedefined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                        protectionDomain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                        classfileBuffer);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Class loader that does nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public class MyClassLoader extends ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
         * Constructor for MyClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        public MyClassLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public String debug_byteArrayToString(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (b == null) return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        buf.append("byte[");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        buf.append(b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        buf.append("] (");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        for (int i = 0; i < b.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            buf.append(b[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            buf.append(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        buf.deleteCharAt(buf.length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        buf.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
}