KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Arrays JavaDoc;
23 import java.util.Collections JavaDoc;
24 import junit.framework.*;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29
30 /**
31  *
32  * @author Andrei Badea
33  */

34 public class TableClosureTest extends TestCase {
35
36     private TableProviderImpl provider;
37     private TableClosure closure;
38
39     public TableClosureTest(String JavaDoc testName) {
40         super(testName);
41     }
42
43     public void setUp() {
44         Map JavaDoc<String JavaDoc, Set JavaDoc<String JavaDoc>> tablesAndRefs = new HashMap JavaDoc<String JavaDoc, Set JavaDoc<String JavaDoc>>();
45         Set JavaDoc<String JavaDoc> empty = Collections.emptySet();
46
47         tablesAndRefs.put("A", Collections.singleton("B"));
48         tablesAndRefs.put("B", Collections.singleton("C"));
49         tablesAndRefs.put("C", empty);
50         tablesAndRefs.put("D", new HashSet JavaDoc(Arrays.asList(new String JavaDoc[] { "B", "F" })));
51         tablesAndRefs.put("E", Collections.singleton("A"));
52         tablesAndRefs.put("F", empty);
53         tablesAndRefs.put("G", Collections.singleton("G"));
54
55         provider = new TableProviderImpl(tablesAndRefs);
56         closure = new TableClosure(provider);
57     }
58
59     public void tearDown() {
60         closure = null;
61     }
62
63     public void testAddRemoveWithClosureEnabled() {
64         assertTables(new String JavaDoc[] { }, closure.getWantedTables());
65         assertTables(new String JavaDoc[] { }, closure.getReferencedTables());
66         assertTables(new String JavaDoc[] { }, closure.getSelectedTables());
67         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "E", "F", "G" }, closure.getAvailableTables());
68
69         closure.addTables(Collections.singleton(provider.getTableByName("F")));
70
71         assertTables(new String JavaDoc[] { "F" }, closure.getWantedTables());
72         assertTables(new String JavaDoc[] { }, closure.getReferencedTables());
73         assertTables(new String JavaDoc[] { "F" }, closure.getSelectedTables());
74         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "E", "G" }, closure.getAvailableTables());
75
76         closure.addTables(Collections.singleton(provider.getTableByName("D")));
77
78         assertTables(new String JavaDoc[] { "D", "F" }, closure.getWantedTables());
79         assertTables(new String JavaDoc[] { "B", "C", "F" }, closure.getReferencedTables());
80         assertTables(new String JavaDoc[] { "B", "C", "D", "F" }, closure.getSelectedTables());
81         assertTables(new String JavaDoc[] { "A", "E", "G" }, closure.getAvailableTables());
82
83         closure.addTables(Collections.singleton(provider.getTableByName("A")));
84
85         assertTables(new String JavaDoc[] { "A", "D", "F" }, closure.getWantedTables());
86         assertTables(new String JavaDoc[] { "B", "C", "F" }, closure.getReferencedTables());
87         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "F" }, closure.getSelectedTables());
88         assertTables(new String JavaDoc[] { "E", "G" }, closure.getAvailableTables());
89
90         // adding a table which references itself -- should not be included in the referenced
91
// tables, since it wouldn't be possible to remove it.
92
closure.addTables(Collections.singleton(provider.getTableByName("G")));
93
94         assertTables(new String JavaDoc[] { "A", "D", "F", "G" }, closure.getWantedTables());
95         assertTables(new String JavaDoc[] { "B", "C", "F" }, closure.getReferencedTables());
96         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "F", "G" }, closure.getSelectedTables());
97         assertTables(new String JavaDoc[] { "E" }, closure.getAvailableTables());
98
99         closure.removeTables(Collections.singleton(provider.getTableByName("D")));
100
101         assertTables(new String JavaDoc[] { "A", "F", "G" }, closure.getWantedTables());
102         assertTables(new String JavaDoc[] { "B", "C" }, closure.getReferencedTables());
103         assertTables(new String JavaDoc[] { "A", "B", "C", "F", "G" }, closure.getSelectedTables());
104         assertTables(new String JavaDoc[] { "D", "E" }, closure.getAvailableTables());
105
106         closure.removeTables(Collections.singleton(provider.getTableByName("G")));
107
108         assertTables(new String JavaDoc[] { "A", "F" }, closure.getWantedTables());
109         assertTables(new String JavaDoc[] { "B", "C" }, closure.getReferencedTables());
110         assertTables(new String JavaDoc[] { "A", "B", "C", "F" }, closure.getSelectedTables());
111         assertTables(new String JavaDoc[] { "D", "E", "G" }, closure.getAvailableTables());
112
113         closure.addTables(Collections.singleton(provider.getTableByName("C")));
114
115         assertTables(new String JavaDoc[] { "A", "C", "F" }, closure.getWantedTables());
116         assertTables(new String JavaDoc[] { "B", "C" }, closure.getReferencedTables());
117         assertTables(new String JavaDoc[] { "A", "B", "C", "F" }, closure.getSelectedTables());
118         assertTables(new String JavaDoc[] { "D", "E", "G" }, closure.getAvailableTables());
119
120         closure.removeTables(Collections.singleton(provider.getTableByName("A")));
121
122         assertTables(new String JavaDoc[] { "C", "F" }, closure.getWantedTables());
123         assertTables(new String JavaDoc[] { }, closure.getReferencedTables());
124         assertTables(new String JavaDoc[] { "C", "F" }, closure.getSelectedTables());
125         assertTables(new String JavaDoc[] { "A", "B", "D", "E", "G" }, closure.getAvailableTables());
126
127         closure.removeTables(Collections.singleton(provider.getTableByName("C")));
128
129         assertTables(new String JavaDoc[] { "F" }, closure.getWantedTables());
130         assertTables(new String JavaDoc[] { }, closure.getReferencedTables());
131         assertTables(new String JavaDoc[] { "F" }, closure.getSelectedTables());
132         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "E", "G" }, closure.getAvailableTables());
133
134         closure.removeTables(Collections.singleton(provider.getTableByName("F")));
135
136         assertTables(new String JavaDoc[] { }, closure.getWantedTables());
137         assertTables(new String JavaDoc[] { }, closure.getReferencedTables());
138         assertTables(new String JavaDoc[] { }, closure.getSelectedTables());
139         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "E", "F", "G" }, closure.getAvailableTables());
140
141         closure.addTables(new HashSet JavaDoc<Table>(Arrays.asList(new Table[] {
142             provider.getTableByName("A"),
143             provider.getTableByName("B"),
144             provider.getTableByName("D"),
145         })));
146
147         assertTables(new String JavaDoc[] { "A", "B", "D" }, closure.getWantedTables());
148         assertTables(new String JavaDoc[] { "B", "C", "F" }, closure.getReferencedTables());
149         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "F" }, closure.getSelectedTables());
150         assertTables(new String JavaDoc[] { "E", "G" }, closure.getAvailableTables());
151
152         // can't remove B, it is referenced
153
closure.removeTables(Collections.singleton(provider.getTableByName("B")));
154
155         assertTables(new String JavaDoc[] { "A", "B", "D" }, closure.getWantedTables());
156         assertTables(new String JavaDoc[] { "B", "C", "F" }, closure.getReferencedTables());
157         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "F" }, closure.getSelectedTables());
158         assertTables(new String JavaDoc[] { "E", "G" }, closure.getAvailableTables());
159
160         closure.removeTables(new HashSet JavaDoc<Table>(Arrays.asList(new Table[] {
161             provider.getTableByName("A"),
162             provider.getTableByName("D"),
163         })));
164
165         assertTables(new String JavaDoc[] { "B" }, closure.getWantedTables());
166         assertTables(new String JavaDoc[] { "C" }, closure.getReferencedTables());
167         assertTables(new String JavaDoc[] { "B", "C" }, closure.getSelectedTables());
168         assertTables(new String JavaDoc[] { "A", "D", "E", "F", "G" }, closure.getAvailableTables());
169
170         closure.removeTables(Collections.singleton(provider.getTableByName("B")));
171
172         assertTables(new String JavaDoc[] { }, closure.getWantedTables());
173         assertTables(new String JavaDoc[] { }, closure.getReferencedTables());
174         assertTables(new String JavaDoc[] { }, closure.getSelectedTables());
175         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "E", "F", "G" }, closure.getAvailableTables());
176     }
177
178     public void testClosureEnabledDisabled() {
179         closure.setClosureEnabled(false);
180
181         closure.addTables(new HashSet JavaDoc<Table>(Arrays.asList(new Table[] {
182             provider.getTableByName("A"),
183             provider.getTableByName("B"),
184             provider.getTableByName("D"),
185         })));
186
187         assertTables(new String JavaDoc[] { "A", "B", "D" }, closure.getWantedTables());
188         assertTables(new String JavaDoc[] { }, closure.getReferencedTables());
189         assertTables(new String JavaDoc[] { "A", "B", "D" }, closure.getSelectedTables());
190         assertTables(new String JavaDoc[] { "C", "E", "F", "G" }, closure.getAvailableTables());
191
192         closure.addTables(new HashSet JavaDoc<Table>(Arrays.asList(new Table[] {
193             provider.getTableByName("F"),
194             provider.getTableByName("G"),
195         })));
196
197         assertTables(new String JavaDoc[] { "A", "B", "D", "F", "G" }, closure.getWantedTables());
198         assertTables(new String JavaDoc[] { }, closure.getReferencedTables());
199         assertTables(new String JavaDoc[] { "A", "B", "D", "F", "G" }, closure.getSelectedTables());
200         assertTables(new String JavaDoc[] { "C", "E" }, closure.getAvailableTables());
201
202         closure.setClosureEnabled(true);
203
204         assertTables(new String JavaDoc[] { "A", "B", "D", "F", "G" }, closure.getWantedTables());
205         assertTables(new String JavaDoc[] { "B", "C", "F" }, closure.getReferencedTables());
206         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "F", "G" }, closure.getSelectedTables());
207         assertTables(new String JavaDoc[] { "E" }, closure.getAvailableTables());
208
209         closure.setClosureEnabled(false);
210
211         assertTables(new String JavaDoc[] { "A", "B", "D", "F", "G" }, closure.getWantedTables());
212         assertTables(new String JavaDoc[] { }, closure.getReferencedTables());
213         assertTables(new String JavaDoc[] { "A", "B", "D", "F", "G" }, closure.getSelectedTables());
214         assertTables(new String JavaDoc[] { "C", "E" }, closure.getAvailableTables());
215
216         closure.removeTables(new HashSet JavaDoc<Table>(Arrays.asList(new Table[] {
217             provider.getTableByName("A"),
218             provider.getTableByName("D"),
219             provider.getTableByName("F"),
220         })));
221
222         assertTables(new String JavaDoc[] { "B", "G" }, closure.getWantedTables());
223         assertTables(new String JavaDoc[] { }, closure.getReferencedTables());
224         assertTables(new String JavaDoc[] { "B", "G" }, closure.getSelectedTables());
225         assertTables(new String JavaDoc[] { "A", "C", "D", "E", "F" }, closure.getAvailableTables());
226
227         closure.removeTables(new HashSet JavaDoc<Table>(Arrays.asList(new Table[] {
228             provider.getTableByName("B"),
229             provider.getTableByName("G"),
230         })));
231
232         assertTables(new String JavaDoc[] { }, closure.getWantedTables());
233         assertTables(new String JavaDoc[] { }, closure.getReferencedTables());
234         assertTables(new String JavaDoc[] { }, closure.getSelectedTables());
235         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "E", "F", "G" }, closure.getAvailableTables());
236     }
237
238     public void testAddRemoveAll() {
239         closure.addAllTables();
240
241         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "E", "F", "G" }, closure.getWantedTables());
242         assertTables(new String JavaDoc[] { "A", "B", "C", "F" }, closure.getReferencedTables());
243         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "E", "F", "G" }, closure.getSelectedTables());
244         assertTables(new String JavaDoc[] { }, closure.getAvailableTables());
245
246         closure.setClosureEnabled(false);
247
248         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "E", "F", "G" }, closure.getWantedTables());
249         assertTables(new String JavaDoc[] { }, closure.getReferencedTables());
250         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "E", "F", "G" }, closure.getSelectedTables());
251         assertTables(new String JavaDoc[] { }, closure.getAvailableTables());
252
253         closure.setClosureEnabled(true);
254
255         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "E", "F", "G" }, closure.getWantedTables());
256         assertTables(new String JavaDoc[] { "A", "B", "C", "F" }, closure.getReferencedTables());
257         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "E", "F", "G" }, closure.getSelectedTables());
258         assertTables(new String JavaDoc[] { }, closure.getAvailableTables());
259
260         closure.removeAllTables();
261
262         assertTables(new String JavaDoc[] { }, closure.getWantedTables());
263         assertTables(new String JavaDoc[] { }, closure.getReferencedTables());
264         assertTables(new String JavaDoc[] { }, closure.getSelectedTables());
265         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "E", "F", "G" }, closure.getAvailableTables());
266
267         closure.setClosureEnabled(false);
268         closure.addAllTables();
269
270         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "E", "F", "G" }, closure.getWantedTables());
271         assertTables(new String JavaDoc[] { }, closure.getReferencedTables());
272         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "E", "F", "G" }, closure.getSelectedTables());
273         assertTables(new String JavaDoc[] { }, closure.getAvailableTables());
274
275         closure.removeAllTables();
276
277         assertTables(new String JavaDoc[] { }, closure.getWantedTables());
278         assertTables(new String JavaDoc[] { }, closure.getReferencedTables());
279         assertTables(new String JavaDoc[] { }, closure.getSelectedTables());
280         assertTables(new String JavaDoc[] { "A", "B", "C", "D", "E", "F", "G" }, closure.getAvailableTables());
281     }
282
283     private void assertTables(String JavaDoc[] expected, Set JavaDoc<Table> actual) {
284         assertEquals(expected.length, actual.size());
285         for (String JavaDoc tableName : expected) {
286             assertNotNull(actual.contains(tableName));
287         }
288     }
289 }
290
Popular Tags