KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > io > swing > FileComboBoxBrowserTest


1 /*
2  * FileComboBoxBrowserTest.java
3  * JUnit based test
4  *
5  * Created on 4. August 2006, 14:09
6  */

7 /*
8  * Copyright 2006 Schlichtherle IT Services
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */

22
23 package de.schlichtherle.io.swing;
24
25 import de.schlichtherle.io.File;
26 import java.awt.event.KeyEvent JavaDoc;
27 import java.io.FilenameFilter JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30 import java.text.Collator JavaDoc;
31 import java.util.Arrays JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.LinkedHashSet JavaDoc;
34 import java.util.LinkedList JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Set JavaDoc;
37 import java.util.SortedSet JavaDoc;
38 import java.util.TreeSet JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41 import javax.swing.ComboBoxModel JavaDoc;
42 import javax.swing.JComboBox JavaDoc;
43
44 import junit.framework.*;
45
46 import org.netbeans.jemmy.JemmyProperties;
47 import org.netbeans.jemmy.TestOut;
48 import org.netbeans.jemmy.operators.JFrameOperator;
49 import org.netbeans.jemmy.operators.JTextComponentOperator;
50
51 /**
52  * @author Christian Schlichtherle
53  * @since TrueZIP 6.2
54  * @version @version@
55  */

56 public class FileComboBoxBrowserTest extends TestCase {
57     static {
58         JemmyProperties.setCurrentOutput(TestOut.getNullOutput()); // shut up!
59
}
60
61     private static final Logger JavaDoc logger
62             = Logger.getLogger(FileComboBoxBrowserTest.class.getName());
63
64     public FileComboBoxBrowserTest(String JavaDoc testName) {
65         super(testName);
66     }
67
68     protected void setUp() throws Exception JavaDoc {
69         
70     }
71
72     protected void tearDown() throws Exception JavaDoc {
73     }
74
75     /**
76      * Test of directory property, of class de.schlichtherle.io.swing.FileComboBoxBrowser.
77      */

78     public void testDirectory() {
79         final FileComboBoxBrowser browser = new FileComboBoxBrowser();
80         final java.io.File JavaDoc cur = new File(".");
81         java.io.File JavaDoc dir;
82
83         dir = browser.getDirectory();
84         assertEquals(cur, dir);
85
86         browser.setDirectory(cur);
87         dir = browser.getDirectory();
88         assertSame(cur, dir);
89
90         browser.setDirectory(null);
91         dir = browser.getDirectory();
92         assertEquals(cur, dir);
93     }
94
95     /**
96      * Test of update method, of class de.schlichtherle.io.swing.FileComboBoxBrowser.
97      */

98     public void testState4Update() {
99         final FileComboBoxBrowser browser = new FileComboBoxBrowser();
100         try {
101             browser.update("");
102             fail("Calling the previous method should throw a NullPointerException!");
103         } catch (NullPointerException JavaDoc expected) {
104         }
105
106         final JComboBox JavaDoc combo = new JComboBox JavaDoc();
107         browser.setComboBox(combo);
108         browser.update("");
109     }
110
111     public void testAutoCompletion() throws IOException JavaDoc {
112         java.io.File JavaDoc dir = new java.io.File JavaDoc(".");
113         testAutoCompletion(dir);
114         testAutoCompletion(dir.getCanonicalFile());
115
116         dir = new File(".");
117         testAutoCompletion(dir);
118         testAutoCompletion(dir.getCanonicalFile());
119     }
120
121     public void testAutoCompletion(final java.io.File JavaDoc dir) {
122         final String JavaDoc[] entries = dir.list();
123         if (entries == null || entries.length == 0) {
124             logger.warning("Current directory does not contain any files - skipping test!");
125             return;
126         }
127
128         final JComboBox JavaDoc combo = new JComboBox JavaDoc();
129         final FileComboBoxBrowser browser = new FileComboBoxBrowser();
130         browser.setDirectory(dir);
131
132         for (int i = 0; i < entries.length; i++) {
133             final String JavaDoc entry = entries[i];
134             for (int j = 0; j < entry.length(); j++)
135                 testAutoCompletion(browser, combo, entry.substring(0, j));
136         }
137     }
138
139     private static void testAutoCompletion(
140             final FileComboBoxBrowser browser,
141             final JComboBox JavaDoc combo,
142             final String JavaDoc initials) {
143         browser.setComboBox(null); // reset
144
combo.removeAllItems(); // reset
145
combo.setEditable(true);
146         combo.setSelectedItem(initials);
147         browser.setComboBox(combo); // initialize
148
assertEquals(filter(initials), asList(combo));
149     }
150
151     private static List JavaDoc filter(final String JavaDoc initials) {
152         final String JavaDoc[] entries = new File(".").list(new FilenameFilter JavaDoc() {
153             final int l = initials.length();
154
155             public boolean accept(java.io.File JavaDoc dir, String JavaDoc name) {
156                 if (name.length() >= l)
157                     return initials.equalsIgnoreCase(name.substring(0, l));
158                 else
159                     return false;
160             }
161         });
162         Arrays.sort(entries, Collator.getInstance());
163         return Arrays.asList(entries);
164     }
165
166     private static List JavaDoc asList(final JComboBox JavaDoc combo) {
167         final List JavaDoc list = new LinkedList JavaDoc();
168         final ComboBoxModel JavaDoc model = combo.getModel();
169         for (int i = 0, l = model.getSize(); i < l; i++)
170             list.add(model.getElementAt(i));
171         return list;
172     }
173
174     public void testGUI() throws InterruptedException JavaDoc, InvocationTargetException JavaDoc {
175         FileComboBoxPanel.main(new String JavaDoc[] { "." });
176
177         // TODO: This is far from being a comprehensive test.
178

179         final JFrameOperator frame = new JFrameOperator();
180         final JTextComponentOperator tc0 = new JTextComponentOperator(frame, 0);
181         final JTextComponentOperator tc1 = new JTextComponentOperator(frame, 1);
182
183         // Type a character in tc0, then check that it's appearing in tc1.
184
tc0.typeText("?");
185         assertEquals("?", tc1.getText());
186         
187         // Clear character in tc1, ensure that its cleared in tc0
188
tc1.clearText();
189         assertEquals("", tc0.getText());
190         
191         // Select first element in list of tc0 (entry in current directory),
192
// if any, and check its appearance in tc1.
193
tc0.pressKey(KeyEvent.VK_DOWN);
194         final String JavaDoc child = tc0.getText();
195         assertEquals(child, tc1.getText());
196     }
197 }
198
Popular Tags