package co.kuntz.sqliteEngine.tests.core;

import co.kuntz.sqliteEngine.core.DataMapper;
import co.kuntz.sqliteEngine.core.LocalDataMapper;

import static org.junit.Assert.fail;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.Before;

import java.security.SecureRandom;
import java.math.BigInteger;
import java.util.Arrays;

public class LocalDataMapperTest {
    @Before
    public void setup() {
        LocalDataMapper.dbName = ":memory:";

        LocalDataMapper.setDataMapperFactory();
    }

    @Test
    public void testPrimitives() {
        DataMapper instance = DataMapper.getInstance();

        // TODO: don't hard code these. Turn them into variables.
        instance.put("one", 1);
        instance.put("twotwo", 2.2);
        instance.put("true", true);
        instance.put("string", "Hi There!");
        instance.put("char", 'c');

        int one = (int) instance.get("one", int.class);
        double twotwo = (double) instance.get("twotwo", double.class);
        boolean tru = (boolean) instance.get("true", boolean.class);
        String hiThere = (String) instance.get("string", String.class);
        char care = (char) instance.get("char", char.class);

        assertEquals(1, one);
        assertEquals(2.2, twotwo, 0);
        assertEquals(true, tru);
        assertEquals("Hi There!", hiThere);
        assertEquals('c', care);
    }

    @Test
    public void testArrays() {
        DataMapper instance = DataMapper.getInstance();

        instance.put("ints", new int[] {1, 2, 3});
        instance.put("doubles", new double[] {1.1, 2.2, 3.3});
        instance.put("booleans", new boolean[] {true, false, true});
        instance.put("strings", new String[] {"first", "second", "third"});
        instance.put("chars", new char[] {'a', 'b', 'c'});

        int[] ints = (int[]) instance.get("ints", int[].class);
        double[] doubles = (double[]) instance.get("doubles", double[].class);
        boolean[] bools = (boolean[]) instance.get("booleans", boolean[].class);
        String[] strings = (String[]) instance.get("strings", String[].class);
        char[] chars = (char[]) instance.get("chars", char[].class);

        assertArrayEquals(new int[] {1, 2, 3}, ints);
        assertTrue(Arrays.equals(new double[] {1.1, 2.2, 3.3}, doubles));
        assertArrayEquals(new boolean[] {true, false, true}, bools);
        assertArrayEquals(new String[] {"first", "second", "third"}, strings);
        assertArrayEquals(new char[] {'a', 'b', 'c'}, chars);
    }
}

@@ -374,83 +374,14 @@
 ava.
-security.SecureRandom;%0Aimport java.math.BigInteger;%0Aimport java.util.Arrays
+util.*
 ;%0A%0Ap
@@ -2555,11 +2555,1192 @@
 );%0A    %7D
+%0A%0A    class BundleOfJunk %7B%0A        public int integer;%0A        public String string;%0A        public List%3CString%3E strings;%0A        public Map%3CInteger, String%3E map;%0A%0A        @Override public boolean equals(Object o) %7B%0A            if (o == null %7C%7C !(o instanceof BundleOfJunk)) %7B%0A                return false;%0A            %7D%0A%0A            BundleOfJunk that = (BundleOfJunk) o;%0A%0A            return that.integer == this.integer &&%0A                that.string.equals(this.string) &&%0A                that.strings.equals(this.strings) &&%0A                that.map.equals(this.map);%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void testArbirtraryClass() %7B%0A        BundleOfJunk a = new BundleOfJunk();%0A        a.integer = 4;%0A        a.string = %22wtf?%22;%0A        a.strings = new ArrayList%3CString%3E();%0A        a.strings.add(%22a%22);%0A        a.strings.add(%22b%22);%0A        a.strings.add(%22c%22);%0A        a.map = new HashMap%3CInteger, String%3E();%0A        a.map.put(1, %22Hello World%22);%0A        a.map.put(2, %22Goodbye World%22);%0A%0A        DataMapper instance = DataMapper.getInstance();%0A        instance.put(%22junk%22, a);%0A%0A        // FUCK YOU JUNIT!%0A        assertTrue(a.equals(instance.get(%22junk%22, BundleOfJunk.class)));%0A    %7D
 %0A%7D%0A

Test arbitrary class insertion / retrieval.