    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
    23  import org.codehaus.activemq.journal.InvalidRecordLocationException;
    24  import org.codehaus.activemq.journal.RecordLocation;
    25
    26  import junit.framework.TestCase;
    27
    28  /***
    29   * Tests the JournalImpl
    30   *
    31   * @version $Revision: 1.1 $
    32   */
    33  public class JournalImplTest extends TestCase {
    34
    35      int size = 1024*512;
    36      int segmentCount=4;
    37      File logDirectory = new File("test-logfile");
    38  	private JournalImpl journal;
    39
    40      /***
    41       * @see junit.framework.TestCase#setUp()
    42       */
    43      protected void setUp() throws Exception {
    44          if( logDirectory.exists() ) {
    45          	deleteDir(logDirectory);
    46          }
    47          assertTrue( !logDirectory.exists() );
    48          journal = new JournalImpl(logDirectory,segmentCount,size);
    49      }
    50
    51      /***
    52  	 */
    53  	private void deleteDir(File f) {
    54  		File[] files = f.listFiles();
    55  		for (int i = 0; i < files.length; i++) {
    56  			File file = files[i];
    57  			file.delete();
    58  		}
    59  		f.delete();
    60  	}
    61
    62  	protected void tearDown() throws Exception {
    63  		journal.close();
    64          if( logDirectory.exists() )
    65          	deleteDir(logDirectory);
    66          assertTrue( !logDirectory.exists() );
    67      }
    68
    69      public void testLogFileCreation() throws IOException {
    70      	RecordLocation mark = journal.getMark();
    71      	assertNull(mark);
    72      }
    73
    74      public void testAppendAndRead() throws InvalidRecordLocationException, InterruptedException, IOException {
    75      	byte data1[] = "Hello World 1".getBytes();
    76      	RecordLocation location1 = journal.write( data1, false);
    77      	byte data2[] = "Hello World 2".getBytes();
    78      	RecordLocation location2 = journal.write( data2, false);
    79      	byte data3[] = "Hello World 3".getBytes();
    80      	RecordLocation location3 = journal.write( data3, false);
    81
    82      	// Now see if we can read that data.
    83      	byte[] data;
    84      	data = journal.read(location2);
    85      	assertEquals( data2, data);
    86      	data = journal.read(location1);
    87      	assertEquals( data1, data);
    88      	data = journal.read(location3);
    89      	assertEquals( data3, data);
    90
    91      	// Can we cursor the data?
    92      	RecordLocation l=journal.getNextRecordLocation(null);
    93      	assertEquals(0, l.compareTo(location1));
    94      	data = journal.read(l);
    95      	assertEquals( data1, data);
    96
    97      	l=journal.getNextRecordLocation(l);
    98      	assertEquals(0, l.compareTo(location2));
    99      	data = journal.read(l);
    100     	assertEquals( data2, data);
    101
    102     	l=journal.getNextRecordLocation(l);
    103     	assertEquals(0, l.compareTo(location3));
    104     	data = journal.read(l);
    105     	assertEquals( data3, data);
    106
    107     	l=journal.getNextRecordLocation(l);
    108     	assertNull(l);
    109
    110     	System.out.println(journal);
    111     }
    112
    113     public static void assertEquals(byte[] arg0, byte[] arg1) {
    114     	if( arg0==null ^ arg1==null )
    115     		fail("Not equal: "+arg0+" != "+arg1);
    116     	if( arg0==null )
    117     		return;
    118     	if( arg0.length!=arg1.length)
    119     		fail("Array lenght not equal: "+arg0.length+" != "+arg1.length);
    120     	for( int i=0; i<arg0.length;i++) {
    121     		if( arg0[i]!= arg1[i]) {
    122         		fail("Array item not equal at index "+i+": "+arg0[i]+" != "+arg1[i]);
    123     		}
    124     	}
    125 	}
    126 }