package org.komamitsu.fluency.buffer;

import org.junit.Test;
import org.komamitsu.fluency.StubSender;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import static org.junit.Assert.*;

public class BufferTest
{
    private static final Charset UTF8 = Charset.forName("UTF-8");

    @Test
    public void testBuffer()
            throws IOException
    {
        TestableBuffer buffer = new TestableBuffer.Config().setMaxBufferSize(10000).createInstance();
        HashMap<String, Object> data = new HashMap<String, Object>();
        data.put("name", "komamitsu");
        for (int i = 0; i < 10; i++) {
            buffer.append("foodb.bartbl" + i, 1420070400, data);
        }
        assertEquals(1000, buffer.getAllocatedSize());
        assertEquals(0.1, buffer.getBufferUsage(), 0.001);
        for (int i = 0; i < 10; i++) {
            buffer.append("foodb.bartbl" + i, 1420070400, data);
        }
        assertEquals(2000, buffer.getAllocatedSize());
        assertEquals(0.2, buffer.getBufferUsage(), 0.001);

        buffer.flush(new StubSender(), false);
        assertEquals(0, buffer.getAllocatedSize());
        assertEquals(0, buffer.getBufferUsage(), 0.001);
    }
    @Test
    public void testFileBackup()
    {
        TestableBuffer.Config config = new TestableBuffer.Config().
                setFileBackupDir(System.getProperty("java.io.tmpdir")).
                setFileBackupPrefix("FileBackupTest");

        // Just for cleaning backup files
        config.createInstance().clearBackupFiles();

        TestableBuffer buffer = config.createInstance();
        buffer.close();
        assertEquals(0, buffer.getLoadedBuffers().size());
        buffer.clearBackupFiles();

        List<String> paramOfFirstBuf = Arrays.asList("hello", "42", "world");
        ByteBuffer bufOfFirstBuf = ByteBuffer.wrap("foobar".getBytes(UTF8));
        List<String> paramOfSecondBuf = Arrays.asList("01234567");
        ByteBuffer bufOfSecondBuf = ByteBuffer.wrap(new byte[] {0x00, (byte)0xff});

        buffer = config.createInstance();
        buffer.setSavableBuffer(paramOfFirstBuf, bufOfFirstBuf);
        buffer.setSavableBuffer(paramOfSecondBuf, bufOfSecondBuf);
        buffer.close();
        assertEquals(0, buffer.getLoadedBuffers().size());

        buffer = config.createInstance();
        buffer.close();
        assertEquals(2, buffer.getLoadedBuffers().size());

        int index = 0;
        assertEquals(paramOfSecondBuf, buffer.getLoadedBuffers().get(index).getFirst());
        ByteBuffer expected = buffer.getLoadedBuffers().get(index).getSecond();
        bufOfSecondBuf.flip();
        ByteBuffer actual = bufOfSecondBuf;
        assertEquals(expected.remaining(), actual.remaining());
        for (int i = 0; i < bufOfSecondBuf.remaining(); i++) {
            assertEquals(expected.get(i), actual.get(i));
        }

        index += 1;
        assertEquals(paramOfFirstBuf, buffer.getLoadedBuffers().get(index).getFirst());
        expected = buffer.getLoadedBuffers().get(index).getSecond();
        bufOfFirstBuf.flip();
        actual = bufOfFirstBuf;
        assertEquals(expected.remaining(), actual.remaining());
        for (int i = 0; i < bufOfSecondBuf.remaining(); i++) {
            assertEquals(expected.get(i), actual.get(i));
        }
    }
}
@@ -95,16 +95,57 @@
 bSender;
+%0Aimport org.komamitsu.fluency.util.Tuple;
 %0A%0Aimport
@@ -1332,24 +1332,25 @@
 001);%0A    %7D%0A
+%0A
     @Test%0A  
@@ -2556,223 +2556,194 @@
     
-int index = 0;%0A        assertEquals(paramOfSecondBuf, buffer.getLoadedBuffers().get(index).getFirst());%0A        ByteBuffer expected = buffer.getLoadedBuffers().get(index).getSecond();%0A        bufOfSecondBuf.flip();%0A
+bufOfFirstBuf.flip();%0A        bufOfSecondBuf.flip();%0A        for (Tuple%3CList%3CString%3E, ByteBuffer%3E loadedBuffer : buffer.getLoadedBuffers()) %7B%0A            ByteBuffer expected = null;%0A    
     
@@ -2766,30 +2766,20 @@
 ctual = 
-bufOfSecondBuf
+null
 ;%0A      
@@ -2784,183 +2784,158 @@
     
+ 
-assertEquals(expected.remaining(), actual.remaining());%0A        for (int i = 0; i %3C bufOfSecondBuf.remaining(); i++) %7B%0A            assertEquals(expected.get(i), actual.get(i))
+   if (loadedBuffer.getFirst().equals(paramOfFirstBuf)) %7B%0A                expected = loadedBuffer.getSecond();%0A                actual = bufOfFirstBuf
 ;%0A  
@@ -2944,250 +2944,268 @@
     
-%7D%0A%0A
     
+%7D%0A
     
-index += 1;%0A        assertEquals(paramOfFirstBuf, buffer.getLoadedBuffers().get(index).getFirst());%0A        expected = buffer.getLoadedBuffers().get(index).getSecond();%0A        bufOfFirstBuf.flip();%0A        actual = bufOfFirstBuf;%0A
+        else if (loadedBuffer.getFirst().equals(paramOfSecondBuf)) %7B%0A                expected = loadedBuffer.getSecond();%0A                actual = bufOfSecondBuf;%0A            %7D%0A            else %7B%0A                assertTrue(false);%0A            %7D%0A%0A    
     
@@ -3264,32 +3264,36 @@
 ing());%0A        
+    
 for (int i = 0; 
@@ -3296,30 +3296,24 @@
  0; i %3C 
-bufOfSecondBuf
+expected
 .remaini
@@ -3317,32 +3317,36 @@
 ining(); i++) %7B%0A
+    
             asse
@@ -3383,24 +3383,38 @@
 al.get(i));%0A
+            %7D%0A
         %7D%0A  

Fix BufferTest#testFileBackup