package org.caffinitas.ohc;

import java.io.DataInput;
import java.io.DataOutput;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import org.caffinitas.ohc.linked.OHCacheImpl;
import org.testng.Assert;
import org.testng.annotations.Test;

public class OHCacheBuilderTest
{

    @Test
    public void testHashTableSize() throws Exception
    {
        OHCacheBuilder<String, String> builder = OHCacheBuilder.newBuilder();
        Assert.assertEquals(builder.getHashTableSize(), 8192);
        builder.hashTableSize(12345);
        Assert.assertEquals(builder.getHashTableSize(), 12345);

        System.setProperty("org.caffinitas.ohc.hashTableSize", "98765");
        builder = OHCacheBuilder.newBuilder();
        Assert.assertEquals(builder.getHashTableSize(), 98765);
    }

    @Test
    public void testBucketLength() throws Exception
    {
        OHCacheBuilder<String, String> builder = OHCacheBuilder.newBuilder();
        Assert.assertEquals(builder.getBucketLength(), 8L);
        builder.bucketLength(12345);
        Assert.assertEquals(builder.getBucketLength(), 12345);

        System.setProperty("org.caffinitas.ohc.bucketLength", "98765");
        builder = OHCacheBuilder.newBuilder();
        Assert.assertEquals(builder.getBucketLength(), 98765);
    }

    @Test
    public void testCapacity() throws Exception
    {
        int cpus = Runtime.getRuntime().availableProcessors();

        OHCacheBuilder<String, String> builder = OHCacheBuilder.newBuilder();
        Assert.assertEquals(builder.getCapacity(), Math.min(cpus * 16, 64) * 1024 * 1024);
        builder.capacity(12345);
        Assert.assertEquals(builder.getCapacity(), 12345);

        System.setProperty("org.caffinitas.ohc.capacity", "98765");
        builder = OHCacheBuilder.newBuilder();
        Assert.assertEquals(builder.getCapacity(), 98765);
    }

    @Test
    public void testSegmentCount() throws Exception
    {
        int cpus = Runtime.getRuntime().availableProcessors();
        int segments = cpus * 2;
        while (Integer.bitCount(segments) != 1)
            segments++;

        OHCacheBuilder<String, String> builder = OHCacheBuilder.newBuilder();
        Assert.assertEquals(builder.getSegmentCount(), segments);
        builder.segmentCount(12345);
        Assert.assertEquals(builder.getSegmentCount(), 12345);

        System.setProperty("org.caffinitas.ohc.segmentCount", "98765");
        builder = OHCacheBuilder.newBuilder();
        Assert.assertEquals(builder.getSegmentCount(), 98765);
    }

    @Test
    public void testLoadFactor() throws Exception
    {
        OHCacheBuilder<String, String> builder = OHCacheBuilder.newBuilder();
        Assert.assertEquals(builder.getLoadFactor(), .75f);
        builder.loadFactor(12345);
        Assert.assertEquals(builder.getLoadFactor(), 12345.0f);

        System.setProperty("org.caffinitas.ohc.loadFactor", "98765");
        builder = OHCacheBuilder.newBuilder();
        Assert.assertEquals(builder.getLoadFactor(), 98765.0f);
    }

    @Test
    public void testMaxEntrySize() throws Exception
    {
        OHCacheBuilder<String, String> builder = OHCacheBuilder.newBuilder();
        Assert.assertEquals(builder.getMaxEntrySize(), 0L);
        builder.maxEntrySize(12345);
        Assert.assertEquals(builder.getMaxEntrySize(), 12345);

        System.setProperty("org.caffinitas.ohc.maxEntrySize", "98765");
        builder = OHCacheBuilder.newBuilder();
        Assert.assertEquals(builder.getMaxEntrySize(), 98765);
    }

    @Test
    public void testThrowOOME() throws Exception
    {
        OHCacheBuilder<String, String> builder = OHCacheBuilder.newBuilder();
        Assert.assertFalse(builder.isThrowOOME());
        builder.throwOOME(true);
        Assert.assertTrue(builder.isThrowOOME());

        System.setProperty("org.caffinitas.ohc.throwOOME", "true");
        builder = OHCacheBuilder.newBuilder();
        Assert.assertTrue(builder.isThrowOOME());
    }

    @Test
    public void testExecutorService() throws Exception
    {
        OHCacheBuilder<String, String> builder = OHCacheBuilder.newBuilder();
        Assert.assertNull(builder.getExecutorService());

        ScheduledExecutorService es = Executors.newScheduledThreadPool(1);
        builder.executorService(es);
        es.shutdown();
        Assert.assertSame(builder.getExecutorService(), es);
    }

    @Test
    public void testType() throws Exception
    {
        OHCacheBuilder<String, String> builder = OHCacheBuilder.newBuilder();
        Assert.assertSame(builder.getType(), OHCacheImpl.class);

        builder.type(org.caffinitas.ohc.tables.OHCacheImpl.class);
        Assert.assertSame(builder.getType(), org.caffinitas.ohc.tables.OHCacheImpl.class);
    }

    @Test
    public void testKeySerializer() throws Exception
    {
        OHCacheBuilder<String, String> builder = OHCacheBuilder.newBuilder();
        Assert.assertNull(builder.getKeySerializer());

        CacheSerializer<String> inst = new CacheSerializer<String>()
        {
            public void serialize(String s, DataOutput out)
            {

            }

            public String deserialize(DataInput in)
            {
                return null;
            }

            public int serializedSize(String s)
            {
                return 0;
            }
        };
        builder.keySerializer(inst);
        Assert.assertSame(builder.getKeySerializer(), inst);
    }

    @Test
    public void testValueSerializer() throws Exception
    {
        OHCacheBuilder<String, String> builder = OHCacheBuilder.newBuilder();
        Assert.assertNull(builder.getValueSerializer());

        CacheSerializer<String> inst = new CacheSerializer<String>()
        {
            public void serialize(String s, DataOutput out)
            {

            }

            public String deserialize(DataInput in)
            {
                return null;
            }

            public int serializedSize(String s)
            {
                return 0;
            }
        };
        builder.valueSerializer(inst);
        Assert.assertSame(builder.getValueSerializer(), inst);
    }
}
@@ -71,24 +71,50 @@
 DataOutput;%0A
+import java.util.HashSet;%0A
 import java.
@@ -140,16 +140,16 @@
 cutors;%0A
-
 import j
@@ -267,16 +267,59 @@
 Assert;%0A
+import org.testng.annotations.AfterMethod;%0A
 import o
@@ -379,17 +379,313 @@
 derTest%0A
-
 %7B
+%0A    @AfterMethod%0A    public void clearProperties()%0A    %7B%0A        for (Object k : new HashSet(System.getProperties().keySet()))%0A        %7B%0A            String key = (String)k;%0A            if (key.startsWith(%22org.caffinitas.ohc.%22))%0A                System.getProperties().remove(key);%0A        %7D%0A    %7D
 %0A%0A    @T

Fix unit test failure due to new unit test