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.

TableUpdaterTest.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. Copyright (c) 2016 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.util.ArrayList;
  15. import java.util.Arrays;
  16. import java.util.LinkedHashMap;
  17. import java.util.List;
  18. import java.util.Map;
  19. import com.healthmarketscience.jackcess.Database.FileFormat;
  20. import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
  21. import com.healthmarketscience.jackcess.impl.DatabaseImpl;
  22. import com.healthmarketscience.jackcess.impl.TableImpl;
  23. import static com.healthmarketscience.jackcess.TestUtil.*;
  24. import static com.healthmarketscience.jackcess.DatabaseBuilder.*;
  25. import static org.junit.jupiter.api.Assertions.*;
  26. import org.junit.jupiter.api.Test;
  27. /**
  28. *
  29. * @author James Ahlborn
  30. */
  31. public class TableUpdaterTest
  32. {
  33. @Test
  34. public void testTableUpdating() throws Exception {
  35. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  36. Database db = create(fileFormat);
  37. doTestUpdating(db, false, true, null);
  38. db.close();
  39. }
  40. }
  41. @Test
  42. public void testTableUpdatingOneToOne() throws Exception {
  43. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  44. Database db = create(fileFormat);
  45. doTestUpdating(db, true, true, null);
  46. db.close();
  47. }
  48. }
  49. @Test
  50. public void testTableUpdatingNoEnforce() throws Exception {
  51. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  52. Database db = create(fileFormat);
  53. doTestUpdating(db, false, false, null);
  54. db.close();
  55. }
  56. }
  57. @Test
  58. public void testTableUpdatingNamedRelationship() throws Exception {
  59. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  60. Database db = create(fileFormat);
  61. doTestUpdating(db, false, true, "FKnun3jvv47l9kyl74h85y8a0if");
  62. db.close();
  63. }
  64. }
  65. private void doTestUpdating(Database db, boolean oneToOne, boolean enforce, String relationshipName)
  66. throws Exception
  67. {
  68. final Table t1 = newTable("TestTable")
  69. .addColumn(newColumn("id", DataType.LONG))
  70. .toTable(db);
  71. final Table t2 = newTable("TestTable2")
  72. .addColumn(newColumn("id2", DataType.LONG))
  73. .toTable(db);
  74. int t1idxs = 1;
  75. newPrimaryKey("id")
  76. .addToTable(t1);
  77. newColumn("data", DataType.TEXT)
  78. .addToTable(t1);
  79. newColumn("bigdata", DataType.MEMO)
  80. .addToTable(t1);
  81. newColumn("data2", DataType.TEXT)
  82. .addToTable(t2);
  83. newColumn("bigdata2", DataType.MEMO)
  84. .addToTable(t2);
  85. int t2idxs = 0;
  86. if(oneToOne) {
  87. ++t2idxs;
  88. newPrimaryKey("id2")
  89. .addToTable(t2);
  90. }
  91. RelationshipBuilder rb = newRelationship("TestTable", "TestTable2")
  92. .addColumns("id", "id2");
  93. if(enforce) {
  94. ++t1idxs;
  95. ++t2idxs;
  96. rb.setReferentialIntegrity()
  97. .setCascadeDeletes();
  98. }
  99. if (relationshipName != null) {
  100. rb.setName(relationshipName);
  101. }
  102. Relationship rel = rb.toRelationship(db);
  103. if (relationshipName == null) {
  104. assertEquals("TestTableTestTable2", rel.getName());
  105. } else {
  106. assertEquals(relationshipName, rel.getName());
  107. }
  108. assertSame(t1, rel.getFromTable());
  109. assertEquals(Arrays.asList(t1.getColumn("id")), rel.getFromColumns());
  110. assertSame(t2, rel.getToTable());
  111. assertEquals(Arrays.asList(t2.getColumn("id2")), rel.getToColumns());
  112. assertEquals(oneToOne, rel.isOneToOne());
  113. assertEquals(enforce, rel.hasReferentialIntegrity());
  114. assertEquals(enforce, rel.cascadeDeletes());
  115. assertFalse(rel.cascadeUpdates());
  116. assertEquals(Relationship.JoinType.INNER, rel.getJoinType());
  117. assertEquals(t1idxs, t1.getIndexes().size());
  118. assertEquals(1, ((TableImpl)t1).getIndexDatas().size());
  119. assertEquals(t2idxs, t2.getIndexes().size());
  120. assertEquals((t2idxs > 0 ? 1 : 0), ((TableImpl)t2).getIndexDatas().size());
  121. ((DatabaseImpl)db).getPageChannel().startWrite();
  122. try {
  123. for(int i = 0; i < 10; ++i) {
  124. t1.addRow(i, "row" + i, "row-data" + i);
  125. }
  126. for(int i = 0; i < 10; ++i) {
  127. t2.addRow(i, "row2_" + i, "row-data2_" + i);
  128. }
  129. } finally {
  130. ((DatabaseImpl)db).getPageChannel().finishWrite();
  131. }
  132. try {
  133. t2.addRow(10, "row10", "row-data10");
  134. if(enforce) {
  135. fail("ConstraintViolationException should have been thrown");
  136. }
  137. } catch(ConstraintViolationException cv) {
  138. // success
  139. if(!enforce) { throw cv; }
  140. }
  141. Row r1 = CursorBuilder.findRowByPrimaryKey(t1, 5);
  142. t1.deleteRow(r1);
  143. int id = 0;
  144. for(Row r : t1) {
  145. assertEquals(id, r.get("id"));
  146. ++id;
  147. if(id == 5) {
  148. ++id;
  149. }
  150. }
  151. id = 0;
  152. for(Row r : t2) {
  153. assertEquals(id, r.get("id2"));
  154. ++id;
  155. if(enforce && (id == 5)) {
  156. ++id;
  157. }
  158. }
  159. }
  160. @Test
  161. public void testInvalidUpdate() throws Exception
  162. {
  163. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS)
  164. {
  165. try (final Database db = create(fileFormat))
  166. {
  167. Table t1 = newTable("TestTable")
  168. .addColumn(newColumn("id", DataType.LONG))
  169. .toTable(db);
  170. assertThrows(IllegalArgumentException.class,
  171. () -> newColumn("ID", DataType.TEXT).addToTable(t1),
  172. "created table with no columns?");
  173. Table t2 = newTable("TestTable2")
  174. .addColumn(newColumn("id2", DataType.LONG))
  175. .toTable(db);
  176. assertThrows(IllegalArgumentException.class,
  177. () -> newRelationship(t1, t2).toRelationship(db),
  178. "created rel with no columns?");
  179. assertThrows(IllegalArgumentException.class,
  180. () -> newRelationship("TestTable", "TestTable2").addColumns("id", "id") .toRelationship(db),
  181. "created rel with wrong columns?");
  182. }
  183. }
  184. }
  185. @Test
  186. public void testUpdateLargeTableDef() throws Exception
  187. {
  188. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  189. Database db = create(fileFormat);
  190. final int numColumns = 89;
  191. Table t = newTable("test")
  192. .addColumn(newColumn("first", DataType.TEXT))
  193. .toTable(db);
  194. List<String> colNames = new ArrayList<String>();
  195. colNames.add("first");
  196. for(int i = 0; i < numColumns; ++i) {
  197. String colName = "MyColumnName" + i;
  198. colNames.add(colName);
  199. DataType type = (((i % 3) == 0) ? DataType.MEMO : DataType.TEXT);
  200. newColumn(colName, type)
  201. .addToTable(t);
  202. }
  203. List<String> row = new ArrayList<String>();
  204. Map<String,Object> expectedRowData = new LinkedHashMap<String, Object>();
  205. for(int i = 0; i < colNames.size(); ++i) {
  206. String value = "" + i + " some row data";
  207. row.add(value);
  208. expectedRowData.put(colNames.get(i), value);
  209. }
  210. t.addRow(row.toArray());
  211. t.reset();
  212. assertEquals(expectedRowData, t.getNextRow());
  213. db.close();
  214. }
  215. }
  216. }