Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ComplexColumnTest.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. Copyright (c) 2011 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;
  14. import java.nio.ByteBuffer;
  15. import java.util.Arrays;
  16. import java.util.Date;
  17. import java.util.List;
  18. import com.healthmarketscience.jackcess.complex.Attachment;
  19. import com.healthmarketscience.jackcess.complex.ComplexDataType;
  20. import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey;
  21. import com.healthmarketscience.jackcess.complex.SingleValue;
  22. import com.healthmarketscience.jackcess.complex.UnsupportedValue;
  23. import com.healthmarketscience.jackcess.complex.Version;
  24. import com.healthmarketscience.jackcess.impl.ByteUtil;
  25. import com.healthmarketscience.jackcess.impl.ColumnImpl;
  26. import com.healthmarketscience.jackcess.impl.PageChannel;
  27. import junit.framework.TestCase;
  28. import static com.healthmarketscience.jackcess.TestUtil.*;
  29. import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
  30. /**
  31. *
  32. * @author James Ahlborn
  33. */
  34. @SuppressWarnings("deprecation")
  35. public class ComplexColumnTest extends TestCase
  36. {
  37. public ComplexColumnTest(String name) {
  38. super(name);
  39. }
  40. public void testVersions() throws Exception
  41. {
  42. for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMPLEX)) {
  43. Database db = openCopy(testDB);
  44. db.setDateTimeType(DateTimeType.DATE);
  45. db.setTimeZone(TEST_TZ);
  46. Table t1 = db.getTable("Table1");
  47. Column col = t1.getColumn("append-memo-data");
  48. assertTrue(col.isAppendOnly());
  49. Column verCol = col.getVersionHistoryColumn();
  50. assertNotNull(verCol);
  51. assertEquals(ComplexDataType.VERSION_HISTORY,
  52. verCol.getComplexInfo().getType());
  53. for(Row row : t1) {
  54. String rowId = row.getString("id");
  55. ComplexValueForeignKey complexValueFk =
  56. (ComplexValueForeignKey)verCol.getRowValue(row);
  57. String curValue = (String)col.getRowValue(row);
  58. if(rowId.equals("row1")) {
  59. checkVersions(1, complexValueFk, curValue);
  60. } else if(rowId.equals("row2")) {
  61. checkVersions(2, complexValueFk, curValue,
  62. "row2-memo", new Date(1315876862334L));
  63. } else if(rowId.equals("row3")) {
  64. checkVersions(3, complexValueFk, curValue,
  65. "row3-memo-again", new Date(1315876965382L),
  66. "row3-memo-revised", new Date(1315876953077L),
  67. "row3-memo", new Date(1315876879126L));
  68. } else if(rowId.equals("row4")) {
  69. checkVersions(4, complexValueFk, curValue,
  70. "row4-memo", new Date(1315876945758L));
  71. } else {
  72. assertTrue(false);
  73. }
  74. }
  75. Object[] row8 = {"row8", Column.AUTO_NUMBER, "some-data", "row8-memo",
  76. Column.AUTO_NUMBER, Column.AUTO_NUMBER};
  77. t1.addRow(row8);
  78. ComplexValueForeignKey row8ValFk = (ComplexValueForeignKey)
  79. verCol.getRowValue(row8);
  80. Date upTime = new Date();
  81. row8ValFk.addVersion("row8-memo", upTime);
  82. checkVersions(row8ValFk.get(), row8ValFk, "row8-memo",
  83. "row8-memo", upTime);
  84. Cursor cursor = CursorBuilder.createCursor(t1);
  85. assertTrue(cursor.findFirstRow(t1.getColumn("id"), "row3"));
  86. ComplexValueForeignKey row3ValFk = (ComplexValueForeignKey)
  87. cursor.getCurrentRowValue(verCol);
  88. cursor.setCurrentRowValue(col, "new-value");
  89. Version v = row3ValFk.addVersion("new-value", upTime);
  90. checkVersions(3, row3ValFk, "new-value",
  91. "new-value", upTime,
  92. "row3-memo-again", new Date(1315876965382L),
  93. "row3-memo-revised", new Date(1315876953077L),
  94. "row3-memo", new Date(1315876879126L));
  95. try {
  96. v.update();
  97. fail("UnsupportedOperationException should have been thrown");
  98. } catch(UnsupportedOperationException expected) {
  99. // success
  100. }
  101. checkVersions(3, row3ValFk, "new-value",
  102. "new-value", upTime,
  103. "row3-memo-again", new Date(1315876965382L),
  104. "row3-memo-revised", new Date(1315876953077L),
  105. "row3-memo", new Date(1315876879126L));
  106. try {
  107. v.delete();
  108. fail("UnsupportedOperationException should have been thrown");
  109. } catch(UnsupportedOperationException expected) {
  110. // success
  111. }
  112. checkVersions(3, row3ValFk, "new-value",
  113. "new-value", upTime,
  114. "row3-memo-again", new Date(1315876965382L),
  115. "row3-memo-revised", new Date(1315876953077L),
  116. "row3-memo", new Date(1315876879126L));
  117. try {
  118. v.getComplexValueForeignKey().deleteAllValues();
  119. fail("UnsupportedOperationException should have been thrown");
  120. } catch(UnsupportedOperationException expected) {
  121. // success
  122. }
  123. checkVersions(3, row3ValFk, "new-value",
  124. "new-value", upTime,
  125. "row3-memo-again", new Date(1315876965382L),
  126. "row3-memo-revised", new Date(1315876953077L),
  127. "row3-memo", new Date(1315876879126L));
  128. db.close();
  129. }
  130. }
  131. public void testAttachments() throws Exception
  132. {
  133. for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMPLEX)) {
  134. Database db = openCopy(testDB);
  135. Table t1 = db.getTable("Table1");
  136. Column col = t1.getColumn("attach-data");
  137. assertEquals(ComplexDataType.ATTACHMENT,
  138. col.getComplexInfo().getType());
  139. for(Row row : t1) {
  140. String rowId = row.getString("id");
  141. ComplexValueForeignKey complexValueFk =
  142. (ComplexValueForeignKey)col.getRowValue(row);
  143. if(rowId.equals("row1")) {
  144. checkAttachments(1, complexValueFk);
  145. } else if(rowId.equals("row2")) {
  146. checkAttachments(2, complexValueFk, "test_data.txt", "test_data2.txt");
  147. } else if(rowId.equals("row3")) {
  148. checkAttachments(3, complexValueFk);
  149. } else if(rowId.equals("row4")) {
  150. checkAttachments(4, complexValueFk, "test_data2.txt");
  151. } else {
  152. assertTrue(false);
  153. }
  154. }
  155. Object[] row8 = {"row8", Column.AUTO_NUMBER, "some-data", "row8-memo",
  156. Column.AUTO_NUMBER, Column.AUTO_NUMBER};
  157. t1.addRow(row8);
  158. ComplexValueForeignKey row8ValFk = (ComplexValueForeignKey)
  159. col.getRowValue(row8);
  160. row8ValFk.addAttachment(null, "test_data.txt", "txt",
  161. getFileBytes("test_data.txt"), (Date)null, null);
  162. checkAttachments(row8ValFk.get(), row8ValFk, "test_data.txt");
  163. row8ValFk.addEncodedAttachment(null, "test_data2.txt", "txt",
  164. getEncodedFileBytes("test_data2.txt"),
  165. (Date)null, null);
  166. checkAttachments(row8ValFk.get(), row8ValFk, "test_data.txt",
  167. "test_data2.txt");
  168. Cursor cursor = CursorBuilder.createCursor(t1);
  169. assertTrue(cursor.findFirstRow(t1.getColumn("id"), "row4"));
  170. ComplexValueForeignKey row4ValFk = (ComplexValueForeignKey)
  171. cursor.getCurrentRowValue(col);
  172. Attachment a = row4ValFk.addAttachment(null, "test_data.txt", "txt",
  173. getFileBytes("test_data.txt"),
  174. (Date)null, null);
  175. checkAttachments(4, row4ValFk, "test_data2.txt", "test_data.txt");
  176. a.setFileType("zip");
  177. a.setFileName("some_data.zip");
  178. byte[] newBytes = "this is not a zip file".getBytes("US-ASCII");
  179. a.setFileData(newBytes);
  180. a.update();
  181. Attachment updated = row4ValFk.getAttachments().get(1);
  182. assertNotSame(updated, a);
  183. assertEquals("zip", updated.getFileType());
  184. assertEquals("some_data.zip", updated.getFileName());
  185. assertTrue(Arrays.equals(newBytes, updated.getFileData()));
  186. byte[] encBytes = updated.getEncodedFileData();
  187. assertEquals(newBytes.length + 28, encBytes.length);
  188. ByteBuffer bb = PageChannel.wrap(encBytes);
  189. assertEquals(0, bb.getInt());
  190. assertTrue(ByteUtil.matchesRange(bb, 28, newBytes));
  191. updated.delete();
  192. checkAttachments(4, row4ValFk, "test_data2.txt");
  193. row4ValFk.getAttachments().get(0).delete();
  194. checkAttachments(4, row4ValFk);
  195. assertTrue(cursor.findFirstRow(t1.getColumn("id"), "row2"));
  196. ComplexValueForeignKey row2ValFk = (ComplexValueForeignKey)
  197. cursor.getCurrentRowValue(col);
  198. row2ValFk.deleteAllValues();
  199. checkAttachments(2, row2ValFk);
  200. db.close();
  201. }
  202. }
  203. public void testMultiValues() throws Exception
  204. {
  205. for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMPLEX)) {
  206. Database db = openCopy(testDB);
  207. Table t1 = db.getTable("Table1");
  208. Column col = t1.getColumn("multi-value-data");
  209. assertEquals(ComplexDataType.MULTI_VALUE,
  210. col.getComplexInfo().getType());
  211. for(Row row : t1) {
  212. String rowId = row.getString("id");
  213. ComplexValueForeignKey complexValueFk =
  214. (ComplexValueForeignKey)col.getRowValue(row);
  215. if(rowId.equals("row1")) {
  216. checkMultiValues(1, complexValueFk);
  217. } else if(rowId.equals("row2")) {
  218. checkMultiValues(2, complexValueFk, "value1", "value4");
  219. } else if(rowId.equals("row3")) {
  220. checkMultiValues(3, complexValueFk,
  221. "value1", "value2", "value3", "value4");
  222. } else if(rowId.equals("row4")) {
  223. checkMultiValues(4, complexValueFk);
  224. } else {
  225. assertTrue(false);
  226. }
  227. }
  228. Object[] row8 = {"row8", Column.AUTO_NUMBER, "some-data", "row8-memo",
  229. Column.AUTO_NUMBER, Column.AUTO_NUMBER};
  230. t1.addRow(row8);
  231. ComplexValueForeignKey row8ValFk = (ComplexValueForeignKey)
  232. col.getRowValue(row8);
  233. row8ValFk.addMultiValue("value1");
  234. row8ValFk.addMultiValue("value2");
  235. checkMultiValues(row8ValFk.get(), row8ValFk, "value1", "value2");
  236. Cursor cursor = CursorBuilder.createCursor(t1);
  237. assertTrue(cursor.findFirstRow(t1.getColumn("id"), "row2"));
  238. ComplexValueForeignKey row2ValFk = (ComplexValueForeignKey)
  239. cursor.getCurrentRowValue(col);
  240. SingleValue v = row2ValFk.addMultiValue("value2");
  241. row2ValFk.addMultiValue("value3");
  242. checkMultiValues(2, row2ValFk, "value1", "value4", "value2", "value3");
  243. v.set("value5");
  244. v.update();
  245. checkMultiValues(2, row2ValFk, "value1", "value4", "value5", "value3");
  246. v.delete();
  247. checkMultiValues(2, row2ValFk, "value1", "value4", "value3");
  248. row2ValFk.getMultiValues().get(0).delete();
  249. checkMultiValues(2, row2ValFk, "value4", "value3");
  250. row2ValFk.getMultiValues().get(1).delete();
  251. checkMultiValues(2, row2ValFk, "value4");
  252. row2ValFk.getMultiValues().get(0).delete();
  253. checkMultiValues(2, row2ValFk);
  254. assertTrue(cursor.findFirstRow(t1.getColumn("id"), "row3"));
  255. ComplexValueForeignKey row3ValFk = (ComplexValueForeignKey)
  256. cursor.getCurrentRowValue(col);
  257. row3ValFk.deleteAllValues();
  258. checkMultiValues(3, row3ValFk);
  259. // test multi-value col props
  260. PropertyMap props = col.getProperties();
  261. assertEquals(Boolean.TRUE, props.getValue(PropertyMap.ALLOW_MULTI_VALUE_PROP));
  262. assertEquals("Value List", props.getValue(PropertyMap.ROW_SOURCE_TYPE_PROP));
  263. assertEquals("\"value1\";\"value2\";\"value3\";\"value4\"",
  264. props.getValue(PropertyMap.ROW_SOURCE_PROP));
  265. db.close();
  266. }
  267. }
  268. public void testUnsupported() throws Exception
  269. {
  270. for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.UNSUPPORTED)) {
  271. Database db = openCopy(testDB);
  272. Table t1 = db.getTable("Test");
  273. Column col = t1.getColumn("UnknownComplex");
  274. assertEquals(ComplexDataType.UNSUPPORTED,
  275. col.getComplexInfo().getType());
  276. for(Row row : t1) {
  277. Integer rowId = row.getInt("ID");
  278. ComplexValueForeignKey complexValueFk =
  279. (ComplexValueForeignKey)col.getRowValue(row);
  280. if(rowId.equals(1)) {
  281. checkUnsupportedValues(1, complexValueFk,
  282. "RawData[(5) FF FE 62 61 7A]");
  283. } else if(rowId.equals(2)) {
  284. checkUnsupportedValues(2, complexValueFk, "RawData[(5) FF FE 66 6F 6F]", "RawData[(5) FF FE 62 61 7A]");
  285. } else if(rowId.equals(3)) {
  286. checkUnsupportedValues(3, complexValueFk);
  287. } else {
  288. assertTrue(false);
  289. }
  290. }
  291. db.close();
  292. }
  293. }
  294. private static void checkVersions(
  295. int cValId, ComplexValueForeignKey complexValueFk,
  296. String curValue, Object... versionInfos)
  297. throws Exception
  298. {
  299. assertEquals(cValId, complexValueFk.get());
  300. List<Version> versions = complexValueFk.getVersions();
  301. if(versionInfos.length == 0) {
  302. assertTrue(versions.isEmpty());
  303. assertNull(curValue);
  304. } else {
  305. assertEquals(versionInfos.length / 2, versions.size());
  306. assertEquals(curValue, versions.get(0).getValue());
  307. for(int i = 0; i < versionInfos.length; i+=2) {
  308. String value = (String)versionInfos[i];
  309. Date modDate = (Date)versionInfos[i+1];
  310. Version v = versions.get(i/2);
  311. assertEquals(value, v.getValue());
  312. assertSameDate(modDate, v.getModifiedDate());
  313. }
  314. }
  315. }
  316. private static void checkAttachments(
  317. int cValId, ComplexValueForeignKey complexValueFk,
  318. String... fileNames)
  319. throws Exception
  320. {
  321. assertEquals(cValId, complexValueFk.get());
  322. List<Attachment> attachments = complexValueFk.getAttachments();
  323. if(fileNames.length == 0) {
  324. assertTrue(attachments.isEmpty());
  325. } else {
  326. assertEquals(fileNames.length, attachments.size());
  327. for(int i = 0; i < fileNames.length; ++i) {
  328. String fname = fileNames[i];
  329. Attachment a = attachments.get(i);
  330. assertEquals(fname, a.getFileName());
  331. assertEquals("txt", a.getFileType());
  332. assertTrue(Arrays.equals(getFileBytes(fname), a.getFileData()));
  333. assertTrue(Arrays.equals(getEncodedFileBytes(fname),
  334. a.getEncodedFileData()));
  335. }
  336. }
  337. }
  338. private static void checkMultiValues(
  339. int cValId, ComplexValueForeignKey complexValueFk,
  340. Object... expectedValues)
  341. throws Exception
  342. {
  343. assertEquals(cValId, complexValueFk.get());
  344. List<SingleValue> values = complexValueFk.getMultiValues();
  345. if(expectedValues.length == 0) {
  346. assertTrue(values.isEmpty());
  347. } else {
  348. assertEquals(expectedValues.length, values.size());
  349. for(int i = 0; i < expectedValues.length; ++i) {
  350. Object value = expectedValues[i];
  351. SingleValue v = values.get(i);
  352. assertEquals(value, v.get());
  353. }
  354. }
  355. }
  356. private static void checkUnsupportedValues(
  357. int cValId, ComplexValueForeignKey complexValueFk,
  358. String... expectedValues)
  359. throws Exception
  360. {
  361. assertEquals(cValId, complexValueFk.get());
  362. List<UnsupportedValue> values = complexValueFk.getUnsupportedValues();
  363. if(expectedValues.length == 0) {
  364. assertTrue(values.isEmpty());
  365. } else {
  366. assertEquals(expectedValues.length, values.size());
  367. for(int i = 0; i < expectedValues.length; ++i) {
  368. String value = expectedValues[i];
  369. UnsupportedValue v = values.get(i);
  370. assertEquals(1, v.getValues().size());
  371. Object rv = v.get("Value");
  372. assertTrue(ColumnImpl.isRawData(rv));
  373. assertEquals(value, rv.toString());
  374. }
  375. }
  376. }
  377. private static byte[] getFileBytes(String fname) throws Exception
  378. {
  379. if("test_data.txt".equals(fname)) {
  380. return TEST_BYTES;
  381. }
  382. if("test_data2.txt".equals(fname)) {
  383. return TEST2_BYTES;
  384. }
  385. throw new RuntimeException("unexpected bytes");
  386. }
  387. private static byte[] getEncodedFileBytes(String fname) throws Exception
  388. {
  389. if("test_data.txt".equals(fname)) {
  390. return TEST_ENC_BYTES;
  391. }
  392. if("test_data2.txt".equals(fname)) {
  393. return TEST2_ENC_BYTES;
  394. }
  395. throw new RuntimeException("unexpected bytes");
  396. }
  397. private static byte b(int i) { return (byte)i; }
  398. private static byte[] getAsciiBytes(String str) {
  399. try {
  400. return str.getBytes("US-ASCII");
  401. } catch(Exception e) {
  402. throw new RuntimeException(e);
  403. }
  404. }
  405. private static final byte[] TEST_ENC_BYTES = new byte[] {
  406. b(0x01),b(0x00),b(0x00),b(0x00),b(0x3A),b(0x00),b(0x00),b(0x00),b(0x78),b(0x5E),b(0x13),b(0x61),b(0x60),b(0x60),b(0x60),b(0x04),b(0x62),b(0x16),b(0x20),b(0x2E),b(0x61),b(0xA8),b(0x00),b(0x62),
  407. b(0x20),b(0x9D),b(0x91),b(0x59),b(0xAC),b(0x00),b(0x44),b(0xC5),b(0xF9),b(0xB9),b(0xA9),b(0x0A),b(0x25),b(0xA9),b(0xC5),b(0x25),b(0x0A),b(0x29),b(0x89),b(0x25),b(0x89),b(0x0A),b(0x69),b(0xF9),
  408. b(0x45),b(0x0A),b(0x89),b(0x25),b(0x25),b(0x89),b(0xC9),b(0x19),b(0xB9),b(0xA9),b(0x79),b(0x25),b(0x7A),b(0x00),b(0x52),b(0xA9),b(0x0F),b(0x7A)
  409. };
  410. private static final byte[] TEST_BYTES = getAsciiBytes("this is some test data for attachment.");
  411. private static final byte[] TEST2_ENC_BYTES = new byte[] {
  412. b(0x01),b(0x00),b(0x00),b(0x00),b(0x3F),b(0x00),b(0x00),b(0x00),b(0x78),b(0x5E),b(0x13),b(0x61),b(0x60),b(0x60),b(0x60),b(0x04),b(0x62),b(0x16),b(0x20),b(0x2E),b(0x61),b(0xA8),b(0x00),b(0x62),
  413. b(0x20),b(0x9D),b(0x91),b(0x59),b(0xAC),b(0x00),b(0x44),b(0xC5),b(0xF9),b(0xB9),b(0xA9),b(0x0A),b(0xB9),b(0xF9),b(0x45),b(0xA9),b(0x0A),b(0x25),b(0xA9),b(0xC5),b(0x25),b(0x0A),b(0x29),b(0x89),
  414. b(0x25),b(0x89),b(0x0A),b(0x69),b(0xF9),b(0x45),b(0x0A),b(0x89),b(0x25),b(0x25),b(0x89),b(0xC9),b(0x19),b(0xB9),b(0xA9),b(0x79),b(0x25),b(0x7A),b(0x00),b(0xA5),b(0x0B),b(0x11),b(0x4D)
  415. };
  416. private static final byte[] TEST2_BYTES = getAsciiBytes("this is some more test data for attachment.");
  417. }