KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > kaha > impl > async > JournalImplTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.kaha.impl.async;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
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 /**
34  * Tests the AsyncDataManager based Journal
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*10;
43     int logFileCount=2;
44     File JavaDoc logDirectory = new File JavaDoc("target/dm-data2");
45     private JournalFacade journal;
46     
47     /**
48      * @see junit.framework.TestCase#setUp()
49      */

50     protected void setUp() throws Exception JavaDoc {
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     /**
63      */

64     private void deleteDir(File JavaDoc f) {
65         File JavaDoc[] files = f.listFiles();
66         for (int i = 0; i < files.length; i++) {
67             File JavaDoc file = files[i];
68             file.delete();
69         }
70         f.delete();
71     }
72
73     protected void tearDown() throws Exception JavaDoc {
74         journal.close();
75         if( logDirectory.exists() )
76             deleteDir(logDirectory);
77         //assertTrue( !logDirectory.exists() );
78
}
79     
80     public void testLogFileCreation() throws IOException JavaDoc {
81             RecordLocation mark = journal.getMark();
82             assertNull(mark);
83     }
84     
85     @SuppressWarnings JavaDoc("unchecked")
86     public void testAppendAndRead() throws InvalidRecordLocationException, InterruptedException JavaDoc, IOException JavaDoc {
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         //Thread.sleep(1000);
96

97         // Now see if we can read that data.
98
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         // Can we cursor the data?
107
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 JavaDoc, IOException JavaDoc {
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         // Now see if we can read that first packet.
147
Packet data;
148         data = journal.read(location1);
149         assertEquals( data1, data);
150         
151     }
152
153     /**
154      * @param string
155      * @return
156      */

157     private Packet createPacket(String JavaDoc 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 // System.out.println("Comparing: "+new String(arg0)+" and "+new String(arg1));
168
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