Path: blob/master/test/jdk/java/lang/invoke/ClassValueTest.java
41149 views
/*1* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @summary tests for class-specific values25* @compile ClassValueTest.java26* @run testng/othervm test.java.lang.invoke.ClassValueTest27*/2829package test.java.lang.invoke;3031import org.testng.*;32import static org.testng.AssertJUnit.*;33import org.testng.annotations.*;3435/**36* @author jrose37*/38public class ClassValueTest {39static String nameForCV1(Class<?> type) {40return "CV1:" + type.getName();41}42int countForCV1;43final ClassValue<String> CV1 = new CV1();44private class CV1 extends ClassValue<String> {45protected String computeValue(Class<?> type) {46countForCV1++;47return nameForCV1(type);48}49}5051static final Class<?>[] CLASSES = {52String.class,53Integer.class,54int.class,55boolean[].class,56char[][].class,57ClassValueTest.class58};5960@Test61public void testGet() {62countForCV1 = 0;63for (Class<?> c : CLASSES) {64assertEquals(nameForCV1(c), CV1.get(c));65}66assertEquals(CLASSES.length, countForCV1);67for (Class<?> c : CLASSES) {68assertEquals(nameForCV1(c), CV1.get(c));69}70assertEquals(CLASSES.length, countForCV1);71}7273@Test74public void testRemove() {75for (Class<?> c : CLASSES) {76CV1.get(c);77}78countForCV1 = 0;79int REMCOUNT = 3;80for (int i = 0; i < REMCOUNT; i++) {81CV1.remove(CLASSES[i]);82}83assertEquals(0, countForCV1); // no change84for (Class<?> c : CLASSES) {85assertEquals(nameForCV1(c), CV1.get(c));86}87assertEquals(REMCOUNT, countForCV1);88}8990static String nameForCVN(Class<?> type, int n) {91return "CV[" + n + "]" + type.getName();92}93int countForCVN;94class CVN extends ClassValue<String> {95final int n;96CVN(int n) { this.n = n; }97protected String computeValue(Class<?> type) {98countForCVN++;99return nameForCVN(type, n);100}101};102103@Test104public void testGetMany() {105int CVN_COUNT1 = 100, CVN_COUNT2 = 100;106CVN cvns[] = new CVN[CVN_COUNT1 * CVN_COUNT2];107for (int n = 0; n < cvns.length; n++) {108cvns[n] = new CVN(n);109}110countForCVN = 0;111for (int pass = 0; pass <= 2; pass++) {112for (int i1 = 0; i1 < CVN_COUNT1; i1++) {113eachClass:114for (Class<?> c : CLASSES) {115for (int i2 = 0; i2 < CVN_COUNT2; i2++) {116int n = i1*CVN_COUNT2 + i2;117assertEquals(0, countForCVN);118assertEquals(nameForCVN(c, n), cvns[n].get(c));119cvns[n].get(c); //get it again120//System.out.println("getting "+n+":"+cvns[n].get(c));121boolean doremove = (((i1 + i2) & 3) == 0);122switch (pass) {123case 0:124assertEquals(1, countForCVN);125break;126case 1:127// remove on middle pass128assertEquals(0, countForCVN);129if (doremove) {130//System.out.println("removing "+n+":"+cvns[n].get(c));131cvns[n].remove(c);132assertEquals(0, countForCVN);133}134break;135case 2:136assertEquals(doremove ? 1 : 0, countForCVN);137break;138}139countForCVN = 0;140if (i1 > i2 && i1 < i2+5) continue eachClass; // leave diagonal gap141}142}143}144}145assertEquals(countForCVN, 0);146System.out.println("[rechecking values]");147for (int i = 0; i < cvns.length * 10; i++) {148int n = i % cvns.length;149for (Class<?> c : CLASSES) {150assertEquals(nameForCVN(c, n), cvns[n].get(c));151}152}153}154}155156157