FileTest.java revision 3819a76e7c1f49253f0e077bd497f149340c02b8
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.apache.harmony.luni.tests.java.io;
18
19import java.io.File;
20
21import junit.framework.TestCase;
22
23import org.apache.harmony.testframework.serialization.SerializationTest;
24
25import dalvik.annotation.TestLevel;
26import dalvik.annotation.TestTargetClass;
27import dalvik.annotation.TestTargetNew;
28
29@TestTargetClass(File.class)
30public class FileTest extends TestCase {
31
32    /**
33     * @tests java.io.File#File(java.io.File, java.lang.String)
34     */
35    @TestTargetNew(
36        level = TestLevel.PARTIAL_COMPLETE,
37        notes = "",
38        method = "File",
39        args = {java.io.File.class, java.lang.String.class}
40    )
41    public void test_ConstructorLjava_io_FileLjava_lang_String() {
42        // Regression test for HARMONY-21
43        File path = new File("/dir/file");
44        File root = new File("/");
45        File file = new File(root, "/dir/file");
46        assertEquals("Assert 1: wrong path result ", path.getPath(), file
47                .getPath());
48        assertFalse("Assert 1.1: path absolute ", new File("\\\\\\a\b").isAbsolute());
49        assertTrue("Assert 1.1: path absolute ", new File("///a/b").isAbsolute());
50
51        // Test data used in a few places below
52        String dirName = System.getProperty("java.io.tmpdir");
53        String fileName = "input.tst";
54
55        // change user.dir to a folder that's writeable on android.
56        String oldUserDir = System.getProperty("user.dir");
57        System.setProperty("user.dir", dirName);
58
59        // Check filename is preserved correctly
60        File d = new File(dirName);
61        File f = new File(d, fileName);
62        if (!dirName
63                .regionMatches((dirName.length() - 1), File.separator, 0, 1)) {
64            dirName += File.separator;
65        }
66        dirName += fileName;
67        assertTrue("Assert 2: Created incorrect file " + f.getPath(), f
68                .getPath().equals(dirName));
69
70        // Check null argument is handled
71        try {
72            f = new File(d, null);
73            fail("Assert 3: NullPointerException not thrown.");
74        } catch (NullPointerException e) {
75            // Expected.
76        }
77
78        f = new File((File) null, fileName);
79        assertEquals("Assert 4: Created incorrect file " + f.getPath(), dirName,
80                f.getAbsolutePath());
81
82        // Regression for HARMONY-46
83        File f1 = new File("a");
84        File f2 = new File("a/");
85        assertEquals("Assert 5: Trailing slash file name is incorrect", f1, f2);
86
87        // reset user.dir
88        System.setProperty("user.dir", oldUserDir);
89    }
90
91    /**
92     * @tests java.io.File#hashCode()
93     */
94    @TestTargetNew(
95        level = TestLevel.COMPLETE,
96        notes = "",
97        method = "hashCode",
98        args = {}
99    )
100    public void test_hashCode() {
101        // Regression for HARMONY-53
102        String mixedFname = "SoMe FiLeNaMe";
103        File mfile = new File(mixedFname);
104        File lfile = new File(mixedFname.toLowerCase());
105
106        if (mfile.equals(lfile)) {
107            assertTrue("Assert 0: wrong hashcode", mfile.hashCode() == lfile.hashCode());
108        } else {
109            assertFalse("Assert 1: wrong hashcode", mfile.hashCode() == lfile.hashCode());
110        }
111    }
112
113    /**
114     * @tests java.io.File#getPath()
115     */
116    @TestTargetNew(
117        level = TestLevel.COMPLETE,
118        notes = "",
119        method = "getPath",
120        args = {}
121    )
122    public void test_getPath() {
123        // Regression for HARMONY-444
124        File file;
125        String separator = File.separator;
126
127        file = new File((File) null, "x/y/z");
128        assertEquals("x" + separator + "y" + separator + "z", file.getPath());
129
130        file = new File((String) null, "x/y/z");
131        assertEquals("x" + separator + "y" + separator + "z", file.getPath());
132    }
133
134    /**
135     * @tests java.io.File#getPath()
136     */
137    @TestTargetNew(
138        level = TestLevel.COMPLETE,
139        notes = "",
140        method = "getPath",
141        args = {}
142    )
143    public void test_getPath_With_Empty_FileName() {
144        // Regression for HARMONY-829
145        String f1ParentName = "01";
146        File f1 = new File(f1ParentName, "");
147        assertEquals(f1ParentName, f1.getPath());
148
149        String f2ParentName = "0";
150        File f2 = new File(f2ParentName, "");
151
152        assertEquals(-1, f2.compareTo(f1));
153        assertEquals(1, f1.compareTo(f2));
154
155        File parent = new File(System.getProperty("java.io.tmpdir"));
156        File f3 = new File(parent, "");
157
158        assertEquals(parent.getPath(), f3.getPath());
159
160
161    }
162
163    /**
164     * @tests serialization/deserialization.
165     */
166    @TestTargetNew(
167        level = TestLevel.COMPLETE,
168        notes = "Verifies self serialization/deserialization.",
169        method = "!SerializationSelf",
170        args = {}
171    )
172    public void test_serialization_self() throws Exception {
173        File testFile = new File("test.ser");
174        SerializationTest.verifySelf(testFile);
175    }
176
177    /**
178     * @tests serialization/deserialization compatibility with RI.
179     */
180    @TestTargetNew(
181        level = TestLevel.COMPLETE,
182        notes = "Verifies serialization/deserialization compatibility.",
183        method = "!SerializationGolden",
184        args = {}
185    )
186    public void test_serialization_compatibility() throws Exception {
187        File file = new File("FileTest.golden.ser");
188        SerializationTest.verifyGolden(this, file);
189    }
190
191}
192