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 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. Copyright (c) 2008 Health Market Science, Inc.
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  14. USA
  15. You can contact Health Market Science at info@healthmarketscience.com
  16. or at the following address:
  17. Health Market Science
  18. 2700 Horizon Drive
  19. Suite 200
  20. King of Prussia, PA 19406
  21. */
  22. package com.healthmarketscience.jackcess;
  23. import java.util.ArrayList;
  24. import java.util.Arrays;
  25. import java.util.Collections;
  26. import java.util.Comparator;
  27. import java.util.List;
  28. import static com.healthmarketscience.jackcess.DatabaseTest.*;
  29. import static com.healthmarketscience.jackcess.JetFormatTest.*;
  30. import junit.framework.TestCase;
  31. /**
  32. * @author James Ahlborn
  33. */
  34. public class RelationshipTest extends TestCase {
  35. private static final Comparator<Relationship> REL_COMP = new Comparator<Relationship>() {
  36. public int compare(Relationship r1, Relationship r2) {
  37. return String.CASE_INSENSITIVE_ORDER.compare(r1.getName(), r2.getName());
  38. }
  39. };
  40. public RelationshipTest(String name) throws Exception {
  41. super(name);
  42. }
  43. public void testTwoTables() throws Exception {
  44. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) {
  45. Database db = open(testDB);
  46. Table t1 = db.getTable("Table1");
  47. Table t2 = db.getTable("Table2");
  48. Table t3 = db.getTable("Table3");
  49. List<Relationship> rels = db.getRelationships(t1, t2);
  50. assertEquals(1, rels.size());
  51. Relationship rel = rels.get(0);
  52. assertEquals("Table2Table1", rel.getName());
  53. assertEquals(t2, rel.getFromTable());
  54. assertEquals(Arrays.asList(t2.getColumn("id")),
  55. rel.getFromColumns());
  56. assertEquals(t1, rel.getToTable());
  57. assertEquals(Arrays.asList(t1.getColumn("otherfk1")),
  58. rel.getToColumns());
  59. assertTrue(rel.hasReferentialIntegrity());
  60. assertEquals(4096, rel.getFlags());
  61. assertTrue(rel.cascadeDeletes());
  62. assertSameRelationships(rels, db.getRelationships(t2, t1), true);
  63. rels = db.getRelationships(t2, t3);
  64. assertTrue(db.getRelationships(t2, t3).isEmpty());
  65. assertSameRelationships(rels, db.getRelationships(t3, t2), true);
  66. rels = db.getRelationships(t1, t3);
  67. assertEquals(1, rels.size());
  68. rel = rels.get(0);
  69. assertEquals("Table3Table1", rel.getName());
  70. assertEquals(t3, rel.getFromTable());
  71. assertEquals(Arrays.asList(t3.getColumn("id")),
  72. rel.getFromColumns());
  73. assertEquals(t1, rel.getToTable());
  74. assertEquals(Arrays.asList(t1.getColumn("otherfk2")),
  75. rel.getToColumns());
  76. assertTrue(rel.hasReferentialIntegrity());
  77. assertEquals(256, rel.getFlags());
  78. assertTrue(rel.cascadeUpdates());
  79. assertSameRelationships(rels, db.getRelationships(t3, t1), true);
  80. try {
  81. db.getRelationships(t1, t1);
  82. fail("IllegalArgumentException should have been thrown");
  83. } catch(IllegalArgumentException ignored) {
  84. // success
  85. }
  86. }
  87. }
  88. public void testOneTable() throws Exception {
  89. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) {
  90. Database db = open(testDB);
  91. Table t1 = db.getTable("Table1");
  92. Table t2 = db.getTable("Table2");
  93. Table t3 = db.getTable("Table3");
  94. List<Relationship> expected = new ArrayList<Relationship>();
  95. expected.addAll(db.getRelationships(t1, t2));
  96. expected.addAll(db.getRelationships(t2, t3));
  97. assertSameRelationships(expected, db.getRelationships(t2), false);
  98. }
  99. }
  100. public void testNoTables() throws Exception {
  101. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) {
  102. Database db = open(testDB);
  103. Table t1 = db.getTable("Table1");
  104. Table t2 = db.getTable("Table2");
  105. Table t3 = db.getTable("Table3");
  106. List<Relationship> expected = new ArrayList<Relationship>();
  107. expected.addAll(db.getRelationships(t1, t2));
  108. expected.addAll(db.getRelationships(t2, t3));
  109. expected.addAll(db.getRelationships(t1, t3));
  110. assertSameRelationships(expected, db.getRelationships(), false);
  111. }
  112. }
  113. private static void assertSameRelationships(
  114. List<Relationship> expected, List<Relationship> found, boolean ordered)
  115. {
  116. assertEquals(expected.size(), found.size());
  117. if(!ordered) {
  118. Collections.sort(expected, REL_COMP);
  119. Collections.sort(found, REL_COMP);
  120. }
  121. for(int i = 0; i < expected.size(); ++i) {
  122. Relationship eRel = expected.get(i);
  123. Relationship fRel = found.get(i);
  124. assertEquals(eRel.getName(), fRel.getName());
  125. }
  126. }
  127. }