jrose | a64264e | 2010-09-14 01:42:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 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. 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 |
| 29 | * @run junit/othervm test.java.dyn.ClassValueTest |
| 30 | */ |
| 31 | |
| 32 | /* |
| 33 | Manually: |
| 34 | $ $JAVA7X_HOME/bin/javac -d foo -cp $JUNIT4_JAR test/java/dyn/ClassValueTest.java |
| 35 | $ $JAVA7X_HOME/bin/java -cp foo:$JUNIT4_JAR org.junit.runner.JUnitCore test.java.dyn.ClassValueTest |
| 36 | Output: .testAdd => 1000 : Integer |
| 37 | */ |
| 38 | |
| 39 | package test.java.dyn; |
| 40 | |
| 41 | import java.util.*; |
| 42 | |
| 43 | import java.dyn.*; |
| 44 | |
| 45 | import org.junit.*; |
| 46 | import static org.junit.Assert.*; |
| 47 | |
| 48 | /** |
| 49 | * @author jrose |
| 50 | */ |
| 51 | public class ClassValueTest { |
| 52 | static String nameForCV1(Class<?> type) { |
| 53 | return "CV1:" + type.getName(); |
| 54 | } |
| 55 | static int countForCV1; |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame^] | 56 | static final ClassValue<String> CV1 = new CV1(); |
| 57 | private static class CV1 extends ClassValue<String> { |
jrose | a64264e | 2010-09-14 01:42:03 -0700 | [diff] [blame] | 58 | protected String computeValue(Class<?> type) { |
| 59 | countForCV1++; |
| 60 | return nameForCV1(type); |
| 61 | } |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame^] | 62 | } |
jrose | a64264e | 2010-09-14 01:42:03 -0700 | [diff] [blame] | 63 | |
| 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 | } |
| 106 | static int countForCVN; |
| 107 | static class CVN extends ClassValue<String> { |
| 108 | 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 | } |