7005986: (zipfs) ZipPath.startsWith() fails because of the implementation of getName(index)
Summary: Updated starsWith/endsWith to be consistent with default file system
Reviewed-by: alanb
--- a/jdk/src/share/demo/nio/zipfs/src/com/sun/nio/zipfs/ZipPath.java Fri Feb 04 12:54:51 2011 -0800
+++ b/jdk/src/share/demo/nio/zipfs/src/com/sun/nio/zipfs/ZipPath.java Fri Feb 04 13:17:30 2011 -0800
@@ -265,7 +265,7 @@
@Override
public boolean isAbsolute() {
- return (this.path[0] == '/');
+ return (this.path.length > 0 && path[0] == '/');
}
@Override
@@ -298,32 +298,40 @@
@Override
public boolean startsWith(Path other) {
final ZipPath o = checkPath(other);
- if (o.isAbsolute() != this.isAbsolute())
+ if (o.isAbsolute() != this.isAbsolute() ||
+ o.path.length > this.path.length)
return false;
- final int oCount = o.getNameCount();
- if (getNameCount() < oCount)
- return false;
- for (int i = 0; i < oCount; i++) {
- if (!o.getName(i).equals(getName(i)))
+ int olast = o.path.length;
+ for (int i = 0; i < olast; i++) {
+ if (o.path[i] != this.path[i])
return false;
}
- return true;
+ olast--;
+ return o.path.length == this.path.length ||
+ o.path[olast] == '/' ||
+ this.path[olast + 1] == '/';
}
@Override
public boolean endsWith(Path other) {
final ZipPath o = checkPath(other);
- if (o.isAbsolute())
- return this.isAbsolute() ? this.equals(o) : false;
- int i = o.getNameCount();
- int j = this.getNameCount();
- if (j < i)
+ int olast = o.path.length - 1;
+ if (olast > 0 && o.path[olast] == '/')
+ olast--;
+ int last = this.path.length - 1;
+ if (last > 0 && this.path[last] == '/')
+ last--;
+ if (olast == -1) // o.path.length == 0
+ return last == -1;
+ if ((o.isAbsolute() &&(!this.isAbsolute() || olast != last)) ||
+ (last < olast))
return false;
- for (--i, --j; i >= 0; i--, j--) {
- if (!o.getName(i).equals(this.getName(j)))
+ for (; olast >= 0; olast--, last--) {
+ if (o.path[olast] != this.path[last])
return false;
}
- return true;
+ return o.path[olast + 1] == '/' ||
+ last == -1 || this.path[last] == '/';
}
@Override
--- a/jdk/test/demo/zipfs/PathOps.java Fri Feb 04 12:54:51 2011 -0800
+++ b/jdk/test/demo/zipfs/PathOps.java Fri Feb 04 13:17:30 2011 -0800
@@ -252,31 +252,41 @@
.name("foo");
// startsWith
+ test("")
+ .starts("")
+ .notStarts("/");
test("/")
.starts("/")
.notStarts("/foo");
test("/foo")
.starts("/")
.starts("/foo")
- .notStarts("/f");
+ .notStarts("/f")
+ .notStarts("");
test("/foo/bar")
.starts("/")
.starts("/foo")
+ .starts("/foo/")
.starts("/foo/bar")
.notStarts("/f")
.notStarts("foo")
- .notStarts("foo/bar");
+ .notStarts("foo/bar")
+ .notStarts("");
test("foo")
.starts("foo")
.notStarts("f");
test("foo/bar")
.starts("foo")
+ .starts("foo/")
.starts("foo/bar")
.notStarts("f")
.notStarts("/foo")
.notStarts("/foo/bar");
// endsWith
+ test("")
+ .ends("")
+ .notEnds("/");
test("/")
.ends("/")
.notEnds("foo")
@@ -288,14 +298,24 @@
test("/foo/bar")
.ends("bar")
.ends("foo/bar")
+ .ends("foo/bar/")
+ .ends("/foo/bar")
+ .notEnds("/bar");
+ test("/foo/bar/")
+ .ends("bar")
+ .ends("foo/bar")
+ .ends("foo/bar/")
.ends("/foo/bar")
.notEnds("/bar");
test("foo")
.ends("foo");
test("foo/bar")
.ends("bar")
+ .ends("bar/")
+ .ends("foo/bar/")
.ends("foo/bar");
+
// elements
test("a/b/c")
.element(0,"a")
@@ -309,6 +329,8 @@
.absolute();
test("tmp")
.notAbsolute();
+ test("")
+ .notAbsolute();
// resolve
test("/tmp")
--- a/jdk/test/demo/zipfs/basic.sh Fri Feb 04 12:54:51 2011 -0800
+++ b/jdk/test/demo/zipfs/basic.sh Fri Feb 04 13:17:30 2011 -0800
@@ -21,7 +21,7 @@
# questions.
#
# @test
-# @bug 6990846 7009092 7009085 7015391 7014948
+# @bug 6990846 7009092 7009085 7015391 7014948 7005986
# @summary Test ZipFileSystem demo
# @build Basic PathOps ZipFSTester
# @run shell basic.sh