1 /* 2 * Copyright (c) 2011, 2012, 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.lang.invoke.ClassValueTest 30 */ 31 32 /* 33 Manually: 34 $ $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 36 Output: .testAdd => 1000 : Integer 37 */ 38 39 package test.java.lang.invoke; 40 41 import org.junit.*; 42 import static org.junit.Assert.*; 43 44 /** 45 * @author jrose 46 */ 47 public class ClassValueTest { 48 static String nameForCV1(Class<?> type) { 49 return "CV1:" + type.getName(); 50 } 51 int countForCV1; 52 final ClassValue<String> CV1 = new CV1(); 53 private class CV1 extends ClassValue<String> { 54 protected String computeValue(Class<?> type) { 55 countForCV1++; 56 return nameForCV1(type); 57 } 58 } 59 60 static final Class<?>[] CLASSES = { 61 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; 72 for (Class<?> c : CLASSES) { 73 assertEquals(nameForCV1(c), CV1.get(c)); 74 } 75 assertEquals(CLASSES.length, countForCV1); 76 for (Class<?> c : CLASSES) { 77 assertEquals(nameForCV1(c), CV1.get(c)); 78 } 79 assertEquals(CLASSES.length, countForCV1); 80 } 81 82 @Test 83 public void testRemove() { 84 for (Class<?> c : CLASSES) { 85 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 93 for (Class<?> c : CLASSES) { 94 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 } 102 int countForCVN; 103 class CVN extends ClassValue<String> { 104 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: 123 for (Class<?> c : CLASSES) { 124 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); 155 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) { 159 assertEquals(nameForCVN(c, n), cvns[n].get(c)); 160 } 161 } 162 } 163 }