KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyChangeListener JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.swing.Icon JavaDoc;
28 import javax.swing.event.ChangeEvent JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
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 /**
36  *
37  * @author Andrei Badea
38  */

39 public class SelectedTablesTest extends NbTestCase {
40
41     public SelectedTablesTest(String JavaDoc testName) {
42         super(testName);
43     }
44
45     public void testBasic() throws Exception JavaDoc {
46         class CL implements ChangeListener JavaDoc {
47             private int changeCount;
48
49             public void stateChanged(ChangeEvent JavaDoc event) {
50                 changeCount++;
51             }
52         }
53
54         clearWorkDir();
55         FileObject workDirFO = FileUtil.toFileObject(getWorkDir());
56         FileObject locationFO = workDirFO.createFolder("location");
57         String JavaDoc package1Name = "package1";
58         FileObject package1FO = locationFO.createFolder(package1Name);
59         String JavaDoc package2Name = "package2";
60         FileObject package2FO = locationFO.createFolder(package2Name);
61         package1FO.createData("Table3", "java");
62         SourceGroup location = new SourceGroupImpl(locationFO);
63
64         Map JavaDoc<String JavaDoc, Set JavaDoc<String JavaDoc>> tablesAndRefs = new HashMap JavaDoc<String JavaDoc, Set JavaDoc<String JavaDoc>>();
65         tablesAndRefs.put("TABLE1", Collections.<String JavaDoc>emptySet());
66         tablesAndRefs.put("TABLE2", Collections.<String JavaDoc>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 JavaDoc listener) {
127         }
128
129         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
130         }
131
132         public boolean contains(FileObject file) throws IllegalArgumentException JavaDoc {
133             return rootFolder.equals(file) || FileUtil.isParentOf(rootFolder, file);
134         }
135
136         public Icon JavaDoc getIcon(boolean opened) {
137             return null;
138         }
139
140         public FileObject getRootFolder() {
141             return rootFolder;
142         }
143
144         public String JavaDoc getName() {
145             return null;
146         }
147
148         public String JavaDoc getDisplayName() {
149             return null;
150         }
151     }
152 }
153
Popular Tags