1
1
/*
2
- * Copyright 2002-2021 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
19
19
import java .beans .PropertyEditor ;
20
20
import java .io .File ;
21
21
import java .nio .file .Path ;
22
+ import java .nio .file .Paths ;
22
23
23
24
import org .junit .jupiter .api .Test ;
24
25
31
32
* @author Juergen Hoeller
32
33
* @since 4.3.2
33
34
*/
34
- public class PathEditorTests {
35
+ class PathEditorTests {
35
36
36
37
@ Test
37
- public void testClasspathPathName () {
38
+ void testClasspathPathName () {
38
39
PropertyEditor pathEditor = new PathEditor ();
39
40
pathEditor .setAsText ("classpath:" + ClassUtils .classPackageAsResourcePath (getClass ()) + "/" +
40
41
ClassUtils .getShortName (getClass ()) + ".class" );
41
42
Object value = pathEditor .getValue ();
42
- assertThat (value instanceof Path ). isTrue ( );
43
+ assertThat (value ). isInstanceOf ( Path . class );
43
44
Path path = (Path ) value ;
44
- assertThat (path .toFile (). exists ()). isTrue ();
45
+ assertThat (path .toFile ()). exists ();
45
46
}
46
47
47
48
@ Test
48
- public void testWithNonExistentResource () {
49
- PropertyEditor propertyEditor = new PathEditor ();
49
+ void testWithNonExistentResource () {
50
+ PropertyEditor pathEditor = new PathEditor ();
50
51
assertThatIllegalArgumentException ().isThrownBy (() ->
51
- propertyEditor .setAsText ("classpath:/no_way_this_file_is_found.doc" ));
52
+ pathEditor .setAsText ("classpath:/no_way_this_file_is_found.doc" ));
52
53
}
53
54
54
55
@ Test
55
- public void testWithNonExistentPath () {
56
+ void testWithNonExistentPath () {
56
57
PropertyEditor pathEditor = new PathEditor ();
57
58
pathEditor .setAsText ("file:/no_way_this_file_is_found.doc" );
58
59
Object value = pathEditor .getValue ();
59
- assertThat (value instanceof Path ). isTrue ( );
60
+ assertThat (value ). isInstanceOf ( Path . class );
60
61
Path path = (Path ) value ;
61
- assertThat (! path .toFile (). exists ()). isTrue ();
62
+ assertThat (path .toFile ()). doesNotExist ();
62
63
}
63
64
64
65
@ Test
65
- public void testAbsolutePath () {
66
+ void testAbsolutePath () {
66
67
PropertyEditor pathEditor = new PathEditor ();
67
68
pathEditor .setAsText ("/no_way_this_file_is_found.doc" );
68
69
Object value = pathEditor .getValue ();
69
- assertThat (value instanceof Path ). isTrue ( );
70
+ assertThat (value ). isInstanceOf ( Path . class );
70
71
Path path = (Path ) value ;
71
- assertThat (! path .toFile (). exists ()). isTrue ();
72
+ assertThat (path .toFile ()). doesNotExist ();
72
73
}
73
74
74
75
@ Test
75
- public void testWindowsAbsolutePath () {
76
+ void testWindowsAbsolutePath () {
76
77
PropertyEditor pathEditor = new PathEditor ();
77
78
pathEditor .setAsText ("C:\\ no_way_this_file_is_found.doc" );
78
79
Object value = pathEditor .getValue ();
79
- assertThat (value instanceof Path ). isTrue ( );
80
+ assertThat (value ). isInstanceOf ( Path . class );
80
81
Path path = (Path ) value ;
81
- assertThat (! path .toFile (). exists ()). isTrue ();
82
+ assertThat (path .toFile ()). doesNotExist ();
82
83
}
83
84
84
85
@ Test
85
- public void testWindowsAbsoluteFilePath () {
86
+ void testWindowsAbsoluteFilePath () {
86
87
PropertyEditor pathEditor = new PathEditor ();
87
88
try {
88
89
pathEditor .setAsText ("file://C:\\ no_way_this_file_is_found.doc" );
89
90
Object value = pathEditor .getValue ();
90
- assertThat (value instanceof Path ). isTrue ( );
91
+ assertThat (value ). isInstanceOf ( Path . class );
91
92
Path path = (Path ) value ;
92
- assertThat (! path .toFile (). exists ()). isTrue ();
93
+ assertThat (path .toFile ()). doesNotExist ();
93
94
}
94
95
catch (IllegalArgumentException ex ) {
95
96
if (File .separatorChar == '\\' ) { // on Windows, otherwise silently ignore
@@ -99,39 +100,49 @@ public void testWindowsAbsoluteFilePath() {
99
100
}
100
101
101
102
@ Test
102
- public void testUnqualifiedPathNameFound () {
103
+ void testCurrentDirectory () {
104
+ PropertyEditor pathEditor = new PathEditor ();
105
+ pathEditor .setAsText ("file:." );
106
+ Object value = pathEditor .getValue ();
107
+ assertThat (value ).isInstanceOf (Path .class );
108
+ Path path = (Path ) value ;
109
+ assertThat (path ).isEqualTo (Paths .get ("." ));
110
+ }
111
+
112
+ @ Test
113
+ void testUnqualifiedPathNameFound () {
103
114
PropertyEditor pathEditor = new PathEditor ();
104
115
String fileName = ClassUtils .classPackageAsResourcePath (getClass ()) + "/" +
105
116
ClassUtils .getShortName (getClass ()) + ".class" ;
106
117
pathEditor .setAsText (fileName );
107
118
Object value = pathEditor .getValue ();
108
- assertThat (value instanceof Path ). isTrue ( );
119
+ assertThat (value ). isInstanceOf ( Path . class );
109
120
Path path = (Path ) value ;
110
121
File file = path .toFile ();
111
- assertThat (file . exists ()). isTrue ();
122
+ assertThat (file ). exists ();
112
123
String absolutePath = file .getAbsolutePath ();
113
124
if (File .separatorChar == '\\' ) {
114
125
absolutePath = absolutePath .replace ('\\' , '/' );
115
126
}
116
- assertThat (absolutePath .endsWith (fileName )). isTrue ( );
127
+ assertThat (absolutePath ) .endsWith (fileName );
117
128
}
118
129
119
130
@ Test
120
- public void testUnqualifiedPathNameNotFound () {
131
+ void testUnqualifiedPathNameNotFound () {
121
132
PropertyEditor pathEditor = new PathEditor ();
122
133
String fileName = ClassUtils .classPackageAsResourcePath (getClass ()) + "/" +
123
134
ClassUtils .getShortName (getClass ()) + ".clazz" ;
124
135
pathEditor .setAsText (fileName );
125
136
Object value = pathEditor .getValue ();
126
- assertThat (value instanceof Path ). isTrue ( );
137
+ assertThat (value ). isInstanceOf ( Path . class );
127
138
Path path = (Path ) value ;
128
139
File file = path .toFile ();
129
- assertThat (file . exists ()). isFalse ();
140
+ assertThat (file ). doesNotExist ();
130
141
String absolutePath = file .getAbsolutePath ();
131
142
if (File .separatorChar == '\\' ) {
132
143
absolutePath = absolutePath .replace ('\\' , '/' );
133
144
}
134
- assertThat (absolutePath .endsWith (fileName )). isTrue ( );
145
+ assertThat (absolutePath ) .endsWith (fileName );
135
146
}
136
147
137
148
}
0 commit comments