aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/com/healthmarketscience/jackcess/util/ErrorHandlerTest.java
blob: dbc7f940292ae474aeafb04ba1eab21e5a3080dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
Copyright (c) 2007 Health Market Science, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.healthmarketscience.jackcess.util;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.ByteOrder;
import java.util.List;

import static com.healthmarketscience.jackcess.Database.*;
import com.healthmarketscience.jackcess.Column;
import com.healthmarketscience.jackcess.ColumnBuilder;
import com.healthmarketscience.jackcess.Cursor;
import com.healthmarketscience.jackcess.CursorBuilder;
import com.healthmarketscience.jackcess.DataType;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.Table;
import com.healthmarketscience.jackcess.TableBuilder;
import com.healthmarketscience.jackcess.impl.ColumnImpl;
import com.healthmarketscience.jackcess.impl.JetFormatTest;
import com.healthmarketscience.jackcess.impl.TableImpl;
import junit.framework.TestCase;
import static com.healthmarketscience.jackcess.TestUtil.*;

/**
 * @author James Ahlborn
 */
public class ErrorHandlerTest extends TestCase 
{

  public ErrorHandlerTest(String name) {
    super(name);
  }

  public void testErrorHandler() throws Exception
  {
    for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
      Database db = create(fileFormat);

      Table table =
        new TableBuilder("test")
        .addColumn(new ColumnBuilder("col", DataType.TEXT))
        .addColumn(new ColumnBuilder("val", DataType.LONG))
        .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 = CursorBuilder.createCursor(table);
      Cursor c2 = CursorBuilder.createCursor(table);
      Cursor c3 = CursorBuilder.createCursor(table);

      c2.setErrorHandler(new DebugErrorHandler("#error"));
      c3.setErrorHandler(ErrorHandler.DEFAULT);

      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 static void replaceColumn(Table t, String colName) throws Exception
  {
    Field colsField = TableImpl.class.getDeclaredField("_columns");
    colsField.setAccessible(true);
    List<Column> cols = (List<Column>)colsField.get(t);

    Column srcCol = null;
    ColumnImpl destCol = new BogusColumn(t, colName);
    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 ColumnImpl
  {
    private BogusColumn(Table table, String name) {
      super((TableImpl)table, name, DataType.LONG, 1, 0, 0);
    }
    
    @Override
    public Object read(byte[] data, ByteOrder order) throws IOException {
      throw new IOException("bogus column");
    }
  }

}