From bf1ec3b2cd0ffedbc0e63a3e606e094ad75395bc Mon Sep 17 00:00:00 2001 From: Fredrik Roubert Date: Wed, 23 Mar 2016 20:21:59 +0000 Subject: [PATCH] ICU-12386 Search for an actual resource, not a directory. X-SVN-Rev: 38563 --- .../test/serializable/CompatibilityTest.java | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/serializable/CompatibilityTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/serializable/CompatibilityTest.java index caf2e8d7ccb..106f30b37b6 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/serializable/CompatibilityTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/serializable/CompatibilityTest.java @@ -1,6 +1,6 @@ /* ******************************************************************************* - * Copyright (C) 1996-2015, International Business Machines Corporation and + * Copyright (C) 1996-2016, International Business Machines Corporation and * others. All Rights Reserved. ******************************************************************************* * @@ -17,6 +17,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.net.JarURLConnection; +import java.net.MalformedURLException; import java.net.URL; import java.util.Enumeration; import java.util.MissingResourceException; @@ -231,6 +232,12 @@ public class CompatibilityTest extends TestFmwk } try { + // Need to trim the directory off the JAR entry before opening the connection otherwise + // it could fail as it will try and find the entry within the JAR which may not exist. + String urlAsString = jarURL.toExternalForm(); + ix = urlAsString.indexOf("!/"); + jarURL = new URL(urlAsString.substring(0, ix + 2)); + JarURLConnection conn = (JarURLConnection) jarURL.openConnection(); JarFile jarFile = conn.getJarFile(); try { @@ -287,10 +294,24 @@ element_loop: return target; } - + + /** + * The path to an actual data resource file in the JAR. This is needed because when the + * code is packaged for Android the resulting archive does not have entries for directories + * and so only actual resources can be found. + */ + private static final String ACTUAL_RESOURCE = "/ICU_3.6/com.ibm.icu.impl.OlsonTimeZone.dat"; + protected Target getTargets(String targetName) { - URL dataURL = getClass().getResource("data"); + // Get the URL to an actual resource and then compute the URL to the directory just in + // case the resources are in a JAR file that doesn't have entries for directories. + URL dataURL = getClass().getResource("data" + ACTUAL_RESOURCE); + try { + dataURL = new URL(dataURL.toExternalForm().replace(ACTUAL_RESOURCE, "")); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } String protocol = dataURL.getProtocol(); if (protocol.equals("jar")) {