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

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