diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2008-11-08 01:47:49 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2008-11-08 01:47:49 +0000 |
commit | 57b74129668f576738cc05b35ea7a10eaedfe06a (patch) | |
tree | 74ca0947f42ec80f35a83660913aa4b6d522da7f /test/src/java/com | |
parent | 130789d1f0a997288c13a6df763a453641545cc9 (diff) | |
download | jackcess-57b74129668f576738cc05b35ea7a10eaedfe06a.tar.gz jackcess-57b74129668f576738cc05b35ea7a10eaedfe06a.zip |
Add ErrorHandler utility for customizing error handling during row
parsing.
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@381 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'test/src/java/com')
3 files changed, 189 insertions, 2 deletions
diff --git a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java index 136780e..aedf3e7 100644 --- a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java @@ -960,9 +960,15 @@ public class DatabaseTest extends TestCase { static void assertTable(List<Map<String, Object>> expectedTable, Table table) { + assertCursor(expectedTable, Cursor.createCursor(table)); + } + + static void assertCursor(List<Map<String, Object>> expectedTable, + Cursor cursor) + { List<Map<String, Object>> foundTable = new ArrayList<Map<String, Object>>(); - for(Map<String, Object> row : Cursor.createCursor(table)) { + for(Map<String, Object> row : cursor) { foundTable.add(row); } assertEquals(expectedTable, foundTable); diff --git a/test/src/java/com/healthmarketscience/jackcess/ErrorHandlerTest.java b/test/src/java/com/healthmarketscience/jackcess/ErrorHandlerTest.java new file mode 100644 index 0000000..8b46a8a --- /dev/null +++ b/test/src/java/com/healthmarketscience/jackcess/ErrorHandlerTest.java @@ -0,0 +1,181 @@ +/* +Copyright (c) 2007 Health Market Science, Inc. + +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 + +You can contact Health Market Science at info@healthmarketscience.com +or at the following address: + +Health Market Science +2700 Horizon Drive +Suite 200 +King of Prussia, PA 19406 +*/ + +package com.healthmarketscience.jackcess; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.nio.ByteOrder; +import java.util.List; + +import junit.framework.TestCase; + +import static com.healthmarketscience.jackcess.DatabaseTest.*; + +/** + * @author James Ahlborn + */ +public class ErrorHandlerTest extends TestCase +{ + + public ErrorHandlerTest(String name) { + super(name); + } + + public void testErrorHandler() throws Exception + { + Database db = create(); + + Table table = + new TableBuilder("test") + .addColumn(new ColumnBuilder("col", DataType.TEXT).toColumn()) + .addColumn(new ColumnBuilder("val", DataType.LONG).toColumn()) + .toTable(db); + + table.addRow("row1", 1); + table.addRow("row2", 2); + table.addRow("row3", 3); + + assertTable(createExpectedTable( + createExpectedRow("col", "row1", + "val", 1), + createExpectedRow("col", "row2", + "val", 2), + createExpectedRow("col", "row3", + "val", 3)), + table); + + + replaceColumn(table, "val"); + + table.reset(); + try { + table.getNextRow(); + fail("IOException should have been thrown"); + } catch(IOException e) { + // success + } + + table.reset(); + table.setErrorHandler(new ReplacementErrorHandler()); + + assertTable(createExpectedTable( + createExpectedRow("col", "row1", + "val", null), + createExpectedRow("col", "row2", + "val", null), + createExpectedRow("col", "row3", + "val", null)), + table); + + Cursor c1 = Cursor.createCursor(table); + Cursor c2 = Cursor.createCursor(table); + Cursor c3 = Cursor.createCursor(table); + + c2.setErrorHandler(new DebugErrorHandler("#error")); + c3.setErrorHandler(Database.DEFAULT_ERROR_HANDLER); + + assertCursor(createExpectedTable( + createExpectedRow("col", "row1", + "val", null), + createExpectedRow("col", "row2", + "val", null), + createExpectedRow("col", "row3", + "val", null)), + c1); + + assertCursor(createExpectedTable( + createExpectedRow("col", "row1", + "val", "#error"), + createExpectedRow("col", "row2", + "val", "#error"), + createExpectedRow("col", "row3", + "val", "#error")), + c2); + + try { + c3.getNextRow(); + fail("IOException should have been thrown"); + } catch(IOException e) { + // success + } + + table.setErrorHandler(null); + c1.setErrorHandler(null); + c1.reset(); + try { + c1.getNextRow(); + fail("IOException should have been thrown"); + } catch(IOException e) { + // success + } + + + db.close(); + } + + @SuppressWarnings("unchecked") + private void replaceColumn(Table t, String colName) throws Exception + { + Field colsField = Table.class.getDeclaredField("_columns"); + colsField.setAccessible(true); + List<Column> cols = (List<Column>)colsField.get(t); + + Column srcCol = null; + Column destCol = new BogusColumn(t); + for(int i = 0; i < cols.size(); ++i) { + srcCol = cols.get(i); + if(srcCol.getName().equals(colName)) { + cols.set(i, destCol); + break; + } + } + + // copy fields from source to dest + for(Field f : Column.class.getDeclaredFields()) { + if(!Modifier.isFinal(f.getModifiers())) { + f.setAccessible(true); + f.set(destCol, f.get(srcCol)); + } + } + + } + + private static class BogusColumn extends Column + { + private BogusColumn(Table table) { + super(true, table); + } + + @Override + public Object read(byte[] data, ByteOrder order) throws IOException { + throw new IOException("bogus column"); + } + } + +} diff --git a/test/src/java/com/healthmarketscience/jackcess/TableTest.java b/test/src/java/com/healthmarketscience/jackcess/TableTest.java index de8de5c..3190846 100644 --- a/test/src/java/com/healthmarketscience/jackcess/TableTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/TableTest.java @@ -168,7 +168,7 @@ public class TableTest extends TestCase { } private Column newTestColumn() { - return new Column(true) { + return new Column(true, null) { @Override public Table getTable() { return _testTable; |