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.

FKEnforcerTest.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. Copyright (c) 2012 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.Iterator;
  16. import java.util.List;
  17. import java.util.Map;
  18. import com.healthmarketscience.jackcess.Column;
  19. import com.healthmarketscience.jackcess.Cursor;
  20. import com.healthmarketscience.jackcess.CursorBuilder;
  21. import com.healthmarketscience.jackcess.Database;
  22. import com.healthmarketscience.jackcess.Row;
  23. import com.healthmarketscience.jackcess.Table;
  24. import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
  25. import junit.framework.TestCase;
  26. import static com.healthmarketscience.jackcess.TestUtil.*;
  27. /**
  28. *
  29. * @author James Ahlborn
  30. */
  31. public class FKEnforcerTest extends TestCase
  32. {
  33. public FKEnforcerTest(String name) throws Exception {
  34. super(name);
  35. }
  36. public void testNoEnforceForeignKeys() throws Exception {
  37. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) {
  38. Database db = openCopy(testDB);
  39. db.setEnforceForeignKeys(false);
  40. Table t1 = db.getTable("Table1");
  41. Table t2 = db.getTable("Table2");
  42. Table t3 = db.getTable("Table3");
  43. t1.addRow(20, 0, 20, "some data", 20);
  44. Cursor c = CursorBuilder.createCursor(t2);
  45. c.moveToNextRow();
  46. c.updateCurrentRow(30, "foo30");
  47. c = CursorBuilder.createCursor(t3);
  48. c.moveToNextRow();
  49. c.deleteCurrentRow();
  50. db.close();
  51. }
  52. }
  53. public void testEnforceForeignKeys() throws Exception {
  54. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) {
  55. Database db = openCopy(testDB);
  56. db.setEvaluateExpressions(false);
  57. Table t1 = db.getTable("Table1");
  58. Table t2 = db.getTable("Table2");
  59. Table t3 = db.getTable("Table3");
  60. try {
  61. t1.addRow(20, 0, 20, "some data", 20);
  62. fail("IOException should have been thrown");
  63. } catch(IOException ignored) {
  64. // success
  65. assertTrue(ignored.getMessage().contains("Table1[otherfk2]"));
  66. }
  67. try {
  68. Cursor c = CursorBuilder.createCursor(t2);
  69. c.moveToNextRow();
  70. c.updateCurrentRow(30, "foo30");
  71. fail("IOException should have been thrown");
  72. } catch(IOException ignored) {
  73. // success
  74. assertTrue(ignored.getMessage().contains("Table2[id]"));
  75. }
  76. try {
  77. Cursor c = CursorBuilder.createCursor(t3);
  78. c.moveToNextRow();
  79. c.deleteCurrentRow();
  80. fail("IOException should have been thrown");
  81. } catch(IOException ignored) {
  82. // success
  83. assertTrue(ignored.getMessage().contains("Table3[id]"));
  84. }
  85. t1.addRow(21, null, null, "null fks", null);
  86. Cursor c = CursorBuilder.createCursor(t3);
  87. Column col = t3.getColumn("id");
  88. for(Row row : c) {
  89. int id = row.getInt("id");
  90. id += 20;
  91. c.setCurrentRowValue(col, id);
  92. }
  93. List<? extends Map<String, Object>> expectedRows =
  94. createExpectedTable(
  95. createT1Row(0, 0, 30, "baz0", 0),
  96. createT1Row(1, 1, 31, "baz11", 0),
  97. createT1Row(2, 1, 31, "baz11-2", 0),
  98. createT1Row(3, 2, 33, "baz13", 0),
  99. createT1Row(21, null, null, "null fks", null));
  100. assertTable(expectedRows, t1);
  101. c = CursorBuilder.createCursor(t2);
  102. for(Iterator<?> iter = c.iterator(); iter.hasNext(); ) {
  103. iter.next();
  104. iter.remove();
  105. }
  106. assertEquals(1, t1.getRowCount());
  107. db.close();
  108. }
  109. }
  110. private static Row createT1Row(
  111. int id1, Integer fk1, Integer fk2, String data, Integer fk3)
  112. {
  113. return createExpectedRow("id", id1, "otherfk1", fk1, "otherfk2", fk2,
  114. "data", data, "otherfk3", fk3);
  115. }
  116. }