3013N/A/*
3793N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3013N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3013N/A *
3013N/A * This code is free software; you can redistribute it and/or modify it
3013N/A * under the terms of the GNU General Public License version 2 only, as
3013N/A * published by the Free Software Foundation. Oracle designates this
3013N/A * particular file as subject to the "Classpath" exception as provided
3013N/A * by Oracle in the LICENSE file that accompanied this code.
3013N/A *
3013N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3013N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3013N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3013N/A * version 2 for more details (a copy is included in the LICENSE file that
3013N/A * accompanied this code).
3013N/A *
3013N/A * You should have received a copy of the GNU General Public License version
3013N/A * 2 along with this work; if not, write to the Free Software Foundation,
3013N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3013N/A *
3013N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3013N/A * or visit www.oracle.com if you need additional information or have any
3013N/A * questions.
3013N/A */
3013N/A
3013N/A/* @test
3013N/A * @summary tests for class-specific values
3013N/A * @compile ClassValueTest.java
3793N/A * @run junit/othervm test.java.lang.invoke.ClassValueTest
3013N/A */
3013N/A
3013N/A/*
3013N/A Manually:
3793N/A $ $JAVA7X_HOME/bin/javac -d foo -cp $JUNIT4_JAR test/java/lang/invoke/ClassValueTest.java
3793N/A $ $JAVA7X_HOME/bin/java -cp foo:$JUNIT4_JAR org.junit.runner.JUnitCore test.java.lang.invoke.ClassValueTest
3013N/A Output: .testAdd => 1000 : Integer
3013N/A */
3013N/A
3793N/Apackage test.java.lang.invoke;
3013N/A
3013N/Aimport org.junit.*;
3013N/Aimport static org.junit.Assert.*;
3013N/A
3013N/A/**
3013N/A * @author jrose
3013N/A */
3013N/Apublic class ClassValueTest {
3013N/A static String nameForCV1(Class<?> type) {
3013N/A return "CV1:" + type.getName();
3013N/A }
3926N/A int countForCV1;
3926N/A final ClassValue<String> CV1 = new CV1();
3926N/A private class CV1 extends ClassValue<String> {
3013N/A protected String computeValue(Class<?> type) {
3013N/A countForCV1++;
3013N/A return nameForCV1(type);
3013N/A }
3247N/A }
3013N/A
4608N/A static final Class<?>[] CLASSES = {
3013N/A String.class,
3013N/A Integer.class,
3013N/A int.class,
3013N/A boolean[].class,
3013N/A char[][].class,
3013N/A ClassValueTest.class
3013N/A };
3013N/A
3013N/A @Test
3013N/A public void testGet() {
3013N/A countForCV1 = 0;
4608N/A for (Class<?> c : CLASSES) {
3013N/A assertEquals(nameForCV1(c), CV1.get(c));
3013N/A }
3013N/A assertEquals(CLASSES.length, countForCV1);
4608N/A for (Class<?> c : CLASSES) {
3013N/A assertEquals(nameForCV1(c), CV1.get(c));
3013N/A }
3013N/A assertEquals(CLASSES.length, countForCV1);
3013N/A }
3013N/A
3013N/A @Test
3013N/A public void testRemove() {
4608N/A for (Class<?> c : CLASSES) {
3013N/A CV1.get(c);
3013N/A }
3013N/A countForCV1 = 0;
3013N/A int REMCOUNT = 3;
3013N/A for (int i = 0; i < REMCOUNT; i++) {
3013N/A CV1.remove(CLASSES[i]);
3013N/A }
3013N/A assertEquals(0, countForCV1); // no change
4608N/A for (Class<?> c : CLASSES) {
3013N/A assertEquals(nameForCV1(c), CV1.get(c));
3013N/A }
3013N/A assertEquals(REMCOUNT, countForCV1);
3013N/A }
3013N/A
3013N/A static String nameForCVN(Class<?> type, int n) {
3013N/A return "CV[" + n + "]" + type.getName();
3013N/A }
3926N/A int countForCVN;
3926N/A class CVN extends ClassValue<String> {
3013N/A final int n;
3013N/A CVN(int n) { this.n = n; }
3013N/A protected String computeValue(Class<?> type) {
3013N/A countForCVN++;
3013N/A return nameForCVN(type, n);
3013N/A }
3013N/A };
3013N/A
3013N/A @Test
3013N/A public void testGetMany() {
3013N/A int CVN_COUNT1 = 100, CVN_COUNT2 = 100;
3013N/A CVN cvns[] = new CVN[CVN_COUNT1 * CVN_COUNT2];
3013N/A for (int n = 0; n < cvns.length; n++) {
3013N/A cvns[n] = new CVN(n);
3013N/A }
3013N/A countForCVN = 0;
3013N/A for (int pass = 0; pass <= 2; pass++) {
3013N/A for (int i1 = 0; i1 < CVN_COUNT1; i1++) {
3013N/A eachClass:
4608N/A for (Class<?> c : CLASSES) {
3013N/A for (int i2 = 0; i2 < CVN_COUNT2; i2++) {
3013N/A int n = i1*CVN_COUNT2 + i2;
3013N/A assertEquals(0, countForCVN);
3013N/A assertEquals(nameForCVN(c, n), cvns[n].get(c));
3013N/A cvns[n].get(c); //get it again
3013N/A //System.out.println("getting "+n+":"+cvns[n].get(c));
3013N/A boolean doremove = (((i1 + i2) & 3) == 0);
3013N/A switch (pass) {
3013N/A case 0:
3013N/A assertEquals(1, countForCVN);
3013N/A break;
3013N/A case 1:
3013N/A // remove on middle pass
3013N/A assertEquals(0, countForCVN);
3013N/A if (doremove) {
3013N/A //System.out.println("removing "+n+":"+cvns[n].get(c));
3013N/A cvns[n].remove(c);
3013N/A assertEquals(0, countForCVN);
3013N/A }
3013N/A break;
3013N/A case 2:
3013N/A assertEquals(doremove ? 1 : 0, countForCVN);
3013N/A break;
3013N/A }
3013N/A countForCVN = 0;
3013N/A if (i1 > i2 && i1 < i2+5) continue eachClass; // leave diagonal gap
3013N/A }
3013N/A }
3013N/A }
3013N/A }
3013N/A assertEquals(countForCVN, 0);
4608N/A System.out.println("[rechecking values]");
4608N/A for (int i = 0; i < cvns.length * 10; i++) {
4608N/A int n = i % cvns.length;
4608N/A for (Class<?> c : CLASSES) {
3013N/A assertEquals(nameForCVN(c, n), cvns[n].get(c));
3013N/A }
3013N/A }
3013N/A }
3013N/A}