You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by nd...@apache.org on 2006/11/20 03:27:56 UTC
svn commit: r477007 - in /harmony/enhanced/classlib/trunk/modules/luni/src:
main/java/java/net/URI.java
test/java/org/apache/harmony/tests/java/net/URITest.java
Author: ndbeyer
Date: Sun Nov 19 18:27:51 2006
New Revision: 477007
URL: http://svn.apache.org/viewvc?view=rev&rev=477007
Log:
Fix java.net.URI#relativize(URI) to return an empty URI when an equivalent URI is passed. Update URITest to include a regression test for this behavior.
This bug was found while running the Eclipse "coreresources" unit tests.
Modified:
harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URI.java
harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/net/URITest.java
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URI.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URI.java?view=diff&rev=477007&r1=477006&r2=477007
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URI.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URI.java Sun Nov 19 18:27:51 2006
@@ -1304,31 +1304,37 @@
if (authority == null ? relative.authority != null : !authority
.equals(relative.authority)) {
return relative;
- }
-
- // append a slash to the end if necessary
- // (for a case like: "dir1" against "dir1/hi" should return "hi", not
- // "/hi")
- String tempPath = null;
- if (path.endsWith("/")) {
- tempPath = path;
- } else {
- tempPath = path + "/";
- }
+ }
// normalize both paths
- String normrel = normalize(relative.path);
- tempPath = normalize(tempPath);
+ String thisPath = normalize(path);
+ String relativePath = normalize(relative.path);
- if (!normrel.startsWith(tempPath)) {
- return relative;
+ /*
+ * if the paths aren't equal, then we need to determine if this
+ * URI's path is a parent path (begins with) the relative URI's
+ * path
+ */
+ if (!thisPath.equals(relativePath)) {
+ // if this URI's path doesn't end in a '/', add one
+ if (!thisPath.endsWith("/")) {
+ thisPath = thisPath + '/';
+ }
+ /*
+ * if the relative URI's path doesn't start with this URI's path,
+ * then just return the relative URI; the URIs have nothing in
+ * common
+ */
+ if (!relativePath.startsWith(thisPath)) {
+ return relative;
+ }
}
URI result = new URI();
result.fragment = relative.fragment;
result.query = relative.query;
- result.path = normrel.substring(tempPath.length());
-
+ // the result URI is the remainder of the relative URI's path
+ result.path = relativePath.substring(thisPath.length());
return result;
}
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/net/URITest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/net/URITest.java?view=diff&rev=477007&r1=477006&r2=477007
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/net/URITest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/net/URITest.java Sun Nov 19 18:27:51 2006
@@ -22,7 +22,6 @@
import junit.framework.TestCase;
public class URITest extends TestCase {
-
/**
* @tests java.net.URI(java.lang.String)
*/
@@ -97,6 +96,22 @@
assertEquals("Assert 4: URI relativized incorrectly,",
new URI("file:///~/first"), b.relativize(a));
}
+
+ public void test_relativizeBasedOneEclipseCoreResources() throws URISyntaxException {
+ URI one = new URI("file:/C:/test/ws");
+ URI two = new URI("file:/C:/test/ws");
+
+ URI empty = new URI("");
+ assertEquals(empty, one.relativize(two));
+
+ one = new URI("file:/C:/test/ws");
+ two = new URI("file:/C:/test/ws/p1");
+ URI result = new URI("p1");
+ assertEquals(result, one.relativize(two));
+
+ one = new URI("file:/C:/test/ws/");
+ assertEquals(result, one.relativize(two));
+ }
/**
* @tests java.net.URI#compareTo(java.net.URI)