package org.gnode.nix;

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

import java.util.ArrayList;
import java.util.Date;

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

public class TestFile {

    private File file_open;
    private File file_other;
    private File file_null;

    private Date statup_time;

    @Before
    public void setUp() {
        statup_time = new Date(System.currentTimeMillis() / 1000);
        file_open = File.open("test_file.h5", FileMode.Overwrite);
        file_other = File.open("test_file_other.h5", FileMode.Overwrite);
        file_null = null;
    }

    @After
    public void tearDown() {
        file_open.close();
        file_other.close();
    }

    @Test
    public void testFormat() {
        assertEquals(file_open.getFormat(), "nix");
    }

    @Test
    public void testLocation() {
        assertEquals(file_open.getLocation(), "test_file.h5");
        assertEquals(file_other.getLocation(), "test_file_other.h5");
    }

    @Test
    public void testVersion() {
        ArrayList<Integer> version = file_open.getVersion();
        assertEquals(version.get(0), Integer.valueOf(1));
        assertEquals(version.get(1), Integer.valueOf(0));
        assertEquals(version.get(2), Integer.valueOf(0));
    }

    @Test
    public void testCreatedAt() {
        assertTrue(file_open.getCreatedAt().compareTo(statup_time) >= 0);
        Date past_time = new Date(System.currentTimeMillis() / 1000 - 10000000);
        file_open.forceCreatedAt(past_time);
        assertTrue(file_open.getCreatedAt().compareTo(statup_time) < 0);
    }

    @Test
    public void testUpdatedAt() {
        assertTrue(file_open.getUpdatedAt().compareTo(statup_time) >= 0);
    }
}

@@ -139,22 +139,23 @@
 til.
-Date;%0A
+Arrays;
 %0Aimport 
 stat
@@ -154,45 +154,47 @@
 ort 
-static org.junit.Assert.assertEquals;
+java.util.Date;%0Aimport java.util.List;%0A
 %0Aimp
@@ -221,26 +221,17 @@
 .Assert.
-assertTrue
+*
 ;%0A%0Apubli
@@ -1762,11 +1762,1943 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testBlockAccess() %7B%0A        List%3CString%3E names = Arrays.asList(%22block_a%22, %22block_b%22, %22block_c%22, %22block_d%22, %22block_e%22);%0A        Block b = null;%0A        assertEquals(file_open.getBlockCount(), 0);%0A        assertEquals(file_open.getBlocks().size(), 0);%0A        assertFalse(file_open.hasBlock(%22invalid_id%22));%0A        try %7B%0A            file_open.hasBlock(b);%0A            fail();%0A        %7D catch (NullPointerException npe) %7B%0A        %7D%0A%0A        ArrayList%3CString%3E ids = new ArrayList%3CString%3E();%0A        for (String name : names) %7B%0A            Block bl = file_open.createBlock(name, %22dataset%22);%0A            assertEquals(bl.getName(), name);%0A            assertTrue(file_open.hasBlock(bl));%0A            assertTrue(file_open.hasBlock(name));%0A%0A            ids.add(bl.getId());%0A        %7D%0A%0A        try %7B%0A            file_open.createBlock(names.get(0), %22dataset%22);%0A            fail();%0A        %7D catch (RuntimeException re) %7B%0A        %7D%0A%0A        assertEquals(file_open.getBlockCount(), names.size());%0A        assertEquals(file_open.getBlocks().size(), names.size());%0A%0A        for (String name : names) %7B%0A            Block bl_name = file_open.getBlock(name);%0A            assertNotNull(bl_name);%0A%0A            Block bl_id = file_open.getBlock(bl_name.getId());%0A            assertNotNull(bl_id);%0A            assertEquals(bl_name.getName(), bl_id.getName());%0A        %7D%0A%0A        for (String id : ids) %7B%0A            Block bl = file_open.getBlock(id);%0A            assertTrue(file_open.hasBlock(id));%0A            assertEquals(bl.getId(), id);%0A%0A            file_open.deleteBlock(id);%0A        %7D%0A%0A        try %7B%0A            file_open.deleteBlock(b);%0A            fail();%0A        %7D catch (NullPointerException npe) %7B%0A        %7D%0A%0A        b = file_open.createBlock(%22test%22, %22test%22);%0A        assertTrue(file_open.deleteBlock(b));%0A        assertEquals(file_open.getBlockCount(), 0);%0A        assertEquals(file_open.getBlocks().size(), 0);%0A    %7D
 %0A%7D%0A

add tests for block funcs in File