Skip to content

Commit a29e93e

Browse files
authored
Add withPosition() factory method to FileSource
- Refactor creation to avoid redundant canonicalization calls - Introduced internal factory methods for clearer construction paths
1 parent 7d02a6a commit a29e93e

File tree

1 file changed

+26
-8
lines changed
  • junit-platform-engine/src/main/java/org/junit/platform/engine/support/descriptor

1 file changed

+26
-8
lines changed

junit-platform-engine/src/main/java/org/junit/platform/engine/support/descriptor/FileSource.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class FileSource implements FileSystemSource {
4444
* @param file the source file; must not be {@code null}
4545
*/
4646
public static FileSource from(File file) {
47-
return new FileSource(file);
47+
return canonicalized(file, null);
4848
}
4949

5050
/**
@@ -55,27 +55,45 @@ public static FileSource from(File file) {
5555
* @param filePosition the position in the source file; may be {@code null}
5656
*/
5757
public static FileSource from(File file, @Nullable FilePosition filePosition) {
58-
return new FileSource(file, filePosition);
58+
return canonicalized(file, filePosition);
59+
}
60+
61+
/**
62+
* Create a new {@code FileSource} from an existing instance but with a different
63+
* {@link FilePosition}. This avoids redundant canonical path resolution.
64+
*
65+
* @param source the existing {@code FileSource}; must not be {@code null}
66+
* @param filePosition the new {@code FilePosition}; may be {@code null}
67+
* @return a new {@code FileSource} with same file and updated position
68+
*/
69+
public static FileSource withPosition(FileSource source, @Nullable FilePosition filePosition) {
70+
Preconditions.notNull(source, "source must not be null");
71+
return direct(source.file, filePosition);
5972
}
6073

6174
private final File file;
6275

6376
@Nullable
6477
private final FilePosition filePosition;
6578

66-
private FileSource(File file) {
67-
this(file, null);
68-
}
69-
7079
private FileSource(File file, @Nullable FilePosition filePosition) {
80+
this.file = file;
81+
this.filePosition = filePosition;
82+
}
83+
84+
private static FileSource canonicalized(File file, @Nullable FilePosition filePosition) {
7185
Preconditions.notNull(file, "file must not be null");
7286
try {
73-
this.file = file.getCanonicalFile();
87+
return new FileSource(file.getCanonicalFile(), filePosition);
7488
}
7589
catch (IOException ex) {
7690
throw new JUnitException("Failed to retrieve canonical path for file: " + file, ex);
7791
}
78-
this.filePosition = filePosition;
92+
}
93+
94+
private static FileSource direct(File file, @Nullable FilePosition filePosition) {
95+
Preconditions.notNull(file, "file must not be null");
96+
return new FileSource(file, filePosition);
7997
}
8098

8199
/**

0 commit comments

Comments
 (0)