View Javadoc
1   /*
2   Copyright (c) 2012 James Ahlborn
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.impl;
18  
19  import java.io.IOException;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  
24  import com.healthmarketscience.jackcess.Column;
25  import com.healthmarketscience.jackcess.Cursor;
26  import com.healthmarketscience.jackcess.CursorBuilder;
27  import com.healthmarketscience.jackcess.Database;
28  import com.healthmarketscience.jackcess.Row;
29  import com.healthmarketscience.jackcess.Table;
30  import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
31  import junit.framework.TestCase;
32  import static com.healthmarketscience.jackcess.TestUtil.*;
33  
34  /**
35   *
36   * @author James Ahlborn
37   */
38  public class FKEnforcerTest extends TestCase
39  {
40  
41    public FKEnforcerTest(String name) throws Exception {
42      super(name);
43    }
44  
45    public void testNoEnforceForeignKeys() throws Exception {
46      for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) {
47  
48        Database db = openCopy(testDB);
49        db.setEnforceForeignKeys(false);
50        Table t1 = db.getTable("Table1");
51        Table t2 = db.getTable("Table2");
52        Table t3 = db.getTable("Table3");
53  
54        t1.addRow(20, 0, 20, "some data", 20);
55  
56        Cursor c = CursorBuilder.createCursor(t2);
57        c.moveToNextRow();
58        c.updateCurrentRow(30, "foo30");
59  
60        c = CursorBuilder.createCursor(t3);
61        c.moveToNextRow();
62        c.deleteCurrentRow();
63  
64        db.close();
65      }
66      
67    }
68  
69    public void testEnforceForeignKeys() throws Exception {
70      for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) {
71  
72        Database db = openCopy(testDB);
73        Table t1 = db.getTable("Table1");
74        Table t2 = db.getTable("Table2");
75        Table t3 = db.getTable("Table3");
76  
77        try {
78          t1.addRow(20, 0, 20, "some data", 20);
79          fail("IOException should have been thrown");
80        } catch(IOException ignored) {
81          // success
82          assertTrue(ignored.getMessage().contains("Table1[otherfk2]"));
83        }
84  
85        try {
86          Cursor c = CursorBuilder.createCursor(t2);
87          c.moveToNextRow();
88          c.updateCurrentRow(30, "foo30");
89          fail("IOException should have been thrown");
90        } catch(IOException ignored) {
91          // success
92          assertTrue(ignored.getMessage().contains("Table2[id]"));
93        }
94  
95        try {
96          Cursor c = CursorBuilder.createCursor(t3);
97          c.moveToNextRow();
98          c.deleteCurrentRow();
99          fail("IOException should have been thrown");
100       } catch(IOException ignored) {
101         // success
102         assertTrue(ignored.getMessage().contains("Table3[id]"));
103       }
104 
105       t1.addRow(21, null, null, "null fks", null);
106 
107       Cursor c = CursorBuilder.createCursor(t3);
108       Column col = t3.getColumn("id");
109       for(Row row : c) {
110         int id = row.getInt("id");
111         id += 20;
112         c.setCurrentRowValue(col, id);
113       }
114 
115       List<? extends Map<String, Object>> expectedRows =
116         createExpectedTable(
117             createT1Row(0, 0, 30, "baz0", 0),
118             createT1Row(1, 1, 31, "baz11", 0),
119             createT1Row(2, 1, 31, "baz11-2", 0),
120             createT1Row(3, 2, 33, "baz13", 0),
121             createT1Row(21, null, null, "null fks", null));
122 
123       assertTable(expectedRows, t1);
124 
125       c = CursorBuilder.createCursor(t2);
126       for(Iterator<?> iter = c.iterator(); iter.hasNext(); ) {
127         iter.next();
128         iter.remove();
129       }
130 
131       assertEquals(1, t1.getRowCount());
132 
133       db.close();
134     }
135     
136   }
137 
138   private static Row createT1Row(
139       int id1, Integer fk1, Integer fk2, String data, Integer fk3)
140   {
141     return createExpectedRow("id", id1, "otherfk1", fk1, "otherfk2", fk2,
142                              "data", data, "otherfk3", fk3);
143   }
144 }