View Javadoc
1   /*
2   Copyright (c) 2008 Health Market Science, Inc.
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16  
17  package com.healthmarketscience.jackcess;
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.Collections;
22  import java.util.Comparator;
23  import java.util.List;
24  
25  import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
26  import com.healthmarketscience.jackcess.impl.RelationshipImpl;
27  import junit.framework.TestCase;
28  import static com.healthmarketscience.jackcess.TestUtil.*;
29  
30  /**
31   * @author James Ahlborn
32   */
33  public class RelationshipTest extends TestCase {
34  
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   
41    public RelationshipTest(String name) throws Exception {
42      super(name);
43    }
44  
45    public void testTwoTables() throws Exception {
46      for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) {
47        Database db = open(testDB);
48        Table t1 = db.getTable("Table1");
49        Table t2 = db.getTable("Table2");
50        Table t3 = db.getTable("Table3");
51  
52        List<Relationship> rels = db.getRelationships(t1, t2);
53        assertEquals(1, rels.size());
54        Relationship rel = rels.get(0);
55        assertEquals("Table2Table1", rel.getName());
56        assertEquals(t2, rel.getFromTable());
57        assertEquals(Arrays.asList(t2.getColumn("id")),
58                     rel.getFromColumns());
59        assertEquals(t1, rel.getToTable());
60        assertEquals(Arrays.asList(t1.getColumn("otherfk1")),
61                     rel.getToColumns());
62        assertTrue(rel.hasReferentialIntegrity());
63        assertEquals(4096, ((RelationshipImpl)rel).getFlags());
64        assertTrue(rel.cascadeDeletes());
65        assertSameRelationships(rels, db.getRelationships(t2, t1), true);
66  
67        rels = db.getRelationships(t2, t3);
68        assertTrue(db.getRelationships(t2, t3).isEmpty());
69        assertSameRelationships(rels, db.getRelationships(t3, t2), true);
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(256, ((RelationshipImpl)rel).getFlags());
83        assertTrue(rel.cascadeUpdates());
84        assertSameRelationships(rels, db.getRelationships(t3, t1), true);
85  
86        try {
87          db.getRelationships(t1, t1);
88          fail("IllegalArgumentException should have been thrown");
89        } catch(IllegalArgumentException ignored) {
90          // success
91        }
92      }
93    }
94  
95    public void testOneTable() throws Exception {
96      for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) {
97        Database db = open(testDB);
98        Table t1 = db.getTable("Table1");
99        Table t2 = db.getTable("Table2");
100       Table t3 = db.getTable("Table3");
101 
102       List<Relationship> expected = new ArrayList<Relationship>();
103       expected.addAll(db.getRelationships(t1, t2));
104       expected.addAll(db.getRelationships(t2, t3));
105 
106       assertSameRelationships(expected, db.getRelationships(t2), false);
107       
108     }
109   }
110 
111   public void testNoTables() throws Exception {
112     for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) {
113       Database db = open(testDB);
114       Table t1 = db.getTable("Table1");
115       Table t2 = db.getTable("Table2");
116       Table t3 = db.getTable("Table3");
117 
118       List<Relationship> expected = new ArrayList<Relationship>();
119       expected.addAll(db.getRelationships(t1, t2));
120       expected.addAll(db.getRelationships(t2, t3));
121       expected.addAll(db.getRelationships(t1, t3));
122 
123       assertSameRelationships(expected, db.getRelationships(), false);
124     }
125   }
126 
127   private static void assertSameRelationships(
128       List<Relationship> expected, List<Relationship> found, boolean ordered)
129   {
130     assertEquals(expected.size(), found.size());
131     if(!ordered) {
132       Collections.sort(expected, REL_COMP);
133       Collections.sort(found, REL_COMP);
134     }
135     for(int i = 0; i < expected.size(); ++i) {
136       Relationship eRel = expected.get(i);
137       Relationship fRel = found.get(i);
138       assertEquals(eRel.getName(), fRel.getName());
139     }
140   }
141   
142 }