KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > util > DbLsnTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: DbLsnTest.java,v 1.17 2006/10/30 21:14:53 bostic Exp $
7  */

8
9 package com.sleepycat.je.util;
10
11 import java.io.File JavaDoc;
12
13 import junit.framework.TestCase;
14
15 import com.sleepycat.je.DbInternal;
16 import com.sleepycat.je.Environment;
17 import com.sleepycat.je.EnvironmentConfig;
18 import com.sleepycat.je.log.FileManager;
19 import com.sleepycat.je.utilint.DbLsn;
20
21 public class DbLsnTest extends TestCase {
22     long[] values = { 0xFF, 0xFFFF, 0xFFFFFF, 0x7FFFFFFF, 0xFFFFFFFFL };
23
24     public void testDbLsn() {
25     for (int i = 0; i < values.length; i++) {
26         long value = values[i];
27         long lsn = DbLsn.makeLsn(value, value);
28         assertTrue((DbLsn.getFileNumber(lsn) == value) &&
29                (DbLsn.getFileOffset(lsn) == value));
30     }
31     }
32
33     public void testComparableEquality() {
34     /* Test equality */
35
36     /* Don't bother with last values[] entry -- it makes NULL_LSN. */
37     int lastValue = values.length - 1;
38     for (int i = 0; i < lastValue; i++) {
39         long value = values[i];
40         long lsn1 = DbLsn.makeLsn(value, value);
41         long lsn2 = DbLsn.makeLsn(value, value);
42         assertTrue(DbLsn.compareTo(lsn1, lsn2) == 0);
43     }
44
45     /* Check NULL_LSN. */
46     assertTrue(DbLsn.makeLsn(values[lastValue],
47                  values[lastValue]) ==
48            DbLsn.makeLsn(values[lastValue],
49                  values[lastValue]));
50     }
51
52     public void testComparableNullPointerException() {
53     /* Check that compareTo throws NullPointerException */
54
55     try {
56         long lsn1 = DbLsn.makeLsn(0, 0);
57         DbLsn.compareTo(lsn1, DbLsn.NULL_LSN);
58         fail("compareTo(null) didn't throw NullPointerException");
59     } catch (NullPointerException JavaDoc NPE) {
60     }
61
62     try {
63         long lsn1 = DbLsn.makeLsn(0, 0);
64         DbLsn.compareTo(DbLsn.NULL_LSN, lsn1);
65         fail("compareTo(null) didn't throw NullPointerException");
66     } catch (NullPointerException JavaDoc NPE) {
67     }
68     }
69
70     public void testComparableInequalityFileNumber() {
71     /* Check for inequality in the file number */
72
73     /* Don't bother with last values[] entry -- it makes NULL_LSN. */
74     int lastValue = values.length - 1;
75     for (int i = 0; i < lastValue; i++) {
76         long value = values[i];
77         long lsn1 = DbLsn.makeLsn(value, value);
78         long lsn2 = DbLsn.makeLsn(0, value);
79         assertTrue(DbLsn.compareTo(lsn1, lsn2) == 1);
80         assertTrue(DbLsn.compareTo(lsn2, lsn1) == -1);
81     }
82
83     /* Check against NULL_LSN. */
84     long lsn1 = DbLsn.makeLsn(values[lastValue], values[lastValue]);
85     long lsn2 = DbLsn.makeLsn(0, values[lastValue]);
86     try {
87         assertTrue(DbLsn.compareTo(lsn1, lsn2) == 1);
88     } catch (NullPointerException JavaDoc NPE) {
89     }
90
91     try {
92         assertTrue(DbLsn.compareTo(lsn2, lsn1) == 1);
93     } catch (NullPointerException JavaDoc NPE) {
94     }
95     }
96
97     public void testComparableInequalityFileOffset() {
98     /* Check for inequality in the file offset */
99
100     for (int i = 0; i < values.length - 1; i++) {
101         long value = values[i];
102         long lsn1 = DbLsn.makeLsn(value, value);
103         long lsn2 = DbLsn.makeLsn(value, 0);
104         /* Can't compareTo(NULL_LSN). */
105         if (lsn1 != DbLsn.NULL_LSN &&
106         lsn2 != DbLsn.NULL_LSN) {
107         assertTrue(DbLsn.compareTo(lsn1, lsn2) == 1);
108         assertTrue(DbLsn.compareTo(lsn2, lsn1) == -1);
109         }
110     }
111     }
112
113     public void testSubtractNoCleaning() {
114         long a = DbLsn.makeLsn(1, 10);
115         long b = DbLsn.makeLsn(3, 40);
116         assertEquals(230, DbLsn.getNoCleaningDistance(b, a, 100));
117         assertEquals(230, DbLsn.getNoCleaningDistance(a, b, 100));
118         
119         long c = DbLsn.makeLsn(1, 50);
120         assertEquals(40, DbLsn.getNoCleaningDistance(a, c, 100));
121         assertEquals(40, DbLsn.getNoCleaningDistance(c, a, 100));
122     }
123
124     public void testSubtractWithCleaning()
125         throws Exception JavaDoc {
126
127         /* Try with non-consecutive files (due to cleaning). */
128
129         File JavaDoc envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
130         TestUtils.removeLogFiles("TestSubtract", envHome, false);
131         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
132         envConfig.setAllowCreate(true);
133         Environment env = new Environment(envHome, envConfig);
134
135         try {
136             File JavaDoc file1 = new File JavaDoc (envHome, "00000001.jdb");
137             File JavaDoc file2 = new File JavaDoc (envHome, "00000003.jdb");
138             file1.createNewFile();
139             file2.createNewFile();
140             long a = DbLsn.makeLsn(1, 10);
141             long b = DbLsn.makeLsn(3, 40);
142             FileManager fileManager =
143                 DbInternal.envGetEnvironmentImpl(env).getFileManager();
144             assertEquals(130, DbLsn.getWithCleaningDistance
145              (b, fileManager, a, 100));
146             assertEquals(130, DbLsn.getWithCleaningDistance
147              (a, fileManager, b, 100));
148
149             long c = DbLsn.makeLsn(1, 50);
150             assertEquals(40, DbLsn.getWithCleaningDistance
151              (a, fileManager, c, 100));
152             assertEquals(40, DbLsn.getWithCleaningDistance
153              (c, fileManager, a, 100));
154         } finally {
155             env.close();
156         }
157     }
158 }
159
Popular Tags