KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > wizard > fromdb > TableClosureJoinTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.wizard.fromdb;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import junit.framework.*;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import org.netbeans.api.progress.ProgressHandle;
31 import org.openide.WizardDescriptor;
32 import org.openide.filesystems.FileObject;
33
34 /**
35  *
36  * @author Andrei Badea
37  */

38 public class TableClosureJoinTest extends TestCase {
39
40     private TableProviderImpl provider;
41     private TableClosure closure;
42
43     public TableClosureJoinTest(String JavaDoc testName) {
44         super(testName);
45     }
46
47     public void setUp() {
48         Map JavaDoc<String JavaDoc, Set JavaDoc<String JavaDoc>> tablesAndRefs = new HashMap JavaDoc<String JavaDoc, Set JavaDoc<String JavaDoc>>();
49         Set JavaDoc<String JavaDoc> empty = Collections.emptySet();
50
51         tablesAndRefs.put("BAR", empty);
52         tablesAndRefs.put("FOO", empty);
53         tablesAndRefs.put("ROOM", empty);
54         tablesAndRefs.put("STUDENT", empty);
55         tablesAndRefs.put("TEACHER", empty);
56         tablesAndRefs.put("STUDENT_TEACHER", new HashSet JavaDoc(Arrays.asList(new String JavaDoc[] { "TEACHER", "STUDENT" })));
57         tablesAndRefs.put("ROOM_STUDENT", new HashSet JavaDoc(Arrays.asList(new String JavaDoc[] { "STUDENT", "ROOM" })));
58         tablesAndRefs.put("FOO_ROOM_STUDENT", new HashSet JavaDoc(Arrays.asList(new String JavaDoc[] { "FOO", "ROOM_STUDENT" })));
59
60         provider = new TableProviderImpl(tablesAndRefs);
61         closure = new TableClosure(provider);
62     }
63
64     public void tearDown() {
65         closure = null;
66     }
67
68     public void testBasic() {
69         closure.addTables(Collections.singleton(provider.getTableByName("STUDENT")));
70
71         assertTables(new String JavaDoc[] { "STUDENT" }, closure.getWantedTables());
72         assertTables(new String JavaDoc[] { }, closure.getReferencedTables());
73         assertTables(new String JavaDoc[] { "STUDENT" }, closure.getSelectedTables());
74         assertTables(new String JavaDoc[] { "BAR", "FOO", "FOO_ROOM_STUDENT", "ROOM", "ROOM_STUDENT", "STUDENT_TEACHER", "TEACHER" }, closure.getAvailableTables());
75
76         closure.addTables(Collections.singleton(provider.getTableByName("TEACHER")));
77
78         assertTables(new String JavaDoc[] { "STUDENT", "TEACHER" }, closure.getWantedTables());
79         assertTables(new String JavaDoc[] { "STUDENT_TEACHER" }, closure.getReferencedTables());
80         assertTables(new String JavaDoc[] { "STUDENT", "STUDENT_TEACHER", "TEACHER" }, closure.getSelectedTables());
81         assertTables(new String JavaDoc[] { "BAR", "FOO", "FOO_ROOM_STUDENT", "ROOM", "ROOM_STUDENT" }, closure.getAvailableTables());
82
83         closure.addTables(Collections.singleton(provider.getTableByName("ROOM")));
84
85         assertTables(new String JavaDoc[] { "ROOM", "STUDENT", "TEACHER" }, closure.getWantedTables());
86         assertTables(new String JavaDoc[] { "ROOM_STUDENT", "STUDENT_TEACHER" }, closure.getReferencedTables());
87         assertTables(new String JavaDoc[] { "ROOM", "ROOM_STUDENT", "STUDENT", "STUDENT_TEACHER", "TEACHER" }, closure.getSelectedTables());
88         assertTables(new String JavaDoc[] { "BAR", "FOO", "FOO_ROOM_STUDENT" }, closure.getAvailableTables());
89
90         closure.removeTables(Collections.singleton(provider.getTableByName("STUDENT")));
91
92         assertTables(new String JavaDoc[] { "ROOM", "TEACHER" }, closure.getWantedTables());
93         assertTables(new String JavaDoc[] { }, closure.getReferencedTables());
94         assertTables(new String JavaDoc[] { "ROOM", "TEACHER" }, closure.getSelectedTables());
95         assertTables(new String JavaDoc[] { "BAR", "FOO", "FOO_ROOM_STUDENT", "ROOM_STUDENT", "STUDENT", "STUDENT_TEACHER" }, closure.getAvailableTables());
96
97         closure.addAllTables();
98
99         // assert adding all tables selects the tables based on the "normal" direction of the
100
// relationships, not on the "inverse" direction, as it happens for join tables.
101
// that is, the STUDENT and TEACHER tables are marked as referenced,
102
// not STUDENT_TEACHER -- since the direction of the relationships is
103
// STUDENT_TEACHER -> (TEACHER, STUDENT)
104
assertTables(new String JavaDoc[] { "BAR", "FOO", "FOO_ROOM_STUDENT", "ROOM", "ROOM_STUDENT", "STUDENT", "STUDENT_TEACHER", "TEACHER" }, closure.getWantedTables());
105         assertTables(new String JavaDoc[] { "FOO", "ROOM", "ROOM_STUDENT", "STUDENT", "TEACHER" }, closure.getReferencedTables());
106         assertTables(new String JavaDoc[] { "BAR", "FOO", "FOO_ROOM_STUDENT", "ROOM", "ROOM_STUDENT", "STUDENT", "STUDENT_TEACHER", "TEACHER" }, closure.getSelectedTables());
107         assertTables(new String JavaDoc[] { }, closure.getAvailableTables());
108
109         closure.removeAllTables();
110
111         assertTables(new String JavaDoc[] { }, closure.getWantedTables());
112         assertTables(new String JavaDoc[] { }, closure.getReferencedTables());
113         assertTables(new String JavaDoc[] { }, closure.getSelectedTables());
114         assertTables(new String JavaDoc[] { "BAR", "FOO", "FOO_ROOM_STUDENT", "ROOM", "ROOM_STUDENT", "STUDENT", "STUDENT_TEACHER", "TEACHER" }, closure.getAvailableTables());
115     }
116
117
118     /**
119      * Tests that adding a table which causes the addition of a join table which causes
120      * the addition of another join table works. Here adding STUDENT causes
121      * ROOM_STUDENT to be added, which causes FOO_ROOM_STUDENT to be added.
122      */

123     public void testRecursiveJoin() {
124         closure.addTables(Collections.singleton(provider.getTableByName("ROOM")));
125         closure.addTables(Collections.singleton(provider.getTableByName("FOO")));
126         closure.addTables(Collections.singleton(provider.getTableByName("STUDENT")));
127
128         assertTables(new String JavaDoc[] { "FOO", "ROOM", "STUDENT" }, closure.getWantedTables());
129         assertTables(new String JavaDoc[] { "FOO_ROOM_STUDENT", "ROOM_STUDENT" }, closure.getReferencedTables());
130         assertTables(new String JavaDoc[] { "FOO", "FOO_ROOM_STUDENT", "ROOM", "ROOM_STUDENT", "STUDENT" }, closure.getSelectedTables());
131         assertTables(new String JavaDoc[] { "BAR", "STUDENT_TEACHER", "TEACHER" }, closure.getAvailableTables());
132     }
133
134     /**
135      * Tests that adding and removing a table from a set containing a
136      * related join table does not cause the reordering or the references (that is,
137      * it is still the join table that is marked as referenced, not the tables it joins).
138      */

139     public void testRemoveDoesNotCauseReferenceReordering() {
140         closure.addTables(Collections.singleton(provider.getTableByName("ROOM")));
141         closure.addTables(Collections.singleton(provider.getTableByName("FOO")));
142         closure.addTables(Collections.singleton(provider.getTableByName("STUDENT")));
143
144         // the result is ok, tested in testRecursiveJoin()
145

146         closure.addTables(Collections.singleton(provider.getTableByName("BAR")));
147
148         assertTables(new String JavaDoc[] { "BAR", "FOO", "ROOM", "STUDENT" }, closure.getWantedTables());
149         assertTables(new String JavaDoc[] { "FOO_ROOM_STUDENT", "ROOM_STUDENT" }, closure.getReferencedTables());
150         assertTables(new String JavaDoc[] { "BAR", "FOO", "FOO_ROOM_STUDENT", "ROOM", "ROOM_STUDENT", "STUDENT" }, closure.getSelectedTables());
151         assertTables(new String JavaDoc[] { "STUDENT_TEACHER", "TEACHER" }, closure.getAvailableTables());
152
153         closure.removeTables(Collections.singleton(provider.getTableByName("BAR")));
154
155         assertTables(new String JavaDoc[] { "FOO", "ROOM", "STUDENT" }, closure.getWantedTables());
156         assertTables(new String JavaDoc[] { "FOO_ROOM_STUDENT", "ROOM_STUDENT" }, closure.getReferencedTables());
157         assertTables(new String JavaDoc[] { "FOO", "FOO_ROOM_STUDENT", "ROOM", "ROOM_STUDENT", "STUDENT" }, closure.getSelectedTables());
158         assertTables(new String JavaDoc[] { "BAR", "STUDENT_TEACHER", "TEACHER" }, closure.getAvailableTables());
159
160         closure.addTables(Collections.singleton(provider.getTableByName("TEACHER")));
161
162         assertTables(new String JavaDoc[] { "FOO", "ROOM", "STUDENT", "TEACHER" }, closure.getWantedTables());
163         assertTables(new String JavaDoc[] { "FOO_ROOM_STUDENT", "ROOM_STUDENT", "STUDENT_TEACHER" }, closure.getReferencedTables());
164         assertTables(new String JavaDoc[] { "FOO", "FOO_ROOM_STUDENT", "ROOM", "ROOM_STUDENT", "STUDENT", "STUDENT_TEACHER", "TEACHER" }, closure.getSelectedTables());
165         assertTables(new String JavaDoc[] { "BAR" }, closure.getAvailableTables());
166
167         closure.removeTables(Collections.singleton(provider.getTableByName("TEACHER")));
168
169         assertTables(new String JavaDoc[] { "FOO", "ROOM", "STUDENT" }, closure.getWantedTables());
170         assertTables(new String JavaDoc[] { "FOO_ROOM_STUDENT", "ROOM_STUDENT" }, closure.getReferencedTables());
171         assertTables(new String JavaDoc[] { "FOO", "FOO_ROOM_STUDENT", "ROOM", "ROOM_STUDENT", "STUDENT" }, closure.getSelectedTables());
172         assertTables(new String JavaDoc[] { "BAR", "STUDENT_TEACHER", "TEACHER" }, closure.getAvailableTables());
173     }
174
175     /**
176      * Tests that an already selected table is never added as a join table. For example,
177      * adding ROOM, STUDENT, STUDENT_TEACHER and TEACHER will add ROOM_STUDENT as a join
178      * table, but should not mark STUDENT_TEACHER as a join (referenced) table.
179      */

180     public void testNeverAddingAlreadySelectedTablesAsJoinTables() {
181         HashSet JavaDoc<Table> tables = new HashSet JavaDoc();
182         tables.add(provider.getTableByName("ROOM"));
183         tables.add(provider.getTableByName("STUDENT"));
184         tables.add(provider.getTableByName("STUDENT_TEACHER"));
185         tables.add(provider.getTableByName("TEACHER"));
186
187         closure.addTables(tables);
188
189         assertTables(new String JavaDoc[] { "ROOM", "STUDENT", "STUDENT_TEACHER", "TEACHER" }, closure.getWantedTables());
190         assertTables(new String JavaDoc[] { "STUDENT", "TEACHER", "ROOM_STUDENT" }, closure.getReferencedTables());
191         assertTables(new String JavaDoc[] { "ROOM", "ROOM_STUDENT", "STUDENT", "STUDENT_TEACHER", "TEACHER" }, closure.getSelectedTables());
192         assertTables(new String JavaDoc[] { "BAR", "FOO", "FOO_ROOM_STUDENT" }, closure.getAvailableTables());
193     }
194
195     private void assertTables(String JavaDoc[] expected, Set JavaDoc<Table> actual) {
196         assertEquals(expected.length, actual.size());
197         for (String JavaDoc tableName : expected) {
198             assertNotNull(actual.contains(tableName));
199         }
200     }
201 }
202
Popular Tags