8186142: ZipPath.{starts,ends}With(nonZipPath) throws an exception, but should return false

Reviewed-by: martin
This commit is contained in:
Xueming Shen 2017-08-23 21:27:02 -07:00
parent 0ad228775f
commit 1152b56c5b
2 changed files with 45 additions and 7 deletions

View File

@ -310,7 +310,10 @@ final class ZipPath implements Path {
@Override @Override
public boolean startsWith(Path other) { public boolean startsWith(Path other) {
final ZipPath o = checkPath(other); Objects.requireNonNull(other, "other");
if (!(other instanceof ZipPath))
return false;
final ZipPath o = (ZipPath)other;
if (o.isAbsolute() != this.isAbsolute() || if (o.isAbsolute() != this.isAbsolute() ||
o.path.length > this.path.length) o.path.length > this.path.length)
return false; return false;
@ -327,7 +330,10 @@ final class ZipPath implements Path {
@Override @Override
public boolean endsWith(Path other) { public boolean endsWith(Path other) {
final ZipPath o = checkPath(other); Objects.requireNonNull(other, "other");
if (!(other instanceof ZipPath))
return false;
final ZipPath o = (ZipPath)other;
int olast = o.path.length - 1; int olast = o.path.length - 1;
if (olast > 0 && o.path[olast] == '/') if (olast > 0 && o.path[olast] == '/')
olast--; olast--;

View File

@ -27,11 +27,13 @@ import java.nio.file.FileSystems;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.InvalidPathException; import java.nio.file.InvalidPathException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.ProviderMismatchException;
/** /**
* *
* @test * @test
* @bug 8038500 8040059 8139956 8146754 8172921 * @bug 8038500 8040059 8139956 8146754 8172921 8186142
* @summary Tests path operations for zip provider. * @summary Tests path operations for zip provider.
* *
* @run main PathOps * @run main PathOps
@ -571,16 +573,46 @@ public class PathOps {
} }
static void mismatchedProviders() {
header("ProviderMismatchException");
Path path = fs.getPath("foo");
Path other = Paths.get("foo");
try {
path.compareTo(other);
throw new RuntimeException("ProviderMismatchException not thrown");
} catch (ProviderMismatchException pme) {}
try {
path.resolve(other);
throw new RuntimeException("ProviderMismatchException not thrown");
} catch (ProviderMismatchException pme) {}
try {
path.relativize(other);
throw new RuntimeException("ProviderMismatchException not thrown");
} catch (ProviderMismatchException pme) {}
try {
if (path.startsWith(other))
throw new RuntimeException("providerMismatched startsWith() returns true ");
if (path.endsWith(other))
throw new RuntimeException("providerMismatched endsWith() returns true ");
} catch (ProviderMismatchException pme) {
throw new RuntimeException("ProviderMismatchException is thrown for starts/endsWith()");
}
}
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
// create empty JAR file, test doesn't require any contents // create empty JAR file, test doesn't require any contents
Path emptyJar = Utils.createJarFile("empty.jar"); Path emptyJar = Utils.createJarFile("empty.jar");
fs = FileSystems.newFileSystem(emptyJar, null); fs = FileSystems.newFileSystem(emptyJar, null);
try { try {
npes(); npes();
doPathOpTests(); mismatchedProviders();
doPathOpTests();
} finally { } finally {
fs.close(); fs.close();
}
} }
} }
}