summaryrefslogtreecommitdiffstats
path: root/test/src/java/com/healthmarketscience/jackcess
diff options
context:
space:
mode:
authorJames Ahlborn <jtahlborn@yahoo.com>2008-07-22 01:35:04 +0000
committerJames Ahlborn <jtahlborn@yahoo.com>2008-07-22 01:35:04 +0000
commitd1359adfbf6199a305b062c0a8da0d05e49e84a7 (patch)
tree44f0b120ad5a87d55fb3764741cf8af949670a9a /test/src/java/com/healthmarketscience/jackcess
parent050db7f0f79f0fa33220042814fcb61b538ba8dd (diff)
downloadjackcess-d1359adfbf6199a305b062c0a8da0d05e49e84a7.tar.gz
jackcess-d1359adfbf6199a305b062c0a8da0d05e49e84a7.zip
Add primitive support for writing unicode compressed text columns.
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@364 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'test/src/java/com/healthmarketscience/jackcess')
-rw-r--r--test/src/java/com/healthmarketscience/jackcess/TableTest.java135
1 files changed, 110 insertions, 25 deletions
diff --git a/test/src/java/com/healthmarketscience/jackcess/TableTest.java b/test/src/java/com/healthmarketscience/jackcess/TableTest.java
index 85b6bf7..de8de5c 100644
--- a/test/src/java/com/healthmarketscience/jackcess/TableTest.java
+++ b/test/src/java/com/healthmarketscience/jackcess/TableTest.java
@@ -27,8 +27,10 @@ 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;
@@ -37,37 +39,30 @@ 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 {
- final JetFormat format = JetFormat.VERSION_4;
- final PageChannel pageChannel = new PageChannel(true);
- List<Column> columns = new ArrayList<Column>();
- Column col = newTestColumn(pageChannel);
+ Column col = newTestColumn();
col.setType(DataType.INT);
- columns.add(col);
- col = newTestColumn(pageChannel);
+ _columns.add(col);
+ col = newTestColumn();
col.setType(DataType.TEXT);
- columns.add(col);
- col = newTestColumn(pageChannel);
+ _columns.add(col);
+ col = newTestColumn();
col.setType(DataType.TEXT);
- columns.add(col);
- Table table = new Table(true, columns) {
- @Override
- public PageChannel getPageChannel() {
- return pageChannel;
- }
- };
- int colCount = 3;
- Object[] row = new Object[colCount];
- row[0] = new Short((short) 9);
- row[1] = "Tim";
- row[2] = "McCune";
- ByteBuffer buffer = table.createRow(row, format.MAX_ROW_SIZE,
- pageChannel.createPageBuffer());
+ _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());
@@ -78,17 +73,107 @@ public class TableTest extends TestCase {
assertEquals((byte) 7, buffer.get(30));
}
- private static Column newTestColumn(final PageChannel pageChannel) {
- return new Column(true) {
+ 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;
+ 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;
+ }
+ };
}
}