FileTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
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 dalvik.annotation.TestInfo; 20import dalvik.annotation.TestLevel; 21import dalvik.annotation.TestTarget; 22import dalvik.annotation.TestTargetClass; 23 24import java.io.File; 25 26import org.apache.harmony.testframework.serialization.SerializationTest; 27 28import junit.framework.TestCase; 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 @TestInfo( 36 level = TestLevel.COMPLETE, 37 purpose = "", 38 targets = { 39 @TestTarget( 40 methodName = "File", 41 methodArgs = {java.io.File.class, java.lang.String.class} 42 ) 43 }) 44 public void test_ConstructorLjava_io_FileLjava_lang_String() { 45 // Regression test for HARMONY-21 46 File path = new File("/dir/file"); 47 File root = new File("/"); 48 File file = new File(root, "/dir/file"); 49 assertEquals("Assert 1: wrong path result ", path.getPath(), file 50 .getPath()); 51 assertTrue("Assert 1.1: path not absolute ", new File("\\\\\\a\b").isAbsolute()); 52 53 // Test data used in a few places below 54 String dirName = System.getProperty("user.dir"); 55 String fileName = "input.tst"; 56 57 // Check filename is preserved correctly 58 File d = new File(dirName); 59 File f = new File(d, fileName); 60 if (!dirName 61 .regionMatches((dirName.length() - 1), File.separator, 0, 1)) { 62 dirName += File.separator; 63 } 64 dirName += fileName; 65 assertTrue("Assert 2: Created incorrect file " + f.getPath(), f 66 .getPath().equals(dirName)); 67 68 // Check null argument is handled 69 try { 70 f = new File(d, null); 71 fail("Assert 3: NullPointerException not thrown."); 72 } catch (NullPointerException e) { 73 // Expected. 74 } 75 76 f = new File((File) null, fileName); 77 assertTrue("Assert 4: Created incorrect file " + f.getPath(), f 78 .getAbsolutePath().equals(dirName)); 79 80 // Regression for HARMONY-46 81 File f1 = new File("a"); 82 File f2 = new File("a/"); 83 assertEquals("Assert 5: Trailing slash file name is incorrect", f1, f2); 84 } 85 86 /** 87 * @tests java.io.File#hashCode() 88 */ 89 @TestInfo( 90 level = TestLevel.COMPLETE, 91 purpose = "", 92 targets = { 93 @TestTarget( 94 methodName = "hashCode", 95 methodArgs = {} 96 ) 97 }) 98 public void test_hashCode() { 99 // Regression for HARMONY-53 100 String mixedFname = "SoMe FiLeNaMe"; 101 File mfile = new File(mixedFname); 102 File lfile = new File(mixedFname.toLowerCase()); 103 104 if (mfile.equals(lfile)) { 105 assertTrue("Assert 0: wrong hashcode", mfile.hashCode() == lfile.hashCode()); 106 } else { 107 assertFalse("Assert 1: wrong hashcode", mfile.hashCode() == lfile.hashCode()); 108 } 109 } 110 111 /** 112 * @tests java.io.File#getPath() 113 */ 114 @TestInfo( 115 level = TestLevel.COMPLETE, 116 purpose = "", 117 targets = { 118 @TestTarget( 119 methodName = "getPath", 120 methodArgs = {} 121 ) 122 }) 123 public void test_getPath() { 124 // Regression for HARMONY-444 125 File file; 126 String separator = File.separator; 127 128 file = new File((File) null, "x/y/z"); 129 assertEquals("x" + separator + "y" + separator + "z", file.getPath()); 130 131 file = new File((String) null, "x/y/z"); 132 assertEquals("x" + separator + "y" + separator + "z", file.getPath()); 133 } 134 135 /** 136 * @tests java.io.File#getPath() 137 */ 138 @TestInfo( 139 level = TestLevel.COMPLETE, 140 purpose = "", 141 targets = { 142 @TestTarget( 143 methodName = "getPath", 144 methodArgs = {} 145 ) 146 }) 147 public void test_getPath_With_Empty_FileName() { 148 // Regression for HARMONY-829 149 String f1ParentName = "01"; 150 File f1 = new File(f1ParentName, ""); 151 assertEquals(f1ParentName, f1.getPath()); 152 153 String f2ParentName = "0"; 154 File f2 = new File(f2ParentName, ""); 155 156 assertEquals(-1, f2.compareTo(f1)); 157 assertEquals(1, f1.compareTo(f2)); 158 159 File parent = new File(System.getProperty("user.dir")); 160 File f3 = new File(parent, ""); 161 162 assertEquals(parent.getPath(), f3.getPath()); 163 164 165 } 166 167 /** 168 * @tests serialization/deserialization. 169 */ 170 @TestInfo( 171 level = TestLevel.COMPLETE, 172 purpose = "Verifies self serialization/deserialization.", 173 targets = { 174 @TestTarget( 175 methodName = "!SerializationSelf", 176 methodArgs = {} 177 ) 178 }) 179 public void test_serialization_self() throws Exception { 180 File testFile = new File("test.ser"); 181 SerializationTest.verifySelf(testFile); 182 } 183 184 /** 185 * @tests serialization/deserialization compatibility with RI. 186 */ 187 @TestInfo( 188 level = TestLevel.COMPLETE, 189 purpose = "Verifies serialization/deserialization compatibility.", 190 targets = { 191 @TestTarget( 192 methodName = "!SerializationGolden", 193 methodArgs = {} 194 ) 195 }) 196 public void test_serialization_compatibility() throws Exception { 197 File file = new File("FileTest.golden.ser"); 198 SerializationTest.verifyGolden(this, file); 199 } 200 201} 202