Skip to content

Commit ac5529c

Browse files
committed
#4630 - Add withPosition instance method to allow reuse of resolved File
1 parent a29e93e commit ac5529c

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

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

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.junit.platform.engine.support.descriptor;
1212

13+
import static org.apiguardian.api.API.Status.EXPERIMENTAL;
1314
import static org.apiguardian.api.API.Status.STABLE;
1415

1516
import java.io.File;
@@ -44,7 +45,7 @@ public class FileSource implements FileSystemSource {
4445
* @param file the source file; must not be {@code null}
4546
*/
4647
public static FileSource from(File file) {
47-
return canonicalized(file, null);
48+
return from(file, null);
4849
}
4950

5051
/**
@@ -55,20 +56,14 @@ public static FileSource from(File file) {
5556
* @param filePosition the position in the source file; may be {@code null}
5657
*/
5758
public static FileSource from(File file, @Nullable FilePosition 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);
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+
}
7267
}
7368

7469
private final File file;
@@ -80,21 +75,6 @@ private FileSource(File file, @Nullable FilePosition filePosition) {
8075
this.file = file;
8176
this.filePosition = filePosition;
8277
}
83-
84-
private static FileSource canonicalized(File file, @Nullable FilePosition filePosition) {
85-
Preconditions.notNull(file, "file must not be null");
86-
try {
87-
return new FileSource(file.getCanonicalFile(), filePosition);
88-
}
89-
catch (IOException ex) {
90-
throw new JUnitException("Failed to retrieve canonical path for file: " + file, ex);
91-
}
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);
97-
}
9878

9979
/**
10080
* Get the {@link URI} for the source {@linkplain #getFile file}.
@@ -123,6 +103,20 @@ public final Optional<FilePosition> getPosition() {
123103
return Optional.ofNullable(this.filePosition);
124104
}
125105

106+
/**
107+
* Return a new {@code FileSource} based on this instance but with a different
108+
* {@link FilePosition}. This avoids redundant canonical path resolution
109+
* by reusing the already-canonical file.
110+
*
111+
* @param filePosition the new {@code FilePosition}; must not be {@code null}
112+
* @return a new {@code FileSource} with the same file and updated position
113+
*/
114+
@API(status = EXPERIMENTAL, since = "1.14")
115+
public FileSource withPosition(FilePosition filePosition) {
116+
Preconditions.notNull(filePosition, "position must not be null");
117+
return new FileSource(this.file, filePosition);
118+
}
119+
126120
@Override
127121
public boolean equals(Object o) {
128122
if (this == o) {

platform-tests/src/test/java/org/junit/platform/engine/support/descriptor/FileSystemSourceTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ void fileWithPosition() {
7676
assertThat(source.getPosition()).hasValue(position);
7777
}
7878

79+
@Test
80+
void fileReuseWithPosition() {
81+
var file = new File("test.txt");
82+
var position = FilePosition.from(42, 23);
83+
var source = FileSource.from(file);
84+
var sourceWithPosition = source.withPosition(position);
85+
86+
assertThat(source.getUri()).isEqualTo(file.getAbsoluteFile().toURI());
87+
assertThat(source.getFile()).isEqualTo(file.getAbsoluteFile());
88+
assertThat(source.getPosition()).isEmpty();
89+
90+
assertThat(source).isNotSameAs(sourceWithPosition);
91+
assertThat(source.getFile()).isSameAs(sourceWithPosition.getFile());
92+
assertThat(sourceWithPosition.getPosition()).hasValue(position);
93+
}
94+
7995
@Test
8096
void equalsAndHashCodeForFileSource() {
8197
var file1 = new File("foo.txt");

0 commit comments

Comments
 (0)