1/*
2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25 * @bug 4889342
26 * @summary This test verifies that when a primitive boolean value is
27 * passed by a dynamic proxy class to an invocation handler, it is
28 * boxed as one of the canonical Boolean instances (Boolean.TRUE or
29 * Boolean.FALSE).  This test also exercises a dynamic proxy class's
30 * boxing of all primitive types.
31 * @author Peter Jones
32 *
33 * @run main Boxing
34 */
35
36import java.lang.reflect.InvocationHandler;
37import java.lang.reflect.Method;
38import java.lang.reflect.Proxy;
39import java.util.Random;
40
41public class Boxing {
42
43    private interface Test {
44        void m(byte b,
45               char c,
46               double d,
47               float f,
48               int i,
49               long j,
50               short s,
51               boolean z);
52    }
53
54    private static final int REPS = 100000;
55
56    private byte b;
57    private char c;
58    private double d;
59    private float f;
60    private int i;
61    private long j;
62    private short s;
63    private boolean z;
64
65    public static void main(String[] args) {
66        (new Boxing()).run();
67        System.err.println("TEST PASSED");
68    }
69
70    private void run() {
71        Random random = new Random(42); // ensure consistent test domain
72
73        Test proxy = (Test) Proxy.newProxyInstance(
74            Test.class.getClassLoader(),
75            new Class<?>[] { Test.class },
76            new TestHandler());
77
78        for (int rep = 0; rep < REPS; rep++) {
79            b = (byte) random.nextInt();
80            c = (char) random.nextInt();
81            d = random.nextDouble();
82            f = random.nextFloat();
83            i = random.nextInt();
84            j = random.nextLong();
85            s = (short) random.nextInt();
86            z = random.nextBoolean();
87            proxy.m(b,c,d,f,i,j,s,z);
88        }
89    }
90
91    private class TestHandler implements InvocationHandler {
92        public Object invoke(Object proxy, Method method, Object[] args)
93            throws Throwable
94        {
95            if (!method.getName().equals("m")) {
96                throw new AssertionError();
97            }
98
99            byte b2 = ((Byte) args[0]).byteValue();
100            if (b2 != b) {
101                throw new RuntimeException("TEST FAILED: " +
102                    "wrong byte, expected " + b + " but got " + b2);
103            }
104
105            char c2 = ((Character) args[1]).charValue();
106            if (c2 != c) {
107                throw new RuntimeException("TEST FAILED: " +
108                    "wrong char, expected " + c + " but got " + c2);
109            }
110
111            double d2 = ((Double) args[2]).doubleValue();
112            if (d2 != d) {
113                throw new RuntimeException("TEST FAILED: " +
114                    "wrong double, expected " + d + " but got " + d2);
115            }
116
117            float f2 = ((Float) args[3]).floatValue();
118            if (f2 != f) {
119                throw new RuntimeException("TEST FAILED: " +
120                    "wrong float, expected " + f + " but got " + f2);
121            }
122
123            int i2 = ((Integer) args[4]).intValue();
124            if (i2 != i) {
125                throw new RuntimeException("TEST FAILED: " +
126                    "wrong int, expected " + i + " but got " + i2);
127            }
128
129            long j2 = ((Long) args[5]).longValue();
130            if (j2 != j) {
131                throw new RuntimeException("TEST FAILED: " +
132                    "wrong long, expected " + j + " but got " + j2);
133            }
134
135            short s2 = ((Short) args[6]).shortValue();
136            if (s2 != s) {
137                throw new RuntimeException("TEST FAILED: " +
138                    "wrong short, expected " + s + " but got " + s2);
139            }
140
141            Boolean Z = Boolean.valueOf(z);
142            Boolean Z2 = (Boolean) args[7];
143            if (Z2 != Z) {
144                throw new RuntimeException("TEST FAILED: " +
145                    "wrong Boolean instance, expected " +
146                    identityToString(Z) + " but got " + identityToString(Z2));
147            }
148
149            return null;
150        }
151    }
152
153    private static String identityToString(Object obj) {
154        return obj.toString() + "@" + System.identityHashCode(obj);
155    }
156}
157