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 Anton Avtamonov
019: * @version $Revision$
020: */package javax.swing.filechooser;
021:
022: import java.io.File;
023: import javax.swing.SwingTestCase;
024: import org.apache.harmony.misc.SystemUtils;
025:
026: public class FileSystemViewTest extends SwingTestCase {
027: private FileSystemView view;
028:
029: private File file;
030:
031: public FileSystemViewTest(final String name) {
032: super (name);
033: }
034:
035: @Override
036: protected void setUp() throws Exception {
037: super .setUp();
038: timeoutDelay = 5 * DEFAULT_TIMEOUT_DELAY;
039: view = FileSystemView.getFileSystemView();
040: file = new File(new File(System.getProperty("user.home")),
041: new Integer((int) (Math.random() * 1000)).toString());
042: file.deleteOnExit();
043: }
044:
045: @Override
046: protected void tearDown() throws Exception {
047: view = null;
048: file.delete();
049: file = null;
050: }
051:
052: public void testGetFileSystemView() throws Exception {
053: assertNotNull(FileSystemView.getFileSystemView());
054: assertEquals(FileSystemView.getFileSystemView(), FileSystemView
055: .getFileSystemView());
056: }
057:
058: public void testIsRoot() throws Exception {
059: assertFalse(view.isRoot(file));
060: if (SystemUtils.getOS() == SystemUtils.OS_WINDOWS) {
061: assertEquals(1, view.getRoots().length);
062: assertNotEmpty(view.getRoots()[0].getName());
063: } else if (SystemUtils.getOS() == SystemUtils.OS_LINUX) {
064: assertEquals(1, view.getRoots().length);
065: assertEquals("/", view.getRoots()[0].getPath());
066: }
067: }
068:
069: public void testIsTraversable() throws Exception {
070: assertFalse(view.isTraversable(file).booleanValue());
071: file.createNewFile();
072: assertTrue(file.isFile());
073: assertFalse(view.isTraversable(file).booleanValue());
074: file.delete();
075: file.mkdir();
076: assertTrue(view.isTraversable(file).booleanValue());
077: file.delete();
078: }
079:
080: public void testGetSystemDisplayName() throws Exception {
081: assertEquals(file.getName(), view.getSystemDisplayName(file));
082: file = new File("a.b");
083: assertEquals(file.getName(), view.getSystemDisplayName(file));
084: file = new File("/a/b");
085: assertEquals(file.getName(), view.getSystemDisplayName(file));
086: File f = File.listRoots()[0];
087: assertNotSame(f.getName(), view.getSystemDisplayName(f));
088: }
089:
090: public void testGetSystemTypeDescription() throws Exception {
091: file.createNewFile();
092: assertNotEmpty(view.getSystemTypeDescription(file));
093: file.delete();
094: file.mkdir();
095: assertNotEmpty(view.getSystemTypeDescription(file));
096: }
097:
098: public void testGetRoot() throws Exception {
099: assertNotNull(File.listRoots());
100: assertTrue(File.listRoots().length > 0);
101: assertNotSame(File.listRoots(), File.listRoots());
102: }
103:
104: public void testGetSystemIcon() throws Exception {
105: }
106:
107: public void testCreateFileObject() throws Exception {
108: File dir = new File(System.getProperty("user.home"));
109: assertEquals(new File(dir, "!!!!!!!"), view.createFileObject(
110: dir, "!!!!!!!"));
111: assertEquals(new File(dir, "***"), view.createFileObject(dir,
112: "***"));
113: assertEquals(new File(dir, "/normal"), view.createFileObject(
114: dir, "/normal"));
115: assertEquals(new File(new File("any"), "/normal"), view
116: .createFileObject(new File("any"), "/normal"));
117: assertEquals(new File(new File("/"), "/normal"), view
118: .createFileObject(new File("/"), "/normal"));
119: }
120:
121: public void testGetChild() throws Exception {
122: File parent = new File("a");
123: assertEquals(new File("a", "b"), view.getChild(parent, "b"));
124: assertEquals(new File("b"), view.getChild(null, "b"));
125: }
126:
127: public void testGetDefaultDirectory() throws Exception {
128: assertNotNull(view.getDefaultDirectory());
129: }
130:
131: public void testGetHomeDirectory() throws Exception {
132: assertNotNull(view.getHomeDirectory());
133: }
134:
135: public void testGetFiles() throws Exception {
136: File dir = new File("a");
137: File f = new File(dir, "a");
138: try {
139: dir.mkdir();
140: assertEquals(0, view.getFiles(dir, false).length);
141: f.createNewFile();
142: assertEquals(1, view.getFiles(dir, false).length);
143: f.delete();
144: assertEquals(0, view.getFiles(dir, false).length);
145: } finally {
146: f.delete();
147: dir.delete();
148: }
149: }
150:
151: public void testHarmony5211() {
152: File[] roots = File.listRoots();
153: for (int i = 0; i < roots.length; i++)
154: assertTrue(view.isFileSystemRoot(roots[i]));
155: }
156:
157: private static void assertNotEmpty(final String name) {
158: assertNotNull(name);
159: assertTrue("name is empty", name.length() > 0);
160: }
161: }
|