10
10
11
11
package org .junit .platform .engine .support .descriptor ;
12
12
13
+ import static org .apiguardian .api .API .Status .EXPERIMENTAL ;
13
14
import static org .apiguardian .api .API .Status .STABLE ;
14
15
15
16
import java .io .File ;
@@ -44,7 +45,7 @@ public class FileSource implements FileSystemSource {
44
45
* @param file the source file; must not be {@code null}
45
46
*/
46
47
public static FileSource from (File file ) {
47
- return new FileSource (file );
48
+ return from (file , null );
48
49
}
49
50
50
51
/**
@@ -55,25 +56,22 @@ public static FileSource from(File file) {
55
56
* @param filePosition the position in the source file; may be {@code null}
56
57
*/
57
58
public static FileSource from (File file , @ Nullable FilePosition filePosition ) {
58
- return new FileSource (file , filePosition );
59
+ Preconditions .notNull (file , "file must not be null" );
60
+ try {
61
+ File canonicalFile = file .getCanonicalFile ();
62
+ return new FileSource (canonicalFile , filePosition );
63
+ }
64
+ catch (IOException ex ) {
65
+ throw new JUnitException ("Failed to retrieve canonical path for file: " + file , ex );
66
+ }
59
67
}
60
68
61
69
private final File file ;
62
70
63
71
private final @ Nullable FilePosition filePosition ;
64
72
65
- private FileSource (File file ) {
66
- this (file , null );
67
- }
68
-
69
73
private FileSource (File file , @ Nullable FilePosition filePosition ) {
70
- Preconditions .notNull (file , "file must not be null" );
71
- try {
72
- this .file = file .getCanonicalFile ();
73
- }
74
- catch (IOException ex ) {
75
- throw new JUnitException ("Failed to retrieve canonical path for file: " + file , ex );
76
- }
74
+ this .file = file ;
77
75
this .filePosition = filePosition ;
78
76
}
79
77
@@ -104,6 +102,20 @@ public final Optional<FilePosition> getPosition() {
104
102
return Optional .ofNullable (this .filePosition );
105
103
}
106
104
105
+ /**
106
+ * Return a new {@code FileSource} based on this instance but with a different
107
+ * {@link FilePosition}. This avoids redundant canonical path resolution
108
+ * by reusing the already-canonical file.
109
+ *
110
+ * @param filePosition the new {@code FilePosition}; must not be {@code null}
111
+ * @return a new {@code FileSource} with the same file and updated position
112
+ */
113
+ @ API (status = EXPERIMENTAL , since = "6.0" )
114
+ public FileSource withPosition (FilePosition filePosition ) {
115
+ Preconditions .notNull (filePosition , "filePosition must not be null" );
116
+ return new FileSource (this .file , filePosition );
117
+ }
118
+
107
119
@ Override
108
120
public boolean equals (Object o ) {
109
121
if (this == o ) {
0 commit comments