0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 7654321
0N/A * @summary Tests the CacheMap class.
0N/A * @author Eamonn McManus
0N/A * @run clean CacheMapTest
0N/A * @run build CacheMapTest
0N/A * @run main CacheMapTest
0N/A */
0N/A
0N/Aimport java.util.Iterator;
0N/Aimport java.util.Map;
0N/A
0N/Aimport com.sun.jmx.remote.util.CacheMap;
0N/A
0N/Apublic class CacheMapTest {
0N/A public static void main(String[] args) {
0N/A try {
0N/A boolean ok = test(5) && test(100);
0N/A if (ok) {
0N/A System.out.println("Test completed");
0N/A return;
0N/A } else {
0N/A System.out.println("Test failed!");
0N/A System.exit(1);
0N/A }
0N/A } catch (Exception e) {
0N/A System.err.println("Unexpected exception: " + e);
0N/A e.printStackTrace();
0N/A System.exit(1);
0N/A }
0N/A }
0N/A
0N/A private static boolean test(int cacheSize) throws Exception {
0N/A System.out.println("CacheMap test with cache size " + cacheSize);
0N/A CacheMap map = new CacheMap(cacheSize);
0N/A int size = 0;
0N/A int maxIterations = cacheSize * 10;
0N/A while (map.size() == size && size < maxIterations) {
0N/A Integer key = new Integer(size);
0N/A Object x = map.put(key, "x");
0N/A if (x != null) {
0N/A System.out.println("Map already had entry " + key + "!");
0N/A return false;
0N/A }
0N/A x = map.get(key);
0N/A if (!"x".equals(x)) {
0N/A System.out.println("Got back surprising value: " + x);
0N/A return false;
0N/A }
0N/A size++;
0N/A }
0N/A System.out.println("Map size is " + map.size() + " after inserting " +
0N/A size + " elements");
0N/A do {
0N/A System.gc();
0N/A Thread.sleep(1);
0N/A System.out.println("Map size is " + map.size() + " after GC");
0N/A } while (map.size() > cacheSize);
0N/A if (map.size() < cacheSize) {
0N/A System.out.println("Map shrank to less than cache size: " +
0N/A map.size() + " (surprising but not wrong)");
0N/A } else
0N/A System.out.println("Map shrank to cache size as expected");
0N/A int lowest = size - cacheSize;
0N/A // lowest value that can still be in cache if LRU is respected
0N/A for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) {
0N/A Map.Entry entry = (Map.Entry) it.next();
0N/A Integer x = (Integer) entry.getKey();
0N/A int xx = x.intValue();
0N/A if (xx < lowest || xx >= size) {
0N/A System.out.println("Old value remained (" + x + "), " +
0N/A "expected none earlier than " + lowest);
0N/A return false;
0N/A }
0N/A Object xxx = entry.getValue();
0N/A if (!"x".equals(xxx)) {
0N/A System.out.println("Got back surprising value: " + xxx);
0N/A return false;
0N/A }
0N/A }
0N/A if (map.size() > 0)
0N/A System.out.println("Remaining elements are the most recent ones");
0N/A System.out.println("Test passed");
0N/A return true;
0N/A }
0N/A}