You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ErrorHandlerTest.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. Copyright (c) 2007 Health Market Science, Inc.
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  14. USA
  15. You can contact Health Market Science at info@healthmarketscience.com
  16. or at the following address:
  17. Health Market Science
  18. 2700 Horizon Drive
  19. Suite 200
  20. King of Prussia, PA 19406
  21. */
  22. package com.healthmarketscience.jackcess;
  23. import java.io.IOException;
  24. import java.lang.reflect.Field;
  25. import java.lang.reflect.Modifier;
  26. import java.nio.ByteOrder;
  27. import java.util.List;
  28. import junit.framework.TestCase;
  29. import static com.healthmarketscience.jackcess.DatabaseTest.*;
  30. /**
  31. * @author James Ahlborn
  32. */
  33. public class ErrorHandlerTest extends TestCase
  34. {
  35. public ErrorHandlerTest(String name) {
  36. super(name);
  37. }
  38. public void testErrorHandler() throws Exception
  39. {
  40. Database db = create();
  41. Table table =
  42. new TableBuilder("test")
  43. .addColumn(new ColumnBuilder("col", DataType.TEXT).toColumn())
  44. .addColumn(new ColumnBuilder("val", DataType.LONG).toColumn())
  45. .toTable(db);
  46. table.addRow("row1", 1);
  47. table.addRow("row2", 2);
  48. table.addRow("row3", 3);
  49. assertTable(createExpectedTable(
  50. createExpectedRow("col", "row1",
  51. "val", 1),
  52. createExpectedRow("col", "row2",
  53. "val", 2),
  54. createExpectedRow("col", "row3",
  55. "val", 3)),
  56. table);
  57. replaceColumn(table, "val");
  58. table.reset();
  59. try {
  60. table.getNextRow();
  61. fail("IOException should have been thrown");
  62. } catch(IOException e) {
  63. // success
  64. }
  65. table.reset();
  66. table.setErrorHandler(new ReplacementErrorHandler());
  67. assertTable(createExpectedTable(
  68. createExpectedRow("col", "row1",
  69. "val", null),
  70. createExpectedRow("col", "row2",
  71. "val", null),
  72. createExpectedRow("col", "row3",
  73. "val", null)),
  74. table);
  75. Cursor c1 = Cursor.createCursor(table);
  76. Cursor c2 = Cursor.createCursor(table);
  77. Cursor c3 = Cursor.createCursor(table);
  78. c2.setErrorHandler(new DebugErrorHandler("#error"));
  79. c3.setErrorHandler(Database.DEFAULT_ERROR_HANDLER);
  80. assertCursor(createExpectedTable(
  81. createExpectedRow("col", "row1",
  82. "val", null),
  83. createExpectedRow("col", "row2",
  84. "val", null),
  85. createExpectedRow("col", "row3",
  86. "val", null)),
  87. c1);
  88. assertCursor(createExpectedTable(
  89. createExpectedRow("col", "row1",
  90. "val", "#error"),
  91. createExpectedRow("col", "row2",
  92. "val", "#error"),
  93. createExpectedRow("col", "row3",
  94. "val", "#error")),
  95. c2);
  96. try {
  97. c3.getNextRow();
  98. fail("IOException should have been thrown");
  99. } catch(IOException e) {
  100. // success
  101. }
  102. table.setErrorHandler(null);
  103. c1.setErrorHandler(null);
  104. c1.reset();
  105. try {
  106. c1.getNextRow();
  107. fail("IOException should have been thrown");
  108. } catch(IOException e) {
  109. // success
  110. }
  111. db.close();
  112. }
  113. @SuppressWarnings("unchecked")
  114. private void replaceColumn(Table t, String colName) throws Exception
  115. {
  116. Field colsField = Table.class.getDeclaredField("_columns");
  117. colsField.setAccessible(true);
  118. List<Column> cols = (List<Column>)colsField.get(t);
  119. Column srcCol = null;
  120. Column destCol = new BogusColumn(t);
  121. for(int i = 0; i < cols.size(); ++i) {
  122. srcCol = cols.get(i);
  123. if(srcCol.getName().equals(colName)) {
  124. cols.set(i, destCol);
  125. break;
  126. }
  127. }
  128. // copy fields from source to dest
  129. for(Field f : Column.class.getDeclaredFields()) {
  130. if(!Modifier.isFinal(f.getModifiers())) {
  131. f.setAccessible(true);
  132. f.set(destCol, f.get(srcCol));
  133. }
  134. }
  135. }
  136. private static class BogusColumn extends Column
  137. {
  138. private BogusColumn(Table table) {
  139. super(true, table);
  140. }
  141. @Override
  142. public Object read(byte[] data, ByteOrder order) throws IOException {
  143. throw new IOException("bogus column");
  144. }
  145. }
  146. }