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