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.

AutoNumberTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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.IOException;
  15. import java.util.Collections;
  16. import java.util.List;
  17. import java.util.Map;
  18. import java.util.UUID;
  19. import com.healthmarketscience.jackcess.Column;
  20. import com.healthmarketscience.jackcess.ColumnBuilder;
  21. import com.healthmarketscience.jackcess.CursorBuilder;
  22. import com.healthmarketscience.jackcess.DataType;
  23. import com.healthmarketscience.jackcess.Database;
  24. import static com.healthmarketscience.jackcess.Database.*;
  25. import com.healthmarketscience.jackcess.Row;
  26. import com.healthmarketscience.jackcess.Table;
  27. import com.healthmarketscience.jackcess.TableBuilder;
  28. import static com.healthmarketscience.jackcess.TestUtil.*;
  29. import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey;
  30. import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
  31. import junit.framework.TestCase;
  32. /**
  33. *
  34. * @author James Ahlborn
  35. */
  36. public class AutoNumberTest extends TestCase
  37. {
  38. public AutoNumberTest(String name) throws Exception {
  39. super(name);
  40. }
  41. public void testAutoNumber() throws Exception
  42. {
  43. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  44. Database db = createMem(fileFormat);
  45. Table table = new TableBuilder("test")
  46. .addColumn(new ColumnBuilder("a", DataType.LONG)
  47. .setAutoNumber(true))
  48. .addColumn(new ColumnBuilder("b", DataType.TEXT))
  49. .toTable(db);
  50. doTestAutoNumber(table);
  51. db.close();
  52. }
  53. }
  54. public void testAutoNumberPK() throws Exception
  55. {
  56. for (final TestDB testDB : SUPPORTED_DBS_TEST) {
  57. Database db = openMem(testDB);
  58. Table table = db.getTable("Table3");
  59. doTestAutoNumber(table);
  60. db.close();
  61. }
  62. }
  63. private static void doTestAutoNumber(Table table) throws Exception
  64. {
  65. Object[] row = {null, "row1"};
  66. assertSame(row, table.addRow(row));
  67. assertEquals(1, ((Integer)row[0]).intValue());
  68. row = table.addRow(13, "row2");
  69. assertEquals(2, ((Integer)row[0]).intValue());
  70. row = table.addRow("flubber", "row3");
  71. assertEquals(3, ((Integer)row[0]).intValue());
  72. table.reset();
  73. row = table.addRow(Column.AUTO_NUMBER, "row4");
  74. assertEquals(4, ((Integer)row[0]).intValue());
  75. row = table.addRow(Column.AUTO_NUMBER, "row5");
  76. assertEquals(5, ((Integer)row[0]).intValue());
  77. Object[] smallRow = {Column.AUTO_NUMBER};
  78. row = table.addRow(smallRow);
  79. assertNotSame(row, smallRow);
  80. assertEquals(6, ((Integer)row[0]).intValue());
  81. table.reset();
  82. List<? extends Map<String, Object>> expectedRows =
  83. createExpectedTable(
  84. createExpectedRow(
  85. "a", 1,
  86. "b", "row1"),
  87. createExpectedRow(
  88. "a", 2,
  89. "b", "row2"),
  90. createExpectedRow(
  91. "a", 3,
  92. "b", "row3"),
  93. createExpectedRow(
  94. "a", 4,
  95. "b", "row4"),
  96. createExpectedRow(
  97. "a", 5,
  98. "b", "row5"),
  99. createExpectedRow(
  100. "a", 6,
  101. "b", null));
  102. assertTable(expectedRows, table);
  103. }
  104. public void testAutoNumberGuid() throws Exception
  105. {
  106. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  107. Database db = createMem(fileFormat);
  108. Table table = new TableBuilder("test")
  109. .addColumn(new ColumnBuilder("a", DataType.GUID)
  110. .setAutoNumber(true))
  111. .addColumn(new ColumnBuilder("b", DataType.TEXT))
  112. .toTable(db);
  113. Object[] row = {null, "row1"};
  114. assertSame(row, table.addRow(row));
  115. assertTrue(ColumnImpl.isGUIDValue(row[0]));
  116. row = table.addRow(13, "row2");
  117. assertTrue(ColumnImpl.isGUIDValue(row[0]));
  118. row = table.addRow("flubber", "row3");
  119. assertTrue(ColumnImpl.isGUIDValue(row[0]));
  120. Object[] smallRow = {Column.AUTO_NUMBER};
  121. row = table.addRow(smallRow);
  122. assertNotSame(row, smallRow);
  123. assertTrue(ColumnImpl.isGUIDValue(row[0]));
  124. db.close();
  125. }
  126. }
  127. public void testInsertLongAutoNumber() throws Exception
  128. {
  129. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  130. Database db = createMem(fileFormat);
  131. Table table = new TableBuilder("test")
  132. .addColumn(new ColumnBuilder("a", DataType.LONG)
  133. .setAutoNumber(true))
  134. .addColumn(new ColumnBuilder("b", DataType.TEXT))
  135. .toTable(db);
  136. doTestInsertLongAutoNumber(table);
  137. db.close();
  138. }
  139. }
  140. public void testInsertLongAutoNumberPK() throws Exception
  141. {
  142. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  143. Database db = createMem(fileFormat);
  144. Table table = new TableBuilder("test")
  145. .addColumn(new ColumnBuilder("a", DataType.LONG)
  146. .setAutoNumber(true))
  147. .addColumn(new ColumnBuilder("b", DataType.TEXT))
  148. .setPrimaryKey("a")
  149. .toTable(db);
  150. doTestInsertLongAutoNumber(table);
  151. db.close();
  152. }
  153. }
  154. private static void doTestInsertLongAutoNumber(Table table) throws Exception
  155. {
  156. assertFalse(table.getDatabase().isAllowAutoNumberInsert());
  157. assertFalse(table.isAllowAutoNumberInsert());
  158. Object[] row = {null, "row1"};
  159. assertSame(row, table.addRow(row));
  160. assertEquals(1, ((Integer)row[0]).intValue());
  161. row = table.addRow(13, "row2");
  162. assertEquals(2, ((Integer)row[0]).intValue());
  163. row = table.addRow("flubber", "row3");
  164. assertEquals(3, ((Integer)row[0]).intValue());
  165. table.reset();
  166. table.setAllowAutoNumberInsert(true);
  167. assertFalse(table.getDatabase().isAllowAutoNumberInsert());
  168. assertTrue(table.isAllowAutoNumberInsert());
  169. Row row2 = CursorBuilder.findRow(
  170. table, Collections.singletonMap("a", 2));
  171. assertEquals("row2", row2.getString("b"));
  172. table.deleteRow(row2);
  173. row = table.addRow(Column.AUTO_NUMBER, "row4");
  174. assertEquals(4, ((Integer)row[0]).intValue());
  175. assertEquals(4, ((TableImpl)table).getLastLongAutoNumber());
  176. row = table.addRow(2, "row2-redux");
  177. assertEquals(2, ((Integer)row[0]).intValue());
  178. assertEquals(4, ((TableImpl)table).getLastLongAutoNumber());
  179. row2 = CursorBuilder.findRow(
  180. table, Collections.singletonMap("a", 2));
  181. assertEquals("row2-redux", row2.getString("b"));
  182. row = table.addRow(13, "row13-mindthegap");
  183. assertEquals(13, ((Integer)row[0]).intValue());
  184. assertEquals(13, ((TableImpl)table).getLastLongAutoNumber());
  185. try {
  186. table.addRow("not a number", "nope");
  187. fail("NumberFormatException should have been thrown");
  188. } catch(NumberFormatException e) {
  189. // success
  190. }
  191. assertEquals(13, ((TableImpl)table).getLastLongAutoNumber());
  192. try {
  193. table.addRow(-10, "uh-uh");
  194. fail("IOException should have been thrown");
  195. } catch(IOException e) {
  196. // success
  197. }
  198. row = table.addRow(Column.AUTO_NUMBER, "row14");
  199. assertEquals(14, ((Integer)row[0]).intValue());
  200. Row row13 = CursorBuilder.findRow(
  201. table, Collections.singletonMap("a", 13));
  202. assertEquals("row13-mindthegap", row13.getString("b"));
  203. row13.put("a", "45");
  204. row13 = table.updateRow(row13);
  205. assertEquals(45, row13.get("a"));
  206. assertEquals(45, ((TableImpl)table).getLastLongAutoNumber());
  207. row13.put("a", -1);
  208. try {
  209. table.updateRow(row13);
  210. fail("IOException should have been thrown");
  211. } catch(IOException e) {
  212. // success
  213. }
  214. assertEquals(45, ((TableImpl)table).getLastLongAutoNumber());
  215. row13.put("a", 55);
  216. table.setAllowAutoNumberInsert(null);
  217. row13 = table.updateRow(row13);
  218. assertEquals(45, row13.get("a"));
  219. assertEquals(45, ((TableImpl)table).getLastLongAutoNumber());
  220. }
  221. public void testInsertComplexAutoNumber() throws Exception
  222. {
  223. for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMPLEX)) {
  224. Database db = openMem(testDB);
  225. Table t1 = db.getTable("Table1");
  226. assertFalse(t1.isAllowAutoNumberInsert());
  227. int lastAutoNum = ((TableImpl)t1).getLastComplexTypeAutoNumber();
  228. Object[] row = t1.addRow("arow");
  229. ++lastAutoNum;
  230. checkAllComplexAutoNums(lastAutoNum, row);
  231. assertEquals(lastAutoNum, ((TableImpl)t1).getLastComplexTypeAutoNumber());
  232. db.setAllowAutoNumberInsert(true);
  233. assertTrue(db.isAllowAutoNumberInsert());
  234. assertTrue(t1.isAllowAutoNumberInsert());
  235. row = t1.addRow("anotherrow");
  236. ++lastAutoNum;
  237. checkAllComplexAutoNums(lastAutoNum, row);
  238. assertEquals(lastAutoNum, ((TableImpl)t1).getLastComplexTypeAutoNumber());
  239. row = t1.addRow("row5", 5, null, null, 5, 5);
  240. checkAllComplexAutoNums(5, row);
  241. assertEquals(lastAutoNum, ((TableImpl)t1).getLastComplexTypeAutoNumber());
  242. row = t1.addRow("row13", 13, null, null, 13, 13);
  243. checkAllComplexAutoNums(13, row);
  244. assertEquals(13, ((TableImpl)t1).getLastComplexTypeAutoNumber());
  245. try {
  246. t1.addRow("nope", "not a number");
  247. fail("NumberFormatException should have been thrown");
  248. } catch(NumberFormatException e) {
  249. // success
  250. }
  251. assertEquals(13, ((TableImpl)t1).getLastComplexTypeAutoNumber());
  252. try {
  253. t1.addRow("uh-uh", -10);
  254. fail("IOException should have been thrown");
  255. } catch(IOException e) {
  256. // success
  257. }
  258. assertEquals(13, ((TableImpl)t1).getLastComplexTypeAutoNumber());
  259. try {
  260. t1.addRow("wut", 6, null, null, 40, 42);
  261. fail("IOException should have been thrown");
  262. } catch(IOException e) {
  263. // success
  264. }
  265. row = t1.addRow("morerows");
  266. checkAllComplexAutoNums(14, row);
  267. assertEquals(14, ((TableImpl)t1).getLastComplexTypeAutoNumber());
  268. Row row13 = CursorBuilder.findRow(
  269. t1, Collections.singletonMap("id", "row13"));
  270. row13.put("VersionHistory_F5F8918F-0A3F-4DA9-AE71-184EE5012880", "45");
  271. row13.put("multi-value-data", "45");
  272. row13.put("attach-data", "45");
  273. row13 = t1.updateRow(row13);
  274. checkAllComplexAutoNums(45, row13);
  275. assertEquals(45, ((TableImpl)t1).getLastComplexTypeAutoNumber());
  276. row13.put("attach-data", -1);
  277. try {
  278. t1.updateRow(row13);
  279. fail("IOException should have been thrown");
  280. } catch(IOException e) {
  281. // success
  282. }
  283. assertEquals(45, ((TableImpl)t1).getLastComplexTypeAutoNumber());
  284. row13.put("attach-data", 55);
  285. try {
  286. t1.updateRow(row13);
  287. fail("IOException should have been thrown");
  288. } catch(IOException e) {
  289. // success
  290. }
  291. assertEquals(45, ((TableImpl)t1).getLastComplexTypeAutoNumber());
  292. row13.put("VersionHistory_F5F8918F-0A3F-4DA9-AE71-184EE5012880", 55);
  293. row13.put("multi-value-data", 55);
  294. db.setAllowAutoNumberInsert(null);
  295. row13 = t1.updateRow(row13);
  296. checkAllComplexAutoNums(45, row13);
  297. assertEquals(45, ((TableImpl)t1).getLastComplexTypeAutoNumber());
  298. db.close();
  299. }
  300. }
  301. private static void checkAllComplexAutoNums(int expected, Object[] row)
  302. {
  303. assertEquals(expected, ((ComplexValueForeignKey)row[1]).get());
  304. assertEquals(expected, ((ComplexValueForeignKey)row[4]).get());
  305. assertEquals(expected, ((ComplexValueForeignKey)row[5]).get());
  306. }
  307. private static void checkAllComplexAutoNums(int expected, Row row)
  308. {
  309. assertEquals(expected, ((Number)row.get("VersionHistory_F5F8918F-0A3F-4DA9-AE71-184EE5012880")).intValue());
  310. assertEquals(expected, ((Number)row.get("multi-value-data")).intValue());
  311. assertEquals(expected, ((Number)row.get("attach-data")).intValue());
  312. }
  313. public void testInsertGuidAutoNumber() throws Exception
  314. {
  315. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  316. Database db = createMem(fileFormat);
  317. Table table = new TableBuilder("test")
  318. .addColumn(new ColumnBuilder("a", DataType.GUID)
  319. .setAutoNumber(true))
  320. .addColumn(new ColumnBuilder("b", DataType.TEXT))
  321. .toTable(db);
  322. db.setAllowAutoNumberInsert(true);
  323. table.setAllowAutoNumberInsert(false);
  324. assertFalse(table.isAllowAutoNumberInsert());
  325. Object[] row = {null, "row1"};
  326. assertSame(row, table.addRow(row));
  327. assertTrue(ColumnImpl.isGUIDValue(row[0]));
  328. row = table.addRow(13, "row2");
  329. assertTrue(ColumnImpl.isGUIDValue(row[0]));
  330. row = table.addRow("flubber", "row3");
  331. assertTrue(ColumnImpl.isGUIDValue(row[0]));
  332. Object[] smallRow = {Column.AUTO_NUMBER};
  333. row = table.addRow(smallRow);
  334. assertNotSame(row, smallRow);
  335. assertTrue(ColumnImpl.isGUIDValue(row[0]));
  336. table.setAllowAutoNumberInsert(null);
  337. assertTrue(table.isAllowAutoNumberInsert());
  338. Row row2 = CursorBuilder.findRow(
  339. table, Collections.singletonMap("b", "row2"));
  340. assertEquals("row2", row2.getString("b"));
  341. String row2Guid = row2.getString("a");
  342. table.deleteRow(row2);
  343. row = table.addRow(Column.AUTO_NUMBER, "row4");
  344. assertTrue(ColumnImpl.isGUIDValue(row[0]));
  345. row = table.addRow(row2Guid, "row2-redux");
  346. assertEquals(row2Guid, row[0]);
  347. row2 = CursorBuilder.findRow(
  348. table, Collections.singletonMap("a", row2Guid));
  349. assertEquals("row2-redux", row2.getString("b"));
  350. try {
  351. table.addRow("not a guid", "nope");
  352. fail("IOException should have been thrown");
  353. } catch(IOException e) {
  354. // success
  355. }
  356. row = table.addRow(Column.AUTO_NUMBER, "row5");
  357. assertTrue(ColumnImpl.isGUIDValue(row[0]));
  358. row2Guid = UUID.randomUUID().toString();
  359. row2.put("a", row2Guid);
  360. row2 = table.updateRow(row2);
  361. assertEquals(row2Guid, row2.get("a"));
  362. row2.put("a", "not a guid");
  363. try {
  364. table.updateRow(row2);
  365. fail("IOException should have been thrown");
  366. } catch(IOException e) {
  367. // success
  368. }
  369. table.setAllowAutoNumberInsert(false);
  370. row2 = table.updateRow(row2);
  371. assertTrue(ColumnImpl.isGUIDValue(row2.get("a")));
  372. assertFalse(row2Guid.equals(row2.get("a")));
  373. db.close();
  374. }
  375. }
  376. }