blob: b28768533ed3a97414a79cc2dcb4dd3abfb6d137 [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
serb9adafbe2013-11-12 20:24:25 +04007 * published by the Free Software Foundation.
jrosea64264e2010-09-14 01:42:03 -07008 *
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 * @summary tests for class-specific values
26 * @compile ClassValueTest.java
henryjenf6613f42013-08-12 12:11:04 -070027 * @run testng/othervm test.java.lang.invoke.ClassValueTest
jrosea64264e2010-09-14 01:42:03 -070028 */
29
jroseada69fa2011-03-23 23:02:31 -070030package test.java.lang.invoke;
jrosea64264e2010-09-14 01:42:03 -070031
henryjenf6613f42013-08-12 12:11:04 -070032import org.testng.*;
33import static org.testng.AssertJUnit.*;
34import org.testng.annotations.*;
jrosea64264e2010-09-14 01:42:03 -070035
36/**
37 * @author jrose
38 */
39public class ClassValueTest {
40 static String nameForCV1(Class<?> type) {
41 return "CV1:" + type.getName();
42 }
jrose691778e2011-04-09 21:38:40 -070043 int countForCV1;
44 final ClassValue<String> CV1 = new CV1();
45 private class CV1 extends ClassValue<String> {
jrosea64264e2010-09-14 01:42:03 -070046 protected String computeValue(Class<?> type) {
47 countForCV1++;
48 return nameForCV1(type);
49 }
jroseb90d2e72010-12-16 15:59:27 -080050 }
jrosea64264e2010-09-14 01:42:03 -070051
jrose291bec92012-01-18 17:34:32 -080052 static final Class<?>[] CLASSES = {
jrosea64264e2010-09-14 01:42:03 -070053 String.class,
54 Integer.class,
55 int.class,
56 boolean[].class,
57 char[][].class,
58 ClassValueTest.class
59 };
60
61 @Test
62 public void testGet() {
63 countForCV1 = 0;
jrose291bec92012-01-18 17:34:32 -080064 for (Class<?> c : CLASSES) {
jrosea64264e2010-09-14 01:42:03 -070065 assertEquals(nameForCV1(c), CV1.get(c));
66 }
67 assertEquals(CLASSES.length, countForCV1);
jrose291bec92012-01-18 17:34:32 -080068 for (Class<?> c : CLASSES) {
jrosea64264e2010-09-14 01:42:03 -070069 assertEquals(nameForCV1(c), CV1.get(c));
70 }
71 assertEquals(CLASSES.length, countForCV1);
72 }
73
74 @Test
75 public void testRemove() {
jrose291bec92012-01-18 17:34:32 -080076 for (Class<?> c : CLASSES) {
jrosea64264e2010-09-14 01:42:03 -070077 CV1.get(c);
78 }
79 countForCV1 = 0;
80 int REMCOUNT = 3;
81 for (int i = 0; i < REMCOUNT; i++) {
82 CV1.remove(CLASSES[i]);
83 }
84 assertEquals(0, countForCV1); // no change
jrose291bec92012-01-18 17:34:32 -080085 for (Class<?> c : CLASSES) {
jrosea64264e2010-09-14 01:42:03 -070086 assertEquals(nameForCV1(c), CV1.get(c));
87 }
88 assertEquals(REMCOUNT, countForCV1);
89 }
90
91 static String nameForCVN(Class<?> type, int n) {
92 return "CV[" + n + "]" + type.getName();
93 }
jrose691778e2011-04-09 21:38:40 -070094 int countForCVN;
95 class CVN extends ClassValue<String> {
jrosea64264e2010-09-14 01:42:03 -070096 final int n;
97 CVN(int n) { this.n = n; }
98 protected String computeValue(Class<?> type) {
99 countForCVN++;
100 return nameForCVN(type, n);
101 }
102 };
103
104 @Test
105 public void testGetMany() {
106 int CVN_COUNT1 = 100, CVN_COUNT2 = 100;
107 CVN cvns[] = new CVN[CVN_COUNT1 * CVN_COUNT2];
108 for (int n = 0; n < cvns.length; n++) {
109 cvns[n] = new CVN(n);
110 }
111 countForCVN = 0;
112 for (int pass = 0; pass <= 2; pass++) {
113 for (int i1 = 0; i1 < CVN_COUNT1; i1++) {
114 eachClass:
jrose291bec92012-01-18 17:34:32 -0800115 for (Class<?> c : CLASSES) {
jrosea64264e2010-09-14 01:42:03 -0700116 for (int i2 = 0; i2 < CVN_COUNT2; i2++) {
117 int n = i1*CVN_COUNT2 + i2;
118 assertEquals(0, countForCVN);
119 assertEquals(nameForCVN(c, n), cvns[n].get(c));
120 cvns[n].get(c); //get it again
121 //System.out.println("getting "+n+":"+cvns[n].get(c));
122 boolean doremove = (((i1 + i2) & 3) == 0);
123 switch (pass) {
124 case 0:
125 assertEquals(1, countForCVN);
126 break;
127 case 1:
128 // remove on middle pass
129 assertEquals(0, countForCVN);
130 if (doremove) {
131 //System.out.println("removing "+n+":"+cvns[n].get(c));
132 cvns[n].remove(c);
133 assertEquals(0, countForCVN);
134 }
135 break;
136 case 2:
137 assertEquals(doremove ? 1 : 0, countForCVN);
138 break;
139 }
140 countForCVN = 0;
141 if (i1 > i2 && i1 < i2+5) continue eachClass; // leave diagonal gap
142 }
143 }
144 }
145 }
146 assertEquals(countForCVN, 0);
jrose291bec92012-01-18 17:34:32 -0800147 System.out.println("[rechecking values]");
148 for (int i = 0; i < cvns.length * 10; i++) {
149 int n = i % cvns.length;
150 for (Class<?> c : CLASSES) {
jrosea64264e2010-09-14 01:42:03 -0700151 assertEquals(nameForCVN(c, n), cvns[n].get(c));
152 }
153 }
154 }
155}