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