package com.paritytrading.nassau.binaryfile;

import static com.paritytrading.nassau.binaryfile.BinaryFILEStatus.*;
import static java.util.Arrays.*;
import static org.junit.Assert.*;

import com.paritytrading.nassau.Messages;
import com.paritytrading.nassau.Strings;
import java.io.InputStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class BinaryFILEReaderTest {

    private InputStream stream;

    private Messages<String> messages;

    @Before
    public void setUp() throws Exception {
        stream = getClass().getResourceAsStream("/binaryfile.dat");

        messages = new Messages<>(Strings.MESSAGE_PARSER);
    }

    @After
    public void tearDown() throws Exception {
        stream.close();
    }

    @Test
    public void readStream() throws Exception {
        BinaryFILEReader reader = new BinaryFILEReader(stream, messages);

        while (reader.read() >= 0);

        assertEquals(asList("foo", "bar", "baz", "quux", ""), messages.collect());
    }

    @Test
    public void readStreamWithStatusListener() throws Exception {
        BinaryFILEStatus status = new BinaryFILEStatus();

        BinaryFILEStatusParser parser = new BinaryFILEStatusParser(messages, status);

        BinaryFILEReader reader = new BinaryFILEReader(stream, parser);

        while (reader.read() >= 0);

        assertEquals(asList("foo", "bar", "baz", "quux"), messages.collect());
        assertEquals(asList(new EndOfSession()), status.collect());
    }

}

@@ -808,32 +808,37 @@
 ption %7B%0A        
+try (
 BinaryFILEReader
@@ -885,19 +885,24 @@
 essages)
-;%0A%0A
+) %7B%0A    
         
@@ -920,32 +920,42 @@
 er.read() %3E= 0);
+%0A        %7D
 %0A%0A        assert
@@ -1252,32 +1252,37 @@
 atus);%0A%0A        
+try (
 BinaryFILEReader
@@ -1327,19 +1327,24 @@
  parser)
-;%0A%0A
+) %7B%0A    
         
@@ -1370,16 +1370,26 @@
 ) %3E= 0);
+%0A        %7D
 %0A%0A      

Use try-with-resources to close as application code should.