All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.objectfabric.WeakMapTest Maven / Gradle / Ivy

There is a newer version: 0.9.1
Show newest version
/**
 * This file is part of ObjectFabric (http://objectfabric.org).
 *
 * ObjectFabric is licensed under the Apache License, Version 2.0, the terms
 * of which may be found at http://www.apache.org/licenses/LICENSE-2.0.html.
 * 
 * Copyright ObjectFabric Inc.
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

package org.objectfabric;

import org.junit.Assert;
import org.junit.Test;

public class WeakMapTest extends TestsHelper {

    private static final class Value {

        final String Key;

        Value(String key) {
            Key = key;
        }
    }

    @Test
    public void run1() throws Exception {
        final List refs = new List();

        WeakCache map = new WeakCache() {

            @Override
            Value create(String key) {
                Value value = new Value(key);
                refs.add(value);
                return value;
            }

            @Override
            void onAdded(PlatformRef ref) {
            }
        };

        for (int i = 0; i < 1000; i++)
            map.getOrCreate("" + i);

        Assert.assertEquals(1000, count(map));
        System.gc();
        Thread.sleep(50);
        Assert.assertEquals(1000, count(map));

        for (int i = 0; i < 1000; i++)
            Assert.assertEquals("" + i, map.get("" + i).Key);

        Assert.assertEquals(1000, map.getInternalMapSize());
        refs.clear();
        System.gc();
        Assert.assertEquals(0, count(map));

        while (map.getInternalMapSize() > 0)
            Thread.sleep(1);
    }

    @SuppressWarnings("unused")
    private static int count(WeakCache map) {
        int count = 0;

        for (Value value : map.values())
            count++;

        return count;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy