Browse Source

tighten up readonly handling for external channels; minor test code refactor

git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1368 f203690c-595d-4dc9-a70b-905162fa7fd2
tags/jackcess-4.0.1
James Ahlborn 2 years ago
parent
commit
1ff24f80a1

+ 14
- 4
src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java View File

boolean success = false; boolean success = false;
try { try {


boolean wrapChannelRO = false;
if(!readOnly) { if(!readOnly) {

// verify that format supports writing // verify that format supports writing
JetFormat jetFormat = JetFormat.getFormat(channel); JetFormat jetFormat = JetFormat.getFormat(channel);


if(jetFormat.READ_ONLY) { if(jetFormat.READ_ONLY) {
// wrap the channel with a read-only version to enforce
// non-writability
channel = new ReadOnlyFileChannel(channel);
// force read-only mode
wrapChannelRO = true;
readOnly = true; readOnly = true;
} }
} else if(!closeChannel) {
// we are in read-only mode but the channel was opened externally, so
// we don't know if it is enforcing read-only status. wrap it just to
// be safe
wrapChannelRO = true;
}

if(wrapChannelRO) {
// wrap the channel with a read-only version to enforce
// non-writability
channel = new ReadOnlyFileChannel(channel);
} }


DatabaseImpl db = new DatabaseImpl(mdbFile, channel, closeChannel, autoSync, DatabaseImpl db = new DatabaseImpl(mdbFile, channel, closeChannel, autoSync,

+ 20
- 18
src/test/java/com/healthmarketscience/jackcess/TestUtil.java View File

import java.io.PrintWriter; import java.io.PrintWriter;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
Charset charset) Charset charset)
throws Exception throws Exception
{ {
FileChannel channel = (inMem ? MemFileChannel.newChannel(
file, MemFileChannel.RW_CHANNEL_MODE)
: null);
final Database db = new DatabaseBuilder(file).setReadOnly(true)
.setAutoSync(getTestAutoSync()).setChannel(channel)
.setCharset(charset).open();
Assert.assertEquals("Wrong JetFormat.",
DatabaseImpl.getFileFormatDetails(fileFormat).getFormat(),
((DatabaseImpl)db).getFormat());
Assert.assertEquals("Wrong FileFormat.", fileFormat, db.getFileFormat());
return db;
return openDB(fileFormat, file, inMem, charset, true);
} }


public static Database open(TestDB testDB) throws Exception { public static Database open(TestDB testDB) throws Exception {
} }


public static Database openMem(TestDB testDB) throws Exception { public static Database openMem(TestDB testDB) throws Exception {
return open(testDB.getExpectedFileFormat(), testDB.getFile(), true,
testDB.getExpectedCharset());
return openDB(testDB.getExpectedFileFormat(), testDB.getFile(), true,
testDB.getExpectedCharset(), false);
} }


public static Database create(FileFormat fileFormat) throws Exception { public static Database create(FileFormat fileFormat) throws Exception {
{ {
File tmp = createTempFile(keep); File tmp = createTempFile(keep);
copyFile(file, tmp); copyFile(file, tmp);
Database db = new DatabaseBuilder(tmp).setAutoSync(getTestAutoSync()).open();
return openDB(fileFormat, tmp, false, null, false);
}

private static Database openDB(
FileFormat fileFormat, File file, boolean inMem, Charset charset,
boolean readOnly)
throws Exception
{
FileChannel channel = (inMem ? MemFileChannel.newChannel(
file, MemFileChannel.RW_CHANNEL_MODE)
: null);
final Database db = new DatabaseBuilder(file).setReadOnly(readOnly)
.setAutoSync(getTestAutoSync()).setChannel(channel)
.setCharset(charset).open();
Assert.assertEquals("Wrong JetFormat.", Assert.assertEquals("Wrong JetFormat.",
DatabaseImpl.getFileFormatDetails(fileFormat).getFormat(),
((DatabaseImpl)db).getFormat());
DatabaseImpl.getFileFormatDetails(fileFormat).getFormat(),
((DatabaseImpl)db).getFormat());
Assert.assertEquals("Wrong FileFormat.", fileFormat, db.getFileFormat()); Assert.assertEquals("Wrong FileFormat.", fileFormat, db.getFileFormat());
return db; return db;
} }



static Object[] createTestRow(String col1Val) { static Object[] createTestRow(String col1Val) {
return new Object[] {col1Val, "R", "McCune", 1234, (byte) 0xad, 555.66d, return new Object[] {col1Val, "R", "McCune", 1234, (byte) 0xad, 555.66d,
777.88f, (short) 999, new Date()}; 777.88f, (short) 999, new Date()};

Loading…
Cancel
Save