blob: 8f9bd953f1eb6be4aa484628a0bdaa1657bad3b2 [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
henryjenf6613f42013-08-12 12:11:04 -070029 * @run testng/othervm test.java.lang.invoke.ClassValueTest
jrosea64264e2010-09-14 01:42:03 -070030 */
31
jroseada69fa2011-03-23 23:02:31 -070032package test.java.lang.invoke;
jrosea64264e2010-09-14 01:42:03 -070033
henryjenf6613f42013-08-12 12:11:04 -070034import org.testng.*;
35import static org.testng.AssertJUnit.*;
36import org.testng.annotations.*;
jrosea64264e2010-09-14 01:42:03 -070037
38/**
39 * @author jrose
40 */
41public class ClassValueTest {
42 static String nameForCV1(Class<?> type) {
43 return "CV1:" + type.getName();
44 }
jrose691778e2011-04-09 21:38:40 -070045 int countForCV1;
46 final ClassValue<String> CV1 = new CV1();
47 private class CV1 extends ClassValue<String> {
jrosea64264e2010-09-14 01:42:03 -070048 protected String computeValue(Class<?> type) {
49 countForCV1++;
50 return nameForCV1(type);
51 }
jroseb90d2e72010-12-16 15:59:27 -080052 }
jrosea64264e2010-09-14 01:42:03 -070053
jrose291bec92012-01-18 17:34:32 -080054 static final Class<?>[] CLASSES = {
jrosea64264e2010-09-14 01:42:03 -070055 String.class,
56 Integer.class,
57 int.class,
58 boolean[].class,
59 char[][].class,
60 ClassValueTest.class
61 };
62
63 @Test
64 public void testGet() {
65 countForCV1 = 0;
jrose291bec92012-01-18 17:34:32 -080066 for (Class<?> c : CLASSES) {
jrosea64264e2010-09-14 01:42:03 -070067 assertEquals(nameForCV1(c), CV1.get(c));
68 }
69 assertEquals(CLASSES.length, countForCV1);
jrose291bec92012-01-18 17:34:32 -080070 for (Class<?> c : CLASSES) {
jrosea64264e2010-09-14 01:42:03 -070071 assertEquals(nameForCV1(c), CV1.get(c));
72 }
73 assertEquals(CLASSES.length, countForCV1);
74 }
75
76 @Test
77 public void testRemove() {
jrose291bec92012-01-18 17:34:32 -080078 for (Class<?> c : CLASSES) {
jrosea64264e2010-09-14 01:42:03 -070079 CV1.get(c);
80 }
81 countForCV1 = 0;
82 int REMCOUNT = 3;
83 for (int i = 0; i < REMCOUNT; i++) {
84 CV1.remove(CLASSES[i]);
85 }
86 assertEquals(0, countForCV1); // no change
jrose291bec92012-01-18 17:34:32 -080087 for (Class<?> c : CLASSES) {
jrosea64264e2010-09-14 01:42:03 -070088 assertEquals(nameForCV1(c), CV1.get(c));
89 }
90 assertEquals(REMCOUNT, countForCV1);
91 }
92
93 static String nameForCVN(Class<?> type, int n) {
94 return "CV[" + n + "]" + type.getName();
95 }
jrose691778e2011-04-09 21:38:40 -070096 int countForCVN;
97 class CVN extends ClassValue<String> {
jrosea64264e2010-09-14 01:42:03 -070098 final int n;
99 CVN(int n) { this.n = n; }
100 protected String computeValue(Class<?> type) {
101 countForCVN++;
102 return nameForCVN(type, n);
103 }
104 };
105
106 @Test
107 public void testGetMany() {
108 int CVN_COUNT1 = 100, CVN_COUNT2 = 100;
109 CVN cvns[] = new CVN[CVN_COUNT1 * CVN_COUNT2];
110 for (int n = 0; n < cvns.length; n++) {
111 cvns[n] = new CVN(n);
112 }
113 countForCVN = 0;
114 for (int pass = 0; pass <= 2; pass++) {
115 for (int i1 = 0; i1 < CVN_COUNT1; i1++) {
116 eachClass:
jrose291bec92012-01-18 17:34:32 -0800117 for (Class<?> c : CLASSES) {
jrosea64264e2010-09-14 01:42:03 -0700118 for (int i2 = 0; i2 < CVN_COUNT2; i2++) {
119 int n = i1*CVN_COUNT2 + i2;
120 assertEquals(0, countForCVN);
121 assertEquals(nameForCVN(c, n), cvns[n].get(c));
122 cvns[n].get(c); //get it again
123 //System.out.println("getting "+n+":"+cvns[n].get(c));
124 boolean doremove = (((i1 + i2) & 3) == 0);
125 switch (pass) {
126 case 0:
127 assertEquals(1, countForCVN);
128 break;
129 case 1:
130 // remove on middle pass
131 assertEquals(0, countForCVN);
132 if (doremove) {
133 //System.out.println("removing "+n+":"+cvns[n].get(c));
134 cvns[n].remove(c);
135 assertEquals(0, countForCVN);
136 }
137 break;
138 case 2:
139 assertEquals(doremove ? 1 : 0, countForCVN);
140 break;
141 }
142 countForCVN = 0;
143 if (i1 > i2 && i1 < i2+5) continue eachClass; // leave diagonal gap
144 }
145 }
146 }
147 }
148 assertEquals(countForCVN, 0);
jrose291bec92012-01-18 17:34:32 -0800149 System.out.println("[rechecking values]");
150 for (int i = 0; i < cvns.length * 10; i++) {
151 int n = i % cvns.length;
152 for (Class<?> c : CLASSES) {
jrosea64264e2010-09-14 01:42:03 -0700153 assertEquals(nameForCVN(c, n), cvns[n].get(c));
154 }
155 }
156 }
157}