Skip to content

Commit c3d041d

Browse files
committed
Add withPosition instance method to allow reuse of resolved canonical file
See #4630
1 parent 88dc121 commit c3d041d

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

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

Lines changed: 20 additions & 0 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;
@@ -77,6 +78,11 @@ private FileSource(File file, @Nullable FilePosition filePosition) {
7778
this.filePosition = filePosition;
7879
}
7980

81+
private FileSource(FileSource fileSource, FilePosition filePosition) {
82+
this.file = fileSource.file;
83+
this.filePosition = filePosition;
84+
}
85+
8086
/**
8187
* Get the {@link URI} for the source {@linkplain #getFile file}.
8288
*
@@ -104,6 +110,20 @@ public Optional<FilePosition> getPosition() {
104110
return Optional.ofNullable(this.filePosition);
105111
}
106112

113+
/**
114+
* Return a new {@code FileSource} based on this instance but with a different
115+
* {@link FilePosition}. This avoids redundant canonical path resolution
116+
* by reusing the already-canonical file.
117+
*
118+
* @param filePosition the new {@code FilePosition}; must not be {@code null}
119+
* @return a new {@code FileSource} with the same file and updated position
120+
*/
121+
@API(status = EXPERIMENTAL, since = "6.0")
122+
public FileSource withPosition(FilePosition filePosition) {
123+
Preconditions.notNull(filePosition, "filePosition must not be null");
124+
return new FileSource(this, filePosition);
125+
}
126+
107127
@Override
108128
public boolean equals(Object o) {
109129
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)