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.

RelationshipTest.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Copyright (c) 2008 Health Market Science, Inc.
  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.Collections;
  17. import java.util.Comparator;
  18. import java.util.List;
  19. import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
  20. import com.healthmarketscience.jackcess.impl.RelationshipImpl;
  21. import static com.healthmarketscience.jackcess.TestUtil.*;
  22. import static org.junit.jupiter.api.Assertions.*;
  23. import org.junit.jupiter.api.Test;
  24. /**
  25. * @author James Ahlborn
  26. */
  27. public class RelationshipTest
  28. {
  29. private static final Comparator<Relationship> REL_COMP = new Comparator<Relationship>() {
  30. public int compare(Relationship r1, Relationship r2) {
  31. return String.CASE_INSENSITIVE_ORDER.compare(r1.getName(), r2.getName());
  32. }
  33. };
  34. @Test
  35. public void testTwoTables() throws Exception {
  36. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) {
  37. Database db = open(testDB);
  38. Table t1 = db.getTable("Table1");
  39. Table t2 = db.getTable("Table2");
  40. Table t3 = db.getTable("Table3");
  41. List<Relationship> rels = db.getRelationships(t1, t2);
  42. assertEquals(1, rels.size());
  43. Relationship rel = rels.get(0);
  44. assertEquals("Table2Table1", rel.getName());
  45. assertEquals(t2, rel.getFromTable());
  46. assertEquals(Arrays.asList(t2.getColumn("id")),
  47. rel.getFromColumns());
  48. assertEquals(t1, rel.getToTable());
  49. assertEquals(Arrays.asList(t1.getColumn("otherfk1")),
  50. rel.getToColumns());
  51. assertTrue(rel.hasReferentialIntegrity());
  52. assertEquals(4096, ((RelationshipImpl)rel).getFlags());
  53. assertTrue(rel.cascadeDeletes());
  54. assertSameRelationships(rels, db.getRelationships(t2, t1), true);
  55. rels = db.getRelationships(t2, t3);
  56. assertTrue(db.getRelationships(t2, t3).isEmpty());
  57. assertSameRelationships(rels, db.getRelationships(t3, t2), true);
  58. rels = db.getRelationships(t1, t3);
  59. assertEquals(1, rels.size());
  60. rel = rels.get(0);
  61. assertEquals("Table3Table1", rel.getName());
  62. assertEquals(t3, rel.getFromTable());
  63. assertEquals(Arrays.asList(t3.getColumn("id")),
  64. rel.getFromColumns());
  65. assertEquals(t1, rel.getToTable());
  66. assertEquals(Arrays.asList(t1.getColumn("otherfk2")),
  67. rel.getToColumns());
  68. assertTrue(rel.hasReferentialIntegrity());
  69. assertEquals(256, ((RelationshipImpl)rel).getFlags());
  70. assertTrue(rel.cascadeUpdates());
  71. assertSameRelationships(rels, db.getRelationships(t3, t1), true);
  72. assertThrows(IllegalArgumentException.class, () -> db.getRelationships(t1, t1));
  73. }
  74. }
  75. @Test
  76. public void testOneTable() throws Exception {
  77. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) {
  78. Database db = open(testDB);
  79. Table t1 = db.getTable("Table1");
  80. Table t2 = db.getTable("Table2");
  81. Table t3 = db.getTable("Table3");
  82. List<Relationship> expected = new ArrayList<Relationship>();
  83. expected.addAll(db.getRelationships(t1, t2));
  84. expected.addAll(db.getRelationships(t2, t3));
  85. assertSameRelationships(expected, db.getRelationships(t2), false);
  86. }
  87. }
  88. @Test
  89. public void testNoTables() throws Exception {
  90. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) {
  91. Database db = open(testDB);
  92. Table t1 = db.getTable("Table1");
  93. Table t2 = db.getTable("Table2");
  94. Table t3 = db.getTable("Table3");
  95. List<Relationship> expected = new ArrayList<Relationship>();
  96. expected.addAll(db.getRelationships(t1, t2));
  97. expected.addAll(db.getRelationships(t2, t3));
  98. expected.addAll(db.getRelationships(t1, t3));
  99. assertSameRelationships(expected, db.getRelationships(), false);
  100. }
  101. }
  102. private static void assertSameRelationships(
  103. List<Relationship> expected, List<Relationship> found, boolean ordered)
  104. {
  105. assertEquals(expected.size(), found.size());
  106. if(!ordered) {
  107. Collections.sort(expected, REL_COMP);
  108. Collections.sort(found, REL_COMP);
  109. }
  110. for(int i = 0; i < expected.size(); ++i) {
  111. Relationship eRel = expected.get(i);
  112. Relationship fRel = found.get(i);
  113. assertEquals(eRel.getName(), fRel.getName());
  114. }
  115. }
  116. }