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.

AutoNumberTest.java 14KB

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