aboutsummaryrefslogtreecommitdiffstats
path: root/test/src/java/com/healthmarketscience/jackcess/TableTest.java
blob: de8de5cf1af5456a5397ebd2fbaf834c6c40f451 (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
/*
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.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import junit.framework.TestCase;

/**
 * @author Tim McCune
 */
public class TableTest extends TestCase {

  private final PageChannel _pageChannel = new PageChannel(true);
  private List<Column> _columns = new ArrayList<Column>();
  private Table _testTable;
  
  public TableTest(String name) {
    super(name);
  }
  
  public void testCreateRow() throws Exception {
    Column col = newTestColumn();
    col.setType(DataType.INT);
    _columns.add(col);
    col = newTestColumn();
    col.setType(DataType.TEXT);
    _columns.add(col);
    col = newTestColumn();
    col.setType(DataType.TEXT);
    _columns.add(col);
    newTestTable();
    
    int colCount = _columns.size();
    ByteBuffer buffer = createRow(9, "Tim", "McCune");

    assertEquals((short) colCount, buffer.getShort());
    assertEquals((short) 9, buffer.getShort());
    assertEquals((byte) 'T', buffer.get());
    assertEquals((short) 22, buffer.getShort(22));
    assertEquals((short) 10, buffer.getShort(24));
    assertEquals((short) 4, buffer.getShort(26));
    assertEquals((short) 2, buffer.getShort(28));
    assertEquals((byte) 7, buffer.get(30));
  }

  public void testUnicodeCompression() throws Exception {
    Column col = newTestColumn();
    col = newTestColumn();
    col.setType(DataType.TEXT);
    _columns.add(col);
    col = newTestColumn();
    col.setType(DataType.MEMO);
    _columns.add(col);
    newTestTable();

    String small = "this is a string";
    String smallNotAscii = "this is a string\0";
    String large = DatabaseTest.createString(30);
    String largeNotAscii = large + "\0";

    ByteBuffer[] buf1 = encodeColumns(small, large);
    ByteBuffer[] buf2 = encodeColumns(smallNotAscii, largeNotAscii);

    for(Column tmp : _columns) {
      tmp.setCompressedUnicode(true);
    }
    
    ByteBuffer[] bufCmp1 = encodeColumns(small, large);
    ByteBuffer[] bufCmp2 = encodeColumns(smallNotAscii, largeNotAscii);
    
    assertEquals(buf1[0].remaining(),
                 (bufCmp1[0].remaining() + small.length() - 2));
    assertEquals(buf1[1].remaining(),
                 (bufCmp1[1].remaining() + large.length() - 2));

    for(int i = 0; i < buf2.length; ++i) {
      assertTrue(Arrays.equals(toBytes(buf2[i]), toBytes(bufCmp2[i])));
    }

    assertEquals(Arrays.asList(small, large),
                 Arrays.asList(decodeColumns(bufCmp1)));
    assertEquals(Arrays.asList(smallNotAscii, largeNotAscii),
                 Arrays.asList(decodeColumns(bufCmp2)));

  }

  private ByteBuffer createRow(Object... row) 
    throws IOException
  {
    return _testTable.createRow(
        row, _testTable.getFormat().MAX_ROW_SIZE,
        _testTable.getPageChannel().createPageBuffer());
  }

  private ByteBuffer[] encodeColumns(Object... row)
    throws IOException
  {
    ByteBuffer[] result = new ByteBuffer[_columns.size()];
    for(int i = 0; i < _columns.size(); ++i) {
      Column col = _columns.get(i);
      result[i] = col.write(row[i], _testTable.getFormat().MAX_ROW_SIZE);
    }
    return result;
  }

  private Object[] decodeColumns(ByteBuffer[] buffers)
    throws IOException
  {
    Object[] result = new Object[_columns.size()];
    for(int i = 0; i < _columns.size(); ++i) {
      Column col = _columns.get(i);
      result[i] = col.read(toBytes(buffers[i]));
    }
    return result;
  }

  private static byte[] toBytes(ByteBuffer buffer) {
    buffer.rewind();
    byte[] b = new byte[buffer.remaining()];
    buffer.get(b);
    return b;
  }

  private Table newTestTable() 
    throws Exception
  {
    _testTable = new Table(true, _columns) {
        @Override
        public PageChannel getPageChannel() {
          return _pageChannel;
        }
        @Override
        public JetFormat getFormat() {
          return JetFormat.VERSION_4;
        }
      };
    return _testTable;
  }

  private Column newTestColumn() {
    return new Column(true) {
        @Override
        public Table getTable() {
          return _testTable;
        }
      };
  }
  
}