package org.apache.cassandra.io.util;
/*
 * 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * 
 */


import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;

import org.junit.Test;

public class BufferedRandomAccessFileTest
{

    @Test
    public void testLength() throws IOException
    {
        File tmpFile = File.createTempFile("lengthtest", "bin");
        BufferedRandomAccessFile rw = new BufferedRandomAccessFile(tmpFile, "rw");
        assertEquals(0, rw.length());

        // write a chunk smaller then our buffer, so will not be flushed
        // to disk
        byte[] lessThenBuffer = new byte[BufferedRandomAccessFile.BuffSz_ / 2];
        rw.write(lessThenBuffer);
        assertEquals(lessThenBuffer.length, rw.length());

        // sync the data and check length
        rw.sync();
        assertEquals(lessThenBuffer.length, rw.length());

        // write more then the buffer can hold and check length
        byte[] biggerThenBuffer = new byte[BufferedRandomAccessFile.BuffSz_ * 2];
        rw.write(biggerThenBuffer);
        assertEquals(biggerThenBuffer.length + lessThenBuffer.length, rw.length());

        // checking that reading doesn't interfere
        rw.seek(0);
        rw.read();
        assertEquals(biggerThenBuffer.length + lessThenBuffer.length, rw.length());

        rw.close();

        // will use cachedlength
        BufferedRandomAccessFile r = new BufferedRandomAccessFile(tmpFile, "r");
        assertEquals(lessThenBuffer.length + biggerThenBuffer.length, r.length());
        r.close();
    }

}

@@ -862,35 +862,50 @@
 ort 
-static org.junit.Assert.*;%0A
+java.io.EOFException;%0Aimport java.io.File;
 %0Aimp
@@ -912,32 +912,44 @@
 ort java.io.File
+OutputStream
 ;%0Aimport java.io
@@ -966,30 +966,101 @@
 on;%0A
-%0Aimport org.junit.Test
+import java.util.Arrays;%0A%0Aimport org.junit.Test;%0A%0Aimport static org.junit.Assert.assertEquals
 ;%0A%0Ap
@@ -2425,10 +2425,1321 @@
 %0A    %7D%0A%0A
+    protected void expectException(int size, int offset, int len, BufferedRandomAccessFile braf)%0A    %7B%0A        boolean threw = false;%0A        try%0A        %7B%0A            braf.readFully(new byte%5Bsize%5D, offset, len);%0A        %7D%0A        catch(Throwable t)%0A        %7B%0A            assert t.getClass().equals(EOFException.class) : t.getClass().getName() + %22 is not %22 + EOFException.class.getName();%0A            threw = true;%0A        %7D%0A        assert threw : EOFException.class.getName() + %22 not received%22;%0A    %7D%0A%0A    @Test%0A    public void testEOF() throws Exception%0A    %7B%0A        for (String mode : Arrays.asList(%22r%22, %22rw%22)) // read, read+write%0A        %7B%0A            for (int buf : Arrays.asList(8, 16, 32, 0))  // smaller, equal, bigger, zero%0A            %7B%0A                for (int off : Arrays.asList(0, 8))%0A                %7B%0A                    expectException(32, off, 17, new BufferedRandomAccessFile(writeTemporaryFile(new byte%5B16%5D), mode, buf));%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    protected File writeTemporaryFile(byte%5B%5D data) throws Exception%0A    %7B%0A        File f = File.createTempFile(%22BRAFTestFile%22, null);%0A        f.deleteOnExit();%0A        FileOutputStream fout = new FileOutputStream(f);%0A        fout.write(data);%0A        fout.getFD().sync();%0A        fout.close();%0A        return f;%0A    %7D%0A%0A
 %7D%0A

add test for BRAF EOFException. patch by mdennis; reviewed by jbellis for CASSANDRA-1200