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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.Database.*;
  30. import static com.healthmarketscience.jackcess.DatabaseTest.*;
  31. /**
  32. * @author James Ahlborn
  33. */
  34. public class ErrorHandlerTest extends TestCase
  35. {
  36. public ErrorHandlerTest(String name) {
  37. super(name);
  38. }
  39. public void testErrorHandler() throws Exception
  40. {
  41. for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
  42. Database db = create(fileFormat);
  43. Table table =
  44. new TableBuilder("test")
  45. .addColumn(new ColumnBuilder("col", DataType.TEXT).toColumn())
  46. .addColumn(new ColumnBuilder("val", DataType.LONG).toColumn())
  47. .toTable(db);
  48. table.addRow("row1", 1);
  49. table.addRow("row2", 2);
  50. table.addRow("row3", 3);
  51. assertTable(createExpectedTable(
  52. createExpectedRow("col", "row1",
  53. "val", 1),
  54. createExpectedRow("col", "row2",
  55. "val", 2),
  56. createExpectedRow("col", "row3",
  57. "val", 3)),
  58. table);
  59. replaceColumn(table, "val");
  60. table.reset();
  61. try {
  62. table.getNextRow();
  63. fail("IOException should have been thrown");
  64. } catch(IOException e) {
  65. // success
  66. }
  67. table.reset();
  68. table.setErrorHandler(new ReplacementErrorHandler());
  69. assertTable(createExpectedTable(
  70. createExpectedRow("col", "row1",
  71. "val", null),
  72. createExpectedRow("col", "row2",
  73. "val", null),
  74. createExpectedRow("col", "row3",
  75. "val", null)),
  76. table);
  77. Cursor c1 = Cursor.createCursor(table);
  78. Cursor c2 = Cursor.createCursor(table);
  79. Cursor c3 = Cursor.createCursor(table);
  80. c2.setErrorHandler(new DebugErrorHandler("#error"));
  81. c3.setErrorHandler(Database.DEFAULT_ERROR_HANDLER);
  82. assertCursor(createExpectedTable(
  83. createExpectedRow("col", "row1",
  84. "val", null),
  85. createExpectedRow("col", "row2",
  86. "val", null),
  87. createExpectedRow("col", "row3",
  88. "val", null)),
  89. c1);
  90. assertCursor(createExpectedTable(
  91. createExpectedRow("col", "row1",
  92. "val", "#error"),
  93. createExpectedRow("col", "row2",
  94. "val", "#error"),
  95. createExpectedRow("col", "row3",
  96. "val", "#error")),
  97. c2);
  98. try {
  99. c3.getNextRow();
  100. fail("IOException should have been thrown");
  101. } catch(IOException e) {
  102. // success
  103. }
  104. table.setErrorHandler(null);
  105. c1.setErrorHandler(null);
  106. c1.reset();
  107. try {
  108. c1.getNextRow();
  109. fail("IOException should have been thrown");
  110. } catch(IOException e) {
  111. // success
  112. }
  113. db.close();
  114. }
  115. }
  116. @SuppressWarnings("unchecked")
  117. private void replaceColumn(Table t, String colName) throws Exception
  118. {
  119. Field colsField = Table.class.getDeclaredField("_columns");
  120. colsField.setAccessible(true);
  121. List<Column> cols = (List<Column>)colsField.get(t);
  122. Column srcCol = null;
  123. Column destCol = new BogusColumn(t);
  124. for(int i = 0; i < cols.size(); ++i) {
  125. srcCol = cols.get(i);
  126. if(srcCol.getName().equals(colName)) {
  127. cols.set(i, destCol);
  128. break;
  129. }
  130. }
  131. // copy fields from source to dest
  132. for(Field f : Column.class.getDeclaredFields()) {
  133. if(!Modifier.isFinal(f.getModifiers())) {
  134. f.setAccessible(true);
  135. f.set(destCol, f.get(srcCol));
  136. }
  137. }
  138. }
  139. private static class BogusColumn extends Column
  140. {
  141. private BogusColumn(Table table) {
  142. super(true, table);
  143. }
  144. @Override
  145. public Object read(byte[] data, ByteOrder order) throws IOException {
  146. throw new IOException("bogus column");
  147. }
  148. }
  149. }