blob: 121c260dfa187621c03e621f9af982e562956e5d [file] [log] [blame]
jrosea64264e2010-09-14 01:42:03 -07001/*
jroseada69fa2011-03-23 23:02:31 -07002 * Copyright (c) 2011, 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
41import java.util.*;
42
jroseada69fa2011-03-23 23:02:31 -070043import java.lang.invoke.*;
jrosea64264e2010-09-14 01:42:03 -070044
45import org.junit.*;
46import static org.junit.Assert.*;
47
48/**
49 * @author jrose
50 */
51public class ClassValueTest {
52 static String nameForCV1(Class<?> type) {
53 return "CV1:" + type.getName();
54 }
jrose691778e2011-04-09 21:38:40 -070055 int countForCV1;
56 final ClassValue<String> CV1 = new CV1();
57 private class CV1 extends ClassValue<String> {
jrosea64264e2010-09-14 01:42:03 -070058 protected String computeValue(Class<?> type) {
59 countForCV1++;
60 return nameForCV1(type);
61 }
jroseb90d2e72010-12-16 15:59:27 -080062 }
jrosea64264e2010-09-14 01:42:03 -070063
64 static final Class[] CLASSES = {
65 String.class,
66 Integer.class,
67 int.class,
68 boolean[].class,
69 char[][].class,
70 ClassValueTest.class
71 };
72
73 @Test
74 public void testGet() {
75 countForCV1 = 0;
76 for (Class c : CLASSES) {
77 assertEquals(nameForCV1(c), CV1.get(c));
78 }
79 assertEquals(CLASSES.length, countForCV1);
80 for (Class c : CLASSES) {
81 assertEquals(nameForCV1(c), CV1.get(c));
82 }
83 assertEquals(CLASSES.length, countForCV1);
84 }
85
86 @Test
87 public void testRemove() {
88 for (Class c : CLASSES) {
89 CV1.get(c);
90 }
91 countForCV1 = 0;
92 int REMCOUNT = 3;
93 for (int i = 0; i < REMCOUNT; i++) {
94 CV1.remove(CLASSES[i]);
95 }
96 assertEquals(0, countForCV1); // no change
97 for (Class c : CLASSES) {
98 assertEquals(nameForCV1(c), CV1.get(c));
99 }
100 assertEquals(REMCOUNT, countForCV1);
101 }
102
103 static String nameForCVN(Class<?> type, int n) {
104 return "CV[" + n + "]" + type.getName();
105 }
jrose691778e2011-04-09 21:38:40 -0700106 int countForCVN;
107 class CVN extends ClassValue<String> {
jrosea64264e2010-09-14 01:42:03 -0700108 final int n;
109 CVN(int n) { this.n = n; }
110 protected String computeValue(Class<?> type) {
111 countForCVN++;
112 return nameForCVN(type, n);
113 }
114 };
115
116 @Test
117 public void testGetMany() {
118 int CVN_COUNT1 = 100, CVN_COUNT2 = 100;
119 CVN cvns[] = new CVN[CVN_COUNT1 * CVN_COUNT2];
120 for (int n = 0; n < cvns.length; n++) {
121 cvns[n] = new CVN(n);
122 }
123 countForCVN = 0;
124 for (int pass = 0; pass <= 2; pass++) {
125 for (int i1 = 0; i1 < CVN_COUNT1; i1++) {
126 eachClass:
127 for (Class c : CLASSES) {
128 for (int i2 = 0; i2 < CVN_COUNT2; i2++) {
129 int n = i1*CVN_COUNT2 + i2;
130 assertEquals(0, countForCVN);
131 assertEquals(nameForCVN(c, n), cvns[n].get(c));
132 cvns[n].get(c); //get it again
133 //System.out.println("getting "+n+":"+cvns[n].get(c));
134 boolean doremove = (((i1 + i2) & 3) == 0);
135 switch (pass) {
136 case 0:
137 assertEquals(1, countForCVN);
138 break;
139 case 1:
140 // remove on middle pass
141 assertEquals(0, countForCVN);
142 if (doremove) {
143 //System.out.println("removing "+n+":"+cvns[n].get(c));
144 cvns[n].remove(c);
145 assertEquals(0, countForCVN);
146 }
147 break;
148 case 2:
149 assertEquals(doremove ? 1 : 0, countForCVN);
150 break;
151 }
152 countForCVN = 0;
153 if (i1 > i2 && i1 < i2+5) continue eachClass; // leave diagonal gap
154 }
155 }
156 }
157 }
158 assertEquals(countForCVN, 0);
159 for (int n = 0; n < cvns.length; n++) {
160 for (Class c : CLASSES) {
161 assertEquals(nameForCVN(c, n), cvns[n].get(c));
162 }
163 }
164 }
165}