blob: 8e6ab29d0f95aa3923a45074df52fae38f3bad37 [file] [log] [blame]
jrosea64264e2010-09-14 01:42:03 -07001/*
alanb0d058232012-11-02 15:50:11 +00002 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
jrosea64264e2010-09-14 01:42:03 -07003 * 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26/* @test
27 * @summary tests for class-specific values
28 * @compile ClassValueTest.java
jroseada69fa2011-03-23 23:02:31 -070029 * @run junit/othervm test.java.lang.invoke.ClassValueTest
jrosea64264e2010-09-14 01:42:03 -070030 */
31
32/*
33 Manually:
jroseada69fa2011-03-23 23:02:31 -070034 $ $JAVA7X_HOME/bin/javac -d foo -cp $JUNIT4_JAR test/java/lang/invoke/ClassValueTest.java
35 $ $JAVA7X_HOME/bin/java -cp foo:$JUNIT4_JAR org.junit.runner.JUnitCore test.java.lang.invoke.ClassValueTest
jrosea64264e2010-09-14 01:42:03 -070036 Output: .testAdd => 1000 : Integer
37 */
38
jroseada69fa2011-03-23 23:02:31 -070039package test.java.lang.invoke;
jrosea64264e2010-09-14 01:42:03 -070040
jrosea64264e2010-09-14 01:42:03 -070041import org.junit.*;
42import static org.junit.Assert.*;
43
44/**
45 * @author jrose
46 */
47public class ClassValueTest {
48 static String nameForCV1(Class<?> type) {
49 return "CV1:" + type.getName();
50 }
jrose691778e2011-04-09 21:38:40 -070051 int countForCV1;
52 final ClassValue<String> CV1 = new CV1();
53 private class CV1 extends ClassValue<String> {
jrosea64264e2010-09-14 01:42:03 -070054 protected String computeValue(Class<?> type) {
55 countForCV1++;
56 return nameForCV1(type);
57 }
jroseb90d2e72010-12-16 15:59:27 -080058 }
jrosea64264e2010-09-14 01:42:03 -070059
jrose291bec92012-01-18 17:34:32 -080060 static final Class<?>[] CLASSES = {
jrosea64264e2010-09-14 01:42:03 -070061 String.class,
62 Integer.class,
63 int.class,
64 boolean[].class,
65 char[][].class,
66 ClassValueTest.class
67 };
68
69 @Test
70 public void testGet() {
71 countForCV1 = 0;
jrose291bec92012-01-18 17:34:32 -080072 for (Class<?> c : CLASSES) {
jrosea64264e2010-09-14 01:42:03 -070073 assertEquals(nameForCV1(c), CV1.get(c));
74 }
75 assertEquals(CLASSES.length, countForCV1);
jrose291bec92012-01-18 17:34:32 -080076 for (Class<?> c : CLASSES) {
jrosea64264e2010-09-14 01:42:03 -070077 assertEquals(nameForCV1(c), CV1.get(c));
78 }
79 assertEquals(CLASSES.length, countForCV1);
80 }
81
82 @Test
83 public void testRemove() {
jrose291bec92012-01-18 17:34:32 -080084 for (Class<?> c : CLASSES) {
jrosea64264e2010-09-14 01:42:03 -070085 CV1.get(c);
86 }
87 countForCV1 = 0;
88 int REMCOUNT = 3;
89 for (int i = 0; i < REMCOUNT; i++) {
90 CV1.remove(CLASSES[i]);
91 }
92 assertEquals(0, countForCV1); // no change
jrose291bec92012-01-18 17:34:32 -080093 for (Class<?> c : CLASSES) {
jrosea64264e2010-09-14 01:42:03 -070094 assertEquals(nameForCV1(c), CV1.get(c));
95 }
96 assertEquals(REMCOUNT, countForCV1);
97 }
98
99 static String nameForCVN(Class<?> type, int n) {
100 return "CV[" + n + "]" + type.getName();
101 }
jrose691778e2011-04-09 21:38:40 -0700102 int countForCVN;
103 class CVN extends ClassValue<String> {
jrosea64264e2010-09-14 01:42:03 -0700104 final int n;
105 CVN(int n) { this.n = n; }
106 protected String computeValue(Class<?> type) {
107 countForCVN++;
108 return nameForCVN(type, n);
109 }
110 };
111
112 @Test
113 public void testGetMany() {
114 int CVN_COUNT1 = 100, CVN_COUNT2 = 100;
115 CVN cvns[] = new CVN[CVN_COUNT1 * CVN_COUNT2];
116 for (int n = 0; n < cvns.length; n++) {
117 cvns[n] = new CVN(n);
118 }
119 countForCVN = 0;
120 for (int pass = 0; pass <= 2; pass++) {
121 for (int i1 = 0; i1 < CVN_COUNT1; i1++) {
122 eachClass:
jrose291bec92012-01-18 17:34:32 -0800123 for (Class<?> c : CLASSES) {
jrosea64264e2010-09-14 01:42:03 -0700124 for (int i2 = 0; i2 < CVN_COUNT2; i2++) {
125 int n = i1*CVN_COUNT2 + i2;
126 assertEquals(0, countForCVN);
127 assertEquals(nameForCVN(c, n), cvns[n].get(c));
128 cvns[n].get(c); //get it again
129 //System.out.println("getting "+n+":"+cvns[n].get(c));
130 boolean doremove = (((i1 + i2) & 3) == 0);
131 switch (pass) {
132 case 0:
133 assertEquals(1, countForCVN);
134 break;
135 case 1:
136 // remove on middle pass
137 assertEquals(0, countForCVN);
138 if (doremove) {
139 //System.out.println("removing "+n+":"+cvns[n].get(c));
140 cvns[n].remove(c);
141 assertEquals(0, countForCVN);
142 }
143 break;
144 case 2:
145 assertEquals(doremove ? 1 : 0, countForCVN);
146 break;
147 }
148 countForCVN = 0;
149 if (i1 > i2 && i1 < i2+5) continue eachClass; // leave diagonal gap
150 }
151 }
152 }
153 }
154 assertEquals(countForCVN, 0);
jrose291bec92012-01-18 17:34:32 -0800155 System.out.println("[rechecking values]");
156 for (int i = 0; i < cvns.length * 10; i++) {
157 int n = i % cvns.length;
158 for (Class<?> c : CLASSES) {
jrosea64264e2010-09-14 01:42:03 -0700159 assertEquals(nameForCVN(c, n), cvns[n].get(c));
160 }
161 }
162 }
163}