1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.fromdb; 21 22 import java.util.Arrays ; 23 import java.util.Collections ; 24 import junit.framework.*; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.Map ; 28 import java.util.Set ; 29 30 34 public class TableClosureTest extends TestCase { 35 36 private TableProviderImpl provider; 37 private TableClosure closure; 38 39 public TableClosureTest(String testName) { 40 super(testName); 41 } 42 43 public void setUp() { 44 Map <String , Set <String >> tablesAndRefs = new HashMap <String , Set <String >>(); 45 Set <String > 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 (Arrays.asList(new String [] { "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 [] { }, closure.getWantedTables()); 65 assertTables(new String [] { }, closure.getReferencedTables()); 66 assertTables(new String [] { }, closure.getSelectedTables()); 67 assertTables(new String [] { "A", "B", "C", "D", "E", "F", "G" }, closure.getAvailableTables()); 68 69 closure.addTables(Collections.singleton(provider.getTableByName("F"))); 70 71 assertTables(new String [] { "F" }, closure.getWantedTables()); 72 assertTables(new String [] { }, closure.getReferencedTables()); 73 assertTables(new String [] { "F" }, closure.getSelectedTables()); 74 assertTables(new String [] { "A", "B", "C", "D", "E", "G" }, closure.getAvailableTables()); 75 76 closure.addTables(Collections.singleton(provider.getTableByName("D"))); 77 78 assertTables(new String [] { "D", "F" }, closure.getWantedTables()); 79 assertTables(new String [] { "B", "C", "F" }, closure.getReferencedTables()); 80 assertTables(new String [] { "B", "C", "D", "F" }, closure.getSelectedTables()); 81 assertTables(new String [] { "A", "E", "G" }, closure.getAvailableTables()); 82 83 closure.addTables(Collections.singleton(provider.getTableByName("A"))); 84 85 assertTables(new String [] { "A", "D", "F" }, closure.getWantedTables()); 86 assertTables(new String [] { "B", "C", "F" }, closure.getReferencedTables()); 87 assertTables(new String [] { "A", "B", "C", "D", "F" }, closure.getSelectedTables()); 88 assertTables(new String [] { "E", "G" }, closure.getAvailableTables()); 89 90 closure.addTables(Collections.singleton(provider.getTableByName("G"))); 93 94 assertTables(new String [] { "A", "D", "F", "G" }, closure.getWantedTables()); 95 assertTables(new String [] { "B", "C", "F" }, closure.getReferencedTables()); 96 assertTables(new String [] { "A", "B", "C", "D", "F", "G" }, closure.getSelectedTables()); 97 assertTables(new String [] { "E" }, closure.getAvailableTables()); 98 99 closure.removeTables(Collections.singleton(provider.getTableByName("D"))); 100 101 assertTables(new String [] { "A", "F", "G" }, closure.getWantedTables()); 102 assertTables(new String [] { "B", "C" }, closure.getReferencedTables()); 103 assertTables(new String [] { "A", "B", "C", "F", "G" }, closure.getSelectedTables()); 104 assertTables(new String [] { "D", "E" }, closure.getAvailableTables()); 105 106 closure.removeTables(Collections.singleton(provider.getTableByName("G"))); 107 108 assertTables(new String [] { "A", "F" }, closure.getWantedTables()); 109 assertTables(new String [] { "B", "C" }, closure.getReferencedTables()); 110 assertTables(new String [] { "A", "B", "C", "F" }, closure.getSelectedTables()); 111 assertTables(new String [] { "D", "E", "G" }, closure.getAvailableTables()); 112 113 closure.addTables(Collections.singleton(provider.getTableByName("C"))); 114 115 assertTables(new String [] { "A", "C", "F" }, closure.getWantedTables()); 116 assertTables(new String [] { "B", "C" }, closure.getReferencedTables()); 117 assertTables(new String [] { "A", "B", "C", "F" }, closure.getSelectedTables()); 118 assertTables(new String [] { "D", "E", "G" }, closure.getAvailableTables()); 119 120 closure.removeTables(Collections.singleton(provider.getTableByName("A"))); 121 122 assertTables(new String [] { "C", "F" }, closure.getWantedTables()); 123 assertTables(new String [] { }, closure.getReferencedTables()); 124 assertTables(new String [] { "C", "F" }, closure.getSelectedTables()); 125 assertTables(new String [] { "A", "B", "D", "E", "G" }, closure.getAvailableTables()); 126 127 closure.removeTables(Collections.singleton(provider.getTableByName("C"))); 128 129 assertTables(new String [] { "F" }, closure.getWantedTables()); 130 assertTables(new String [] { }, closure.getReferencedTables()); 131 assertTables(new String [] { "F" }, closure.getSelectedTables()); 132 assertTables(new String [] { "A", "B", "C", "D", "E", "G" }, closure.getAvailableTables()); 133 134 closure.removeTables(Collections.singleton(provider.getTableByName("F"))); 135 136 assertTables(new String [] { }, closure.getWantedTables()); 137 assertTables(new String [] { }, closure.getReferencedTables()); 138 assertTables(new String [] { }, closure.getSelectedTables()); 139 assertTables(new String [] { "A", "B", "C", "D", "E", "F", "G" }, closure.getAvailableTables()); 140 141 closure.addTables(new HashSet <Table>(Arrays.asList(new Table[] { 142 provider.getTableByName("A"), 143 provider.getTableByName("B"), 144 provider.getTableByName("D"), 145 }))); 146 147 assertTables(new String [] { "A", "B", "D" }, closure.getWantedTables()); 148 assertTables(new String [] { "B", "C", "F" }, closure.getReferencedTables()); 149 assertTables(new String [] { "A", "B", "C", "D", "F" }, closure.getSelectedTables()); 150 assertTables(new String [] { "E", "G" }, closure.getAvailableTables()); 151 152 closure.removeTables(Collections.singleton(provider.getTableByName("B"))); 154 155 assertTables(new String [] { "A", "B", "D" }, closure.getWantedTables()); 156 assertTables(new String [] { "B", "C", "F" }, closure.getReferencedTables()); 157 assertTables(new String [] { "A", "B", "C", "D", "F" }, closure.getSelectedTables()); 158 assertTables(new String [] { "E", "G" }, closure.getAvailableTables()); 159 160 closure.removeTables(new HashSet <Table>(Arrays.asList(new Table[] { 161 provider.getTableByName("A"), 162 provider.getTableByName("D"), 163 }))); 164 165 assertTables(new String [] { "B" }, closure.getWantedTables()); 166 assertTables(new String [] { "C" }, closure.getReferencedTables()); 167 assertTables(new String [] { "B", "C" }, closure.getSelectedTables()); 168 assertTables(new String [] { "A", "D", "E", "F", "G" }, closure.getAvailableTables()); 169 170 closure.removeTables(Collections.singleton(provider.getTableByName("B"))); 171 172 assertTables(new String [] { }, closure.getWantedTables()); 173 assertTables(new String [] { }, closure.getReferencedTables()); 174 assertTables(new String [] { }, closure.getSelectedTables()); 175 assertTables(new String [] { "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 <Table>(Arrays.asList(new Table[] { 182 provider.getTableByName("A"), 183 provider.getTableByName("B"), 184 provider.getTableByName("D"), 185 }))); 186 187 assertTables(new String [] { "A", "B", "D" }, closure.getWantedTables()); 188 assertTables(new String [] { }, closure.getReferencedTables()); 189 assertTables(new String [] { "A", "B", "D" }, closure.getSelectedTables()); 190 assertTables(new String [] { "C", "E", "F", "G" }, closure.getAvailableTables()); 191 192 closure.addTables(new HashSet <Table>(Arrays.asList(new Table[] { 193 provider.getTableByName("F"), 194 provider.getTableByName("G"), 195 }))); 196 197 assertTables(new String [] { "A", "B", "D", "F", "G" }, closure.getWantedTables()); 198 assertTables(new String [] { }, closure.getReferencedTables()); 199 assertTables(new String [] { "A", "B", "D", "F", "G" }, closure.getSelectedTables()); 200 assertTables(new String [] { "C", "E" }, closure.getAvailableTables()); 201 202 closure.setClosureEnabled(true); 203 204 assertTables(new String [] { "A", "B", "D", "F", "G" }, closure.getWantedTables()); 205 assertTables(new String [] { "B", "C", "F" }, closure.getReferencedTables()); 206 assertTables(new String [] { "A", "B", "C", "D", "F", "G" }, closure.getSelectedTables()); 207 assertTables(new String [] { "E" }, closure.getAvailableTables()); 208 209 closure.setClosureEnabled(false); 210 211 assertTables(new String [] { "A", "B", "D", "F", "G" }, closure.getWantedTables()); 212 assertTables(new String [] { }, closure.getReferencedTables()); 213 assertTables(new String [] { "A", "B", "D", "F", "G" }, closure.getSelectedTables()); 214 assertTables(new String [] { "C", "E" }, closure.getAvailableTables()); 215 216 closure.removeTables(new HashSet <Table>(Arrays.asList(new Table[] { 217 provider.getTableByName("A"), 218 provider.getTableByName("D"), 219 provider.getTableByName("F"), 220 }))); 221 222 assertTables(new String [] { "B", "G" }, closure.getWantedTables()); 223 assertTables(new String [] { }, closure.getReferencedTables()); 224 assertTables(new String [] { "B", "G" }, closure.getSelectedTables()); 225 assertTables(new String [] { "A", "C", "D", "E", "F" }, closure.getAvailableTables()); 226 227 closure.removeTables(new HashSet <Table>(Arrays.asList(new Table[] { 228 provider.getTableByName("B"), 229 provider.getTableByName("G"), 230 }))); 231 232 assertTables(new String [] { }, closure.getWantedTables()); 233 assertTables(new String [] { }, closure.getReferencedTables()); 234 assertTables(new String [] { }, closure.getSelectedTables()); 235 assertTables(new String [] { "A", "B", "C", "D", "E", "F", "G" }, closure.getAvailableTables()); 236 } 237 238 public void testAddRemoveAll() { 239 closure.addAllTables(); 240 241 assertTables(new String [] { "A", "B", "C", "D", "E", "F", "G" }, closure.getWantedTables()); 242 assertTables(new String [] { "A", "B", "C", "F" }, closure.getReferencedTables()); 243 assertTables(new String [] { "A", "B", "C", "D", "E", "F", "G" }, closure.getSelectedTables()); 244 assertTables(new String [] { }, closure.getAvailableTables()); 245 246 closure.setClosureEnabled(false); 247 248 assertTables(new String [] { "A", "B", "C", "D", "E", "F", "G" }, closure.getWantedTables()); 249 assertTables(new String [] { }, closure.getReferencedTables()); 250 assertTables(new String [] { "A", "B", "C", "D", "E", "F", "G" }, closure.getSelectedTables()); 251 assertTables(new String [] { }, closure.getAvailableTables()); 252 253 closure.setClosureEnabled(true); 254 255 assertTables(new String [] { "A", "B", "C", "D", "E", "F", "G" }, closure.getWantedTables()); 256 assertTables(new String [] { "A", "B", "C", "F" }, closure.getReferencedTables()); 257 assertTables(new String [] { "A", "B", "C", "D", "E", "F", "G" }, closure.getSelectedTables()); 258 assertTables(new String [] { }, closure.getAvailableTables()); 259 260 closure.removeAllTables(); 261 262 assertTables(new String [] { }, closure.getWantedTables()); 263 assertTables(new String [] { }, closure.getReferencedTables()); 264 assertTables(new String [] { }, closure.getSelectedTables()); 265 assertTables(new String [] { "A", "B", "C", "D", "E", "F", "G" }, closure.getAvailableTables()); 266 267 closure.setClosureEnabled(false); 268 closure.addAllTables(); 269 270 assertTables(new String [] { "A", "B", "C", "D", "E", "F", "G" }, closure.getWantedTables()); 271 assertTables(new String [] { }, closure.getReferencedTables()); 272 assertTables(new String [] { "A", "B", "C", "D", "E", "F", "G" }, closure.getSelectedTables()); 273 assertTables(new String [] { }, closure.getAvailableTables()); 274 275 closure.removeAllTables(); 276 277 assertTables(new String [] { }, closure.getWantedTables()); 278 assertTables(new String [] { }, closure.getReferencedTables()); 279 assertTables(new String [] { }, closure.getSelectedTables()); 280 assertTables(new String [] { "A", "B", "C", "D", "E", "F", "G" }, closure.getAvailableTables()); 281 } 282 283 private void assertTables(String [] expected, Set <Table> actual) { 284 assertEquals(expected.length, actual.size()); 285 for (String tableName : expected) { 286 assertNotNull(actual.contains(tableName)); 287 } 288 } 289 } 290 | Popular Tags |