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.

TableTest.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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;
  14. import java.io.IOException;
  15. import java.nio.ByteBuffer;
  16. import java.nio.charset.Charset;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.List;
  20. import java.util.TimeZone;
  21. import com.healthmarketscience.jackcess.impl.ColumnImpl;
  22. import com.healthmarketscience.jackcess.impl.JetFormat;
  23. import com.healthmarketscience.jackcess.impl.PageChannel;
  24. import com.healthmarketscience.jackcess.impl.TableImpl;
  25. import junit.framework.TestCase;
  26. /**
  27. * @author Tim McCune
  28. */
  29. public class TableTest extends TestCase {
  30. private final PageChannel _pageChannel = new PageChannel(true) {};
  31. private List<ColumnImpl> _columns = new ArrayList<ColumnImpl>();
  32. private TestTable _testTable;
  33. private int _varLenIdx;
  34. private int _fixedOffset;
  35. public TableTest(String name) {
  36. super(name);
  37. }
  38. private void reset() {
  39. _testTable = null;
  40. _columns = new ArrayList<ColumnImpl>();
  41. _varLenIdx = 0;
  42. _fixedOffset = 0;
  43. }
  44. public void testCreateRow() throws Exception {
  45. reset();
  46. newTestColumn(DataType.INT, false);
  47. newTestColumn(DataType.TEXT, false);
  48. newTestColumn(DataType.TEXT, false);
  49. newTestTable();
  50. int colCount = _columns.size();
  51. ByteBuffer buffer = createRow(9, "Tim", "McCune");
  52. assertEquals((short) colCount, buffer.getShort());
  53. assertEquals((short) 9, buffer.getShort());
  54. assertEquals((byte) 'T', buffer.get());
  55. assertEquals((short) 22, buffer.getShort(22));
  56. assertEquals((short) 10, buffer.getShort(24));
  57. assertEquals((short) 4, buffer.getShort(26));
  58. assertEquals((short) 2, buffer.getShort(28));
  59. assertEquals((byte) 7, buffer.get(30));
  60. }
  61. public void testUnicodeCompression() throws Exception {
  62. reset();
  63. newTestColumn(DataType.TEXT, false);
  64. newTestColumn(DataType.TEXT, false);
  65. newTestTable();
  66. String small = "this is a string";
  67. String smallNotAscii = "this is a string\0";
  68. String large = TestUtil.createString(30);
  69. String largeNotAscii = large + "\0";
  70. ByteBuffer[] buf1 = encodeColumns(small, large);
  71. ByteBuffer[] buf2 = encodeColumns(smallNotAscii, largeNotAscii);
  72. reset();
  73. newTestColumn(DataType.TEXT, true);
  74. newTestColumn(DataType.TEXT, true);
  75. newTestTable();
  76. ByteBuffer[] bufCmp1 = encodeColumns(small, large);
  77. ByteBuffer[] bufCmp2 = encodeColumns(smallNotAscii, largeNotAscii);
  78. assertEquals(buf1[0].remaining(),
  79. (bufCmp1[0].remaining() + small.length() - 2));
  80. assertEquals(buf1[1].remaining(),
  81. (bufCmp1[1].remaining() + large.length() - 2));
  82. for(int i = 0; i < buf2.length; ++i) {
  83. assertTrue(Arrays.equals(toBytes(buf2[i]), toBytes(bufCmp2[i])));
  84. }
  85. assertEquals(Arrays.asList(small, large),
  86. Arrays.asList(decodeColumns(bufCmp1)));
  87. assertEquals(Arrays.asList(smallNotAscii, largeNotAscii),
  88. Arrays.asList(decodeColumns(bufCmp2)));
  89. }
  90. private ByteBuffer createRow(Object... row)
  91. throws IOException
  92. {
  93. return _testTable.createRow(row);
  94. }
  95. private ByteBuffer[] encodeColumns(Object... row)
  96. throws IOException
  97. {
  98. ByteBuffer[] result = new ByteBuffer[_columns.size()];
  99. for(int i = 0; i < _columns.size(); ++i) {
  100. ColumnImpl col = _columns.get(i);
  101. result[i] = col.write(row[i], _testTable.getFormat().MAX_ROW_SIZE);
  102. }
  103. return result;
  104. }
  105. private Object[] decodeColumns(ByteBuffer[] buffers)
  106. throws IOException
  107. {
  108. Object[] result = new Object[_columns.size()];
  109. for(int i = 0; i < _columns.size(); ++i) {
  110. ColumnImpl col = _columns.get(i);
  111. result[i] = col.read(toBytes(buffers[i]));
  112. }
  113. return result;
  114. }
  115. private static byte[] toBytes(ByteBuffer buffer) {
  116. buffer.rewind();
  117. byte[] b = new byte[buffer.remaining()];
  118. buffer.get(b);
  119. return b;
  120. }
  121. private TableImpl newTestTable()
  122. throws Exception
  123. {
  124. _testTable = new TestTable();
  125. return _testTable;
  126. }
  127. private void newTestColumn(DataType type, final boolean compressedUnicode) {
  128. int nextColIdx = _columns.size();
  129. int nextVarLenIdx = 0;
  130. int nextFixedOff = 0;
  131. if(type.isVariableLength()) {
  132. nextVarLenIdx = _varLenIdx++;
  133. } else {
  134. nextFixedOff = _fixedOffset;
  135. _fixedOffset += type.getFixedSize();
  136. }
  137. ColumnImpl col = new ColumnImpl(null, null, type, nextColIdx, nextFixedOff,
  138. nextVarLenIdx) {
  139. @Override
  140. public TableImpl getTable() {
  141. return _testTable;
  142. }
  143. @Override
  144. public JetFormat getFormat() {
  145. return getTable().getFormat();
  146. }
  147. @Override
  148. public PageChannel getPageChannel() {
  149. return getTable().getPageChannel();
  150. }
  151. @Override
  152. protected Charset getCharset() {
  153. return getFormat().CHARSET;
  154. }
  155. @Override
  156. public TimeZone getTimeZone() {
  157. return TimeZone.getDefault();
  158. }
  159. @Override
  160. public boolean isCompressedUnicode() {
  161. return compressedUnicode;
  162. }
  163. };
  164. _columns.add(col);
  165. }
  166. private class TestTable extends TableImpl {
  167. private TestTable() throws IOException {
  168. super(true, _columns);
  169. }
  170. public ByteBuffer createRow(Object... row) throws IOException {
  171. return super.createRow(row, getPageChannel().createPageBuffer());
  172. }
  173. @Override
  174. public PageChannel getPageChannel() {
  175. return _pageChannel;
  176. }
  177. @Override
  178. public JetFormat getFormat() {
  179. return JetFormat.VERSION_4;
  180. }
  181. }
  182. }