package Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.sql.Connection;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import Cache.Cache;
import Cache.CacheReplacement;
import Cache.CacheItem.CacheReason;
import Cache.Simulator;

public class CacheTest {

    private static Connection conn;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        try {
            conn = TestHelper.getJDBCConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        if (conn != null) {
            conn.close();
        }
    }

    @Before
    public void setUp() throws Exception {
        TestHelper.handleSetUpOperation();
    }

    @After
    public void tearDown() throws Exception {
        TestHelper.cleanDatabase();
    }

    @Test
    public void testCacheAdd() {
        Cache cache = new Cache(5, new CacheReplacement(
                CacheReplacement.Policy.AUTHORS), "2009-10-20 01:32:19.0", "2010-01-01 01:01:01.0",1);
        cache.add(1, 1, "2009-10-20 01:32:19.0", CacheReason.NewEntity);
        assertEquals(cache.getCacheSize(), 1);
        cache.add(2, 1, "2009-10-20 01:32:19.0", CacheReason.NewEntity);
        assertEquals(cache.getCacheSize(), 2);
        cache.add(3, 1, "2009-10-20 01:32:19.0", CacheReason.NewEntity);
        assertEquals(cache.getCacheSize(), 3);
        cache.add(4, 1, "2009-10-20 01:32:19.0", CacheReason.NewEntity);
        assertEquals(cache.getCacheSize(), 4);
        cache.add(1, 2, "2009-10-20 14:37:38.0", CacheReason.ModifiedEntity);
        assertEquals(cache.getCacheSize(), 4);
        cache.add(4, 2, "2009-10-20 14:37:38.0", CacheReason.ModifiedEntity);
        assertEquals(cache.getCacheSize(), 4);
        cache.add(2, 3, "2009-10-20 14:37:47.0", CacheReason.BugEntity);
        assertEquals(cache.getCacheSize(), 4);
        cache.add(5, 3, "2009-10-20 14:37:47.0", CacheReason.BugEntity);
        assertTrue(cache.isFull());
        assertEquals(1, cache.getLoadCount(1));
    }

    @Test
    public void testGetTotalDuration()
    {
        Simulator sim = new Simulator(3, 2, 5, 1,
                CacheReplacement.Policy.LRU, null, "2009-10-20 14:37:38.0", false);
        sim.checkParameter();
        sim.initialPreLoad();
        sim.simulate();
        sim.getCsvWriter().close();
        sim.outputFileDist();
    }
}
@@ -348,32 +348,8 @@
 son;
-%0Aimport Cache.Simulator;
 %0A%0Apu
@@ -2210,350 +2210,5 @@
  %7D%0A%0A
-    @Test%0A    public void testGetTotalDuration()%0A    %7B%0A        Simulator sim = new Simulator(3, 2, 5, 1,%0A                CacheReplacement.Policy.LRU, null, %222009-10-20 14:37:38.0%22, false);%0A        sim.checkParameter();%0A        sim.initialPreLoad();%0A        sim.simulate();%0A        sim.getCsvWriter().close();%0A        sim.outputFileDist();%0A    %7D%0A
 %7D

delete testTotalDuration