001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.unit;
007:
008: import java.sql.SQLException;
009: import java.util.Random;
010:
011: import org.h2.constant.SysProperties;
012: import org.h2.store.DataHandler;
013: import org.h2.store.FileStore;
014: import org.h2.test.TestBase;
015: import org.h2.util.FileUtils;
016: import org.h2.util.SmallLRUCache;
017: import org.h2.value.Value;
018:
019: /**
020: * Tests the in-memory file system.
021: */
022: public class TestFile extends TestBase implements DataHandler {
023:
024: public void test() throws Exception {
025: doTest(false);
026: doTest(true);
027: }
028:
029: private void doTest(boolean compress) throws Exception {
030: byte[] magic = new byte[0];
031: int len = getSize(1000, 10000);
032: Random random = new Random();
033: FileStore mem = null, file = null;
034: byte[] buffMem = null;
035: byte[] buffFile = null;
036: String prefix = compress ? "memLZF:" : "memFS:";
037: FileUtils.delete(prefix + "test");
038: FileUtils.delete("~/test");
039:
040: // config.traceTest = true;
041:
042: for (int i = 0; i < len; i++) {
043: if (buffMem == null) {
044: int l = 1 + random.nextInt(1000);
045: buffMem = new byte[l];
046: buffFile = new byte[l];
047: }
048: if (file == null) {
049: mem = FileStore
050: .open(this , prefix + "test", "rw", magic);
051: file = FileStore.open(this , "~/test", "rw", magic);
052: }
053: check(file.getFilePointer(), mem.getFilePointer());
054: check(file.length(), mem.length());
055: int x = random.nextInt(100);
056: if ((x -= 20) < 0) {
057: if (file.length() > 0) {
058: long pos = random
059: .nextInt((int) (file.length() / 16)) * 16;
060: trace("seek " + pos);
061: mem.seek(pos);
062: file.seek(pos);
063: }
064: } else if ((x -= 20) < 0) {
065: trace("close");
066: mem.close();
067: file.close();
068: mem = null;
069: file = null;
070: } else if ((x -= 20) < 0) {
071: if (buffFile.length > 16) {
072: random.nextBytes(buffFile);
073: System.arraycopy(buffFile, 0, buffMem, 0,
074: buffFile.length);
075: int off = random.nextInt(buffFile.length - 16);
076: int l = random
077: .nextInt((buffFile.length - off) / 16) * 16;
078: trace("write " + off + " " + l);
079: mem.write(buffMem, off, l);
080: file.write(buffFile, off, l);
081: }
082: } else if ((x -= 20) < 0) {
083: if (buffFile.length > 16) {
084: int off = random.nextInt(buffFile.length - 16);
085: int l = random
086: .nextInt((buffFile.length - off) / 16) * 16;
087: l = (int) Math.min(l, file.length()
088: - file.getFilePointer());
089: trace("read " + off + " " + l);
090: Exception a = null, b = null;
091: try {
092: file.readFully(buffFile, off, l);
093: } catch (Exception e) {
094: a = e;
095: }
096: try {
097: mem.readFully(buffMem, off, l);
098: } catch (Exception e) {
099: b = e;
100: }
101: if (a != b) {
102: if (a == null || b == null) {
103: error("only one threw an exception");
104: }
105: }
106: check(buffMem, buffFile);
107: }
108: } else if ((x -= 10) < 0) {
109: trace("reset buffers");
110: buffMem = null;
111: buffFile = null;
112: } else {
113: int l = random.nextInt(10000) * 16;
114: long p = file.getFilePointer();
115: file.setLength(l);
116: mem.setLength(l);
117: trace("setLength " + l);
118: if (p > l) {
119: file.seek(l);
120: mem.seek(l);
121: }
122: }
123: }
124: if (mem != null) {
125: mem.close();
126: file.close();
127: }
128: FileUtils.delete("inmemory:test");
129: FileUtils.delete("~/test");
130: }
131:
132: public int allocateObjectId(boolean needFresh, boolean dataFile) {
133: return 0;
134: }
135:
136: public void checkPowerOff() throws SQLException {
137: }
138:
139: public void checkWritingAllowed() throws SQLException {
140: }
141:
142: public int compareTypeSave(Value a, Value b) throws SQLException {
143: return 0;
144: }
145:
146: public String createTempFile() throws SQLException {
147: return null;
148: }
149:
150: public void freeUpDiskSpace() throws SQLException {
151: }
152:
153: public int getChecksum(byte[] data, int start, int end) {
154: return 0;
155: }
156:
157: public String getDatabasePath() {
158: return null;
159: }
160:
161: public String getLobCompressionAlgorithm(int type) {
162: return null;
163: }
164:
165: public Object getLobSyncObject() {
166: return null;
167: }
168:
169: public int getMaxLengthInplaceLob() {
170: return 0;
171: }
172:
173: public boolean getTextStorage() {
174: return false;
175: }
176:
177: public void handleInvalidChecksum() throws SQLException {
178: }
179:
180: public FileStore openFile(String name, String mode,
181: boolean mustExist) throws SQLException {
182: return null;
183: }
184:
185: public boolean getLobFilesInDirectories() {
186: return SysProperties.LOB_FILES_IN_DIRECTORIES;
187: }
188:
189: public SmallLRUCache getLobFileListCache() {
190: return null;
191: }
192:
193: }
|