Browse Source

Bug 42275: ant doesnt run from a network share, because of an error when trying to work out where we loaded from.

1. this exception is still raised, but caught and causes ant to skip setting ant.lib.

2. we now have a test class for Locator

3. I've split out the java1.3 support and made it public, so we can test it separately. 

One of the tests is failing, showing the problem is still there. We need to decide what to do about it (ignore, switch to java1.3 code...)

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@533082 13f79535-47bb-0310-9956-ffa450edef68
master
Steve Loughran 18 years ago
parent
commit
bcf84f5f14
2 changed files with 133 additions and 15 deletions
  1. +33
    -15
      src/main/org/apache/tools/ant/launch/Locator.java
  2. +100
    -0
      src/tests/junit/org/apache/tools/ant/launch/LocatorTest.java

+ 33
- 15
src/main/org/apache/tools/ant/launch/Locator.java View File

@@ -49,6 +49,8 @@ public final class Locator {
private static char[] gAfterEscaping2 = new char[128];
private static char[] gHexChs = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
public static final String ERROR_NOT_FILE_URI = "Can only handle valid file: URIs, not ";

// initialize the above 3 arrays
static {
for (int i = 0; i <= 0x1f; i++) {
@@ -113,14 +115,19 @@ public final class Locator {
}
if (url != null) {
String u = url.toString();
if (u.startsWith("jar:file:")) {
int pling = u.indexOf("!");
String jarName = u.substring(4, pling);
return new File(fromURI(jarName));
} else if (u.startsWith("file:")) {
int tail = u.indexOf(resource);
String dirName = u.substring(0, tail);
return new File(fromURI(dirName));
try {
if (u.startsWith("jar:file:")) {
int pling = u.indexOf("!");
String jarName = u.substring(4, pling);
return new File(fromURI(jarName));
} else if (u.startsWith("file:")) {
int tail = u.indexOf(resource);
String dirName = u.substring(0, tail);
return new File(fromURI(dirName));
}
} catch (IllegalArgumentException e) {
//unable to determine the URI for reasons unknown.
return null;
}
}
return null;
@@ -169,7 +176,7 @@ public final class Locator {
Throwable e2 = e.getTargetException();
if (e2 instanceof IllegalArgumentException) {
// Bad URI, pass this on.
throw (IllegalArgumentException) e2;
throw new IllegalArgumentException("Bad URI "+uri+ ":"+e2.getMessage(),e2);
} else {
// Unexpected target exception? Should not happen.
e2.printStackTrace();
@@ -179,7 +186,15 @@ public final class Locator {
e.printStackTrace();
}
}
return fromURIJava13(uri);
}

/**
* This is only public for testing purposes, so its use is strongly discouraged.
* @param uri uri to expand
* @return the decoded URI
*/
public static String fromURIJava13(String uri) {
// Fallback method for Java 1.3 or earlier.

URL url = null;
@@ -189,7 +204,7 @@ public final class Locator {
// Ignore malformed exception
}
if (url == null || !("file".equals(url.getProtocol()))) {
throw new IllegalArgumentException("Can only handle valid file: URIs");
throw new IllegalArgumentException(ERROR_NOT_FILE_URI +uri);
}
StringBuffer buf = new StringBuffer(url.getHost());
if (buf.length() > 0) {
@@ -216,7 +231,7 @@ public final class Locator {
} catch (UnsupportedEncodingException exc) {
// not sure whether this is clean, but this method is
// declared not to throw exceptions.
throw new IllegalStateException("Could not convert URI to path: "
throw new IllegalStateException("Could not convert URI "+uri+" to path: "
+ exc.getMessage());
}
return path;
@@ -369,15 +384,16 @@ public final class Locator {
}
// couldn't find compiler - try to find tools.jar
// based on java.home setting
String libToolsJar= File.separator + "lib" + File.separator + "tools.jar";
String javaHome = System.getProperty("java.home");
File toolsJar = new File(javaHome + "/lib/tools.jar");
File toolsJar = new File(javaHome + libToolsJar);
if (toolsJar.exists()) {
// Found in java.home as given
return toolsJar;
}
if (javaHome.toLowerCase(Locale.US).endsWith(File.separator + "jre")) {
javaHome = javaHome.substring(0, javaHome.length() - 4);
toolsJar = new File(javaHome + "/lib/tools.jar");
toolsJar = new File(javaHome + libToolsJar );
}
if (!toolsJar.exists()) {
System.out.println("Unable to locate tools.jar. "
@@ -430,8 +446,9 @@ public final class Locator {
if (!location.isDirectory()) {
urls = new URL[1];
String path = location.getPath();
String littlePath = path.toLowerCase(Locale.US);
for (int i = 0; i < extensions.length; ++i) {
if (path.toLowerCase().endsWith(extensions[i])) {
if (littlePath.endsWith(extensions[i])) {
urls[0] = fileToURL(location);
break;
}
@@ -441,8 +458,9 @@ public final class Locator {
File[] matches = location.listFiles(
new FilenameFilter() {
public boolean accept(File dir, String name) {
String littleName = name.toLowerCase(Locale.US);
for (int i = 0; i < extensions.length; ++i) {
if (name.toLowerCase().endsWith(extensions[i])) {
if (littleName.endsWith(extensions[i])) {
return true;
}
}


+ 100
- 0
src/tests/junit/org/apache/tools/ant/launch/LocatorTest.java View File

@@ -0,0 +1,100 @@
/** (C) Copyright 2007 Hewlett-Packard Development Company, LP

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

For more information: www.smartfrog.org

*/
package org.apache.tools.ant.launch;

import junit.framework.TestCase;

/** created 27-Apr-2007 12:26:47 */

public class LocatorTest extends TestCase {


/**
* No-arg constructor to enable serialization. This method is not intended to be used by mere mortals without calling
* setName().
*/
public LocatorTest() {
}

/** Constructs a test case with the given name. */
public LocatorTest(String name) {
super(name);
}

private String resolve(String uri) {
String j14= Locator.fromURI(uri);
String j13 = Locator.fromURIJava13(uri);
assertEquals(uri,j14,j13);
return j14;
}

private void resolveTo(String uri,String expectedResult) {
String result = resolve(uri);
assertEquals(uri,expectedResult,result);
}

private void resolveTo13(String uri, String expectedResult) {
String result = Locator.fromURIJava13(uri);
assertEquals(uri, expectedResult, result);
}
/**
* this isnt really a valid URI, except maybe in IE
* @throws Exception
*/
public void testNetworkURI() throws Exception {
resolveTo("file:\\\\PC03\\jclasses\\lib\\ant-1.7.0.jar","\\\\PC03\\jclasses\\lib\\ant-1.7.0.jar");
}

public void testTripleForwardSlashNetworkURI_BugID_42275() throws Exception {
resolveTo("file:///PC03/jclasses/lib/ant-1.7.0.jar", "///PC03/jclasses/lib/ant-1.7.0.jar");
}

public void testUnixNetworkPath() throws Exception {
resolveTo("file://cluster/home/ant/lib", "//cluster/home/ant/lib");
}

public void testUnixNetworkPath13() throws Exception {
resolveTo13("file://cluster/home/ant/lib", "//cluster/home/ant/lib");
}

public void testUnixPath() throws Exception {
resolveTo("file:/home/ant/lib", "/home/ant/lib");
}

public void testSpacedURI() throws Exception {
resolveTo("file:C:\\Program Files\\Ant\\lib","C:\\Program Files\\Ant\\lib");
}

public void testHttpURI() throws Exception {
String url = "http://ant.apache.org";
try {
Locator.fromURI(url);
} catch (IllegalArgumentException e) {
String message = e.getMessage();
assertTrue(message,message.indexOf(Locator.ERROR_NOT_FILE_URI)>=0);
assertTrue(message, message.indexOf(url) >= 0);
}
}





}

Loading…
Cancel
Save