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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 junit.framework.TestCase;
  22. import static com.healthmarketscience.jackcess.TestUtil.*;
  23. /**
  24. * @author James Ahlborn
  25. */
  26. public class RelationshipTest extends TestCase {
  27. private static final Comparator<Relationship> REL_COMP = new Comparator<Relationship>() {
  28. public int compare(Relationship r1, Relationship r2) {
  29. return String.CASE_INSENSITIVE_ORDER.compare(r1.getName(), r2.getName());
  30. }
  31. };
  32. public RelationshipTest(String name) throws Exception {
  33. super(name);
  34. }
  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. try {
  73. db.getRelationships(t1, t1);
  74. fail("IllegalArgumentException should have been thrown");
  75. } catch(IllegalArgumentException ignored) {
  76. // success
  77. }
  78. }
  79. }
  80. public void testOneTable() throws Exception {
  81. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) {
  82. Database db = open(testDB);
  83. Table t1 = db.getTable("Table1");
  84. Table t2 = db.getTable("Table2");
  85. Table t3 = db.getTable("Table3");
  86. List<Relationship> expected = new ArrayList<Relationship>();
  87. expected.addAll(db.getRelationships(t1, t2));
  88. expected.addAll(db.getRelationships(t2, t3));
  89. assertSameRelationships(expected, db.getRelationships(t2), false);
  90. }
  91. }
  92. public void testNoTables() throws Exception {
  93. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) {
  94. Database db = open(testDB);
  95. Table t1 = db.getTable("Table1");
  96. Table t2 = db.getTable("Table2");
  97. Table t3 = db.getTable("Table3");
  98. List<Relationship> expected = new ArrayList<Relationship>();
  99. expected.addAll(db.getRelationships(t1, t2));
  100. expected.addAll(db.getRelationships(t2, t3));
  101. expected.addAll(db.getRelationships(t1, t3));
  102. assertSameRelationships(expected, db.getRelationships(), false);
  103. }
  104. }
  105. private static void assertSameRelationships(
  106. List<Relationship> expected, List<Relationship> found, boolean ordered)
  107. {
  108. assertEquals(expected.size(), found.size());
  109. if(!ordered) {
  110. Collections.sort(expected, REL_COMP);
  111. Collections.sort(found, REL_COMP);
  112. }
  113. for(int i = 0; i < expected.size(); ++i) {
  114. Relationship eRel = expected.get(i);
  115. Relationship fRel = found.get(i);
  116. assertEquals(eRel.getName(), fRel.getName());
  117. }
  118. }
  119. }