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.

LongValueTest.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. Copyright (c) 2015 James Ahlborn
  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.impl;
  14. import java.io.File;
  15. import java.nio.ByteBuffer;
  16. import java.sql.Types;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.Map;
  22. import com.healthmarketscience.jackcess.ColumnBuilder;
  23. import com.healthmarketscience.jackcess.DataType;
  24. import com.healthmarketscience.jackcess.Database;
  25. import static com.healthmarketscience.jackcess.Database.*;
  26. import com.healthmarketscience.jackcess.Row;
  27. import com.healthmarketscience.jackcess.Table;
  28. import com.healthmarketscience.jackcess.TableBuilder;
  29. import static com.healthmarketscience.jackcess.TestUtil.*;
  30. import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
  31. import junit.framework.TestCase;
  32. /**
  33. *
  34. * @author James Ahlborn
  35. */
  36. public class LongValueTest extends TestCase
  37. {
  38. public LongValueTest(String name) throws Exception {
  39. super(name);
  40. }
  41. public void testReadLongValue() throws Exception {
  42. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.TEST2, true)) {
  43. Database db = openMem(testDB);
  44. Table table = db.getTable("MSP_PROJECTS");
  45. Row row = table.getNextRow();
  46. assertEquals("Jon Iles this is a a vawesrasoih aksdkl fas dlkjflkasjd flkjaslkdjflkajlksj dfl lkasjdf lkjaskldfj lkas dlk lkjsjdfkl; aslkdf lkasjkldjf lka skldf lka sdkjfl;kasjd falksjdfljaslkdjf laskjdfk jalskjd flkj aslkdjflkjkjasljdflkjas jf;lkasjd fjkas dasdf asd fasdf asdf asdmhf lksaiyudfoi jasodfj902384jsdf9 aw90se fisajldkfj lkasj dlkfslkd jflksjadf as", row.get("PROJ_PROP_AUTHOR"));
  47. assertEquals("T", row.get("PROJ_PROP_COMPANY"));
  48. assertEquals("Standard", row.get("PROJ_INFO_CAL_NAME"));
  49. assertEquals("Project1", row.get("PROJ_PROP_TITLE"));
  50. byte[] foundBinaryData = row.getBytes("RESERVED_BINARY_DATA");
  51. byte[] expectedBinaryData =
  52. toByteArray(new File("src/test/data/test2BinData.dat"));
  53. assertTrue(Arrays.equals(expectedBinaryData, foundBinaryData));
  54. db.close();
  55. }
  56. }
  57. public void testWriteLongValue() throws Exception {
  58. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  59. Database db = createMem(fileFormat);
  60. Table table =
  61. new TableBuilder("test")
  62. .addColumn(new ColumnBuilder("A", DataType.TEXT))
  63. .addColumn(new ColumnBuilder("B", DataType.MEMO))
  64. .addColumn(new ColumnBuilder("C", DataType.OLE))
  65. .toTable(db);
  66. String testStr = "This is a test";
  67. String longMemo = createString(2030);
  68. byte[] oleValue = toByteArray(new File("src/test/data/test2BinData.dat"));
  69. table.addRow(testStr, testStr, null);
  70. table.addRow(testStr, longMemo, oleValue);
  71. table.addRow("", "", new byte[0]);
  72. table.addRow(null, null, null);
  73. table.reset();
  74. Row row = table.getNextRow();
  75. assertEquals(testStr, row.get("A"));
  76. assertEquals(testStr, row.get("B"));
  77. assertNull(row.get("C"));
  78. row = table.getNextRow();
  79. assertEquals(testStr, row.get("A"));
  80. assertEquals(longMemo, row.get("B"));
  81. assertTrue(Arrays.equals(oleValue, row.getBytes("C")));
  82. row = table.getNextRow();
  83. assertEquals("", row.get("A"));
  84. assertEquals("", row.get("B"));
  85. assertTrue(Arrays.equals(new byte[0], row.getBytes("C")));
  86. row = table.getNextRow();
  87. assertNull(row.get("A"));
  88. assertNull(row.get("B"));
  89. assertNull(row.getBytes("C"));
  90. db.close();
  91. }
  92. }
  93. public void testManyMemos() throws Exception {
  94. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  95. Database db = createMem(fileFormat);
  96. final int numColumns = 126;
  97. TableBuilder bigTableBuilder = new TableBuilder("test");
  98. for (int i = 0; i < numColumns; i++)
  99. {
  100. bigTableBuilder.addColumn(new ColumnBuilder("column_" + i, DataType.MEMO));
  101. }
  102. Table bigTable = bigTableBuilder.toTable(db);
  103. List<Object[]> expectedRows = new ArrayList<Object[]>();
  104. for (int j = 0; j < 3; j++)
  105. {
  106. Object[] rowData = new String[numColumns];
  107. for (int i = 0; i < numColumns; i++)
  108. {
  109. rowData[i] = "v_" + i + ";" + (j + 999);
  110. }
  111. expectedRows.add(rowData);
  112. bigTable.addRow(rowData);
  113. }
  114. String extra1 = createString(100);
  115. String extra2 = createString(2050);
  116. for (int j = 0; j < 1; j++)
  117. {
  118. Object[] rowData = new String[numColumns];
  119. for (int i = 0; i < numColumns; i++)
  120. {
  121. rowData[i] = "v_" + i + ";" + (j + 999) + extra2;
  122. }
  123. expectedRows.add(rowData);
  124. bigTable.addRow(rowData);
  125. }
  126. for (int j = 0; j < 2; j++)
  127. {
  128. Object[] rowData = new String[numColumns];
  129. for (int i = 0; i < numColumns; i++)
  130. {
  131. String tmp = "v_" + i + ";" + (j + 999);
  132. if((i % 3) == 0) {
  133. tmp += extra1;
  134. } else if((i % 7) == 0) {
  135. tmp += extra2;
  136. }
  137. rowData[i] = tmp;
  138. }
  139. expectedRows.add(rowData);
  140. bigTable.addRow(rowData);
  141. }
  142. bigTable.reset();
  143. Iterator<Object[]> expIter = expectedRows.iterator();
  144. for(Map<?,?> row : bigTable) {
  145. Object[] expectedRow = expIter.next();
  146. assertEquals(Arrays.asList(expectedRow),
  147. new ArrayList<Object>(row.values()));
  148. }
  149. db.close();
  150. }
  151. }
  152. public void testLongValueAsMiddleColumn() throws Exception
  153. {
  154. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  155. Database db = createMem(fileFormat);
  156. Table newTable = new TableBuilder("NewTable")
  157. .addColumn(new ColumnBuilder("a").setSQLType(Types.INTEGER))
  158. .addColumn(new ColumnBuilder("b").setSQLType(Types.LONGVARCHAR))
  159. .addColumn(new ColumnBuilder("c").setSQLType(Types.VARCHAR))
  160. .toTable(db);
  161. String lval = createString(2000); // "--2000 chars long text--";
  162. String tval = createString(40); // "--40chars long text--";
  163. newTable.addRow(new Integer(1), lval, tval);
  164. newTable = db.getTable("NewTable");
  165. Map<String, Object> readRow = newTable.getNextRow();
  166. assertEquals(new Integer(1), readRow.get("a"));
  167. assertEquals(lval, readRow.get("b"));
  168. assertEquals(tval, readRow.get("c"));
  169. db.close();
  170. }
  171. }
  172. public void testUnicodeCompression() throws Exception
  173. {
  174. File dbFile = new File("src/test/data/V2003/testUnicodeCompV2003.mdb");
  175. Database db = open(Database.FileFormat.V2003, new File("src/test/data/V2003/testUnicodeCompV2003.mdb"), true);
  176. StringBuilder sb = new StringBuilder(127);
  177. for(int i = 1; i <= 0xFF; ++i) {
  178. sb.append((char)i);
  179. }
  180. String longStr = sb.toString();
  181. String[] expectedStrs = {
  182. "only ascii chars",
  183. "\u00E4\u00E4kk\u00F6si\u00E4",
  184. "\u041C\u0438\u0440",
  185. "\u03F0\u03B1\u1F76 \u03C4\u1F79\u03C4' \u1F10\u03B3\u1F7C \u039A\u1F7B\u03F0\u03BB\u03C9\u03C0\u03B1",
  186. "\u6F22\u5B57\u4EEE\u540D\u4EA4\u3058\u308A\u6587",
  187. "3L9\u001D52\u0002_AB(\u00A5\u0005!!V",
  188. "\u00FCmlaut",
  189. longStr
  190. };
  191. Table t = db.getTable("Table");
  192. for(Row row : t) {
  193. int id = (Integer)row.get("ID");
  194. String str = (String)row.get("Unicode");
  195. assertEquals(expectedStrs[id-1], str);
  196. }
  197. ColumnImpl col = (ColumnImpl)t.getColumn("Unicode");
  198. ByteBuffer bb = col.write(longStr, 1000);
  199. assertEquals(longStr.length() + 2, bb.remaining());
  200. byte[] bytes = new byte[bb.remaining()];
  201. bb.get(bytes);
  202. assertEquals(longStr, col.read(bytes));
  203. longStr = longStr.replace('a', '\u0440');
  204. bb = col.write(longStr, 1000);
  205. assertEquals(longStr.length() * 2, bb.remaining());
  206. bytes = new byte[bb.remaining()];
  207. bb.get(bytes);
  208. assertEquals(longStr, col.read(bytes));
  209. db.close();
  210. }
  211. }