changeset 17708:49163d0109ec

8186142: ZipPath.{starts,ends}With(nonZipPath) throws an exception, but should return false Reviewed-by: martin
author sherman
date Wed, 23 Aug 2017 21:27:02 -0700
parents 46e9f2b0a472
children 4e08a69241ea
files src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipPath.java test/jdk/nio/zipfs/PathOps.java
diffstat 2 files changed, 45 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipPath.java	Wed Aug 23 10:58:11 2017 -0700
+++ b/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipPath.java	Wed Aug 23 21:27:02 2017 -0700
@@ -310,7 +310,10 @@
 
     @Override
     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() ||
             o.path.length > this.path.length)
             return false;
@@ -327,7 +330,10 @@
 
     @Override
     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;
         if (olast > 0 && o.path[olast] == '/')
             olast--;
--- a/test/jdk/nio/zipfs/PathOps.java	Wed Aug 23 10:58:11 2017 -0700
+++ b/test/jdk/nio/zipfs/PathOps.java	Wed Aug 23 21:27:02 2017 -0700
@@ -27,11 +27,13 @@
 import java.nio.file.Files;
 import java.nio.file.InvalidPathException;
 import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.ProviderMismatchException;
 
 /**
  *
  * @test
- * @bug 8038500 8040059 8139956 8146754 8172921
+ * @bug 8038500 8040059 8139956 8146754 8172921 8186142
  * @summary Tests path operations for zip provider.
  *
  * @run main PathOps
@@ -571,16 +573,46 @@
 
     }
 
+    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 {
         // create empty JAR file, test doesn't require any contents
         Path emptyJar = Utils.createJarFile("empty.jar");
 
         fs = FileSystems.newFileSystem(emptyJar, null);
         try {
-        npes();
-        doPathOpTests();
+            npes();
+            mismatchedProviders();
+            doPathOpTests();
         } finally {
-        fs.close();
+            fs.close();
+        }
     }
 }
-}