PageRenderTime 79ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 1ms

/luni/src/test/java/tests/api/java/io/FileTest.java

https://bitbucket.org/cyanogenmod/android_libcore
Java | 2540 lines | 1959 code | 212 blank | 369 comment | 199 complexity | 2e56c6b9368380173f07c01bef477072 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package tests.api.java.io;
  18. import java.io.File;
  19. import java.io.FileFilter;
  20. import java.io.FileInputStream;
  21. import java.io.FileOutputStream;
  22. import java.io.FilenameFilter;
  23. import java.io.IOException;
  24. import java.io.ObjectStreamClass;
  25. import java.io.ObjectStreamField;
  26. import java.io.RandomAccessFile;
  27. import java.net.MalformedURLException;
  28. import java.net.URI;
  29. import java.net.URISyntaxException;
  30. import java.net.URL;
  31. import dalvik.annotation.AndroidOnly;
  32. import dalvik.annotation.KnownFailure;
  33. import dalvik.annotation.TestLevel;
  34. import dalvik.annotation.TestTargetClass;
  35. import dalvik.annotation.TestTargetNew;
  36. import dalvik.annotation.TestTargets;
  37. import static tests.support.Support_Exec.javaProcessBuilder;
  38. import static tests.support.Support_Exec.execAndGetOutput;
  39. import tests.util.TestEnvironment;
  40. @TestTargetClass(File.class)
  41. public class FileTest extends junit.framework.TestCase {
  42. /** Location to store tests in */
  43. private File tempDirectory;
  44. /** Temp file that does exist */
  45. private File tempFile;
  46. /** File separator */
  47. private String slash = File.separator;
  48. public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\nTest_File\nTest_FileDescriptor\nTest_FileInputStream\nTest_FileNotFoundException\nTest_FileOutputStream\nTest_java_io_FilterInputStream\nTest_java_io_FilterOutputStream\nTest_java_io_InputStream\nTest_java_io_IOException\nTest_java_io_OutputStream\nTest_java_io_PrintStream\nTest_java_io_RandomAccessFile\nTest_java_io_SyncFailedException\nTest_java_lang_AbstractMethodError\nTest_java_lang_ArithmeticException\nTest_java_lang_ArrayIndexOutOfBoundsException\nTest_java_lang_ArrayStoreException\nTest_java_lang_Boolean\nTest_java_lang_Byte\nTest_java_lang_Character\nTest_java_lang_Class\nTest_java_lang_ClassCastException\nTest_java_lang_ClassCircularityError\nTest_java_lang_ClassFormatError\nTest_java_lang_ClassLoader\nTest_java_lang_ClassNotFoundException\nTest_java_lang_CloneNotSupportedException\nTest_java_lang_Double\nTest_java_lang_Error\nTest_java_lang_Exception\nTest_java_lang_ExceptionInInitializerError\nTest_java_lang_Float\nTest_java_lang_IllegalAccessError\nTest_java_lang_IllegalAccessException\nTest_java_lang_IllegalArgumentException\nTest_java_lang_IllegalMonitorStateException\nTest_java_lang_IllegalThreadStateException\nTest_java_lang_IncompatibleClassChangeError\nTest_java_lang_IndexOutOfBoundsException\nTest_java_lang_InstantiationError\nTest_java_lang_InstantiationException\nTest_java_lang_Integer\nTest_java_lang_InternalError\nTest_java_lang_InterruptedException\nTest_java_lang_LinkageError\nTest_java_lang_Long\nTest_java_lang_Math\nTest_java_lang_NegativeArraySizeException\nTest_java_lang_NoClassDefFoundError\nTest_java_lang_NoSuchFieldError\nTest_java_lang_NoSuchMethodError\nTest_java_lang_NullPointerException\nTest_java_lang_Number\nTest_java_lang_NumberFormatException\nTest_java_lang_Object\nTest_java_lang_OutOfMemoryError\nTest_java_lang_RuntimeException\nTest_java_lang_SecurityManager\nTest_java_lang_Short\nTest_java_lang_StackOverflowError\nTest_java_lang_String\nTest_java_lang_StringBuffer\nTest_java_lang_StringIndexOutOfBoundsException\nTest_java_lang_System\nTest_java_lang_Thread\nTest_java_lang_ThreadDeath\nTest_java_lang_ThreadGroup\nTest_java_lang_Throwable\nTest_java_lang_UnknownError\nTest_java_lang_UnsatisfiedLinkError\nTest_java_lang_VerifyError\nTest_java_lang_VirtualMachineError\nTest_java_lang_vm_Image\nTest_java_lang_vm_MemorySegment\nTest_java_lang_vm_ROMStoreException\nTest_java_lang_vm_VM\nTest_java_lang_Void\nTest_java_net_BindException\nTest_java_net_ConnectException\nTest_java_net_DatagramPacket\nTest_java_net_DatagramSocket\nTest_java_net_DatagramSocketImpl\nTest_java_net_InetAddress\nTest_java_net_NoRouteToHostException\nTest_java_net_PlainDatagramSocketImpl\nTest_java_net_PlainSocketImpl\nTest_java_net_Socket\nTest_java_net_SocketException\nTest_java_net_SocketImpl\nTest_java_net_SocketInputStream\nTest_java_net_SocketOutputStream\nTest_java_net_UnknownHostException\nTest_java_util_ArrayEnumerator\nTest_java_util_Date\nTest_java_util_EventObject\nTest_java_util_HashEnumerator\nTest_java_util_Hashtable\nTest_java_util_Properties\nTest_java_util_ResourceBundle\nTest_java_util_tm\nTest_java_util_Vector\n";
  49. private static String platformId = "Android"
  50. + System.getProperty("java.vm.version").replace('.', '-');
  51. {
  52. // Delete all old temporary files
  53. File tempDir = new File(System.getProperty("java.io.tmpdir"));
  54. String[] files = tempDir.list();
  55. for (int i = 0; i < files.length; i++) {
  56. File f = new File(tempDir, files[i]);
  57. if (f.isDirectory()) {
  58. if (files[i].startsWith("hyts_resources"))
  59. deleteTempFolder(f);
  60. }
  61. if (files[i].startsWith("hyts_") || files[i].startsWith("hyjar_"))
  62. new File(tempDir, files[i]).delete();
  63. }
  64. }
  65. private void deleteTempFolder(File dir) {
  66. String files[] = dir.list();
  67. for (int i = 0; i < files.length; i++) {
  68. File f = new File(dir, files[i]);
  69. if (f.isDirectory())
  70. deleteTempFolder(f);
  71. else {
  72. f.delete();
  73. }
  74. }
  75. dir.delete();
  76. }
  77. /**
  78. * @tests java.io.File#File(java.io.File, java.lang.String)
  79. */
  80. @TestTargetNew(
  81. level = TestLevel.PARTIAL_COMPLETE,
  82. method = "File",
  83. args = {java.io.File.class, java.lang.String.class}
  84. )
  85. public void test_ConstructorLjava_io_FileLjava_lang_String() throws Exception {
  86. String error;
  87. String dirName = System.getProperty("java.io.tmpdir");
  88. System.setProperty("user.dir", dirName);
  89. File d = new File(dirName);
  90. File f = new File(d, "input.tst");
  91. if (!dirName.regionMatches((dirName.length() - 1), slash, 0, 1))
  92. dirName += slash;
  93. dirName += "input.tst";
  94. error = String.format("Test 1: Incorrect file created: %s; %s expected.", f.getPath(), dirName);
  95. assertTrue(error, f.getPath().equals(dirName));
  96. String fileName = null;
  97. try {
  98. f = new File(d, fileName);
  99. fail("Test 2: NullPointerException expected.");
  100. } catch (NullPointerException e) {
  101. }
  102. d = null;
  103. f = new File(d, "input.tst");
  104. error = String.format("Test 3: Incorrect file created: %s; %s expected.",
  105. f.getAbsolutePath(), dirName);
  106. assertTrue(error, f.getAbsolutePath().equals(dirName));
  107. // Regression test for Harmony-382
  108. File s = null;
  109. f = new File("/abc");
  110. d = new File(s, "/abc");
  111. assertEquals("Test 4: Incorrect file created;",
  112. f.getAbsolutePath(), d.getAbsolutePath());
  113. }
  114. /**
  115. * @tests java.io.File#File(java.lang.String)
  116. */
  117. @TestTargetNew(
  118. level = TestLevel.COMPLETE,
  119. notes = "Verifies File(java.lang.String) constructor.",
  120. method = "File",
  121. args = {java.lang.String.class}
  122. )
  123. public void test_ConstructorLjava_lang_String() {
  124. // Test for method java.io.File(java.lang.String)
  125. String fileName = null;
  126. try {
  127. new File(fileName);
  128. fail("Test 1: NullPointerException expected.");
  129. } catch (NullPointerException e) {
  130. }
  131. fileName = System.getProperty("java.io.tmpdir");
  132. if (!fileName.regionMatches((fileName.length() - 1), slash, 0, 1))
  133. fileName += slash;
  134. fileName += "input.tst";
  135. File f = new File(fileName);
  136. assertTrue("Created incorrect file " + f.getPath(), f.getPath().equals(
  137. fileName));
  138. }
  139. /**
  140. * @tests java.io.File#File(java.lang.String, java.lang.String)
  141. */
  142. @TestTargetNew(
  143. level = TestLevel.PARTIAL_COMPLETE,
  144. method = "File",
  145. args = {java.lang.String.class, java.lang.String.class}
  146. )
  147. public void test_ConstructorLjava_lang_StringLjava_lang_String() {
  148. String error;
  149. String dirName = null;
  150. String fileName = "input.tst";
  151. String userDir = System.getProperty("java.io.tmpdir");
  152. System.setProperty("user.dir", userDir);
  153. File f = new File(dirName, fileName);
  154. if (!userDir.regionMatches((userDir.length() - 1), slash, 0, 1))
  155. userDir += slash;
  156. userDir += "input.tst";
  157. error = String.format("Test 1: Incorrect file created: %s; %s expected.",
  158. f.getAbsolutePath(), userDir);
  159. assertTrue(error, f.getAbsolutePath().equals(userDir));
  160. dirName = System.getProperty("java.io.tmpdir");
  161. fileName = null;
  162. try {
  163. f = new File(dirName, fileName);
  164. fail("Test 2: NullPointerException expected.");
  165. } catch (NullPointerException e) {
  166. // Expected.
  167. }
  168. fileName = "input.tst";
  169. f = new File(dirName, fileName);
  170. assertTrue("Test 3: Incorrect file created.", f.getPath()
  171. .equals(userDir));
  172. // Regression test for Harmony-382
  173. String s = null;
  174. f = new File("/abc");
  175. File d = new File(s, "/abc");
  176. assertEquals("Test 4: Incorrect file created;", d.getAbsolutePath(), f
  177. .getAbsolutePath());
  178. assertEquals("Test3: Created Incorrect File", "/abc", f
  179. .getAbsolutePath());
  180. }
  181. /**
  182. * @tests java.io.File#File(java.lang.String, java.lang.String)
  183. */
  184. @TestTargetNew(
  185. level = TestLevel.PARTIAL_COMPLETE,
  186. method = "File",
  187. args = {java.lang.String.class, java.lang.String.class}
  188. )
  189. public void test_ConstructorLjava_lang_StringLjava_lang_String_112270() {
  190. File ref1 = new File("/dir1/file1");
  191. File file1 = new File("/", "/dir1/file1");
  192. assertEquals("wrong result 1: " + file1, ref1.getPath(), file1
  193. .getPath());
  194. File file2 = new File("/", "//dir1/file1");
  195. assertTrue("wrong result 2: " + file2, file2.getPath().equals(
  196. ref1.getPath()));
  197. File file3 = new File("\\", "\\dir1\\file1");
  198. assertTrue("wrong result 3: " + file3, file3.getPath().equals(
  199. "\\/\\dir1\\file1"));
  200. File file4 = new File("\\", "\\\\dir1\\file1");
  201. assertTrue("wrong result 4: " + file4, file4.getPath().equals(
  202. "\\/\\\\dir1\\file1"));
  203. File ref2 = new File("/lib/content-types.properties");
  204. File file5 = new File("/", "lib/content-types.properties");
  205. assertTrue("wrong result 5: " + file5, file5.getPath().equals(
  206. ref2.getPath()));
  207. }
  208. /**
  209. * @tests java.io.File#File(java.io.File, java.lang.String)
  210. */
  211. @TestTargetNew(
  212. level = TestLevel.PARTIAL_COMPLETE,
  213. method = "File",
  214. args = {java.io.File.class, java.lang.String.class}
  215. )
  216. public void test_ConstructorLjava_io_FileLjava_lang_String_112270() {
  217. File ref1 = new File("/dir1/file1");
  218. File root = new File("/");
  219. File file1 = new File(root, "/dir1/file1");
  220. assertTrue("wrong result 1: " + file1, file1.getPath().equals(
  221. ref1.getPath()));
  222. File file2 = new File(root, "//dir1/file1");
  223. assertTrue("wrong result 2: " + file2, file2.getPath().equals(
  224. ref1.getPath()));
  225. File file3 = new File(root, "\\dir1\\file1");
  226. assertTrue("wrong result 3: " + file3, file3.getPath().equals(
  227. "/\\dir1\\file1"));
  228. File file4 = new File(root, "\\\\dir1\\file1");
  229. assertTrue("wrong result 4: " + file4, file4.getPath().equals(
  230. "/\\\\dir1\\file1"));
  231. File ref2 = new File("/lib/content-types.properties");
  232. File file5 = new File(root, "lib/content-types.properties");
  233. assertTrue("wrong result 5: " + file5, file5.getPath().equals(
  234. ref2.getPath()));
  235. }
  236. /**
  237. * @tests java.io.File#File(java.net.URI)
  238. */
  239. @TestTargetNew(
  240. level = TestLevel.COMPLETE,
  241. notes = "Verifies File(java.net.URI) constructor with incorrect parameter.",
  242. method = "File",
  243. args = {java.net.URI.class}
  244. )
  245. public void test_ConstructorLjava_net_URI() {
  246. // Test for method java.io.File(java.net.URI)
  247. URI uri = null;
  248. try {
  249. new File(uri);
  250. fail("NullPointerException Not Thrown.");
  251. } catch (NullPointerException e) {
  252. }
  253. // invalid file URIs
  254. String[] uris = new String[] { "mailto:user@domain.com", // not
  255. // hierarchical
  256. "ftp:///path", // not file scheme
  257. "//host/path/", // not absolute
  258. "file://host/path", // non empty authority
  259. "file:///path?query", // non empty query
  260. "file:///path#fragment", // non empty fragment
  261. "file:///path?", "file:///path#" };
  262. for (int i = 0; i < uris.length; i++) {
  263. try {
  264. uri = new URI(uris[i]);
  265. } catch (URISyntaxException e) {
  266. fail("Unexpected exception:" + e);
  267. }
  268. try {
  269. new File(uri);
  270. fail("Expected IllegalArgumentException for new File(" + uri
  271. + ")");
  272. } catch (IllegalArgumentException e) {
  273. }
  274. }
  275. // a valid File URI
  276. try {
  277. File f = new File(new URI("file:///pa%20th/another\u20ac/pa%25th"));
  278. assertTrue("Created incorrect File " + f.getPath(), f.getPath()
  279. .equals(
  280. slash + "pa th" + slash + "another\u20ac" + slash
  281. + "pa%th"));
  282. } catch (URISyntaxException e) {
  283. fail("Unexpected exception:" + e);
  284. } catch (IllegalArgumentException e) {
  285. fail("Unexpected exception:" + e);
  286. }
  287. }
  288. /**
  289. * @tests java.io.File#canRead()
  290. */
  291. @TestTargetNew(
  292. level = TestLevel.PARTIAL_COMPLETE,
  293. method = "canRead",
  294. args = {}
  295. )
  296. public void test_canRead() {
  297. // Test for method boolean java.io.File.canRead()
  298. // canRead only returns if the file exists so cannot be fully tested.
  299. File f = new File(System.getProperty("java.io.tmpdir"), platformId
  300. + "canRead.tst");
  301. try {
  302. FileOutputStream fos = new FileOutputStream(f);
  303. fos.close();
  304. assertTrue("canRead returned false", f.canRead());
  305. f.delete();
  306. } catch (IOException e) {
  307. fail("Unexpected IOException: " + e.getMessage());
  308. } finally {
  309. f.delete();
  310. }
  311. }
  312. /**
  313. * @tests java.io.File#canWrite()
  314. */
  315. @TestTargetNew(
  316. level = TestLevel.PARTIAL_COMPLETE,
  317. method = "canWrite",
  318. args = {}
  319. )
  320. public void test_canWrite() {
  321. // Test for method boolean java.io.File.canWrite()
  322. // canWrite only returns if the file exists so cannot be fully tested.
  323. File f = new File(System.getProperty("java.io.tmpdir"), platformId
  324. + "canWrite.tst");
  325. try {
  326. FileOutputStream fos = new FileOutputStream(f);
  327. fos.close();
  328. assertTrue("canWrite returned false", f.canWrite());
  329. } catch (IOException e) {
  330. fail("Unexpected IOException: " + e.getMessage());
  331. } finally {
  332. f.delete();
  333. }
  334. }
  335. /**
  336. * @tests java.io.File#compareTo(java.io.File)
  337. */
  338. @TestTargetNew(
  339. level = TestLevel.COMPLETE,
  340. notes = "Verifies compareTo(java.io.File) method.",
  341. method = "compareTo",
  342. args = {java.io.File.class}
  343. )
  344. public void test_compareToLjava_io_File() {
  345. File f1 = new File("thisFile.file");
  346. File f2 = new File("thisFile.file");
  347. File f3 = new File("thatFile.file");
  348. assertEquals("Equal files did not answer zero for compareTo", 0, f1
  349. .compareTo(f2));
  350. assertTrue("f3.compareTo(f1) did not result in value < 0", f3
  351. .compareTo(f1) < 0);
  352. assertTrue("f1.compareTo(f3) did not result in vale > 0", f1
  353. .compareTo(f3) > 0);
  354. }
  355. /**
  356. * @tests java.io.File#createNewFile()
  357. */
  358. @TestTargetNew(
  359. level = TestLevel.PARTIAL_COMPLETE,
  360. notes = "IOException checked.",
  361. method = "createNewFile",
  362. args = {}
  363. )
  364. public void test_createNewFile_EmptyString() {
  365. File f = new File("");
  366. try {
  367. f.createNewFile();
  368. fail("should throw IOException");
  369. } catch (IOException e) {
  370. // expected
  371. }
  372. }
  373. /**
  374. * @tests java.io.File#createNewFile()
  375. */
  376. @TestTargetNew(
  377. level = TestLevel.PARTIAL_COMPLETE,
  378. method = "createNewFile",
  379. args = {}
  380. )
  381. public void test_createNewFile() throws IOException {
  382. // Test for method java.io.File.createNewFile()
  383. String base = System.getProperty("java.io.tmpdir");
  384. boolean dirExists = true;
  385. int numDir = 1;
  386. File dir = new File(base, String.valueOf(numDir));
  387. // Making sure that the directory does not exist.
  388. while (dirExists) {
  389. // If the directory exists, add one to the directory number
  390. // (making
  391. // it a new directory name.)
  392. if (dir.exists()) {
  393. numDir++;
  394. dir = new File(base, String.valueOf(numDir));
  395. } else {
  396. dirExists = false;
  397. }
  398. }
  399. // Test for trying to create a file in a directory that does not
  400. // exist.
  401. try {
  402. // Try to create a file in a directory that does not exist
  403. File f1 = new File(dir, "tempfile.tst");
  404. f1.createNewFile();
  405. fail("Test 1: IOException expected.");
  406. } catch (IOException e) {
  407. // Expected.
  408. }
  409. dir.mkdir();
  410. File f1 = new File(dir, "tempfile.tst");
  411. File f2 = new File(dir, "tempfile.tst");
  412. f1.deleteOnExit();
  413. f2.deleteOnExit();
  414. dir.deleteOnExit();
  415. assertFalse("Test 2: File should not exist.", f1.isFile());
  416. f1.createNewFile();
  417. assertTrue("Test 3: File should exist.", f1.isFile());
  418. assertTrue("Test 4: File should exist.", f2.isFile());
  419. String dirName = f1.getParent();
  420. if (!dirName.endsWith(slash))
  421. dirName += slash;
  422. assertTrue("Test 5: File saved in the wrong directory.",
  423. dirName.equals(dir.getPath() + slash));
  424. assertEquals("Test 6: File saved with incorrect name;",
  425. "tempfile.tst", f1.getName());
  426. // Test for creating a file that already exists.
  427. assertFalse("Test 7: File already exists, createNewFile should " +
  428. "return false.", f2.createNewFile());
  429. // Trying to create an illegal file.
  430. String sep = File.separator;
  431. f1 = new File(sep+"a"+sep+".."+sep+".."+sep);
  432. try {
  433. f1.createNewFile();
  434. fail("Test 8: IOException expected.");
  435. } catch (IOException e) {
  436. // Expected.
  437. }
  438. f1 = new File(base);
  439. assertFalse("Test 9: False expected when trying to create an " +
  440. "existing file", f1.createNewFile());
  441. }
  442. /**
  443. * @tests java.io.File#createTempFile(java.lang.String, java.lang.String)
  444. */
  445. @TestTargetNew(
  446. level = TestLevel.PARTIAL_COMPLETE,
  447. method = "createTempFile",
  448. args = {java.lang.String.class, java.lang.String.class}
  449. )
  450. @AndroidOnly("The RI does not throw an IOException when an illegal" +
  451. "file prefix is passed to createTempFile.")
  452. public void test_createTempFileLjava_lang_StringLjava_lang_String() {
  453. // Test for method java.io.File.createTempFile(String, String)
  454. // Error protection against using a suffix without a "."?
  455. File f1 = null;
  456. File f2 = null;
  457. try {
  458. f1 = File.createTempFile("hyts_abc", ".tmp");
  459. f2 = File.createTempFile("hyts_tf", null);
  460. String fileLocation = f1.getParent();
  461. if (!fileLocation.endsWith(slash))
  462. ;
  463. fileLocation += slash;
  464. String tempDir = System.getProperty("java.io.tmpdir");
  465. if (!tempDir.endsWith(slash))
  466. tempDir += slash;
  467. assertTrue(
  468. "File did not save to the default temporary-file location.",
  469. fileLocation.equals(tempDir));
  470. // Test to see if correct suffix was used to create the tempfile.
  471. File currentFile;
  472. String fileName;
  473. // Testing two files, one with suffix ".tmp" and one with null
  474. for (int i = 0; i < 2; i++) {
  475. currentFile = i == 0 ? f1 : f2;
  476. fileName = currentFile.getPath();
  477. assertTrue("File Created With Incorrect Suffix.", fileName
  478. .endsWith(".tmp"));
  479. }
  480. // Tests to see if the correct prefix was used to create the
  481. // tempfiles.
  482. fileName = f1.getName();
  483. assertTrue("Test 1: File Created With Incorrect Prefix.", fileName
  484. .startsWith("hyts_abc"));
  485. fileName = f2.getName();
  486. assertTrue("Test 2: File Created With Incorrect Prefix.", fileName
  487. .startsWith("hyts_tf"));
  488. // Tests for creating a tempfile with a filename shorter than 3
  489. // characters.
  490. try {
  491. File f3 = File.createTempFile("ab", ".tst");
  492. f3.delete();
  493. fail("IllegalArgumentException Not Thrown.");
  494. } catch (IllegalArgumentException e) {
  495. }
  496. try {
  497. File f3 = File.createTempFile("a", ".tst");
  498. f3.delete();
  499. fail("IllegalArgumentException Not Thrown.");
  500. } catch (IllegalArgumentException e) {
  501. }
  502. try {
  503. File f3 = File.createTempFile("", ".tst");
  504. f3.delete();
  505. fail("IllegalArgumentException Not Thrown.");
  506. } catch (IllegalArgumentException e) {
  507. }
  508. try {
  509. // Providing an illegal file prefix.
  510. File f3 = File.createTempFile("/../../../../../", null);
  511. f3.delete();
  512. fail("IOException not thrown");
  513. } catch (IOException e) {
  514. }
  515. } catch (IOException e) {
  516. fail("Unexpected IOException: " + e.getMessage());
  517. } finally {
  518. if (f1 != null)
  519. f1.delete();
  520. if (f2 != null)
  521. f2.delete();
  522. }
  523. }
  524. /**
  525. * @tests java.io.File#createTempFile(java.lang.String, java.lang.String,
  526. * java.io.File)
  527. */
  528. @TestTargetNew(
  529. level = TestLevel.PARTIAL_COMPLETE,
  530. method = "createTempFile",
  531. args = {java.lang.String.class, java.lang.String.class, java.io.File.class}
  532. )
  533. public void test_createTempFileLjava_lang_StringLjava_lang_StringLjava_io_File() {
  534. // Test for method java.io.File.createTempFile(String, String, File)
  535. File f1 = null;
  536. File f2 = null;
  537. String base = System.getProperty("java.io.tmpdir");
  538. try {
  539. // Test to make sure that the tempfile was saved in the correct
  540. // location
  541. // and with the correct prefix/suffix.
  542. f1 = File.createTempFile("hyts_tf", null, null);
  543. File dir = new File(base);
  544. f2 = File.createTempFile("hyts_tf", ".tmp", dir);
  545. File currentFile;
  546. String fileLocation;
  547. String fileName;
  548. for (int i = 0; i < 2; i++) {
  549. currentFile = i == 0 ? f1 : f2;
  550. fileLocation = currentFile.getParent();
  551. if (!fileLocation.endsWith(slash))
  552. fileLocation += slash;
  553. if (!base.endsWith(slash))
  554. base += slash;
  555. assertTrue(
  556. "File not created in the default temporary-file location.",
  557. fileLocation.equals(base));
  558. fileName = currentFile.getName();
  559. assertTrue("File created with incorrect suffix.", fileName
  560. .endsWith(".tmp"));
  561. assertTrue("File created with incorrect prefix.", fileName
  562. .startsWith("hyts_tf"));
  563. currentFile.delete();
  564. }
  565. // Test for creating a tempfile in a directory that does not exist.
  566. int dirNumber = 1;
  567. boolean dirExists = true;
  568. // Set dir to a non-existent directory inside the temporary
  569. // directory
  570. dir = new File(base, String.valueOf(dirNumber));
  571. // Making sure that the directory does not exist.
  572. while (dirExists) {
  573. // If the directory exists, add one to the directory number
  574. // (making it
  575. // a new directory name.)
  576. if (dir.exists()) {
  577. dirNumber++;
  578. dir = new File(base, String.valueOf(dirNumber));
  579. } else {
  580. dirExists = false;
  581. }
  582. }
  583. try {
  584. // Try to create a file in a directory that does not exist
  585. File f3 = File.createTempFile("hyts_tf", null, dir);
  586. f3.delete();
  587. fail("IOException not thrown");
  588. } catch (IOException e) {
  589. }
  590. dir.delete();
  591. // Tests for creating a tempfile with a filename shorter than 3
  592. // characters.
  593. try {
  594. File f4 = File.createTempFile("ab", null, null);
  595. f4.delete();
  596. fail("IllegalArgumentException not thrown.");
  597. } catch (IllegalArgumentException e) {
  598. }
  599. try {
  600. File f4 = File.createTempFile("a", null, null);
  601. f4.delete();
  602. fail("IllegalArgumentException not thrown.");
  603. } catch (IllegalArgumentException e) {
  604. }
  605. try {
  606. File f4 = File.createTempFile("", null, null);
  607. f4.delete();
  608. fail("IllegalArgumentException not thrown.");
  609. } catch (IllegalArgumentException e) {
  610. }
  611. } catch (IOException e) {
  612. fail("Unexpected IOException: " + e.getMessage());
  613. } finally {
  614. if (f1 != null)
  615. f1.delete();
  616. if (f2 != null)
  617. f1.delete();
  618. }
  619. }
  620. /**
  621. * @tests java.io.File#delete()
  622. */
  623. @TestTargetNew(
  624. level = TestLevel.PARTIAL_COMPLETE,
  625. method = "delete",
  626. args = {}
  627. )
  628. public void test_delete() {
  629. // Test for method boolean java.io.File.delete()
  630. try {
  631. File dir = new File(System.getProperty("java.io.tmpdir"), platformId
  632. + "filechk");
  633. dir.mkdir();
  634. assertTrue("Directory Does Not Exist", dir.exists()
  635. && dir.isDirectory());
  636. File f = new File(dir, "filechk.tst");
  637. FileOutputStream fos = new FileOutputStream(f);
  638. fos.close();
  639. assertTrue("Error Creating File For Delete Test", f.exists());
  640. dir.delete();
  641. assertTrue("Directory Should Not Have Been Deleted.", dir.exists());
  642. f.delete();
  643. assertTrue("File Was Not Deleted", !f.exists());
  644. dir.delete();
  645. assertTrue("Directory Was Not Deleted", !dir.exists());
  646. } catch (IOException e) {
  647. fail("Unexpected IOException During Delete Test : "
  648. + e.getMessage());
  649. }
  650. }
  651. /**
  652. * @tests java.io.File#equals(java.lang.Object)
  653. */
  654. @TestTargetNew(
  655. level = TestLevel.COMPLETE,
  656. notes = "Verifies equals() method.",
  657. method = "equals",
  658. args = {java.lang.Object.class}
  659. )
  660. public void test_equalsLjava_lang_Object() {
  661. // Test for method boolean java.io.File.equals(java.lang.Object)
  662. File f1 = new File("filechk.tst");
  663. File f2 = new File("filechk.tst");
  664. File f3 = new File("xxxx");
  665. assertTrue("Equality test failed", f1.equals(f2));
  666. assertTrue("Files Should Not Return Equal.", !f1.equals(f3));
  667. f3 = new File("FiLeChK.tst");
  668. boolean onWindows = File.separatorChar == '\\';
  669. boolean onUnix = File.separatorChar == '/';
  670. if (onWindows)
  671. assertTrue("Files Should Return Equal.", f1.equals(f3));
  672. else if (onUnix)
  673. assertTrue("Files Should NOT Return Equal.", !f1.equals(f3));
  674. try {
  675. f1 = new File(System.getProperty("java.io.tmpdir"), "casetest.tmp");
  676. f2 = new File(System.getProperty("java.io.tmpdir"), "CaseTest.tmp");
  677. new FileOutputStream(f1).close(); // create the file
  678. if (f1.equals(f2)) {
  679. try {
  680. new FileInputStream(f2);
  681. } catch (IOException e) {
  682. fail("File system is case sensitive");
  683. }
  684. } else {
  685. boolean exception = false;
  686. try {
  687. new FileInputStream(f2);
  688. } catch (IOException e) {
  689. exception = true;
  690. }
  691. assertTrue("File system is case insensitive", exception);
  692. }
  693. f1.delete();
  694. } catch (IOException e) {
  695. fail("Unexpected using case sensitive test : " + e.getMessage());
  696. }
  697. }
  698. /**
  699. * @tests java.io.File#exists()
  700. */
  701. @TestTargetNew(
  702. level = TestLevel.PARTIAL_COMPLETE,
  703. method = "exists",
  704. args = {}
  705. )
  706. public void test_exists() {
  707. // Test for method boolean java.io.File.exists()
  708. try {
  709. File f = new File(System.getProperty("java.io.tmpdir"), platformId
  710. + "exists.tst");
  711. assertTrue("Exists returned true for non-existent file", !f
  712. .exists());
  713. FileOutputStream fos = new FileOutputStream(f);
  714. fos.close();
  715. assertTrue("Exists returned false file", f.exists());
  716. f.delete();
  717. } catch (IOException e) {
  718. fail("Unexpected IOException During Test : " + e.getMessage());
  719. }
  720. }
  721. /**
  722. * @tests java.io.File#getAbsoluteFile()
  723. */
  724. @TestTargetNew(
  725. level = TestLevel.PARTIAL_COMPLETE,
  726. method = "getAbsoluteFile",
  727. args = {}
  728. )
  729. public void test_getAbsoluteFile() {
  730. // Test for method java.io.File getAbsoluteFile()
  731. String base = System.getProperty("java.io.tmpdir");
  732. if (!base.endsWith(slash))
  733. base += slash;
  734. File f = new File(base, "temp.tst");
  735. File f2 = f.getAbsoluteFile();
  736. assertEquals("Test 1: Incorrect File Returned.", 0, f2.compareTo(f
  737. .getAbsoluteFile()));
  738. f = new File(base + "Temp" + slash + slash + "temp.tst");
  739. f2 = f.getAbsoluteFile();
  740. assertEquals("Test 2: Incorrect File Returned.", 0, f2.compareTo(f
  741. .getAbsoluteFile()));
  742. f = new File(base + slash + ".." + slash + "temp.tst");
  743. f2 = f.getAbsoluteFile();
  744. assertEquals("Test 3: Incorrect File Returned.", 0, f2.compareTo(f
  745. .getAbsoluteFile()));
  746. f.delete();
  747. f2.delete();
  748. }
  749. /**
  750. * @tests java.io.File#getAbsolutePath()
  751. */
  752. @TestTargetNew(
  753. level = TestLevel.PARTIAL_COMPLETE,
  754. method = "getAbsolutePath",
  755. args = {}
  756. )
  757. public void test_getAbsolutePath() {
  758. // Test for method java.lang.String java.io.File.getAbsolutePath()
  759. String base = System.getProperty("java.io.tmpdir");
  760. if (!base.regionMatches((base.length() - 1), slash, 0, 1))
  761. base += slash;
  762. File f = new File(base, "temp.tst");
  763. assertTrue("Test 1: Incorrect path returned.", f.getAbsolutePath()
  764. .equals(base + "temp.tst"));
  765. f = new File(base + "Temp" + slash + slash + slash + "Testing" + slash
  766. + "temp.tst");
  767. assertTrue("Test 2: Incorrect path returned.", f.getAbsolutePath()
  768. .equals(base + "Temp" + slash + "Testing" + slash + "temp.tst"));
  769. f = new File(base + "a" + slash + slash + ".." + slash + "temp.tst");
  770. assertTrue("Test 3: Incorrect path returned." + f.getAbsolutePath(), f
  771. .getAbsolutePath().equals(
  772. base + "a" + slash + ".." + slash + "temp.tst"));
  773. f.delete();
  774. }
  775. /**
  776. * @tests java.io.File#getCanonicalFile()
  777. */
  778. @TestTargetNew(
  779. level = TestLevel.SUFFICIENT,
  780. notes = "IOException not checked since it may only occur if" +
  781. "the native access to the filesystem fails.",
  782. method = "getCanonicalFile",
  783. args = {}
  784. )
  785. public void test_getCanonicalFile() {
  786. // Test for method java.io.File.getCanonicalFile()
  787. try {
  788. String base = System.getProperty("java.io.tmpdir");
  789. if (!base.endsWith(slash))
  790. base += slash;
  791. File f = new File(base, "temp.tst");
  792. File f2 = f.getCanonicalFile();
  793. assertEquals("Test 1: Incorrect file returned.", 0, f2
  794. .getCanonicalFile().compareTo(f.getCanonicalFile()));
  795. f = new File(base + "Temp" + slash + slash + "temp.tst");
  796. f2 = f.getCanonicalFile();
  797. assertEquals("Test 2: Incorrect file returned.", 0, f2
  798. .getCanonicalFile().compareTo(f.getCanonicalFile()));
  799. f = new File(base + "Temp" + slash + slash + ".." + slash
  800. + "temp.tst");
  801. f2 = f.getCanonicalFile();
  802. assertEquals("Test 3: Incorrect file returned.", 0, f2
  803. .getCanonicalFile().compareTo(f.getCanonicalFile()));
  804. // Test for when long directory/file names in Windows
  805. boolean onWindows = File.separatorChar == '\\';
  806. // String userDir = System.getProperty("java.io.tmpdir");
  807. if (onWindows) {
  808. File testdir = new File(base, "long-" + platformId);
  809. testdir.mkdir();
  810. File dir = new File(testdir, "longdirectory" + platformId);
  811. try {
  812. dir.mkdir();
  813. f = new File(dir, "longfilename.tst");
  814. f2 = f.getCanonicalFile();
  815. assertEquals("Test 4: Incorrect file returned.",
  816. 0, f2.getCanonicalFile().compareTo(
  817. f.getCanonicalFile()));
  818. FileOutputStream fos = new FileOutputStream(f);
  819. fos.close();
  820. f2 = new File(testdir + slash + "longdi~1" + slash
  821. + "longfi~1.tst");
  822. File canonicalf2 = f2.getCanonicalFile();
  823. /*
  824. * If the "short file name" doesn't exist, then assume that
  825. * the 8.3 file name compatibility is disabled.
  826. */
  827. if (canonicalf2.exists()) {
  828. assertTrue("Test 5: Incorrect file returned: "
  829. + canonicalf2, canonicalf2.compareTo(f
  830. .getCanonicalFile()) == 0);
  831. }
  832. } finally {
  833. f.delete();
  834. f2.delete();
  835. dir.delete();
  836. testdir.delete();
  837. }
  838. }
  839. } catch (IOException e) {
  840. fail ("Unexpected IOException during Test : " + e.getMessage());
  841. }
  842. }
  843. /**
  844. * @tests java.io.File#getCanonicalPath()
  845. */
  846. @TestTargetNew(
  847. level = TestLevel.SUFFICIENT,
  848. notes = "IOException not checked since it may only occur if" +
  849. "the native access to the filesystem fails.",
  850. method = "getCanonicalPath",
  851. args = {}
  852. )
  853. public void test_getCanonicalPath() {
  854. // Test for method java.lang.String java.io.File.getCanonicalPath()
  855. // Should work for Unix/Windows.
  856. String dots = "..";
  857. String expected;
  858. String error;
  859. String tmpDir = System.getProperty("java.io.tmpdir");
  860. System.setProperty("user.dir", tmpDir);
  861. try {
  862. String base = new File(tmpDir).getCanonicalPath();
  863. if (!base.regionMatches((base.length() - 1), slash, 0, 1))
  864. base += slash;
  865. File f = new File(base, "temp.tst");
  866. assertEquals("Test 1: Incorrect Path Returned.", base + "temp.tst", f
  867. .getCanonicalPath());
  868. f = new File(base + "Temp" + slash + dots + slash + "temp.tst");
  869. assertEquals("Test 2: Incorrect Path Returned.", base + "temp.tst", f
  870. .getCanonicalPath());
  871. // Finding a non-existent directory for tests 3 and 4
  872. // This is necessary because getCanonicalPath is case sensitive and
  873. // could
  874. // cause a failure in the test if the directory exists but with
  875. // different
  876. // case letters (e.g "Temp" and "temp")
  877. int dirNumber = 1;
  878. boolean dirExists = true;
  879. File dir1 = new File(base, String.valueOf(dirNumber));
  880. while (dirExists) {
  881. if (dir1.exists()) {
  882. dirNumber++;
  883. dir1 = new File(base, String.valueOf(dirNumber));
  884. } else {
  885. dirExists = false;
  886. }
  887. }
  888. f = new File(base + dirNumber + slash + dots + slash + dirNumber + slash
  889. + "temp.tst");
  890. assertEquals("Test 3: Incorrect path returned.", base + dirNumber + slash
  891. + "temp.tst", f.getCanonicalPath());
  892. f = new File(base + dirNumber + slash + "Temp" + slash + dots + slash + "Test"
  893. + slash + "temp.tst");
  894. assertEquals("Test 4: Incorrect path returned.", base + dirNumber + slash + "Test"
  895. + slash + "temp.tst", f.getCanonicalPath());
  896. f = new File("1234.567");
  897. expected = System.getProperty("user.dir") + "/1234.567";
  898. error = String.format("Test 5: Incorrect path %s returned; %s expected.",
  899. f.getCanonicalPath(), expected);
  900. assertTrue(error, f.getCanonicalPath().equals(expected));
  901. } catch (IOException e) {
  902. fail("Unexpected IOException During Test : " + e.getMessage());
  903. }
  904. }
  905. /**
  906. * @tests java.io.File#getName()
  907. */
  908. @TestTargetNew(
  909. level = TestLevel.COMPLETE,
  910. notes = "Verifies getName() method.",
  911. method = "getName",
  912. args = {}
  913. )
  914. public void test_getName() {
  915. // Test for method java.lang.String java.io.File.getName()
  916. File f = new File("name.tst");
  917. assertEquals("Test 1: Returned incorrect name",
  918. "name.tst", f.getName());
  919. f = new File("");
  920. assertTrue("Test 2: Returned incorrect name", f.getName().equals(""));
  921. f.delete();
  922. }
  923. /**
  924. * @tests java.io.File#getParent()
  925. */
  926. @TestTargetNew(
  927. level = TestLevel.COMPLETE,
  928. notes = "Verifies getParent() method.",
  929. method = "getParent",
  930. args = {}
  931. )
  932. public void test_getParent() throws Exception {
  933. // Test for method java.lang.String java.io.File.getParent()
  934. String dir = System.getProperty("java.io.tmpdir");
  935. if (dir == null)
  936. throw new Exception("System property java.io.tmpdir not defined.");
  937. File f = new File("p.tst");
  938. assertNull("Test 1: Incorrect path returned", f.getParent());
  939. f = new File(dir, "p.tst");
  940. assertTrue("Test 2: Incorrect path returned",
  941. f.getParent().equals(dir));
  942. try {
  943. f.delete();
  944. } catch (Exception e) {
  945. fail("Unexpected exception during tests : " + e.getMessage());
  946. }
  947. File f1 = new File("/directory");
  948. assertTrue("Wrong parent test 1", f1.getParent().equals(slash));
  949. f1 = new File("/directory/file");
  950. assertTrue("Wrong parent test 2", f1.getParent().equals(
  951. slash + "directory"));
  952. f1 = new File("directory/file");
  953. assertEquals("Wrong parent test 3", "directory", f1.getParent());
  954. f1 = new File("/");
  955. assertNull("Wrong parent test 4", f1.getParent());
  956. f1 = new File("directory");
  957. assertNull("Wrong parent test 5", f1.getParent());
  958. if (File.separatorChar == '\\' && new File("d:/").isAbsolute()) {
  959. f1 = new File("d:/directory");
  960. assertTrue("Wrong parent test 1a", f1.getParent().equals(
  961. "d:" + slash));
  962. f1 = new File("d:/directory/file");
  963. assertTrue("Wrong parent test 2a", f1.getParent().equals(
  964. "d:" + slash + "directory"));
  965. f1 = new File("d:directory/file");
  966. assertEquals("Wrong parent test 3a",
  967. "d:directory", f1.getParent());
  968. f1 = new File("d:/");
  969. assertNull("Wrong parent test 4a", f1.getParent());
  970. f1 = new File("d:directory");
  971. assertEquals("Wrong parent test 5a", "d:", f1.getParent());
  972. }
  973. }
  974. /**
  975. * @tests java.io.File#getParentFile()
  976. */
  977. @TestTargetNew(
  978. level = TestLevel.COMPLETE,
  979. notes = "Verifies getParentFile() method.",
  980. method = "getParentFile",
  981. args = {}
  982. )
  983. public void test_getParentFile() {
  984. // Test for method java.io.File.getParentFile()
  985. File f = new File("tempfile.tst");
  986. assertNull("Incorrect path returned", f.getParentFile());
  987. f = new File(System.getProperty("java.io.tmpdir"), "tempfile1.tmp");
  988. File f2 = new File(System.getProperty("java.io.tmpdir"), "tempfile2.tmp");
  989. File f3 = new File(System.getProperty("java.io.tmpdir"), "/a/tempfile.tmp");
  990. assertEquals("Incorrect file returned", 0, f.getParentFile().compareTo(
  991. f2.getParentFile()));
  992. assertTrue("Incorrect file returned", f.getParentFile().compareTo(
  993. f3.getParentFile()) != 0);
  994. f.delete();
  995. f2.delete();
  996. f3.delete();
  997. }
  998. /**
  999. * @tests java.io.File#getPath()
  1000. */
  1001. @TestTargetNew(
  1002. level = TestLevel.COMPLETE,
  1003. notes = "Verifies getPath() method.",
  1004. method = "getPath",
  1005. args = {}
  1006. )
  1007. public void test_getPath() {
  1008. System.setProperty("user.dir", System.getProperty("java.io.tmpdir"));
  1009. String base = System.getProperty("user.dir");
  1010. String fname;
  1011. File f1;
  1012. if (!base.regionMatches((base.length() - 1), slash, 0, 1))
  1013. base += slash;
  1014. fname = base + "filechk.tst";
  1015. f1 = new File(base, "filechk.tst");
  1016. File f2 = new File("filechk.tst");
  1017. File f3 = new File("c:");
  1018. File f4 = new File(base + "a" + slash + slash + ".." + slash
  1019. + "filechk.tst");
  1020. assertTrue("getPath returned incorrect path(f1) " + f1.getPath(), f1
  1021. .getPath().equals(fname));
  1022. assertTrue("getPath returned incorrect path(f2) " + f2.getPath(), f2
  1023. .getPath().equals("filechk.tst"));
  1024. assertTrue("getPath returned incorrect path(f3) " + f3.getPath(), f3
  1025. .getPath().equals("c:"));
  1026. assertTrue("getPath returned incorrect path(f4) " + f4.getPath(), f4
  1027. .getPath().equals(
  1028. base + "a" + slash + ".." + slash + "filechk.tst"));
  1029. f1.delete();
  1030. f2.delete();
  1031. f3.delete();
  1032. f4.delete();
  1033. }
  1034. /**
  1035. * @tests java.io.File#isAbsolute()
  1036. */
  1037. @TestTargetNew(
  1038. level = TestLevel.COMPLETE,
  1039. notes = "Verifies isAbsolute() method.",
  1040. method = "isAbsolute",
  1041. args = {}
  1042. )
  1043. public void test_isAbsolute() {
  1044. // Test for method boolean java.io.File.isAbsolute()
  1045. if (File.separatorChar == '\\') {
  1046. File f = new File("c:\\test");
  1047. File f1 = new File("\\test");
  1048. // One or the other should be absolute on Windows or CE
  1049. assertTrue("Absolute returned false", (f.isAbsolute() && !f1
  1050. .isAbsolute())
  1051. || (!f.isAbsolute() && f1.isAbsolute()));
  1052. } else {
  1053. File f = new File("/test");
  1054. assertTrue("Absolute returned false", f.isAbsolute());
  1055. }
  1056. assertTrue("Non-Absolute returned true", !new File("../test")
  1057. .isAbsolute());
  1058. }
  1059. /**
  1060. * @tests java.io.File#isDirectory()
  1061. */
  1062. @TestTargetNew(
  1063. level = TestLevel.PARTIAL_COMPLETE,
  1064. method = "isDirectory",
  1065. args = {}
  1066. )
  1067. public void test_isDirectory() {
  1068. // Test for method boolean java.io.File.isDirectory()
  1069. String base = System.getProperty("java.io.tmpdir");
  1070. if (!base.regionMatches((base.length() - 1), slash, 0, 1))
  1071. base += slash;
  1072. File f = new File(base);
  1073. assertTrue("Test 1: Directory returned false", f.isDirectory());
  1074. f = new File(base + "zxzxzxz" + platformId);
  1075. assertTrue("Test 2: (Not created) directory returned true.", !f
  1076. .isDirectory());
  1077. f.mkdir();
  1078. try {
  1079. assertTrue("Test 3: Directory returned false.", f.isDirectory());
  1080. } finally {
  1081. f.delete();
  1082. }
  1083. }
  1084. /**
  1085. * @tests java.io.File#isFile()
  1086. */
  1087. @TestTargetNew(
  1088. level = TestLevel.PARTIAL_COMPLETE,
  1089. method = "isFile",
  1090. args = {}
  1091. )
  1092. public void test_isFile() {
  1093. // Test for method boolean java.io.File.isFile()
  1094. try {
  1095. String base = System.getProperty("java.io.tmpdir");
  1096. File f = new File(base);
  1097. assertTrue("Directory returned true as being a file.", !f.isFile());
  1098. if (!base.regionMatches((base.length() - 1), slash, 0, 1))
  1099. base += slash;
  1100. f = new File(base, platformId + "amiafile");
  1101. assertTrue("Non-existent file returned true", !f.isFile());
  1102. FileOutputStream fos = new FileOutputStream(f);
  1103. fos.close();
  1104. assertTrue("File returned false", f.isFile());
  1105. f.delete();
  1106. } catch (IOException e) {
  1107. fail("IOException during isFile " + e.getMessage());
  1108. }
  1109. }
  1110. /**
  1111. * @tests java.io.File#isHidden()
  1112. */
  1113. @TestTargetNew(
  1114. level = TestLevel.PARTIAL_COMPLETE,
  1115. method = "isHidden",
  1116. args = {}
  1117. )
  1118. public void test_isHidden() {
  1119. // Test for method boolean java.io.File.isHidden()
  1120. boolean onUnix = File.separatorChar == '/';
  1121. try {
  1122. File f = File.createTempFile("hyts_", ".tmp");
  1123. // On Unix hidden files are marked with a "." at the beginning
  1124. // of the file name.
  1125. if (onUnix) {
  1126. File f2 = new File(System.getProperty("java.io.tmpdir"), ".test.tst" + platformId);
  1127. FileOutputStream fos2 = new FileOutputStream(f2);
  1128. fos2.close();
  1129. assertTrue("File returned hidden on Unix", !f.isHidden());
  1130. assertTrue("File returned visible on Unix", f2.isHidden());
  1131. assertTrue("File did not delete.", f2.delete());
  1132. } else {
  1133. // For windows, the file is being set hidden by the attrib
  1134. // command.
  1135. Runtime r = Runtime.getRuntime();
  1136. assertTrue("File returned hidden", !f.isHidden());
  1137. Process p = r.exec("attrib +h \"" + f.getAbsolutePath() + "\"");
  1138. p.waitFor();
  1139. assertTrue("File returned visible", f.isHidden());
  1140. p = r.exec("attrib -h \"" + f.getAbsolutePath() + "\"");
  1141. p.waitFor();
  1142. assertTrue("File returned hidden", !f.isHidden());
  1143. }
  1144. f.delete();
  1145. } catch (IOException e) {
  1146. fail("Unexpected IOException during test : " + e.getMessage());
  1147. } catch (InterruptedException e) {
  1148. fail("Unexpected InterruptedExcepti…

Large files files are truncated, but you can click here to view the full file