ICU-12386 Search for an actual resource, not a directory.
authorFredrik Roubert <roubert@google.com>
Wed, 9 Nov 2016 18:02:48 +0000 (18:02 +0000)
committerFredrik Roubert <roubert@google.com>
Wed, 9 Nov 2016 18:02:48 +0000 (18:02 +0000)
R=markus.icu@gmail.com, yoshito_umaoka@us.ibm.com
BUG=

Review URL: https://codereview.appspot.com/318730043 .

X-SVN-Rev: 39502

icu4j/main/tests/core/src/com/ibm/icu/dev/test/serializable/CompatibilityTest.java

index 9af35422729b4a2e311dbe1d3b9c25216cb381af..cf3297e4447c2bb1361e01f0914a1dce40ad28ee 100644 (file)
@@ -2,7 +2,7 @@
 // License & terms of use: http://www.unicode.org/copyright.html#License
 /*
  *******************************************************************************
- * Copyright (C) 1996-2015, International Business Machines Corporation and
+ * Copyright (C) 1996-2016, International Business Machines Corporation and
  * others. All Rights Reserved.
  *******************************************************************************
  *
@@ -15,6 +15,7 @@ import java.io.FileFilter;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.net.JarURLConnection;
+import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Enumeration;
@@ -95,9 +96,23 @@ public class CompatibilityTest extends TestFmwk
         }
     }
 
+    /**
+     * 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";
+
     @SuppressWarnings("unused")
     private List<FileHolder> generateClassList() throws IOException {
-        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")) {
@@ -152,6 +167,8 @@ public class CompatibilityTest extends TestFmwk
 
         JarFile jarFile = null;
         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));