Skip to content

Commit

Permalink
Improve #subpath exception messages
Browse files Browse the repository at this point in the history
Fixes #158
  • Loading branch information
marschall committed Mar 25, 2024
1 parent d4d8624 commit adb8a2f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ void checkNameRange(int beginIndex, int endIndex) {
throw new IllegalArgumentException("beginIndex must not be negative but was " + beginIndex);
}
if (beginIndex >= nameCount) {
throw new IllegalArgumentException("beginIndex must not be bigger than " + nameCount + " but was " + beginIndex);
throw new IllegalArgumentException("beginIndex must be smaller than " + nameCount + " but was " + beginIndex);
}
if (endIndex <= beginIndex) {
throw new IllegalArgumentException("endIndex must not be smaller than or equal to " + beginIndex + " but was " + beginIndex);
if (beginIndex >= endIndex) {
throw new IllegalArgumentException("beginIndex must be smaller than " + beginIndex + " but was " + beginIndex);
}
if (endIndex > nameCount) {
throw new IllegalArgumentException("endIndex must not be bigger than " + nameCount + " but was " + beginIndex);
throw new IllegalArgumentException("endIndex must be smaller than or equal to " + nameCount + " but was " + endIndex);
}
}

Expand Down Expand Up @@ -212,14 +212,14 @@ public Path normalize() {
}
} else {
if (nameElementsSize == 1) {
// path is just "/.."
normalized = this.handleSingleDotDot(normalized);
modified = normalized != nameElements;
break;
} else {
normalized = this.handleDotDotNormalizationNotYetModified(nameElements, nameElementsSize, i);
modified = normalized != nameElements;
}
// path is just "/.."
normalized = this.handleSingleDotDot(normalized);
modified = normalized != nameElements;
break;
} else {
normalized = this.handleDotDotNormalizationNotYetModified(nameElements, nameElementsSize, i);
modified = normalized != nameElements;
}
}
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,11 @@ void relativeSymlinks(boolean useDefault) throws IOException {
}
}

@CompatibilityTest
void subPath(boolean useDefault) {
FileSystem fileSystem = this.getFileSystem(useDefault);
Path target = fileSystem.getPath("target");
assertThrows(IllegalArgumentException.class, () -> target.subpath(0, 0));
}

}
41 changes: 41 additions & 0 deletions src/test/java/com/github/marschall/memoryfilesystem/PathTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.github.marschall.memoryfilesystem;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Path;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

class PathTests {

@RegisterExtension
final FileSystemExtension extension = new FileSystemExtension();

@Test
void subPath() throws IOException {
try (FileSystem fileSystem = this.extension.getFileSystem()) {
Path absolutePath = fileSystem.getPath("/parent/child.txt");
assertEquals(fileSystem.getPath("parent/child.txt"), absolutePath.subpath(0, 2));
assertEquals(fileSystem.getPath("parent"), absolutePath.subpath(0, 1));
assertEquals(fileSystem.getPath("child.txt"), absolutePath.subpath(1, 2));

IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> absolutePath.subpath(1, 1));
assertEquals("beginIndex must be smaller than 1 but was 1", e.getMessage());

e = assertThrows(IllegalArgumentException.class, () -> absolutePath.subpath(-1, 1));
assertEquals("beginIndex must not be negative but was -1", e.getMessage());

e = assertThrows(IllegalArgumentException.class, () -> absolutePath.subpath(0, 3));
assertEquals("endIndex must be smaller than or equal to 2 but was 3", e.getMessage());

e = assertThrows(IllegalArgumentException.class, () -> absolutePath.subpath(2, 1));
assertEquals("beginIndex must be smaller than 2 but was 2", e.getMessage());
}

}

}

0 comments on commit adb8a2f

Please sign in to comment.