Skip to content

Commit 64836f5

Browse files
authored
Add withPosition(FilePosition) method to FileSource
Calling this method rather than creating a new `FileSource` via `FileSource.from(File, FilePosition)` avoids the overhead of redundant canonical path resolution. Resolves #4630.
1 parent d55cb84 commit 64836f5

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-6.0.0-RC3.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ JUnit repository on GitHub.
3838

3939
* New `Resource.from(String, URI)` static factory method for creating an
4040
`org.junit.platform.commons.support.Resource`.
41+
* New `FileSource.withPosition(FilePosition)` method to avoid the overhead of redundant
42+
canonicalization of files when using `FileSource.from(File, FilePosition)` with many
43+
different `FilePosition` instances for the same `File`.
4144

4245

4346
[[release-notes-6.0.0-RC3-junit-jupiter]]

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

Lines changed: 31 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;
@@ -53,6 +54,7 @@ public static FileSource from(File file) {
5354
*
5455
* @param file the source file; must not be {@code null}
5556
* @param filePosition the position in the source file; may be {@code null}
57+
* @see #withPosition(FilePosition)
5658
*/
5759
public static FileSource from(File file, @Nullable FilePosition filePosition) {
5860
return new FileSource(file, filePosition);
@@ -77,6 +79,11 @@ private FileSource(File file, @Nullable FilePosition filePosition) {
7779
this.filePosition = filePosition;
7880
}
7981

82+
private FileSource(FileSource fileSource, @Nullable FilePosition filePosition) {
83+
this.file = fileSource.file;
84+
this.filePosition = filePosition;
85+
}
86+
8087
/**
8188
* Get the {@link URI} for the source {@linkplain #getFile file}.
8289
*
@@ -104,6 +111,30 @@ public Optional<FilePosition> getPosition() {
104111
return Optional.ofNullable(this.filePosition);
105112
}
106113

114+
/**
115+
* {@return a {@code FileSource} based on this instance but with the
116+
* supplied {@link FilePosition}}
117+
*
118+
* <p>If the supplied {@code FilePosition}
119+
* {@linkplain Objects#equals(Object, Object) equals} the existing one, this
120+
* method returns {@code this}. Otherwise, a new instance is created and
121+
* returned.
122+
*
123+
* <p>Calling this method rather than creating a new {@code FileSource} via
124+
* {@link #from(File, FilePosition)} avoids the overhead of redundant
125+
* canonical path resolution.
126+
*
127+
* @param filePosition the position in the source file; may be {@code null}
128+
* @since 6.0
129+
*/
130+
@API(status = EXPERIMENTAL, since = "6.0")
131+
public FileSource withPosition(@Nullable FilePosition filePosition) {
132+
if (Objects.equals(this.filePosition, filePosition)) {
133+
return this;
134+
}
135+
return new FileSource(this, filePosition);
136+
}
137+
107138
@Override
108139
public boolean equals(Object o) {
109140
if (this == o) {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,27 @@ 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+
assertThat(sourceWithPosition.withPosition(null).getPosition()).isEmpty();
95+
96+
assertThat(source.withPosition(null)).isSameAs(source);
97+
assertThat(sourceWithPosition.withPosition(position)).isSameAs(sourceWithPosition);
98+
}
99+
79100
@Test
80101
void equalsAndHashCodeForFileSource() {
81102
var file1 = new File("foo.txt");

0 commit comments

Comments
 (0)