jdk/test/java/lang/reflect/Proxy/Boxing.java
author mchung
Fri, 18 Jun 2010 09:35:22 -0700
changeset 5808 3a1f603c5ca7
parent 5506 202f599c92aa
child 7668 d4a77089c587
permissions -rw-r--r--
6961894: TEST_BUG: jdk_lang tests fail in samevm mode Summary: Fixed jdk_lang tests to run in samevm mode or mark to run in othervm Reviewed-by: alanb
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) 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
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4889342
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary This test verifies that when a primitive boolean value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * passed by a dynamic proxy class to an invocation handler, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * boxed as one of the canonical Boolean instances (Boolean.TRUE or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Boolean.FALSE).  This test also exercises a dynamic proxy class's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * boxing of all primitive types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @author Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run main Boxing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.reflect.InvocationHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.lang.reflect.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class Boxing {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private interface Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        void m(byte b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
               char c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
               double d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
               float f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
               int i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
               long j,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
               short s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
               boolean z);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final int REPS = 100000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private byte b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private double d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private float f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private long j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private short s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private boolean z;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        (new Boxing()).run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        System.err.println("TEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        Random random = new Random(42); // ensure consistent test domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        Test proxy = (Test) Proxy.newProxyInstance(
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    74
            Test.class.getClassLoader(),
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    75
            new Class<?>[] { Test.class },
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            new TestHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        for (int rep = 0; rep < REPS; rep++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            b = (byte) random.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            c = (char) random.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            d = random.nextDouble();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            f = random.nextFloat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            i = random.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            j = random.nextLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            s = (short) random.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            z = random.nextBoolean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            proxy.m(b,c,d,f,i,j,s,z);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private class TestHandler implements InvocationHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        public Object invoke(Object proxy, Method method, Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            throws Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if (!method.getName().equals("m")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            byte b2 = ((Byte) args[0]).byteValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (b2 != b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    "wrong byte, expected " + b + " but got " + b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            char c2 = ((Character) args[1]).charValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (c2 != c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    "wrong char, expected " + c + " but got " + c2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            double d2 = ((Double) args[2]).doubleValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (d2 != d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    "wrong double, expected " + d + " but got " + d2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            float f2 = ((Float) args[3]).floatValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (f2 != f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    "wrong float, expected " + f + " but got " + f2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            int i2 = ((Integer) args[4]).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (i2 != i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    "wrong int, expected " + i + " but got " + i2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            long j2 = ((Long) args[5]).longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            if (j2 != j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    "wrong long, expected " + j + " but got " + j2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            short s2 = ((Short) args[6]).shortValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (s2 != s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    "wrong short, expected " + s + " but got " + s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            Boolean Z = Boolean.valueOf(z);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            Boolean Z2 = (Boolean) args[7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (Z2 != Z) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    "wrong Boolean instance, expected " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    identityToString(Z) + " but got " + identityToString(Z2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private static String identityToString(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return obj.toString() + "@" + System.identityHashCode(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}