|
1 | 1 | /*
|
2 |
| - * Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
22 | 22 | */
|
23 | 23 |
|
24 | 24 | /* @test
|
25 |
| - @bug 4131169 4109131 |
26 |
| - @summary Basic test for getAbsolutePath method |
| 25 | + * @bug 4131169 4109131 8287843 |
| 26 | + * @summary Basic test for getAbsolutePath method |
| 27 | + * @run junit GetAbsolutePath |
27 | 28 | */
|
28 | 29 |
|
29 |
| -import java.io.*; |
| 30 | +import java.io.File; |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.stream.Stream; |
30 | 33 |
|
| 34 | +import org.junit.jupiter.api.BeforeAll; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | +import org.junit.jupiter.api.condition.EnabledOnOs; |
| 37 | +import org.junit.jupiter.api.condition.OS; |
| 38 | +import org.junit.jupiter.params.provider.Arguments; |
| 39 | +import org.junit.jupiter.params.ParameterizedTest; |
| 40 | +import org.junit.jupiter.params.provider.MethodSource; |
| 41 | +import static org.junit.jupiter.api.Assertions.*; |
31 | 42 |
|
32 | 43 | public class GetAbsolutePath {
|
33 | 44 |
|
34 |
| - private static boolean ignoreCase = false; |
| 45 | + private static final String USER_DIR = System.getProperty("user.dir"); |
35 | 46 |
|
36 |
| - private static void ck(String path, String ans) throws Exception { |
37 |
| - File f = new File(path); |
38 |
| - String p = f.getAbsolutePath(); |
39 |
| - if ((ignoreCase && p.equalsIgnoreCase(ans)) || p.equals(ans)) |
40 |
| - System.err.println(path + " ==> " + p); |
41 |
| - else |
42 |
| - throw new Exception(path + ": expected " + ans + ", got " + p); |
| 47 | + private static char driveLetter() { |
| 48 | + assert System.getProperty("os.name").startsWith("Windows"); |
| 49 | + |
| 50 | + if ((USER_DIR.length() > 2) && (USER_DIR.charAt(1) == ':') |
| 51 | + && (USER_DIR.charAt(2) == '\\')) |
| 52 | + return USER_DIR.charAt(0); |
| 53 | + |
| 54 | + throw new RuntimeException("Current directory has no drive"); |
43 | 55 | }
|
44 | 56 |
|
45 |
| - private static void testWin32() throws Exception { |
46 |
| - String wd = System.getProperty("user.dir"); |
47 |
| - char d; |
48 |
| - if ((wd.length() > 2) && (wd.charAt(1) == ':') |
49 |
| - && (wd.charAt(2) == '\\')) |
50 |
| - d = wd.charAt(0); |
51 |
| - else |
52 |
| - throw new Exception("Current directory has no drive"); |
53 |
| - ck("/foo/bar", d + ":\\foo\\bar"); |
54 |
| - ck("\\foo\\bar", d + ":\\foo\\bar"); |
55 |
| - ck("c:\\foo\\bar", "c:\\foo\\bar"); |
56 |
| - ck("c:/foo/bar", "c:\\foo\\bar"); |
57 |
| - ck("\\\\foo\\bar", "\\\\foo\\bar"); |
| 57 | + private static Stream<Arguments> windowsSource() { |
| 58 | + char drive = driveLetter(); |
| 59 | + return Stream.of(Arguments.of("/foo/bar", drive + ":\\foo\\bar"), |
| 60 | + Arguments.of("\\foo\\bar", drive + ":\\foo\\bar"), |
| 61 | + Arguments.of("c:\\foo\\bar", "c:\\foo\\bar"), |
| 62 | + Arguments.of("c:/foo/bar", "c:\\foo\\bar"), |
| 63 | + Arguments.of("\\\\foo\\bar", "\\\\foo\\bar"), |
| 64 | + Arguments.of("", USER_DIR), // empty path |
| 65 | + Arguments.of("\\\\?\\foo", USER_DIR + "\\foo"), |
| 66 | + Arguments.of("\\\\?\\C:\\Users\\x", "C:\\Users\\x"), |
| 67 | + Arguments.of("\\\\?\\" + drive + ":", USER_DIR), |
| 68 | + Arguments.of("\\\\?\\" + drive + ":bar", USER_DIR + "\\bar")); |
| 69 | + } |
| 70 | + |
| 71 | + @EnabledOnOs(OS.WINDOWS) |
| 72 | + @ParameterizedTest |
| 73 | + @MethodSource("windowsSource") |
| 74 | + public void windows(String path, String absolute) throws IOException { |
| 75 | + File file = new File(path); |
| 76 | + assertEquals(0, absolute.compareToIgnoreCase(file.getAbsolutePath())); |
| 77 | + } |
58 | 78 |
|
59 |
| - /* Tricky directory-relative case */ |
60 |
| - d = Character.toLowerCase(d); |
| 79 | + @EnabledOnOs(OS.WINDOWS) |
| 80 | + @Test |
| 81 | + public void windowsDriveRelative() throws IOException { |
| 82 | + // Tricky directory-relative case |
| 83 | + char d = Character.toLowerCase(driveLetter()); |
61 | 84 | char z = 0;
|
62 | 85 | if (d != 'c') z = 'c';
|
63 | 86 | else if (d != 'd') z = 'd';
|
64 | 87 | if (z != 0) {
|
65 | 88 | File f = new File(z + ":.");
|
66 | 89 | if (f.exists()) {
|
67 |
| - String zwd = f.getCanonicalPath(); |
68 |
| - ck(z + ":foo", zwd + "\\foo"); |
| 90 | + String zUSER_DIR = f.getCanonicalPath(); |
| 91 | + assertEquals(z + ":foo", zUSER_DIR + "\\foo"); |
69 | 92 | }
|
70 | 93 | }
|
71 |
| - |
72 |
| - /* Empty path */ |
73 |
| - ck("", wd); |
74 | 94 | }
|
75 | 95 |
|
76 |
| - private static void testUnix() throws Exception { |
77 |
| - String wd = System.getProperty("user.dir"); |
78 |
| - ck("foo", wd + "/foo"); |
79 |
| - ck("foo/bar", wd + "/foo/bar"); |
80 |
| - ck("/foo", "/foo"); |
81 |
| - ck("/foo/bar", "/foo/bar"); |
82 |
| - |
83 |
| - /* Empty path */ |
84 |
| - ck("", wd); |
| 96 | + private static Stream<Arguments> unixSource() { |
| 97 | + return Stream.of(Arguments.of("foo", USER_DIR + "/foo"), |
| 98 | + Arguments.of("foo/bar", USER_DIR + "/foo/bar"), |
| 99 | + Arguments.of("/foo", "/foo"), |
| 100 | + Arguments.of("/foo/bar", "/foo/bar"), |
| 101 | + Arguments.of("", USER_DIR)); |
85 | 102 | }
|
86 | 103 |
|
87 |
| - public static void main(String[] args) throws Exception { |
88 |
| - if (File.separatorChar == '\\') { |
89 |
| - ignoreCase = true; |
90 |
| - testWin32(); |
91 |
| - } |
92 |
| - if (File.separatorChar == '/') testUnix(); |
| 104 | + @EnabledOnOs({OS.LINUX, OS.MAC}) |
| 105 | + @ParameterizedTest |
| 106 | + @MethodSource("unixSource") |
| 107 | + public void unix(String path, String absolute) throws IOException { |
| 108 | + assertEquals(absolute, new File(path).getAbsolutePath()); |
93 | 109 | }
|
94 |
| - |
95 | 110 | }
|
0 commit comments