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 import java.nio.ByteBuffer;
23
24 import junit.framework.TestCase;
25
26 import org.activeio.journal.InvalidRecordLocationException;
27 import org.activeio.packet.ByteArrayPacket;
28 import org.activeio.packet.ByteBufferPacket;
29
30 /***
31 * Tests the LogFile used by JournalImpl
32 *
33 * @version $Revision: 1.1 $
34 */
35 public class LogFileManagerTest extends TestCase {
36
37 int size = 1024 * 512;
38
39 int logFileCount = 4;
40
41 File logDirectory = new File("test-logfile");
42
43 private LogFileManager logFile;
44
45 /***
46 * @see junit.framework.TestCase#setUp()
47 */
48 protected void setUp() throws Exception {
49 if (logDirectory.exists()) {
50 deleteDir(logDirectory);
51 }
52 assertTrue(!logDirectory.exists());
53 logFile = new LogFileManager(logDirectory, logFileCount, size);
54 }
55
56 /***
57 */
58 private void deleteDir(File f) {
59 File[] files = f.listFiles();
60 for (int i = 0; i < files.length; i++) {
61 File file = files[i];
62 file.delete();
63 }
64 f.delete();
65 }
66
67 protected void tearDown() throws Exception {
68 logFile.dispose();
69 if (logDirectory.exists())
70 deleteDir(logDirectory);
71 assertTrue(!logDirectory.exists());
72 }
73
74 public void testLogFileCreation() throws IOException {
75 assertTrue(logFile.canActivateNextLogFile());
76 assertEquals(null,logFile.getFirstActiveLogLocation());
77 assertNull(logFile.getLastMarkedRecordLocation());
78 assertEquals(new Location(0, 0),logFile.getNextAppendLocation());
79 }
80
81 public void testAppendAndRead() throws IOException, InvalidRecordLocationException, InterruptedException {
82
83 System.out.println("Initial:"+logFile.getNextAppendLocation());
84 appendHelloRecord(1001);
85 Location loc2 = logFile.getNextAppendLocation();
86 appendHelloRecord(2002);
87 appendHelloRecord(3003);
88 appendHelloRecord(3004);
89
90 Location loc3 = logFile.getNextDataRecordLocation(loc2);
91 assertTrue(loc3.getLogFileOffset() > loc2.getLogFileOffset());
92 Location loc4 = logFile.getNextDataRecordLocation(loc3);
93 assertTrue(loc4.getLogFileOffset() > loc3.getLogFileOffset());
94
95 }
96
97 public void testRollOver() throws IOException, InvalidRecordLocationException, InterruptedException {
98
99 int lastId = logFile.getNextAppendLocation().getLogFileId();
100 int counter = 0;
101 for (int i = 0; i < logFileCount; i++) {
102 counter += 500;
103 appendHelloRecord(counter);
104 if (i + 1 == logFileCount) {
105 assertFalse(logFile.canActivateNextLogFile());
106 } else {
107 assertTrue(logFile.canActivateNextLogFile());
108 logFile.activateNextLogFile();
109 assertEquals(lastId + 1, logFile.getNextAppendLocation().getLogFileId());
110 lastId = logFile.getNextAppendLocation().getLogFileId();
111 }
112 }
113
114 }
115
116 /***
117 * @param i
118 * @throws IOException
119 * @throws InterruptedException
120 */
121 private void appendHelloRecord(int i) throws IOException, InterruptedException {
122 byte data[] = ("Hello World: " + i).getBytes();
123 Record batchedRecord = new Record(LogFileManager.DATA_RECORD_TYPE, new ByteArrayPacket(data), null);
124 batchedRecord.setLocation(logFile.getNextAppendLocation());
125
126 BatchedWrite write = new BatchedWrite(new ByteBufferPacket(ByteBuffer.allocate(1024)));
127 write.append(batchedRecord,null, true);
128 write.flip();
129 logFile.append(write);
130 }
131 }