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        db.setEvaluateExpressions(false);
74        Table t1 = db.getTable("Table1");
75        Table t2 = db.getTable("Table2");
76        Table t3 = db.getTable("Table3");
77  
78        try {
79          t1.addRow(20, 0, 20, "some data", 20);
80          fail("IOException should have been thrown");
81        } catch(IOException ignored) {
82          // success
83          assertTrue(ignored.getMessage().contains("Table1[otherfk2]"));
84        }
85  
86        try {
87          Cursor c = CursorBuilder.createCursor(t2);
88          c.moveToNextRow();
89          c.updateCurrentRow(30, "foo30");
90          fail("IOException should have been thrown");
91        } catch(IOException ignored) {
92          // success
93          assertTrue(ignored.getMessage().contains("Table2[id]"));
94        }
95  
96        try {
97          Cursor c = CursorBuilder.createCursor(t3);
98          c.moveToNextRow();
99          c.deleteCurrentRow();
100         fail("IOException should have been thrown");
101       } catch(IOException ignored) {
102         // success
103         assertTrue(ignored.getMessage().contains("Table3[id]"));
104       }
105 
106       t1.addRow(21, null, null, "null fks", null);
107 
108       Cursor c = CursorBuilder.createCursor(t3);
109       Column col = t3.getColumn("id");
110       for(Row row : c) {
111         int id = row.getInt("id");
112         id += 20;
113         c.setCurrentRowValue(col, id);
114       }
115 
116       List<? extends Map<String, Object>> expectedRows =
117         createExpectedTable(
118             createT1Row(0, 0, 30, "baz0", 0),
119             createT1Row(1, 1, 31, "baz11", 0),
120             createT1Row(2, 1, 31, "baz11-2", 0),
121             createT1Row(3, 2, 33, "baz13", 0),
122             createT1Row(21, null, null, "null fks", null));
123 
124       assertTable(expectedRows, t1);
125 
126       c = CursorBuilder.createCursor(t2);
127       for(Iterator<?> iter = c.iterator(); iter.hasNext(); ) {
128         iter.next();
129         iter.remove();
130       }
131 
132       assertEquals(1, t1.getRowCount());
133 
134       db.close();
135     }
136 
137   }
138 
139   private static Row createT1Row(
140       int id1, Integer fk1, Integer fk2, String data, Integer fk3)
141   {
142     return createExpectedRow("id", id1, "otherfk1", fk1, "otherfk2", fk2,
143                              "data", data, "otherfk3", fk3);
144   }
145 }