From 2c646fd415e0ec611407dc5e2aac79cac5af70c8 Mon Sep 17 00:00:00 2001 From: Dan Rollo Date: Wed, 7 Apr 2010 02:14:41 +0000 Subject: [PATCH] Detect unsupported Jet and File formats and throw specific exceptions early. Remove old @todo comments and sys.outs. git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/newformats@456 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../jackcess/Database.java | 15 ++++++++- .../jackcess/DatabaseTest.java | 33 +++++++++++++------ .../jackcess/JetFormatTest.java | 1 - .../jackcess/UsageMapTest.java | 7 ++-- 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/java/com/healthmarketscience/jackcess/Database.java b/src/java/com/healthmarketscience/jackcess/Database.java index 5044de7..00ac78f 100644 --- a/src/java/com/healthmarketscience/jackcess/Database.java +++ b/src/java/com/healthmarketscience/jackcess/Database.java @@ -170,7 +170,12 @@ public class Database private static final String CAT_COL_FLAGS = "Flags"; /** System catalog column name of the properties column */ private static final String CAT_COL_PROPS = "LvProp"; - + + /** Error message prefix for unsupported file format. */ + static final String MSG_PREFIX_CREATE_UNSUPPORTED_FILE_FORMAT = "Unsupported file format: "; + /** Error message prefix for unsupported file format. */ + static final String MSG_PREFIX_OPEN_UNSUPPORTED_JET_FORMAT = "Unsupported Jet format: "; + public static enum FileFormat { V1997(null, JetFormat.VERSION_3), // v97 is not supported, so no empty template is provided @@ -439,6 +444,10 @@ public class Database boolean autoSync) throws IOException { + if (JetFormat.VERSION_3.equals(fileFormat.getJetFormat())) { + throw new IllegalArgumentException(MSG_PREFIX_CREATE_UNSUPPORTED_FILE_FORMAT + fileFormat); + } + FileChannel channel = openChannel(mdbFile, false); channel.truncate(0); channel.transferFrom(Channels.newChannel( @@ -478,6 +487,10 @@ public class Database throws IOException { _format = JetFormat.getFormat(channel); + if (JetFormat.VERSION_3.equals(_format)) { + channel.close(); + throw new IllegalArgumentException(MSG_PREFIX_OPEN_UNSUPPORTED_JET_FORMAT + _format); + } _fileFormat = fileFormat; _pageChannel = new PageChannel(channel, _format, autoSync); // note, it's slighly sketchy to pass ourselves along partially diff --git a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java index 828f76d..63c0add 100644 --- a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java @@ -101,6 +101,27 @@ public class DatabaseTest extends TestCase { } + public void testUnsupportedCreate() throws Exception { + try { + create(JetFormatTest.UNSUPPORTED_TEST_V1997.getExpectedFileFormat()); + fail("Create unsupported JetFormat should fail."); + } catch (IllegalArgumentException e) { + assertEquals(Database.MSG_PREFIX_CREATE_UNSUPPORTED_FILE_FORMAT + FileFormat.V1997, e.getMessage()); + } + } + + public void testUnsupportedOpen() throws Exception { + try { + open(JetFormatTest.UNSUPPORTED_TEST_V1997); + fail("Open unsupported JetFormat should fail."); + } catch (IllegalArgumentException e) { + assertEquals( + MSG_PREFIX_OPEN_UNSUPPORTED_JET_FORMAT + UNSUPPORTED_TEST_V1997.getExpectedFormat(), + e.getMessage()); + } + } + + public void testInvalidTableDefs() throws Exception { for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { Database db = create(fileFormat); @@ -783,15 +804,9 @@ public class DatabaseTest extends TestCase { String lval = createString(255); // "--255 chars long text--"; - if (FileFormat.V2007.equals(testDB.getExpectedFileFormat())) { - // @todo Field order differs with V2007 - System.err.println("\n*** TODO: " + getName() - + "(): Is OK that " + testDB.getExpectedFileFormat().name() + " field order differs? ***"); - } - for(int i = 0; i < 1000; ++i) { if (FileFormat.V2007.equals(testDB.getExpectedFileFormat())) { - // @todo Field order differs with V2007 + // Field order differs with V2007 t.addRow(i, 13, 57, lval, lval, lval, lval, lval, lval, 47.0d); } else { t.addRow(i, 13, 57, 47.0d, lval, lval, lval, lval, lval, lval); @@ -986,9 +1001,7 @@ public class DatabaseTest extends TestCase { && !FileFormat.V2007.equals(fileFormat)) { assertNotNull("file format: " + fileFormat, db.getSystemTable("MSysAccessObjects")); } else { - // @todo Does is matter that v2003, v2007 template files have no "MSysAccessObjects" table? - System.err.println("\n*** TODO: " + getName() - + "(): Is OK that " + fileFormat.name() + " template file has no \"MSysAccessObjects\" table? ***"); + // v2003, v2007 template files have no "MSysAccessObjects" table assertNull("file format: " + fileFormat, db.getSystemTable("MSysAccessObjects")); } diff --git a/test/src/java/com/healthmarketscience/jackcess/JetFormatTest.java b/test/src/java/com/healthmarketscience/jackcess/JetFormatTest.java index 96365e4..a51e865 100644 --- a/test/src/java/com/healthmarketscience/jackcess/JetFormatTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/JetFormatTest.java @@ -50,7 +50,6 @@ public final class JetFormatTest extends TestCase { Database.FileFormat.V2000, Database.FileFormat.V2003, Database.FileFormat.V2007, - // @todo Uncomment these elements to run test other formats }; /** diff --git a/test/src/java/com/healthmarketscience/jackcess/UsageMapTest.java b/test/src/java/com/healthmarketscience/jackcess/UsageMapTest.java index 85b0630..4d55449 100644 --- a/test/src/java/com/healthmarketscience/jackcess/UsageMapTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/UsageMapTest.java @@ -5,6 +5,7 @@ import junit.framework.TestCase; import java.io.File; import java.io.IOException; +import static com.healthmarketscience.jackcess.Database.MSG_PREFIX_OPEN_UNSUPPORTED_JET_FORMAT; import static com.healthmarketscience.jackcess.JetFormatTest.*; /** @@ -18,8 +19,10 @@ public final class UsageMapTest extends TestCase { try { Database.open(UNSUPPORTED_TEST_V1997.getFile()); fail("mdb v97 usage map unsupported"); - } catch (IOException e) { - assertEquals(UsageMap.MSG_PREFIX_UNRECOGNIZED_MAP + 2, e.getMessage()); + } catch (IllegalArgumentException e) { + assertEquals( + MSG_PREFIX_OPEN_UNSUPPORTED_JET_FORMAT + UNSUPPORTED_TEST_V1997.getExpectedFormat(), + e.getMessage()); } for (final TestDB testDB : SUPPORTED_DBS_TEST) { -- 2.39.5