Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

FKEnforcerTest.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 static com.healthmarketscience.jackcess.TestUtil.*;
  26. import static org.junit.jupiter.api.Assertions.*;
  27. import org.junit.jupiter.api.Test;
  28. /**
  29. *
  30. * @author James Ahlborn
  31. */
  32. public class FKEnforcerTest
  33. {
  34. @Test
  35. public void testNoEnforceForeignKeys() throws Exception
  36. {
  37. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX))
  38. {
  39. Database db = openCopy(testDB);
  40. db.setEnforceForeignKeys(false);
  41. Table t1 = db.getTable("Table1");
  42. Table t2 = db.getTable("Table2");
  43. Table t3 = db.getTable("Table3");
  44. t1.addRow(20, 0, 20, "some data", 20);
  45. Cursor c = CursorBuilder.createCursor(t2);
  46. c.moveToNextRow();
  47. c.updateCurrentRow(30, "foo30");
  48. c = CursorBuilder.createCursor(t3);
  49. c.moveToNextRow();
  50. c.deleteCurrentRow();
  51. db.close();
  52. }
  53. }
  54. @Test
  55. public void testEnforceForeignKeys() throws Exception
  56. {
  57. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX))
  58. {
  59. try (final Database db = openCopy(testDB))
  60. {
  61. db.setEvaluateExpressions(false);
  62. Table t1 = db.getTable("Table1");
  63. Table t2 = db.getTable("Table2");
  64. Table t3 = db.getTable("Table3");
  65. IOException ioe = assertThrows(IOException.class, () -> t1.addRow(20, 0, 20, "some data", 20));
  66. assertTrue(ioe.getMessage().contains("Table1[otherfk2]"));
  67. ioe = assertThrows(IOException.class,
  68. () ->
  69. {
  70. Cursor c = CursorBuilder.createCursor(t2);
  71. c.moveToNextRow();
  72. c.updateCurrentRow(30, "foo30");
  73. });
  74. assertTrue(ioe.getMessage().contains("Table2[id]"));
  75. ioe = assertThrows(IOException.class,
  76. () ->
  77. {
  78. Cursor c = CursorBuilder.createCursor(t3);
  79. c.moveToNextRow();
  80. c.deleteCurrentRow();
  81. fail("IOException should have been thrown");
  82. });
  83. assertTrue(ioe.getMessage().contains("Table3[id]"));
  84. t1.addRow(21, null, null, "null fks", null);
  85. Cursor c = CursorBuilder.createCursor(t3);
  86. Column col = t3.getColumn("id");
  87. for (Row row : c)
  88. {
  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. {
  104. iter.next();
  105. iter.remove();
  106. }
  107. assertEquals(1, t1.getRowCount());
  108. }
  109. }
  110. }
  111. private static Row createT1Row(
  112. int id1, Integer fk1, Integer fk2, String data, Integer fk3)
  113. {
  114. return createExpectedRow("id", id1, "otherfk1", fk1, "otherfk2", fk2,
  115. "data", data, "otherfk3", fk3);
  116. }
  117. }