diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2012-09-24 01:42:23 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2012-09-24 01:42:23 +0000 |
commit | 74b23acf24e0b16f3d57654680930e55ddd697a0 (patch) | |
tree | efb3e054cfb568e222aed59936730d2b07c48460 /test | |
parent | fe78870364c29c07a8f4269b9c38ccd244665ed0 (diff) | |
download | jackcess-74b23acf24e0b16f3d57654680930e55ddd697a0.tar.gz jackcess-74b23acf24e0b16f3d57654680930e55ddd697a0.zip |
Added the MemFileChannel to enable working with dbs completely in memory
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@642 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'test')
3 files changed, 191 insertions, 7 deletions
diff --git a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java index 51c7efd..1c0c2eb 100644 --- a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java @@ -37,6 +37,7 @@ import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigDecimal; import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; import java.sql.Types; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -78,8 +79,17 @@ public class DatabaseTest extends TestCase { public static Database open(FileFormat fileFormat, File file) throws Exception { + return open(fileFormat, file, false); + } + + private static Database open(FileFormat fileFormat, File file, + boolean inMem) + throws Exception + { + FileChannel channel = (inMem ? MemFileChannel.newChannel(file, "r") + : null); final Database db = new DatabaseBuilder(file).setReadOnly(true) - .setAutoSync(_autoSync).open(); + .setAutoSync(_autoSync).setChannel(channel).open(); assertEquals("Wrong JetFormat.", fileFormat.getJetFormat(), db.getFormat()); assertEquals("Wrong FileFormat.", fileFormat, db.getFileFormat()); @@ -90,6 +100,10 @@ public class DatabaseTest extends TestCase { return open(testDB.getExpectedFileFormat(), testDB.getFile()); } + public static Database openMem(TestDB testDB) throws Exception { + return open(testDB.getExpectedFileFormat(), testDB.getFile(), true); + } + public static Database create(FileFormat fileFormat) throws Exception { return create(fileFormat, false); } @@ -97,8 +111,20 @@ public class DatabaseTest extends TestCase { public static Database create(FileFormat fileFormat, boolean keep) throws Exception { + return create(fileFormat, keep, false); + } + + public static Database createMem(FileFormat fileFormat) throws Exception { + return create(fileFormat, false, true); + } + + private static Database create(FileFormat fileFormat, boolean keep, + boolean inMem) + throws Exception + { + FileChannel channel = (inMem ? MemFileChannel.newChannel() : null); return new DatabaseBuilder(createTempFile(keep)).setFileFormat(fileFormat) - .setAutoSync(_autoSync).create(); + .setAutoSync(_autoSync).setChannel(channel).create(); } @@ -301,6 +327,20 @@ public class DatabaseTest extends TestCase { public void testWriteAndRead() throws Exception { for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { Database db = create(fileFormat); + doTestWriteAndRead(db); + db.close(); + } + } + + public void testWriteAndReadInMem() throws Exception { + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = createMem(fileFormat); + doTestWriteAndRead(db); + db.close(); + } + } + + private static void doTestWriteAndRead(Database db) throws Exception { createTestTable(db); Object[] row = createTestRow(); row[3] = null; @@ -320,11 +360,8 @@ public class DatabaseTest extends TestCase { assertEquals(row[6], readRow.get("G")); assertEquals(row[7], readRow.get("H")); } - - db.close(); - } } - + public void testWriteAndReadInBatch() throws Exception { for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { Database db = create(fileFormat); diff --git a/test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java b/test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java index a479038..ed71ebe 100644 --- a/test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/IndexCodesTest.java @@ -68,7 +68,7 @@ public class IndexCodesTest extends TestCase { public void testIndexCodes() throws Exception { for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX_CODES)) { - Database db = open(testDB); + Database db = openMem(testDB); for(Table t : db) { for(Index index : t.getIndexes()) { diff --git a/test/src/java/com/healthmarketscience/jackcess/MemFileChannelTest.java b/test/src/java/com/healthmarketscience/jackcess/MemFileChannelTest.java new file mode 100644 index 0000000..6fa0425 --- /dev/null +++ b/test/src/java/com/healthmarketscience/jackcess/MemFileChannelTest.java @@ -0,0 +1,147 @@ +/* +Copyright (c) 2012 James Ahlborn + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +USA +*/ + +package com.healthmarketscience.jackcess; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.channels.NonWritableChannelException; +import java.util.Arrays; + +import junit.framework.TestCase; + +/** + * + * @author James Ahlborn + */ +public class MemFileChannelTest extends TestCase +{ + + public MemFileChannelTest(String name) { + super(name); + } + + public void testReadOnlyChannel() throws Exception + { + File testFile = new File("test/data/V1997/compIndexTestV1997.mdb"); + MemFileChannel ch = MemFileChannel.newChannel(testFile, "r"); + assertEquals(testFile.length(), ch.size()); + assertEquals(0L, ch.position()); + + try { + ByteBuffer bb = ByteBuffer.allocate(1024); + ch.write(bb); + fail("NonWritableChannelException should have been thrown"); + } catch(NonWritableChannelException ignored) { + // success + } + + try { + ch.truncate(0L); + fail("NonWritableChannelException should have been thrown"); + } catch(NonWritableChannelException ignored) { + // success + } + + try { + ch.transferFrom(null, 0L, 10L); + fail("NonWritableChannelException should have been thrown"); + } catch(NonWritableChannelException ignored) { + // success + } + + assertEquals(testFile.length(), ch.size()); + assertEquals(0L, ch.position()); + + ch.close(); + } + + public void testChannel() throws Exception + { + ByteBuffer bb = ByteBuffer.allocate(1024); + + MemFileChannel ch = MemFileChannel.newChannel(); + assertTrue(ch.isOpen()); + assertEquals(0L, ch.size()); + assertEquals(0L, ch.position()); + assertEquals(-1, ch.read(bb)); + + ch.close(); + + assertFalse(ch.isOpen()); + + File testFile = new File("test/data/V1997/compIndexTestV1997.mdb"); + ch = MemFileChannel.newChannel(testFile, "r"); + assertEquals(testFile.length(), ch.size()); + assertEquals(0L, ch.position()); + + MemFileChannel ch2 = MemFileChannel.newChannel(); + ch.transferTo(ch2); + ch2.force(true); + assertEquals(testFile.length(), ch2.size()); + assertEquals(testFile.length(), ch2.position()); + + long trucSize = ch2.size()/3; + ch2.truncate(trucSize); + assertEquals(trucSize, ch2.size()); + assertEquals(trucSize, ch2.position()); + ch2.position(0L); + copy(ch, ch2, bb); + + File tmpFile = File.createTempFile("chtest_", ".dat"); + tmpFile.deleteOnExit(); + FileChannel fc = new RandomAccessFile(tmpFile, "rw").getChannel(); + + copy(ch2, fc, bb); + + fc.close(); + + assertEquals(testFile.length(), tmpFile.length()); + + assertTrue(Arrays.equals(DatabaseTest.toByteArray(testFile), + DatabaseTest.toByteArray(tmpFile))); + + ch2.truncate(0L); + assertTrue(ch2.isOpen()); + assertEquals(0L, ch2.size()); + assertEquals(0L, ch2.position()); + assertEquals(-1, ch2.read(bb)); + + ch2.close(); + assertFalse(ch2.isOpen()); + } + + private static void copy(FileChannel src, FileChannel dst, ByteBuffer bb) + throws IOException + { + src.position(0L); + while(true) { + bb.clear(); + if(src.read(bb) < 0) { + break; + } + bb.flip(); + dst.write(bb); + } + } + +} |