/** 
 * 
 * Copyright 2004 Hiram Chirino
 * 
 * Licensed 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. 
 * 
 **/
package org.codehaus.activemq.journal.impl;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;

import junit.framework.TestCase;

/**
 * Tests the LogFile used by JournalImpl 
 * 
 * @version $Revision: 1.1 $
 */
public class SegmentTest extends TestCase {

    int size = 1024*512;
    File file = new File("test-segment.log");
    RecordHeader header = new RecordHeader();
    Segment segment;
    byte data[] = new byte[]{'H','e','l','l','o',' ','W','o','r','l','d'};
    
    /**
     * @see junit.framework.TestCase#setUp()
     */
    protected void setUp() throws Exception {
        if( file.exists() )
            file.delete();
        assertTrue( !file.exists() );
        
        segment = new Segment(file, size, (byte)0);
        
        header.length=data.length;
        header.recordType=LogFile.DATA_RECORD_TYPE;
        header.sequenceId=5;
    }

    protected void tearDown() throws Exception {
        segment.close();
        if( file.exists() )
            file.delete();
        assertTrue( !file.exists() );
    }
    
    public void testSegmentCreation() throws IOException {
        assertTrue( file.exists() );
        assertTrue( file.length() ==  size );
        assertFalse( segment.isActive() );
        assertTrue( segment.isReadOnly() );
    }

    public void testWrite() throws IOException {

        // Seek to the append offset
        segment.activate(1);
        int pos = segment.getAppendOffset();
        segment.seek( pos );        
        assertTrue( segment.isAtAppendOffset() );
        
        BatchedWrite write = new BatchedWrite(ByteBuffer.allocate(1024));
        Record batchedRecord = new Record(header, data);
        write.append(batchedRecord);
        batchedRecord = new Record(header, data);
        write.append(batchedRecord);
        
        // Write 2 records.
        segment.write(write);
        segment.force();
        
        // Check the things that change after a write. 
        assertTrue( pos!=segment.getAppendOffset() );
        assertEquals( header.sequenceId, segment.getLastSequenceId());

        segment.setReadOnly(true);
        assertTrue( segment.isReadOnly() );
        
        // read the first record header.
        segment.seek(pos);
        RecordHeader header2 = new RecordHeader();
        segment.readRecordHeader(header2);        
        assertTrue( header2.isValid() );
        assertEquals( header.length, header2.length);
        assertEquals( header.recordType, header2.recordType);
        assertEquals( header.sequenceId, header2.sequenceId);        
    }
}
