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 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. Copyright (c) 2007 Health Market Science, Inc.
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  14. USA
  15. You can contact Health Market Science at info@healthmarketscience.com
  16. or at the following address:
  17. Health Market Science
  18. 2700 Horizon Drive
  19. Suite 200
  20. King of Prussia, PA 19406
  21. */
  22. package com.healthmarketscience.jackcess;
  23. import java.io.IOException;
  24. import java.nio.ByteBuffer;
  25. import java.nio.charset.Charset;
  26. import java.util.ArrayList;
  27. import java.util.Arrays;
  28. import java.util.List;
  29. import java.util.TimeZone;
  30. import junit.framework.TestCase;
  31. /**
  32. * @author Tim McCune
  33. */
  34. public class TableTest extends TestCase {
  35. private final PageChannel _pageChannel = new PageChannel(true);
  36. private List<Column> _columns = new ArrayList<Column>();
  37. private Table _testTable;
  38. public TableTest(String name) {
  39. super(name);
  40. }
  41. public void testCreateRow() throws Exception {
  42. Column col = newTestColumn();
  43. col.setType(DataType.INT);
  44. _columns.add(col);
  45. col = newTestColumn();
  46. col.setType(DataType.TEXT);
  47. _columns.add(col);
  48. col = newTestColumn();
  49. col.setType(DataType.TEXT);
  50. _columns.add(col);
  51. newTestTable();
  52. int colCount = _columns.size();
  53. ByteBuffer buffer = createRow(9, "Tim", "McCune");
  54. assertEquals((short) colCount, buffer.getShort());
  55. assertEquals((short) 9, buffer.getShort());
  56. assertEquals((byte) 'T', buffer.get());
  57. assertEquals((short) 22, buffer.getShort(22));
  58. assertEquals((short) 10, buffer.getShort(24));
  59. assertEquals((short) 4, buffer.getShort(26));
  60. assertEquals((short) 2, buffer.getShort(28));
  61. assertEquals((byte) 7, buffer.get(30));
  62. }
  63. public void testUnicodeCompression() throws Exception {
  64. Column col = newTestColumn();
  65. col = newTestColumn();
  66. col.setType(DataType.TEXT);
  67. _columns.add(col);
  68. col = newTestColumn();
  69. col.setType(DataType.MEMO);
  70. _columns.add(col);
  71. newTestTable();
  72. String small = "this is a string";
  73. String smallNotAscii = "this is a string\0";
  74. String large = DatabaseTest.createString(30);
  75. String largeNotAscii = large + "\0";
  76. ByteBuffer[] buf1 = encodeColumns(small, large);
  77. ByteBuffer[] buf2 = encodeColumns(smallNotAscii, largeNotAscii);
  78. for(Column tmp : _columns) {
  79. tmp.setCompressedUnicode(true);
  80. }
  81. ByteBuffer[] bufCmp1 = encodeColumns(small, large);
  82. ByteBuffer[] bufCmp2 = encodeColumns(smallNotAscii, largeNotAscii);
  83. assertEquals(buf1[0].remaining(),
  84. (bufCmp1[0].remaining() + small.length() - 2));
  85. assertEquals(buf1[1].remaining(),
  86. (bufCmp1[1].remaining() + large.length() - 2));
  87. for(int i = 0; i < buf2.length; ++i) {
  88. assertTrue(Arrays.equals(toBytes(buf2[i]), toBytes(bufCmp2[i])));
  89. }
  90. assertEquals(Arrays.asList(small, large),
  91. Arrays.asList(decodeColumns(bufCmp1)));
  92. assertEquals(Arrays.asList(smallNotAscii, largeNotAscii),
  93. Arrays.asList(decodeColumns(bufCmp2)));
  94. }
  95. private ByteBuffer createRow(Object... row)
  96. throws IOException
  97. {
  98. return _testTable.createRow(
  99. row, _testTable.getPageChannel().createPageBuffer());
  100. }
  101. private ByteBuffer[] encodeColumns(Object... row)
  102. throws IOException
  103. {
  104. ByteBuffer[] result = new ByteBuffer[_columns.size()];
  105. for(int i = 0; i < _columns.size(); ++i) {
  106. Column col = _columns.get(i);
  107. result[i] = col.write(row[i], _testTable.getFormat().MAX_ROW_SIZE);
  108. }
  109. return result;
  110. }
  111. private Object[] decodeColumns(ByteBuffer[] buffers)
  112. throws IOException
  113. {
  114. Object[] result = new Object[_columns.size()];
  115. for(int i = 0; i < _columns.size(); ++i) {
  116. Column col = _columns.get(i);
  117. result[i] = col.read(toBytes(buffers[i]));
  118. }
  119. return result;
  120. }
  121. private static byte[] toBytes(ByteBuffer buffer) {
  122. buffer.rewind();
  123. byte[] b = new byte[buffer.remaining()];
  124. buffer.get(b);
  125. return b;
  126. }
  127. private Table newTestTable()
  128. throws Exception
  129. {
  130. _testTable = new Table(true, _columns) {
  131. @Override
  132. public PageChannel getPageChannel() {
  133. return _pageChannel;
  134. }
  135. @Override
  136. public JetFormat getFormat() {
  137. return JetFormat.VERSION_4;
  138. }
  139. };
  140. return _testTable;
  141. }
  142. private Column newTestColumn() {
  143. return new Column(true, null) {
  144. @Override
  145. public Table getTable() {
  146. return _testTable;
  147. }
  148. @Override
  149. public JetFormat getFormat() {
  150. return getTable().getFormat();
  151. }
  152. @Override
  153. public PageChannel getPageChannel() {
  154. return getTable().getPageChannel();
  155. }
  156. @Override
  157. protected Charset getCharset() {
  158. return getFormat().CHARSET;
  159. }
  160. @Override
  161. protected TimeZone getTimeZone() {
  162. return TimeZone.getDefault();
  163. }
  164. };
  165. }
  166. }