/**
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mybatis.caches.ignite;

import java.io.File;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

/**
 * Ignite Test Case.
 *
 * @version $Id$
 */
public final class IgniteCacheAdapterTestCase {
    private static final String DEFAULT_ID = "Ignite";

    private static IgniteCacheAdapter cache;

    @BeforeClass
    public static void newCache() {
        cache = new IgniteCacheAdapter(DEFAULT_ID);
    }

    @Test
    public void shouldDemonstrateCopiesAreKeptAndEqual() {
        for (int i = 0; i < 1000; i++) {
            cache.putObject(i, i);
            assertEquals(i, cache.getObject(i));
        }
        assertEquals(1000, cache.getSize());
    }

    @Test
    public void shouldRemoveItemOnDemand() {
        cache.putObject(0, 0);
        assertNotNull(cache.getObject(0));
        cache.removeObject(0);
        assertNull(cache.getObject(0));
    }

    @Test
    public void shouldFlushAllItemsOnDemand() {
        for (int i = 0; i < 5; i++) {
            cache.putObject(i, i);
        }
        assertNotNull(cache.getObject(0));
        assertNotNull(cache.getObject(4));
        cache.clear();
        assertNull(cache.getObject(0));
        assertNull(cache.getObject(4));
    }

    @Test(expected = IllegalArgumentException.class)
    public void shouldNotCreateCache() {
        cache = new IgniteCacheAdapter(null);
    }

    @Test
    public void shouldCreateDefaultCache() throws Exception {
        String cfgPath = "config/default-config.xml";

        File cfgFile = new File(cfgPath);
        File cfgBkpFile = new File(cfgPath + ".bkp");

        if (cfgFile.renameTo(cfgBkpFile)) {
            try {
                new IgniteCacheAdapter(DEFAULT_ID);
            }
            finally {
                cfgBkpFile.renameTo(cfgFile);
            }
        }
        else
            throw new Exception("Failed to rename config file!");
    }

    @Test
    public void shouldVerifyCacheId() {
        assertEquals(DEFAULT_ID, cache.getId());
    }
}

@@ -1050,16 +1050,61 @@
 cache;%0A%0A
+    private static int TEST_OBJ_NUM = 1000;%0A%0A
     @Bef
@@ -1304,20 +1304,28 @@
  0; i %3C 
-1000
+TEST_OBJ_NUM
 ; i++) %7B
@@ -1440,20 +1440,28 @@
 tEquals(
-1000
+TEST_OBJ_NUM
 , cache.
@@ -1776,9 +1776,20 @@
 i %3C 
-5
+TEST_OBJ_NUM
 ; i+
@@ -1850,43 +1850,53 @@
     
-assertNotNull(cache.getObject(0));%0A
+for (int i = 0; i %3C TEST_OBJ_NUM; i++) %7B%0A    
     
@@ -1925,29 +1925,39 @@
 e.getObject(
-4
+i
 ));%0A
+        %7D%0A
         cach
@@ -1979,40 +1979,53 @@
     
-assertNull(cache.getObject(0));%0A
+for (int i = 0; i %3C TEST_OBJ_NUM; i++) %7B%0A    
     
@@ -2059,13 +2059,23 @@
 ect(
-4
+i
 ));%0A
+        %7D%0A
     

Check all objects after flushing.