001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Dmitry A. Durnev
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.io.File;
023: import java.io.FilenameFilter;
024:
025: import junit.framework.TestCase;
026:
027: /**
028: * FileDialogTest
029: */
030: public class FileDialogTest extends TestCase {
031: Frame frame;
032: Dialog dialog;
033: FileDialog fd;
034:
035: public static void main(String[] args) {
036: }
037:
038: @Override
039: protected void setUp() throws Exception {
040: super .setUp();
041: frame = new Frame();
042: dialog = new Dialog(frame);
043: fd = new FileDialog(frame);
044: }
045:
046: @Override
047: protected void tearDown() throws Exception {
048: super .tearDown();
049: if (frame != null) {
050: frame.dispose();
051: }
052: }
053:
054: public final void testAddNotify() {
055: assertNull(fd.getGraphics());
056: assertNull(fd.getLayout());
057: fd.addNotify();
058: assertTrue(fd.isDisplayable());
059: assertNull(fd.getLayout());
060: assertNotNull(fd.getGraphics());
061: }
062:
063: public final void testParamString() {
064: String str = fd.paramString();
065: assertEquals("name is correct", 0, str.indexOf("filedlg"));
066: assertTrue(str.indexOf("modal") > 0);
067: assertTrue(str.indexOf("dir=") > 0);
068: assertTrue(str.indexOf("file=") > 0);
069: assertTrue(str.indexOf("load") > 0);
070: }
071:
072: private final void constructorTest() {
073: assertSame(frame, fd.getParent());
074: assertTrue(fd.isModal());
075: assertNull(fd.getFile());
076: assertNull(fd.getDirectory());
077: }
078:
079: private final void constructorTestDialog() {
080: assertSame(dialog, fd.getParent());
081: assertTrue(fd.isModal());
082: assertNull(fd.getFile());
083: assertNull(fd.getDirectory());
084: }
085:
086: /*
087: * Class under test for void FileDialog(java.awt.Frame)
088: */
089: public final void testFileDialogDialog() {
090: fd = new FileDialog(dialog);
091: constructorTestDialog();
092: assertEquals("", fd.getTitle());
093: assertEquals(FileDialog.LOAD, fd.getMode());
094:
095: }
096:
097: /*
098: * Class under test for void FileDialog(java.awt.Frame, java.lang.String)
099: */
100: public final void testFileDialogDialogString() {
101: String title = "Open";
102: fd = new FileDialog(dialog, title);
103: constructorTestDialog();
104: assertEquals(title, fd.getTitle());
105: assertEquals(FileDialog.LOAD, fd.getMode());
106: }
107:
108: /*
109: * Class under test for void FileDialog(java.awt.Frame, java.lang.String, int)
110: */
111: public final void testFileDialogDialogStringint() {
112: String title = "Save";
113: int mode = FileDialog.SAVE;
114: fd = new FileDialog(dialog, title, mode);
115: constructorTestDialog();
116: assertEquals(title, fd.getTitle());
117: assertEquals(mode, fd.getMode());
118: boolean iae = false;
119: try {
120: fd = new FileDialog(dialog, title, mode = -666);
121: } catch (IllegalArgumentException e) {
122: iae = true;
123: }
124: assertTrue(iae);
125: assertEquals(FileDialog.SAVE, fd.getMode());
126: }
127:
128: /*
129: * Class under test for void FileDialog(java.awt.Frame)
130: */
131: public final void testFileDialogFrame() {
132: constructorTest();
133: assertEquals("", fd.getTitle());
134: assertEquals(FileDialog.LOAD, fd.getMode());
135:
136: }
137:
138: /*
139: * Class under test for void FileDialog(java.awt.Frame, java.lang.String)
140: */
141: public final void testFileDialogFrameString() {
142: String title = "Open";
143: fd = new FileDialog(frame, title);
144: constructorTest();
145: assertEquals(title, fd.getTitle());
146: assertEquals(FileDialog.LOAD, fd.getMode());
147: }
148:
149: /*
150: * Class under test for void FileDialog(java.awt.Frame, java.lang.String, int)
151: */
152: public final void testFileDialogFrameStringint() {
153: String title = "Save";
154: int mode = FileDialog.SAVE;
155: fd = new FileDialog(frame, title, mode);
156: constructorTest();
157: assertEquals(title, fd.getTitle());
158: assertEquals(mode, fd.getMode());
159: boolean iae = false;
160: try {
161: fd = new FileDialog(frame, title, mode = -666);
162: } catch (IllegalArgumentException e) {
163: iae = true;
164: }
165: assertTrue(iae);
166: assertEquals(FileDialog.SAVE, fd.getMode());
167: }
168:
169: public final void testGetFile() {
170: assertNull(fd.getFile());
171: }
172:
173: public final void testGetDirectory() {
174: assertNull(fd.getDirectory());
175: }
176:
177: public final void testGetFilenameFilter() {
178: assertNull(fd.getFilenameFilter());
179: }
180:
181: public final void testGetMode() {
182: assertEquals(FileDialog.LOAD, fd.getMode());
183: }
184:
185: public final void testSetDirectory() {
186: String dir = "dir";
187: fd.setDirectory(dir);
188: assertEquals(dir, fd.getDirectory());
189: fd.setDirectory(null);
190: assertNull(fd.getDirectory());
191: fd.setFile(dir = "");
192: assertNull(fd.getDirectory());
193: }
194:
195: public final void testSetFile() {
196: String file = "file";
197: fd.setFile(file);
198: assertEquals(file, fd.getFile());
199: fd.setFile(null);
200: assertNull(fd.getFile());
201: fd.setFile(file = "");
202: assertNull(fd.getFile());
203: }
204:
205: public final void testSetFilenameFilter() {
206: FilenameFilter filter = new FilenameFilter() {
207: public boolean accept(File dir, String name) {
208: return false;
209: }
210: };
211: fd.setFilenameFilter(filter);
212: assertSame(filter, fd.getFilenameFilter());
213: fd.setFilenameFilter(null);
214: assertNull(fd.getFilenameFilter());
215: }
216:
217: public final void testSetMode() {
218: int mode = 1000;
219: boolean iae = false;
220: try {
221: fd.setMode(mode);
222: } catch (IllegalArgumentException e) {
223: iae = true;
224: }
225: assertTrue(iae);
226: assertEquals(FileDialog.LOAD, fd.getMode());
227: fd.setMode(mode = FileDialog.SAVE);
228: assertEquals(mode, fd.getMode());
229: fd.setMode(mode = FileDialog.LOAD);
230: assertEquals(mode, fd.getMode());
231: }
232:
233: }
|