package net.openhft.chronicle.map;

import net.openhft.lang.model.DataValueClasses;
import net.openhft.lang.model.constraints.MaxSize;
import net.openhft.lang.values.IntValue;
import org.junit.Ignore;
import org.junit.Test;

import java.io.File;
import java.util.Map;

import static org.junit.Assert.*;

public class DemoChronicleMapTest {

    @Test
    @Ignore
    public void testMap() throws Exception {
        File file = File.createTempFile("DummyOrders", ".test");
        file.deleteOnExit();
        int maxEntries = 1000;
        ChronicleMap<IntValue, DemoOrderVOInterface> map = ChronicleMapBuilder
                .of(IntValue.class, DemoOrderVOInterface.class)
                .putReturnsNull(true)
                .removeReturnsNull(true)
                .entries(maxEntries)
                .createPersistedTo(file);


        IntValue key = DataValueClasses.newInstance(IntValue.class);

        DemoOrderVOInterface value = DataValueClasses.newDirectReference(DemoOrderVOInterface.class);
        DemoOrderVOInterface value2 = DataValueClasses.newDirectReference(DemoOrderVOInterface.class);

        // Initially populate the map
        for (int i = 0; i < maxEntries; i++) {
            key.setValue(i);

            map.acquireUsing(key, value);

            // TODO Shouldn't throw an NPE, because acquire should set the value.
            value.setSymbol("IBM-" + i);
            value.addAtomicOrderQty(1000);

            map.getUsing(key, value2);
            assertEquals("IBM-" + i, value.getSymbol());
            assertEquals(1000, value.getOrderQty(), 0.0);
        }

        for (Map.Entry<IntValue, DemoOrderVOInterface> entry : map.entrySet()) {

            IntValue k = entry.getKey();
            DemoOrderVOInterface v = entry.getValue();

            System.out.println(String.format("Key %d %s", k.getValue(), v == null ? "<null>" : v.getSymbol()));
            assertNotNull(v);
        }
    }

    @Test
    @Ignore
    public void testMapLocked() throws Exception {
        File file = File.createTempFile("DummyOrders", ".test");
        file.deleteOnExit();
        int maxEntries = 1000;
        ChronicleMap<IntValue, DemoOrderVOInterface> map = ChronicleMapBuilder
                .of(IntValue.class, DemoOrderVOInterface.class)
                .putReturnsNull(true)
                .removeReturnsNull(true)
                .entries(maxEntries)
                .createPersistedTo(file);


        IntValue key = DataValueClasses.newInstance(IntValue.class);

        DemoOrderVOInterface value = DataValueClasses.newDirectReference(DemoOrderVOInterface.class);
        DemoOrderVOInterface value2 = DataValueClasses.newDirectReference(DemoOrderVOInterface.class);

        // Initially populate the map
        for (int i = 0; i < maxEntries; i++) {
            key.setValue(i);

            try (WriteContext wc = map.acquireUsingLocked(key, value)) {
                // TODO Shouldn't throw an NPE, because acquire should set the value.
                value.setSymbol("IBM-" + i);
                value.addAtomicOrderQty(1000);
            }

            try (ReadContext rc = map.getUsingLocked(key, value2)) {
                assertTrue(rc.present());
                assertEquals("IBM-" + i, value.getSymbol());
                assertEquals(1000, value.getOrderQty(), 0.0);
            }
        }

        for (Map.Entry<IntValue, DemoOrderVOInterface> entry : map.entrySet()) {

            IntValue k = entry.getKey();
            DemoOrderVOInterface v = entry.getValue();

            System.out.println(String.format("Key %d %s", k.getValue(), v == null ? "<null>" : v.getSymbol()));
            assertNotNull(v);
        }
    }
}

interface DemoOrderVOInterface {
    public CharSequence getSymbol();
//    public StringBuilder getUsingSymbol(StringBuilder sb);

    public void setSymbol(@MaxSize(20) CharSequence symbol);

    public double addAtomicOrderQty(double toAdd);

    public double getOrderQty();

    public void setOrderQty(double orderQty);

}

@@ -173,33 +173,8 @@
 ue;%0A
-import org.junit.Ignore;%0A
 impo
@@ -192,16 +192,16 @@
 t.Test;%0A
+
 %0Aimport 
@@ -315,36 +315,24 @@
 %0A%0A    @Test%0A
-    @Ignore%0A
     public v
@@ -481,32 +481,34 @@
 Entries = 1000;%0A
+//
         Chronicl
@@ -562,32 +562,127 @@
 nicleMapBuilder%0A
+        ChronicleMap%3CIntValue, DemoOrderVOInterface%3E map = OffHeapUpdatableChronicleMapBuilder%0A
                 
@@ -924,32 +924,38 @@
 ValueClasses.new
+Direct
 Instance(IntValu
@@ -2013,20 +2013,8 @@
 est%0A
-    @Ignore%0A
     
@@ -2185,16 +2185,18 @@
 = 1000;%0A
+//
         
@@ -2258,32 +2258,127 @@
 nicleMapBuilder%0A
+        ChronicleMap%3CIntValue, DemoOrderVOInterface%3E map = OffHeapUpdatableChronicleMapBuilder%0A
                 
@@ -2577,32 +2577,32 @@
 stedTo(file);%0A%0A%0A
-
         IntValue
@@ -2628,16 +2628,22 @@
 sses.new
+Direct
 Instance

Make the tests runnable.