    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.ByteArrayInputStream;
    21  import java.io.ByteArrayOutputStream;
    22  import java.io.DataInputStream;
    23  import java.io.DataOutputStream;
    24  import java.io.IOException;
    25  import java.nio.ByteBuffer;
    26  import java.util.ArrayList;
    27  import java.util.Collections;
    28
    29  import junit.framework.TestCase;
    30
    31  /***
    32   * Tests the data structures used JournalImpl
    33   *
    34   * @version $Revision: 1.2 $
    35   */
    36  public class DataStruturesTest extends TestCase {
    37
    38      public void testMarkSerialization() throws IOException {
    39          doTestSerialization(Integer.MIN_VALUE,Long.MIN_VALUE);
    40          doTestSerialization(Integer.MAX_VALUE,Long.MAX_VALUE);
    41          doTestSerialization(0,0);
    42          doTestSerialization(-1,1);
    43      }
    44
    45      public void doTestSerialization(int value1, long value2) throws IOException {
    46          Mark mark = new Mark();
    47          mark.offsetId = value1;
    48          mark.sequenceId = value2;
    49
    50          byte[] data = mark.writeExternal();
    51          assertNotNull(data);
    52
    53          Mark mark2 = new Mark();
    54          mark2.readExternal(data);
    55          assertEquals(mark.offsetId, mark2.offsetId);
    56          assertEquals(mark.sequenceId, mark2.sequenceId);
    57
    58          assertEquals(data.length, Mark.MARK_RECORD_SIZE);
    59      }
    60
    61      public void testRecordHeaderSerialization() throws IOException {
    62          doTestRecordHeader(Integer.MIN_VALUE,Long.MIN_VALUE,Byte.MIN_VALUE);
    63          doTestRecordHeader(Integer.MAX_VALUE,Long.MAX_VALUE,Byte.MAX_VALUE);
    64      }
    65
    66      public void doTestRecordHeader(int value1, long value2, byte value3) throws IOException {
    67          RecordHeader header = new RecordHeader();
    68          header.length = value1;
    69          header.sequenceId = value2;
    70          header.recordType = value3;
    71
    72          ByteArrayOutputStream os = new ByteArrayOutputStream();
    73          DataOutputStream dos = new DataOutputStream(os);
    74          header.writeRecordHeader(dos);
    75          byte[] data = os.toByteArray();
    76          assertNotNull(data);
    77
    78          RecordHeader header2 = new RecordHeader();
    79          DataInputStream is = new DataInputStream(new ByteArrayInputStream(data));
    80          header2.readRecordHeader(is);
    81
    82          assertEquals(header.length, header2.length);
    83          assertEquals(header.sequenceId, header2.sequenceId);
    84          assertEquals(header.recordType, header2.recordType);
    85          assertEquals(data.length, RecordHeader.RECORD_HEADER_SIZE);
    86
    87          // Validate that the toByteBuffer serializes correctly.
    88          ByteBuffer buffer = header.toByteBuffer();
    89          data = buffer.array();
    90          assertNotNull(data);
    91
    92          header2 = new RecordHeader();
    93          is = new DataInputStream(new ByteArrayInputStream(data));
    94          header2.readRecordHeader(is);
    95
    96          assertEquals(header.length, header2.length);
    97          assertEquals(header.sequenceId, header2.sequenceId);
    98          assertEquals(header.recordType, header2.recordType);
    99          assertEquals(data.length, RecordHeader.RECORD_HEADER_SIZE);
    100
    101     }
    102     public void testRecordFooterSerialization() throws IOException {
    103         doTestRecordFooter(Long.MIN_VALUE,Long.MIN_VALUE);
    104         doTestRecordFooter(Long.MAX_VALUE,Long.MAX_VALUE);
    105     }
    106
    107     public void doTestRecordFooter(long value1, long value2) throws IOException {
    108         RecordFooter footer = new RecordFooter();
    109         footer.sequenceId = value1;
    110         footer.checksum=value2;
    111
    112         ByteArrayOutputStream os = new ByteArrayOutputStream();
    113         DataOutputStream dos = new DataOutputStream(os);
    114         footer.writeRecordFooter(dos);
    115         byte[] data = os.toByteArray();
    116         assertNotNull(data);
    117
    118         RecordFooter footer2 = new RecordFooter();
    119         DataInputStream is = new DataInputStream(new ByteArrayInputStream(data));
    120         footer2.readRecordFooter(is);
    121
    122         assertEquals(footer.checksum, footer2.checksum);
    123         assertEquals(footer.sequenceId, footer2.sequenceId);
    124         assertEquals(data.length, RecordFooter.RECORD_FOOTER_SIZE);
    125
    126         // Validate that the toByteBuffer serializes correctly.
    127         ByteBuffer buffer = footer.toByteBuffer();
    128         data = buffer.array();
    129         assertNotNull(data);
    130
    131         footer2 = new RecordFooter();
    132         is = new DataInputStream(new ByteArrayInputStream(data));
    133         footer2.readRecordFooter(is);
    134
    135         assertEquals(footer.checksum, footer2.checksum);
    136         assertEquals(footer.sequenceId, footer2.sequenceId);
    137         assertEquals(data.length, RecordFooter.RECORD_FOOTER_SIZE);
    138     }
    139
    140     public void testRecordLocationImplComparison() throws IOException {
    141         RecordLocationImpl l1 = new RecordLocationImpl((byte)0,(byte)0,0, 1L);
    142         RecordLocationImpl l2 = new RecordLocationImpl((byte)0,(byte)0,0, 2L);
    143         RecordLocationImpl l3 = new RecordLocationImpl((byte)0,(byte)0,0, 3L);
    144
    145         assertTrue( l1.compareTo(l2)<0 );
    146
    147         // Sort them using a list.  Put them in the wrong order.
    148         ArrayList l = new ArrayList();
    149         l.add(l2);
    150         l.add(l3);
    151         l.add(l1);
    152         Collections.sort(l);
    153
    154         // Did they get sorted to the correct order?
    155         assertSame( l.get(0), l1 );
    156         assertSame( l.get(1), l2 );
    157         assertSame( l.get(2), l3 );
    158     }
    159 }