1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.fromdb; 21 22 import java.beans.PropertyChangeListener ; 23 import java.util.Collections ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import javax.swing.Icon ; 28 import javax.swing.event.ChangeEvent ; 29 import javax.swing.event.ChangeListener ; 30 import org.netbeans.api.project.SourceGroup; 31 import org.netbeans.junit.NbTestCase; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.FileUtil; 34 35 39 public class SelectedTablesTest extends NbTestCase { 40 41 public SelectedTablesTest(String testName) { 42 super(testName); 43 } 44 45 public void testBasic() throws Exception { 46 class CL implements ChangeListener { 47 private int changeCount; 48 49 public void stateChanged(ChangeEvent event) { 50 changeCount++; 51 } 52 } 53 54 clearWorkDir(); 55 FileObject workDirFO = FileUtil.toFileObject(getWorkDir()); 56 FileObject locationFO = workDirFO.createFolder("location"); 57 String package1Name = "package1"; 58 FileObject package1FO = locationFO.createFolder(package1Name); 59 String package2Name = "package2"; 60 FileObject package2FO = locationFO.createFolder(package2Name); 61 package1FO.createData("Table3", "java"); 62 SourceGroup location = new SourceGroupImpl(locationFO); 63 64 Map <String , Set <String >> tablesAndRefs = new HashMap <String , Set <String >>(); 65 tablesAndRefs.put("TABLE1", Collections.<String >emptySet()); 66 tablesAndRefs.put("TABLE2", Collections.<String >emptySet()); 67 68 TableProviderImpl provider = new TableProviderImpl(tablesAndRefs); 69 TableClosure closure = new TableClosure(provider); 70 PersistenceGenerator persistenceGen = new PersistenceGeneratorImpl(); 71 72 SelectedTables selectedTables = new SelectedTables(persistenceGen, closure, location, package1Name); 73 CL cl = new CL(); 74 selectedTables.addChangeListener(cl); 75 76 assertEquals(0, selectedTables.getTables().size()); 77 assertNull(selectedTables.getFirstProblem()); 78 assertEquals(0, cl.changeCount); 79 80 closure.addTables(Collections.singleton(provider.getTableByName("TABLE1"))); 81 82 assertEquals(1, selectedTables.getTables().size()); 83 assertNull(selectedTables.getFirstProblem()); 84 assertEquals(1, cl.changeCount); 85 86 Table table = provider.getTableByName("TABLE2"); 87 closure.addTables(Collections.singleton(table)); 88 89 assertEquals(2, selectedTables.getTables().size()); 90 assertNull(selectedTables.getFirstProblem()); 91 assertEquals(2, cl.changeCount); 92 93 selectedTables.setClassName(table,"Table@"); 94 95 assertEquals(2, selectedTables.getTables().size()); 96 assertEquals(SelectedTables.Problem.NO_JAVA_IDENTIFIER, selectedTables.getFirstProblem()); 97 assertEquals(3, cl.changeCount); 98 99 selectedTables.setClassName(table,"SELECT"); 100 101 assertEquals(2, selectedTables.getTables().size()); 102 assertEquals(SelectedTables.Problem.JPA_QL_IDENTIFIER, selectedTables.getFirstProblem()); 103 assertEquals(4, cl.changeCount); 104 105 selectedTables.setClassName(table,"Table3"); 106 107 assertEquals(2, selectedTables.getTables().size()); 108 assertEquals(SelectedTables.Problem.ALREADY_EXISTS, selectedTables.getFirstProblem()); 109 assertEquals(5, cl.changeCount); 110 111 selectedTables.setTargetFolder(location, package2Name); 112 113 assertEquals(2, selectedTables.getTables().size()); 114 assertNull(selectedTables.getFirstProblem()); 115 assertEquals(6, cl.changeCount); 116 } 117 118 public static final class SourceGroupImpl implements SourceGroup { 119 120 private final FileObject rootFolder; 121 122 public SourceGroupImpl(FileObject rootFolder) { 123 this.rootFolder = rootFolder; 124 } 125 126 public void addPropertyChangeListener(PropertyChangeListener listener) { 127 } 128 129 public void removePropertyChangeListener(PropertyChangeListener listener) { 130 } 131 132 public boolean contains(FileObject file) throws IllegalArgumentException { 133 return rootFolder.equals(file) || FileUtil.isParentOf(rootFolder, file); 134 } 135 136 public Icon getIcon(boolean opened) { 137 return null; 138 } 139 140 public FileObject getRootFolder() { 141 return rootFolder; 142 } 143 144 public String getName() { 145 return null; 146 } 147 148 public String getDisplayName() { 149 return null; 150 } 151 } 152 } 153 | Popular Tags |