001: /*
002: * TableDependencySorterTest.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.db.importer;
013:
014: import java.sql.Statement;
015: import java.util.ArrayList;
016: import java.util.List;
017: import junit.framework.TestCase;
018: import workbench.TestUtil;
019: import workbench.db.ConnectionMgr;
020: import workbench.db.TableIdentifier;
021: import workbench.db.WbConnection;
022:
023: /**
024: *
025: * @author support@sql-workbench.net
026: */
027: public class TableDependencySorterTest extends TestCase {
028: private WbConnection dbConn;
029:
030: public TableDependencySorterTest(String testName) {
031: super (testName);
032: }
033:
034: @Override
035: protected void setUp() throws Exception {
036: super .setUp();
037: TestUtil util = new TestUtil("dependencyTest");
038: dbConn = util.getConnection();
039: Statement stmt = dbConn.createStatement();
040: String baseSql = "CREATE TABLE base \n" + "( \n"
041: + " id1 INTEGER NOT NULL, \n"
042: + " id2 INTEGER NOT NULL, \n"
043: + " primary key (id1, id2) \n" + ")";
044: stmt.execute(baseSql);
045:
046: String child1Sql = "CREATE TABLE child1 \n"
047: + "( \n"
048: + " id INTEGER NOT NULL PRIMARY KEY, \n"
049: + " base_id1 INTEGER NOT NULL, \n"
050: + " base_id2 INTEGER NOT NULL, \n"
051: + " FOREIGN KEY (base_id1, base_id2) REFERENCES base (id1,id2) \n"
052: + ")";
053: stmt.executeUpdate(child1Sql);
054:
055: String child2Sql = "CREATE TABLE child2 \n"
056: + "( \n"
057: + " id INTEGER NOT NULL PRIMARY KEY, \n"
058: + " base_id1 INTEGER NOT NULL, \n"
059: + " base_id2 INTEGER NOT NULL, \n"
060: + " FOREIGN KEY (base_id1, base_id2) REFERENCES base (id1,id2) \n"
061: + ")";
062: stmt.executeUpdate(child2Sql);
063:
064: String child3Sql = "CREATE TABLE child2_detail \n" + "( \n"
065: + " id INTEGER NOT NULL PRIMARY KEY, \n"
066: + " child_id INTEGER NOT NULL, \n"
067: + " FOREIGN KEY (child_id) REFERENCES child2 (id) \n"
068: + ")";
069: stmt.executeUpdate(child3Sql);
070:
071: String sql = "CREATE TABLE child1_detail \n"
072: + "( \n"
073: + " id INTEGER NOT NULL PRIMARY KEY, \n"
074: + " child1_id INTEGER NOT NULL, \n"
075: + " FOREIGN KEY (child1_id) REFERENCES child1 (id) \n"
076: + ")";
077: stmt.executeUpdate(sql);
078:
079: sql = "CREATE TABLE child1_detail2 \n"
080: + "( \n"
081: + " id INTEGER NOT NULL PRIMARY KEY, \n"
082: + " detail_id INTEGER NOT NULL, \n"
083: + " FOREIGN KEY (detail_id) REFERENCES child1_detail (id) \n"
084: + ")";
085: stmt.executeUpdate(sql);
086:
087: sql = "CREATE TABLE tbl1 \n" + "( \n"
088: + " id INTEGER NOT NULL PRIMARY KEY" + ")";
089: stmt.executeUpdate(sql);
090:
091: sql = "CREATE TABLE tbl2 \n" + "( \n"
092: + " id INTEGER NOT NULL PRIMARY KEY" + ")";
093: stmt.executeUpdate(sql);
094:
095: sql = "CREATE TABLE tbl3 \n" + "( \n"
096: + " id INTEGER NOT NULL PRIMARY KEY" + ")";
097: stmt.executeUpdate(sql);
098: }
099:
100: @Override
101: protected void tearDown() throws Exception {
102: ConnectionMgr.getInstance().disconnectAll();
103: super .tearDown();
104: }
105:
106: public void testPlainTables() {
107: ArrayList<TableIdentifier> tables = new ArrayList<TableIdentifier>();
108: for (int i = 0; i < 3; i++) {
109: tables.add(new TableIdentifier("tbl" + (i + 1)));
110: }
111:
112: TableDependencySorter sorter = new TableDependencySorter(
113: this .dbConn);
114: List<TableIdentifier> result = sorter.sortForInsert(tables);
115: assertEquals("Not enough entries", tables.size(), result.size());
116: }
117:
118: public void testCheckDependencies() {
119: TableIdentifier base = new TableIdentifier("base");
120: TableIdentifier child1 = new TableIdentifier("child1");
121: TableIdentifier child2 = new TableIdentifier("child2");
122: TableIdentifier child1_detail = new TableIdentifier(
123: "child1_detail");
124: TableIdentifier child1_detail2 = new TableIdentifier(
125: "child1_detail2");
126: TableIdentifier child2_detail = new TableIdentifier(
127: "child2_detail");
128: ArrayList<TableIdentifier> tbl = new ArrayList<TableIdentifier>();
129: tbl.add(child1);
130: tbl.add(base);
131: tbl.add(child2);
132: tbl.add(child1_detail);
133: tbl.add(child2_detail);
134: tbl.add(child1_detail2);
135: TableDependencySorter sorter = new TableDependencySorter(
136: this .dbConn);
137: List<TableIdentifier> result = sorter.sortForDelete(tbl, false);
138: // for (TableIdentifier t : result)
139: // {
140: // System.out.println(t.toString());
141: // }
142: // System.out.println("--------------------");
143: assertEquals("Not enough entries", tbl.size(), result.size());
144: assertEquals("Wrong first table", result.get(0), child1_detail2);
145:
146: // the second entry is either child1_detail or child2_detail
147: TableIdentifier second = result.get(1);
148: assertEquals(true, second.equals(child1_detail)
149: || second.equals(child2_detail));
150:
151: TableIdentifier last = result.get(result.size() - 1);
152: assertEquals("Wrong last table", true, last.equals(base));
153:
154: TableIdentifier lastButOne = result.get(result.size() - 2);
155: assertEquals(true, lastButOne.equals(child1)
156: || lastButOne.equals(child2));
157:
158: List<TableIdentifier> insertList = sorter.sortForInsert(tbl);
159: // for (TableIdentifier t : insertList)
160: // {
161: // System.out.println(t.toString());
162: // }
163: assertEquals("Not enough entries", tbl.size(), insertList
164: .size());
165: assertEquals("Wrong first table for insert", base, insertList
166: .get(0));
167: }
168:
169: public void testCheckAddMissing() {
170: TableIdentifier child1 = new TableIdentifier("child1");
171: ArrayList<TableIdentifier> tbl = new ArrayList<TableIdentifier>();
172: tbl.add(child1);
173:
174: TableDependencySorter sorter = new TableDependencySorter(
175: this .dbConn);
176: List<TableIdentifier> result = sorter.sortForDelete(tbl, true);
177: // for (TableIdentifier t : result)
178: // {
179: // System.out.println(t.toString());
180: // }
181: // System.out.println("--------------------");
182:
183: // Should have added child1_detail and child1_detail2
184: assertEquals("Not enough entries", 3, result.size());
185: String first = result.get(0).getTableName().toLowerCase();
186: String second = result.get(1).getTableName().toLowerCase();
187: assertEquals("Wrong first table", true, first
188: .equals("child1_detail")
189: || first.equals("child1_detail2"));
190: assertEquals("Wrong second table", true, second
191: .equals("child1_detail")
192: || second.equals("child1_detail2"));
193: assertEquals("Wrong third table", "child1", result.get(2)
194: .getTableName().toLowerCase());
195: }
196:
197: }
|