Skip to content

Commit 8924760

Browse files
committed
Fix some bugs in PathFileObject, add test pack
- Mark methods in PathFileObject as raising NoSuchFileException rather than FileNotFoundException which was previously incorrect information. - Fix bug where the 'name' of a PathFileObject could previously have been an absolute path if the PathFileObject was initialised with an absolute file path. Now it will match the string representation of the relative path on the object. - Rename getRoot to the clearer 'getRootPath' which is consistent with other method naming in this class. - Update IllegalArgumentException that is thrown if a root path is not absolute so that it conveys the value of the erroneous parameter in the error message. - Implement tests for PathFileObject.
1 parent f051912 commit 8924760

File tree

3 files changed

+801
-15
lines changed

3 files changed

+801
-15
lines changed

java-compiler-testing/src/main/java/io/github/ascopes/jct/filemanagers/PathFileObject.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.io.BufferedOutputStream;
2424
import java.io.BufferedReader;
2525
import java.io.BufferedWriter;
26-
import java.io.FileNotFoundException;
2726
import java.io.IOException;
2827
import java.io.InputStream;
2928
import java.io.InputStreamReader;
@@ -37,6 +36,7 @@
3736
import java.nio.charset.StandardCharsets;
3837
import java.nio.file.FileSystem;
3938
import java.nio.file.Files;
39+
import java.nio.file.NoSuchFileException;
4040
import java.nio.file.Path;
4141
import javax.annotation.Nullable;
4242
import javax.annotation.WillNotClose;
@@ -90,7 +90,7 @@ public PathFileObject(Location location, Path rootPath, Path relativePath) {
9090
requireNonNull(relativePath, "relativePath");
9191

9292
if (!rootPath.isAbsolute()) {
93-
throw new IllegalArgumentException("rootPath must be absolute");
93+
throw new IllegalArgumentException("Expected rootPath to be absolute, but got " + rootPath);
9494
}
9595

9696
this.location = location;
@@ -101,7 +101,7 @@ public PathFileObject(Location location, Path rootPath, Path relativePath) {
101101
: relativePath;
102102

103103
fullPath = rootPath.resolve(relativePath);
104-
name = relativePath.toString();
104+
name = this.relativePath.toString();
105105
uri = fullPath.toUri();
106106
kind = FileUtils.pathToKind(relativePath);
107107
}
@@ -138,8 +138,8 @@ public boolean equals(@Nullable Object other) {
138138
* Get the class access level, where appropriate.
139139
*
140140
* <p>In this implementation, this class will always return {@code null}, since this
141-
* information is not readily available without preloading the file in question and
142-
* parsing it first.
141+
* information is not readily available without preloading the file in question and parsing it
142+
* first.
143143
*
144144
* <p>At the time of writing, the OpenJDK implementations of the JavaFileObject class
145145
* do not provide an implementation for this method either.
@@ -227,8 +227,8 @@ public String getName() {
227227
* Determine the class nesting kind, where appropriate.
228228
*
229229
* <p>In this implementation, this class will always return {@code null}, since this
230-
* information is not readily available without preloading the file in question and
231-
* parsing it first.
230+
* information is not readily available without preloading the file in question and parsing it
231+
* first.
232232
*
233233
* <p>At the time of writing, the OpenJDK implementations of the JavaFileObject class
234234
* do not provide an implementation for this method either.
@@ -255,7 +255,7 @@ public Path getRelativePath() {
255255
*
256256
* @return the root path.
257257
*/
258-
public Path getRoot() {
258+
public Path getRootPath() {
259259
return rootPath;
260260
}
261261

@@ -295,8 +295,8 @@ public boolean isNameCompatible(String simpleName, Kind kind) {
295295
* <p>The returned implementation will always be buffered.
296296
*
297297
* @return a buffered input stream.
298-
* @throws FileNotFoundException if the file does not exist.
299-
* @throws IOException if an IO error occurs.
298+
* @throws NoSuchFileException if the file does not exist.
299+
* @throws IOException if an IO error occurs.
300300
*/
301301
@Override
302302
@WillNotClose
@@ -315,7 +315,6 @@ public BufferedInputStream openInputStream() throws IOException {
315315
*
316316
* <p>The returned implementation will always be buffered.
317317
*
318-
*
319318
* @return a buffered output stream.
320319
* @throws IOException if an IO error occurs.
321320
*/
@@ -336,8 +335,8 @@ public BufferedOutputStream openOutputStream() throws IOException {
336335
* @param ignoreEncodingErrors {@code true} to suppress encoding errors, or {@code false} to throw
337336
* them to the caller.
338337
* @return a buffered reader.
339-
* @throws FileNotFoundException if the file does not exist.
340-
* @throws IOException if an IO error occurs.
338+
* @throws NoSuchFileException if the file does not exist.
339+
* @throws IOException if an IO error occurs.
341340
*/
342341
@Override
343342
@WillNotClose
@@ -351,7 +350,7 @@ public BufferedReader openReader(boolean ignoreEncodingErrors) throws IOExceptio
351350
/**
352351
* Open a writer to this file using the default charset (UTF-8).
353352
*
354-
* <p>This will Ccreate the file first if it does not already exist. If it does exist,
353+
* <p>This will create the file first if it does not already exist. If it does exist,
355354
* this will first overwrite the file and truncate it.
356355
*
357356
* <p>This input stream must be closed once finished with, otherwise

java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/helpers/Fixtures.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,31 @@ public static Path somePath() {
418418
return root.resolve("some-dummy-path");
419419
}
420420

421+
/**
422+
* Get some dummy absolute path.
423+
*
424+
* @return some dummy absolute path.
425+
*/
426+
public static Path someAbsolutePath() {
427+
return somePath().resolve("some-absolute-path").toAbsolutePath();
428+
}
429+
430+
/**
431+
* Get some relative path.
432+
*
433+
* @return some dummy relative path.
434+
*/
435+
public static Path someRelativePath() {
436+
var absolutePath = someAbsolutePath();
437+
var relativePath = absolutePath;
438+
439+
for (var i = 0; i < someInt(1, 4); ++i) {
440+
relativePath = absolutePath.resolve(someText());
441+
}
442+
443+
return absolutePath.relativize(relativePath.resolve("some-relative-path"));
444+
}
445+
421446
/**
422447
* Get some module reference.
423448
*
@@ -521,7 +546,6 @@ private TempFileSystem() {
521546
.setAttributeViews("basic", "posix")
522547
.setRoots("/")
523548
.setWorkingDirectory("/")
524-
.setPathEqualityUsesCanonicalForm(true)
525549
.build();
526550

527551
fs = Jimfs.newFileSystem(name, config);

0 commit comments

Comments
 (0)