package net.openhft.chronicle.wire;

import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.IORuntimeException;
import net.openhft.chronicle.bytes.MappedFile;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.core.pool.ClassAliasPool;
import net.openhft.chronicle.core.values.IntValue;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

/**
 * Created by peter on 22/09/15.
 */
public class WiredFileTest {

    @Test
    public void testBuild() throws IOException {
        // use a class alias for MyHeader_1_0
//        ClassAliasPool.CLASS_ALIASES.addAlias(MyHeader_1_0.class, "my-header");
        ClassAliasPool.CLASS_ALIASES.addAlias(MyHeader_1_0.class, "MyHeader-1.0");

        String masterFile = OS.TARGET + "/wired-file-" + System.nanoTime();
        for (int i = 1; i <= 5; i++) {
            WiredFile<MyHeader_1_0> wf = WiredFile.build(masterFile,
                    file -> MappedFile.mappedFile(file, 64 << 10, 0),
                    WireType.TEXT,
                    MyHeader_1_0::new,
                    wf0 -> wf0.delegate().install(wf0)
            );
            MyHeader_1_0 header = wf.delegate();
            assertEquals(i, header.installCount.getValue());
            Bytes<?> bytes = wf.acquireWiredChunk(0).bytes();
            bytes.readLimit(88);
            System.out.println(Wires.fromSizePrefixedBlobs(bytes));
            wf.close();
        }
    }
}

class MyHeader_1_0 implements Marshallable {
    IntValue installCount;

    @Override
    public void readMarshallable(@NotNull WireIn wire) throws IORuntimeException {
        wire.read(() -> "install-count").int32(null, this, (t, i) -> t.installCount = i);
    }

    @Override
    public void writeMarshallable(@NotNull WireOut wire) {
        wire.write(() -> "install-count").int32forBinding(0, installCount = wire.newIntReference());

    }

    public void install(WiredFile<MyHeader_1_0> wiredFile) {
        installCount.addAtomicValue(1);
    }
}
@@ -565,16 +565,20 @@
 estBuild
+Text
 () throw
@@ -1515,16 +1515,1001 @@
  %7D%0A    %7D
+%0A%0A    @Test%0A    public void testBuild() throws IOException %7B%0A        // use a class alias for MyHeader_1_0%0A//        ClassAliasPool.CLASS_ALIASES.addAlias(MyHeader_1_0.class, %22my-header%22);%0A        ClassAliasPool.CLASS_ALIASES.addAlias(MyHeader_1_0.class, %22MyHeader-1.0%22);%0A%0A        String masterFile = OS.TARGET + %22/wired-file-%22 + System.nanoTime();%0A        for (int i = 1; i %3C= 5; i++) %7B%0A            WiredFile%3CMyHeader_1_0%3E wf = WiredFile.build(masterFile,%0A                    file -%3E MappedFile.mappedFile(file, 64 %3C%3C 10, 0),%0A                    WireType.BINARY,%0A                    MyHeader_1_0::new,%0A                    wf0 -%3E wf0.delegate().install(wf0)%0A            );%0A            MyHeader_1_0 header = wf.delegate();%0A            assertEquals(i, header.installCount.getValue());%0A            Bytes%3C?%3E bytes = wf.acquireWiredChunk(0).bytes();%0A            bytes.readLimit(42);%0A            System.out.println(Wires.fromSizePrefixedBlobs(bytes));%0A            wf.close();%0A        %7D%0A    %7D
 %0A%7D%0A%0Aclas

Add a test demonstrating how WiredFile can be used.