PageRenderTime 62ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/openjdk7/jdk/test/java/nio/file/Path/UriImportExport.java

https://github.com/Helbrass/openjdk-fontfix
Java | 143 lines | 90 code | 13 blank | 40 comment | 7 complexity | 5d80e022562e05eac75580c597f48de8 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 7003155
  25. * @summary Unit test for java.nio.file.Path
  26. */
  27. import java.nio.file.*;
  28. import java.net.URI;
  29. import java.net.URISyntaxException;
  30. import java.io.PrintStream;
  31. public class UriImportExport {
  32. static final PrintStream log = System.out;
  33. static int failures = 0;
  34. /**
  35. * Test Path -> URI -> Path
  36. */
  37. static void testPath(String s) {
  38. Path path = Paths.get(s);
  39. log.println(path);
  40. URI uri = path.toUri();
  41. log.println(" --> " + uri);
  42. Path result = Paths.get(uri);
  43. log.println(" --> " + result);
  44. if (!result.equals(path.toAbsolutePath())) {
  45. log.println("FAIL: Expected " + path + ", got " + result);
  46. failures++;
  47. }
  48. log.println();
  49. }
  50. /**
  51. * Test Path -> (expected) URI -> Path
  52. */
  53. static void testPath(String s, String expectedUri) {
  54. Path path = Paths.get(s);
  55. log.println(path);
  56. URI uri = path.toUri();
  57. log.println(" --> " + uri);
  58. if (!uri.toString().equals(expectedUri)) {
  59. log.println("FAILED: Expected " + expectedUri + ", got " + uri);
  60. failures++;
  61. return;
  62. }
  63. Path result = Paths.get(uri);
  64. log.println(" --> " + result);
  65. if (!result.equals(path.toAbsolutePath())) {
  66. log.println("FAIL: Expected " + path + ", got " + result);
  67. failures++;
  68. }
  69. log.println();
  70. }
  71. /**
  72. * Test URI -> Path -> URI
  73. */
  74. static void testUri(String s) throws Exception {
  75. URI uri = URI.create(s);
  76. log.println(uri);
  77. Path path = Paths.get(uri);
  78. log.println(" --> " + path);
  79. URI result = path.toUri();
  80. log.println(" --> " + result);
  81. if (!result.equals(uri)) {
  82. log.println("FAIL: Expected " + uri + ", got " + result);
  83. failures++;
  84. }
  85. log.println();
  86. }
  87. /**
  88. * Test URI -> Path fails with IllegalArgumentException
  89. */
  90. static void testBadUri(String s) throws Exception {
  91. URI uri = URI.create(s);
  92. log.println(uri);
  93. try {
  94. Path path = Paths.get(uri);
  95. log.format(" --> %s FAIL: Expected IllegalArgumentException\n", path);
  96. failures++;
  97. } catch (IllegalArgumentException expected) {
  98. log.println(" --> IllegalArgumentException (expected)");
  99. }
  100. log.println();
  101. }
  102. public static void main(String[] args) throws Exception {
  103. testBadUri("file:foo");
  104. testBadUri("file:/foo?q");
  105. testBadUri("file:/foo#f");
  106. String osname = System.getProperty("os.name");
  107. if (osname.startsWith("Windows")) {
  108. testPath("C:\\doesnotexist");
  109. testPath("C:doesnotexist");
  110. testPath("\\\\server.nowhere.oracle.com\\share\\");
  111. testPath("\\\\fe80--203-baff-fe5a-749ds1.ipv6-literal.net\\share\\missing",
  112. "file://[fe80::203:baff:fe5a:749d%1]/share/missing");
  113. } else {
  114. testPath("doesnotexist");
  115. testPath("/doesnotexist");
  116. testPath("/does not exist");
  117. testUri("file:///");
  118. testUri("file:///foo/bar/doesnotexist");
  119. testUri("file:/foo/bar/doesnotexist");
  120. // file:///foo/bar/\u0440\u0443\u0441\u0441\u043A\u0438\u0439 (Russian)
  121. testUri("file:///foo/bar/%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9");
  122. // invalid
  123. testBadUri("file:foo");
  124. testBadUri("file://server/foo");
  125. testBadUri("file:///foo%00");
  126. }
  127. if (failures > 0)
  128. throw new RuntimeException(failures + " test(s) failed");
  129. }
  130. }