/jdk/test/java/nio/file/Path/Misc.java

https://github.com/Morriar/ProxyJDK · Java · 169 lines · 81 code · 24 blank · 64 comment · 7 complexity · 731857b556ccec37bd44463478defeb3 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This code is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * version 2 for more details (a copy is included in the LICENSE file that
  13. * accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License version
  16. * 2 along with this work; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20. * or visit www.oracle.com if you need additional information or have any
  21. * questions.
  22. */
  23. /* @test
  24. * @bug 4313887 6838333 7029979
  25. * @summary Unit test for miscellenous java.nio.file.Path methods
  26. * @library ..
  27. */
  28. import java.nio.file.*;
  29. import static java.nio.file.LinkOption.*;
  30. import java.io.*;
  31. public class Misc {
  32. static final boolean isWindows =
  33. System.getProperty("os.name").startsWith("Windows");
  34. static boolean supportsLinks;
  35. public static void main(String[] args) throws IOException {
  36. Path dir = TestUtil.createTemporaryDirectory();
  37. try {
  38. supportsLinks = TestUtil.supportsLinks(dir);
  39. // equals and hashCode methods
  40. testEqualsAndHashCode();
  41. // toFile method
  42. testToFile(dir);
  43. // toRealPath method
  44. testToRealPath(dir);
  45. } finally {
  46. TestUtil.removeAll(dir);
  47. }
  48. }
  49. /**
  50. * Exercise equals and hashCode methods
  51. */
  52. static void testEqualsAndHashCode() {
  53. Path thisFile = Paths.get("this");
  54. Path thatFile = Paths.get("that");
  55. assertTrue(thisFile.equals(thisFile));
  56. assertTrue(!thisFile.equals(thatFile));
  57. assertTrue(!thisFile.equals(null));
  58. assertTrue(!thisFile.equals(new Object()));
  59. Path likeThis = Paths.get("This");
  60. if (isWindows) {
  61. // case insensitive
  62. assertTrue(thisFile.equals(likeThis));
  63. assertTrue(thisFile.hashCode() == likeThis.hashCode());
  64. } else {
  65. // case senstive
  66. assertTrue(!thisFile.equals(likeThis));
  67. }
  68. }
  69. /**
  70. * Exercise toFile method
  71. */
  72. static void testToFile(Path dir) throws IOException {
  73. File d = dir.toFile();
  74. assertTrue(d.toString().equals(dir.toString()));
  75. assertTrue(d.toPath().equals(dir));
  76. }
  77. /**
  78. * Exercise toRealPath method
  79. */
  80. static void testToRealPath(Path dir) throws IOException {
  81. final Path file = Files.createFile(dir.resolve("foo"));
  82. final Path link = dir.resolve("link");
  83. /**
  84. * Test: totRealPath() will access same file as toRealPath(NOFOLLOW_LINKS)
  85. */
  86. assertTrue(Files.isSameFile(file.toRealPath(), file.toRealPath(NOFOLLOW_LINKS)));
  87. /**
  88. * Test: toRealPath should fail if file does not exist
  89. */
  90. Path doesNotExist = dir.resolve("DoesNotExist");
  91. try {
  92. doesNotExist.toRealPath();
  93. throw new RuntimeException("IOException expected");
  94. } catch (IOException expected) {
  95. }
  96. try {
  97. doesNotExist.toRealPath(NOFOLLOW_LINKS);
  98. throw new RuntimeException("IOException expected");
  99. } catch (IOException expected) {
  100. }
  101. /**
  102. * Test: toRealPath() should resolve links
  103. */
  104. if (supportsLinks) {
  105. Files.createSymbolicLink(link, file.toAbsolutePath());
  106. assertTrue(link.toRealPath().equals(file.toRealPath()));
  107. Files.delete(link);
  108. }
  109. /**
  110. * Test: toRealPath(NOFOLLOW_LINKS) should not resolve links
  111. */
  112. if (supportsLinks) {
  113. Files.createSymbolicLink(link, file.toAbsolutePath());
  114. assertTrue(link.toRealPath(NOFOLLOW_LINKS).getFileName().equals(link.getFileName()));
  115. Files.delete(link);
  116. }
  117. /**
  118. * Test: toRealPath(NOFOLLOW_LINKS) with broken link
  119. */
  120. if (supportsLinks) {
  121. Path broken = Files.createSymbolicLink(link, doesNotExist);
  122. assertTrue(link.toRealPath(NOFOLLOW_LINKS).getFileName().equals(link.getFileName()));
  123. Files.delete(link);
  124. }
  125. /**
  126. * Test: toRealPath should eliminate "."
  127. */
  128. assertTrue(dir.resolve(".").toRealPath().equals(dir.toRealPath()));
  129. assertTrue(dir.resolve(".").toRealPath(NOFOLLOW_LINKS).equals(dir.toRealPath(NOFOLLOW_LINKS)));
  130. /**
  131. * Test: toRealPath should eliminate ".." when it doesn't follow a
  132. * symbolic link
  133. */
  134. Path subdir = Files.createDirectory(dir.resolve("subdir"));
  135. assertTrue(subdir.resolve("..").toRealPath().equals(dir.toRealPath()));
  136. assertTrue(subdir.resolve("..").toRealPath(NOFOLLOW_LINKS).equals(dir.toRealPath(NOFOLLOW_LINKS)));
  137. Files.delete(subdir);
  138. // clean-up
  139. Files.delete(file);
  140. }
  141. static void assertTrue(boolean okay) {
  142. if (!okay)
  143. throw new RuntimeException("Assertion Failed");
  144. }
  145. }