1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.fromdb; 21 22 import java.io.IOException ; 23 import java.util.Arrays ; 24 import java.util.Collections ; 25 import junit.framework.*; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import org.netbeans.api.progress.ProgressHandle; 31 import org.openide.WizardDescriptor; 32 import org.openide.filesystems.FileObject; 33 34 38 public class TableClosureJoinTest extends TestCase { 39 40 private TableProviderImpl provider; 41 private TableClosure closure; 42 43 public TableClosureJoinTest(String testName) { 44 super(testName); 45 } 46 47 public void setUp() { 48 Map <String , Set <String >> tablesAndRefs = new HashMap <String , Set <String >>(); 49 Set <String > 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 (Arrays.asList(new String [] { "TEACHER", "STUDENT" }))); 57 tablesAndRefs.put("ROOM_STUDENT", new HashSet (Arrays.asList(new String [] { "STUDENT", "ROOM" }))); 58 tablesAndRefs.put("FOO_ROOM_STUDENT", new HashSet (Arrays.asList(new String [] { "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 [] { "STUDENT" }, closure.getWantedTables()); 72 assertTables(new String [] { }, closure.getReferencedTables()); 73 assertTables(new String [] { "STUDENT" }, closure.getSelectedTables()); 74 assertTables(new String [] { "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 [] { "STUDENT", "TEACHER" }, closure.getWantedTables()); 79 assertTables(new String [] { "STUDENT_TEACHER" }, closure.getReferencedTables()); 80 assertTables(new String [] { "STUDENT", "STUDENT_TEACHER", "TEACHER" }, closure.getSelectedTables()); 81 assertTables(new String [] { "BAR", "FOO", "FOO_ROOM_STUDENT", "ROOM", "ROOM_STUDENT" }, closure.getAvailableTables()); 82 83 closure.addTables(Collections.singleton(provider.getTableByName("ROOM"))); 84 85 assertTables(new String [] { "ROOM", "STUDENT", "TEACHER" }, closure.getWantedTables()); 86 assertTables(new String [] { "ROOM_STUDENT", "STUDENT_TEACHER" }, closure.getReferencedTables()); 87 assertTables(new String [] { "ROOM", "ROOM_STUDENT", "STUDENT", "STUDENT_TEACHER", "TEACHER" }, closure.getSelectedTables()); 88 assertTables(new String [] { "BAR", "FOO", "FOO_ROOM_STUDENT" }, closure.getAvailableTables()); 89 90 closure.removeTables(Collections.singleton(provider.getTableByName("STUDENT"))); 91 92 assertTables(new String [] { "ROOM", "TEACHER" }, closure.getWantedTables()); 93 assertTables(new String [] { }, closure.getReferencedTables()); 94 assertTables(new String [] { "ROOM", "TEACHER" }, closure.getSelectedTables()); 95 assertTables(new String [] { "BAR", "FOO", "FOO_ROOM_STUDENT", "ROOM_STUDENT", "STUDENT", "STUDENT_TEACHER" }, closure.getAvailableTables()); 96 97 closure.addAllTables(); 98 99 assertTables(new String [] { "BAR", "FOO", "FOO_ROOM_STUDENT", "ROOM", "ROOM_STUDENT", "STUDENT", "STUDENT_TEACHER", "TEACHER" }, closure.getWantedTables()); 105 assertTables(new String [] { "FOO", "ROOM", "ROOM_STUDENT", "STUDENT", "TEACHER" }, closure.getReferencedTables()); 106 assertTables(new String [] { "BAR", "FOO", "FOO_ROOM_STUDENT", "ROOM", "ROOM_STUDENT", "STUDENT", "STUDENT_TEACHER", "TEACHER" }, closure.getSelectedTables()); 107 assertTables(new String [] { }, closure.getAvailableTables()); 108 109 closure.removeAllTables(); 110 111 assertTables(new String [] { }, closure.getWantedTables()); 112 assertTables(new String [] { }, closure.getReferencedTables()); 113 assertTables(new String [] { }, closure.getSelectedTables()); 114 assertTables(new String [] { "BAR", "FOO", "FOO_ROOM_STUDENT", "ROOM", "ROOM_STUDENT", "STUDENT", "STUDENT_TEACHER", "TEACHER" }, closure.getAvailableTables()); 115 } 116 117 118 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 [] { "FOO", "ROOM", "STUDENT" }, closure.getWantedTables()); 129 assertTables(new String [] { "FOO_ROOM_STUDENT", "ROOM_STUDENT" }, closure.getReferencedTables()); 130 assertTables(new String [] { "FOO", "FOO_ROOM_STUDENT", "ROOM", "ROOM_STUDENT", "STUDENT" }, closure.getSelectedTables()); 131 assertTables(new String [] { "BAR", "STUDENT_TEACHER", "TEACHER" }, closure.getAvailableTables()); 132 } 133 134 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 146 closure.addTables(Collections.singleton(provider.getTableByName("BAR"))); 147 148 assertTables(new String [] { "BAR", "FOO", "ROOM", "STUDENT" }, closure.getWantedTables()); 149 assertTables(new String [] { "FOO_ROOM_STUDENT", "ROOM_STUDENT" }, closure.getReferencedTables()); 150 assertTables(new String [] { "BAR", "FOO", "FOO_ROOM_STUDENT", "ROOM", "ROOM_STUDENT", "STUDENT" }, closure.getSelectedTables()); 151 assertTables(new String [] { "STUDENT_TEACHER", "TEACHER" }, closure.getAvailableTables()); 152 153 closure.removeTables(Collections.singleton(provider.getTableByName("BAR"))); 154 155 assertTables(new String [] { "FOO", "ROOM", "STUDENT" }, closure.getWantedTables()); 156 assertTables(new String [] { "FOO_ROOM_STUDENT", "ROOM_STUDENT" }, closure.getReferencedTables()); 157 assertTables(new String [] { "FOO", "FOO_ROOM_STUDENT", "ROOM", "ROOM_STUDENT", "STUDENT" }, closure.getSelectedTables()); 158 assertTables(new String [] { "BAR", "STUDENT_TEACHER", "TEACHER" }, closure.getAvailableTables()); 159 160 closure.addTables(Collections.singleton(provider.getTableByName("TEACHER"))); 161 162 assertTables(new String [] { "FOO", "ROOM", "STUDENT", "TEACHER" }, closure.getWantedTables()); 163 assertTables(new String [] { "FOO_ROOM_STUDENT", "ROOM_STUDENT", "STUDENT_TEACHER" }, closure.getReferencedTables()); 164 assertTables(new String [] { "FOO", "FOO_ROOM_STUDENT", "ROOM", "ROOM_STUDENT", "STUDENT", "STUDENT_TEACHER", "TEACHER" }, closure.getSelectedTables()); 165 assertTables(new String [] { "BAR" }, closure.getAvailableTables()); 166 167 closure.removeTables(Collections.singleton(provider.getTableByName("TEACHER"))); 168 169 assertTables(new String [] { "FOO", "ROOM", "STUDENT" }, closure.getWantedTables()); 170 assertTables(new String [] { "FOO_ROOM_STUDENT", "ROOM_STUDENT" }, closure.getReferencedTables()); 171 assertTables(new String [] { "FOO", "FOO_ROOM_STUDENT", "ROOM", "ROOM_STUDENT", "STUDENT" }, closure.getSelectedTables()); 172 assertTables(new String [] { "BAR", "STUDENT_TEACHER", "TEACHER" }, closure.getAvailableTables()); 173 } 174 175 180 public void testNeverAddingAlreadySelectedTablesAsJoinTables() { 181 HashSet <Table> tables = new HashSet (); 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 [] { "ROOM", "STUDENT", "STUDENT_TEACHER", "TEACHER" }, closure.getWantedTables()); 190 assertTables(new String [] { "STUDENT", "TEACHER", "ROOM_STUDENT" }, closure.getReferencedTables()); 191 assertTables(new String [] { "ROOM", "ROOM_STUDENT", "STUDENT", "STUDENT_TEACHER", "TEACHER" }, closure.getSelectedTables()); 192 assertTables(new String [] { "BAR", "FOO", "FOO_ROOM_STUDENT" }, closure.getAvailableTables()); 193 } 194 195 private void assertTables(String [] expected, Set <Table> actual) { 196 assertEquals(expected.length, actual.size()); 197 for (String tableName : expected) { 198 assertNotNull(actual.contains(tableName)); 199 } 200 } 201 } 202 | Popular Tags |