SegmentTest xref

    1   /***
    2    *
    3    * Copyright 2004 Hiram Chirino
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.journal.impl;
    19
    20  import java.io.File;
    21  import java.io.IOException;
    22  import java.nio.ByteBuffer;
    23
    24  import junit.framework.TestCase;
    25
    26  /***
    27   * Tests the LogFile used by JournalImpl
    28   *
    29   * @version $Revision: 1.1 $
    30   */
    31  public class SegmentTest extends TestCase {
    32
    33      int size = 1024*512;
    34      File file = new File("test-segment.log");
    35      RecordHeader header = new RecordHeader();
    36      Segment segment;
    37      byte data[] = new byte[]{'H','e','l','l','o',' ','W','o','r','l','d'};
    38
    39      /***
    40       * @see junit.framework.TestCase#setUp()
    41       */
    42      protected void setUp() throws Exception {
    43          if( file.exists() )
    44              file.delete();
    45          assertTrue( !file.exists() );
    46
    47          segment = new Segment(file, size, (byte)0);
    48
    49          header.length=data.length;
    50          header.recordType=LogFile.DATA_RECORD_TYPE;
    51          header.sequenceId=5;
    52      }
    53
    54      protected void tearDown() throws Exception {
    55          segment.close();
    56          if( file.exists() )
    57              file.delete();
    58          assertTrue( !file.exists() );
    59      }
    60
    61      public void testSegmentCreation() throws IOException {
    62          assertTrue( file.exists() );
    63          assertTrue( file.length() ==  size );
    64          assertFalse( segment.isActive() );
    65          assertTrue( segment.isReadOnly() );
    66      }
    67
    68      public void testWrite() throws IOException {
    69
    70          // Seek to the append offset
    71          segment.activate(1);
    72          int pos = segment.getAppendOffset();
    73          segment.seek( pos );
    74          assertTrue( segment.isAtAppendOffset() );
    75
    76          BatchedWrite write = new BatchedWrite(ByteBuffer.allocate(1024));
    77          Record batchedRecord = new Record(header, data);
    78          write.append(batchedRecord);
    79          batchedRecord = new Record(header, data);
    80          write.append(batchedRecord);
    81
    82          // Write 2 records.
    83          segment.write(write);
    84          segment.force();
    85
    86          // Check the things that change after a write.
    87          assertTrue( pos!=segment.getAppendOffset() );
    88          assertEquals( header.sequenceId, segment.getLastSequenceId());
    89
    90          segment.setReadOnly(true);
    91          assertTrue( segment.isReadOnly() );
    92
    93          // read the first record header.
    94          segment.seek(pos);
    95          RecordHeader header2 = new RecordHeader();
    96          segment.readRecordHeader(header2);
    97          assertTrue( header2.isValid() );
    98          assertEquals( header.length, header2.length);
    99          assertEquals( header.recordType, header2.recordType);
    100         assertEquals( header.sequenceId, header2.sequenceId);
    101     }
    102 }