1 18 package org.apache.activemq.kaha.impl.async; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 23 import junit.framework.TestCase; 24 25 import org.apache.activeio.journal.InvalidRecordLocationException; 26 import org.apache.activeio.journal.RecordLocation; 27 import org.apache.activeio.packet.ByteArrayPacket; 28 import org.apache.activeio.packet.Packet; 29 import org.apache.activemq.kaha.impl.async.JournalFacade.RecordLocationFacade; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 38 public class JournalImplTest extends TestCase { 39 40 Log log = LogFactory.getLog(JournalImplTest.class); 41 42 int size = 1024*10; 43 int logFileCount=2; 44 File logDirectory = new File ("target/dm-data2"); 45 private JournalFacade journal; 46 47 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 AsyncDataManager dm = new AsyncDataManager(); 56 dm.setDirectory(logDirectory); 57 dm.setMaxFileLength(1024*64); 58 dm.start(); 59 journal = new JournalFacade(dm); 60 } 61 62 64 private void deleteDir(File f) { 65 File [] files = f.listFiles(); 66 for (int i = 0; i < files.length; i++) { 67 File file = files[i]; 68 file.delete(); 69 } 70 f.delete(); 71 } 72 73 protected void tearDown() throws Exception { 74 journal.close(); 75 if( logDirectory.exists() ) 76 deleteDir(logDirectory); 77 } 79 80 public void testLogFileCreation() throws IOException { 81 RecordLocation mark = journal.getMark(); 82 assertNull(mark); 83 } 84 85 @SuppressWarnings ("unchecked") 86 public void testAppendAndRead() throws InvalidRecordLocationException, InterruptedException , IOException { 87 88 Packet data1 = createPacket("Hello World 1"); 89 RecordLocation location1 = journal.write( data1, false); 90 Packet data2 = createPacket("Hello World 2"); 91 RecordLocation location2 = journal.write( data2, false); 92 Packet data3 = createPacket("Hello World 3"); 93 RecordLocation location3 = journal.write( data3, false); 94 95 97 Packet data; 99 data = journal.read(location2); 100 assertEquals( data2, data); 101 data = journal.read(location1); 102 assertEquals( data1, data); 103 data = journal.read(location3); 104 assertEquals( data3, data); 105 106 RecordLocation l=journal.getNextRecordLocation(null); 108 int t = l.compareTo(location1); 109 assertEquals(0, t); 110 data = journal.read(l); 111 assertEquals( data1, data); 112 113 l=journal.getNextRecordLocation(l); 114 assertEquals(0, l.compareTo(location2)); 115 data = journal.read(l); 116 assertEquals( data2, data); 117 118 l=journal.getNextRecordLocation(l); 119 assertEquals(0, l.compareTo(location3)); 120 data = journal.read(l); 121 assertEquals( data3, data); 122 123 l=journal.getNextRecordLocation(l); 124 assertNull(l); 125 126 log.info(journal); 127 } 128 129 public void testCanReadFromArchivedLogFile() throws InvalidRecordLocationException, InterruptedException , IOException { 130 131 Packet data1 = createPacket("Hello World 1"); 132 RecordLocationFacade location1 = (RecordLocationFacade) journal.write( data1, false); 133 134 RecordLocationFacade pos; 135 int counter = 0; 136 do { 137 138 Packet p = createPacket("<<<data>>>"); 139 pos = (RecordLocationFacade) journal.write( p, false); 140 if( counter++ % 1000 == 0 ) { 141 journal.setMark(pos, false); 142 } 143 144 } while( pos.getLocation().getDataFileId() < 5 ); 145 146 Packet data; 148 data = journal.read(location1); 149 assertEquals( data1, data); 150 151 } 152 153 157 private Packet createPacket(String string) { 158 return new ByteArrayPacket(string.getBytes()); 159 } 160 161 public static void assertEquals(Packet arg0, Packet arg1) { 162 assertEquals(arg0.sliceAsBytes(), arg1.sliceAsBytes()); 163 } 164 165 public static void assertEquals(byte[] arg0, byte[] arg1) { 166 167 if( arg0==null ^ arg1==null ) 169 fail("Not equal: "+arg0+" != "+arg1); 170 if( arg0==null ) 171 return; 172 if( arg0.length!=arg1.length) 173 fail("Array lenght not equal: "+arg0.length+" != "+arg1.length); 174 for( int i=0; i<arg0.length;i++) { 175 if( arg0[i]!= arg1[i]) { 176 fail("Array item not equal at index "+i+": "+arg0[i]+" != "+arg1[i]); 177 } 178 } 179 } 180 } 181 | Popular Tags |