KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import junit.framework.TestCase;
29 import org.netbeans.modules.j2ee.persistence.wizard.fromdb.Table.DisabledReason;
30
31 /**
32  *
33  * @author Andrei Badea
34  */

35 public class TableClosureDisabledTest extends TestCase {
36
37     private TableProviderImpl provider;
38     private TableClosure closure;
39
40     public TableClosureDisabledTest(String JavaDoc testName) {
41         super(testName);
42     }
43
44     public void setUp() {
45         Map JavaDoc<String JavaDoc, Set JavaDoc<String JavaDoc>> tablesAndRefs = new HashMap JavaDoc<String JavaDoc, Set JavaDoc<String JavaDoc>>();
46         Map JavaDoc<String JavaDoc, DisabledReason> disabledReasons = new HashMap JavaDoc<String JavaDoc, DisabledReason>();
47         Set JavaDoc<String JavaDoc> empty = Collections.emptySet();
48
49         tablesAndRefs.put("ROOM", empty);
50         tablesAndRefs.put("STUDENT", empty);
51         tablesAndRefs.put("TEACHER", empty);
52         tablesAndRefs.put("STUDENT_TEACHER", new HashSet JavaDoc(Arrays.asList(new String JavaDoc[] { "TEACHER", "STUDENT" })));
53         tablesAndRefs.put("ZOO1", empty);
54         tablesAndRefs.put("ZOO2", empty);
55         tablesAndRefs.put("ZOO1_ZOO2", new HashSet JavaDoc(Arrays.asList(new String JavaDoc[] { "ZOO1", "ZOO2" })));
56
57         disabledReasons.put("ROOM", new DisabledReason("Disabled", "Description"));
58         disabledReasons.put("STUDENT", new DisabledReason("Disabled", "Description"));
59         disabledReasons.put("ZOO1_ZOO2", new DisabledReason("Disabled", "Description"));
60
61         provider = new TableProviderImpl(tablesAndRefs, disabledReasons);
62         closure = new TableClosure(provider);
63     }
64
65     public void tearDown() {
66         closure = null;
67     }
68
69     public void testAddAllWithClosureEnabledDoesntAddDisabledTables() {
70         closure.addAllTables();
71
72
73         assertTables(new String JavaDoc[] { "STUDENT_TEACHER", "TEACHER", "ZOO1", "ZOO2" }, closure.getWantedTables());
74         assertTables(new String JavaDoc[] { "TEACHER" }, closure.getReferencedTables());
75         assertTables(new String JavaDoc[] { "STUDENT_TEACHER", "TEACHER", "ZOO1", "ZOO2" }, closure.getSelectedTables());
76         assertTables(new String JavaDoc[] { "ROOM", "STUDENT", "ZOO1_ZOO2" }, closure.getAvailableTables());
77     }
78
79     public void testAddAllWithClosureDisabledDoesntAddDisabledTables() {
80         closure.setClosureEnabled(false);
81         closure.addAllTables();
82
83         assertTables(new String JavaDoc[] { "STUDENT_TEACHER", "TEACHER", "ZOO1", "ZOO2" }, closure.getWantedTables());
84         assertTables(new String JavaDoc[] { }, closure.getReferencedTables());
85         assertTables(new String JavaDoc[] { "STUDENT_TEACHER", "TEACHER", "ZOO1", "ZOO2" }, closure.getSelectedTables());
86         assertTables(new String JavaDoc[] { "ROOM", "STUDENT", "ZOO1_ZOO2" }, closure.getAvailableTables());
87     }
88
89     public void testCannotAddDisabledReferencedTable() {
90         closure.addTables(Collections.singleton(provider.getTableByName("STUDENT_TEACHER")));
91
92         // STUDENT, which is also referenced, but disabled, was not added
93
assertTables(new String JavaDoc[] { "STUDENT_TEACHER" }, closure.getWantedTables());
94         assertTables(new String JavaDoc[] { "TEACHER" }, closure.getReferencedTables());
95         assertTables(new String JavaDoc[] { "STUDENT_TEACHER", "TEACHER" }, closure.getSelectedTables());
96         assertTables(new String JavaDoc[] { "ROOM", "STUDENT", "ZOO1", "ZOO2", "ZOO1_ZOO2" }, closure.getAvailableTables());
97     }
98
99     public void testCannotAddDisabledJoinTable() {
100         closure.addTables(Collections.singleton(provider.getTableByName("ZOO1")));
101         closure.addTables(Collections.singleton(provider.getTableByName("ZOO2")));
102
103         // ZOO1_ZOO2, which is a join table for the added tables, but disabled, was not added
104
assertTables(new String JavaDoc[] { "ZOO1", "ZOO2" }, closure.getWantedTables());
105         assertTables(new String JavaDoc[] { }, closure.getReferencedTables());
106         assertTables(new String JavaDoc[] { "ZOO1", "ZOO2" }, closure.getSelectedTables());
107         assertTables(new String JavaDoc[] { "ROOM", "STUDENT", "STUDENT_TEACHER", "TEACHER", "ZOO1_ZOO2" }, closure.getAvailableTables());
108     }
109
110     private void assertTables(String JavaDoc[] expected, Set JavaDoc<Table> actual) {
111         assertEquals(expected.length, actual.size());
112         for (String JavaDoc tableName : expected) {
113             assertNotNull(actual.contains(tableName));
114         }
115     }
116 }
117
Popular Tags