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  import org.codehaus.activemq.journal.InvalidRecordLocationException;
27  
28  
29  /***
30   * Tests the LogFile used by JournalImpl 
31   * 
32   * @version $Revision: 1.1 $
33   */
34  public class LogFileTest extends TestCase {
35  	
36      int size = 1024*512;
37      int segmentCount=4;
38      File logDirectory = new File("test-logfile");
39  	private LogFile logFile;
40      
41      /***
42       * @see junit.framework.TestCase#setUp()
43       */
44      protected void setUp() throws Exception {
45          if( logDirectory.exists() ) {
46          	deleteDir(logDirectory);
47          }
48          assertTrue( !logDirectory.exists() );
49          logFile = new LogFile(logDirectory,segmentCount,size);
50      }
51  
52      /***
53  	 */
54  	private void deleteDir(File f) {
55  		File[] files = f.listFiles();
56  		for (int i = 0; i < files.length; i++) {
57  			File file = files[i];
58  			file.delete();
59  		}
60  		f.delete();
61  	}
62  
63  	protected void tearDown() throws Exception {
64  		logFile.close();
65          if( logDirectory.exists() )
66          	deleteDir(logDirectory);
67          assertTrue( !logDirectory.exists() );
68      }
69      
70      public void testLogFileCreation() throws IOException {
71      	assertTrue( logFile.isSegmentIndexActive((byte)0) );
72      	assertEquals( logFile.getAppendSegmentIndex(), (byte)0);
73      	assertTrue( logFile.canActivateNextSegment() );
74      	assertEquals( -1, logFile.getLastSequenceId());
75      }
76      
77      public void testAppendAndRead() throws IOException, InvalidRecordLocationException {
78  
79      	appendHelloRecord(1001);    		
80      	RecordLocationImpl loc = new RecordLocationImpl((byte)0, logFile.getAppendSegmentIndex(), logFile.getAppendSegmentOffset());			
81      	appendHelloRecord(2002);
82      	appendHelloRecord(3003);
83      	
84      	RecordLocationImpl loc2 = logFile.readRecordLocation(loc);
85      	assertEquals( loc2.getSequenceId(), 2002 );
86      	loc2 = logFile.getNextDataRecordLocation(loc2);
87      	assertEquals( loc2.getSequenceId(), 3003 );
88      	loc2 = logFile.getNextDataRecordLocation(loc2);
89      	assertNull(loc2 );
90      	
91      }
92      
93      public void testRollOver() throws IOException, InvalidRecordLocationException {
94  
95      	int counter=0;
96      	for( int i=0; i < segmentCount; i++ ) {
97      		counter += 500;
98          	appendHelloRecord(counter);
99          	if( i+1==segmentCount ) {
100             	assertFalse( logFile.canActivateNextSegment() );
101         	} else {
102             	assertTrue( logFile.getAppendSegmentIndex()==i );
103             	assertTrue( logFile.canActivateNextSegment() );
104             	logFile.activateNextSegment();
105             	assertTrue( logFile.getAppendSegmentIndex()==i+1 );
106         	}
107     	}
108     	
109     	    	
110     }
111     
112 
113 	/***
114 	 * @param i
115 	 * @throws IOException
116 	 */
117 	private void appendHelloRecord(int i) throws IOException {
118 		byte data[] = ("Hello World: "+i).getBytes();
119 		
120         BatchedWrite write = new BatchedWrite(ByteBuffer.allocate(1024));
121         Record batchedRecord = new Record((long)i, LogFile.DATA_RECORD_TYPE, data, null);
122         write.append(batchedRecord);
123 		logFile.appendAndForce(write);
124 	}
125 }