您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ErrorHandlerTest.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. Copyright (c) 2007 Health Market Science, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.util;
  14. import java.io.IOException;
  15. import java.lang.reflect.Field;
  16. import java.lang.reflect.Modifier;
  17. import java.nio.ByteOrder;
  18. import java.util.List;
  19. import static com.healthmarketscience.jackcess.Database.*;
  20. import com.healthmarketscience.jackcess.Column;
  21. import com.healthmarketscience.jackcess.ColumnBuilder;
  22. import com.healthmarketscience.jackcess.Cursor;
  23. import com.healthmarketscience.jackcess.CursorBuilder;
  24. import com.healthmarketscience.jackcess.DataType;
  25. import com.healthmarketscience.jackcess.Database;
  26. import com.healthmarketscience.jackcess.Table;
  27. import com.healthmarketscience.jackcess.TableBuilder;
  28. import com.healthmarketscience.jackcess.impl.ColumnImpl;
  29. import com.healthmarketscience.jackcess.impl.JetFormatTest;
  30. import com.healthmarketscience.jackcess.impl.TableImpl;
  31. import static com.healthmarketscience.jackcess.TestUtil.*;
  32. import static org.junit.jupiter.api.Assertions.*;
  33. import org.junit.jupiter.api.Test;
  34. /**
  35. * @author James Ahlborn
  36. */
  37. public class ErrorHandlerTest
  38. {
  39. @Test
  40. public void testErrorHandler() throws Exception
  41. {
  42. for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS)
  43. {
  44. try (final Database db = create(fileFormat))
  45. {
  46. final Table table
  47. = new TableBuilder("test")
  48. .addColumn(new ColumnBuilder("col", DataType.TEXT))
  49. .addColumn(new ColumnBuilder("val", DataType.LONG))
  50. .toTable(db);
  51. table.addRow("row1", 1);
  52. table.addRow("row2", 2);
  53. table.addRow("row3", 3);
  54. assertTable(createExpectedTable(createExpectedRow("col", "row1", "val", 1),
  55. createExpectedRow("col", "row2", "val", 2),
  56. createExpectedRow("col", "row3", "val", 3)),
  57. table);
  58. replaceColumn(table, "val");
  59. table.reset();
  60. assertThrows(IOException.class, () -> table.getNextRow());
  61. table.reset();
  62. table.setErrorHandler(new ReplacementErrorHandler());
  63. assertTable(createExpectedTable(createExpectedRow("col", "row1", "val", null),
  64. createExpectedRow("col", "row2", "val", null),
  65. createExpectedRow("col", "row3", "val", null)),
  66. table);
  67. Cursor c1 = CursorBuilder.createCursor(table);
  68. Cursor c2 = CursorBuilder.createCursor(table);
  69. Cursor c3 = CursorBuilder.createCursor(table);
  70. c2.setErrorHandler(new DebugErrorHandler("#error"));
  71. c3.setErrorHandler(ErrorHandler.DEFAULT);
  72. assertCursor(createExpectedTable(createExpectedRow("col", "row1", "val", null),
  73. createExpectedRow("col", "row2", "val", null),
  74. createExpectedRow("col", "row3", "val", null)),
  75. c1);
  76. assertCursor(createExpectedTable(createExpectedRow("col", "row1", "val", "#error"),
  77. createExpectedRow("col", "row2", "val", "#error"),
  78. createExpectedRow("col", "row3", "val", "#error")),
  79. c2);
  80. assertThrows(IOException.class, () -> c3.getNextRow());
  81. table.setErrorHandler(null);
  82. c1.setErrorHandler(null);
  83. c1.reset();
  84. assertThrows(IOException.class, () -> c1.getNextRow());
  85. }
  86. }
  87. }
  88. @SuppressWarnings("unchecked")
  89. private static void replaceColumn(Table t, String colName) throws Exception
  90. {
  91. Field colsField = TableImpl.class.getDeclaredField("_columns");
  92. colsField.setAccessible(true);
  93. List<Column> cols = (List<Column>)colsField.get(t);
  94. Column srcCol = null;
  95. ColumnImpl destCol = new BogusColumn(t, colName);
  96. for(int i = 0; i < cols.size(); ++i) {
  97. srcCol = cols.get(i);
  98. if(srcCol.getName().equals(colName)) {
  99. cols.set(i, destCol);
  100. break;
  101. }
  102. }
  103. // copy fields from source to dest
  104. for(Field f : Column.class.getDeclaredFields()) {
  105. if(!Modifier.isFinal(f.getModifiers())) {
  106. f.setAccessible(true);
  107. f.set(destCol, f.get(srcCol));
  108. }
  109. }
  110. }
  111. private static class BogusColumn extends ColumnImpl
  112. {
  113. private BogusColumn(Table table, String name) {
  114. super((TableImpl)table, name, DataType.LONG, 1, 0, 0);
  115. }
  116. @Override
  117. public Object read(byte[] data, ByteOrder order) throws IOException {
  118. throw new IOException("bogus column");
  119. }
  120. }
  121. }