Merge "Tidy-up changes to time zone classes" am: 020085b55b
am: 82b123cba3

Change-Id: I3f791ad53fee36b7857d3143e27db3da22ed902a
diff --git a/luni/src/main/java/libcore/timezone/TimeZoneDataFiles.java b/luni/src/main/java/libcore/timezone/TimeZoneDataFiles.java
index 592fac1..73f5038 100644
--- a/luni/src/main/java/libcore/timezone/TimeZoneDataFiles.java
+++ b/luni/src/main/java/libcore/timezone/TimeZoneDataFiles.java
@@ -41,16 +41,16 @@
      * <li>[1] - the location of the file from the time zone module under /apex (may not exist).
      * </li>
      * <li>[2] - the location of the file from the runtime module under /apex (should exist).</li>
-     * </ul>
      * <li>[3] - the location of the file in the /system partition (should exist).</li>
+     * </ul>
      */
     // VisibleForTesting
     public static String[] getTimeZoneFilePaths(String fileName) {
         return new String[] {
                 getDataTimeZoneFile(fileName),
-                getTimeZoneModuleFile("tz/" + fileName),
-                getRuntimeModuleFile("tz/" + fileName),
-                getSystemTimeZoneFile(fileName)
+                getTimeZoneModuleTzFile(fileName),
+                getRuntimeModuleTzFile(fileName),
+                getSystemTzFile(fileName)
         };
     }
 
@@ -65,6 +65,15 @@
         return getDataTimeZoneRootDir() + "current/" + fileName;
     }
 
+    public static String getTimeZoneModuleTzFile(String fileName) {
+        return getTimeZoneModuleFile("tz/" + fileName);
+    }
+
+    public static String getTimeZoneModuleIcuFile(String fileName) {
+        return getTimeZoneModuleFile("icu/" + fileName);
+    }
+
+    // VisibleForTesting
     public static String getTimeZoneModuleFile(String fileName) {
         return System.getenv(ANDROID_TZDATA_ROOT_ENV) + "/etc/" + fileName;
     }
@@ -72,14 +81,22 @@
     // Remove from CorePlatformApi when all users in platform code are removed. http://b/123398797
     @libcore.api.CorePlatformApi
     public static String getRuntimeModuleTzVersionFile() {
-        return getRuntimeModuleFile("tz/" + TzDataSetVersion.DEFAULT_FILE_NAME);
+        return getRuntimeModuleTzFile(TzDataSetVersion.DEFAULT_FILE_NAME);
     }
 
-    public static String getRuntimeModuleFile(String fileName) {
+    public static String getRuntimeModuleTzFile(String fileName) {
+        return getRuntimeModuleFile("tz/" + fileName);
+    }
+
+    public static String getRuntimeModuleIcuFile(String fileName) {
+        return getRuntimeModuleFile("icu/" + fileName);
+    }
+
+    private static String getRuntimeModuleFile(String fileName) {
         return System.getenv(ANDROID_RUNTIME_ROOT_ENV) + "/etc/" + fileName;
     }
 
-    public static String getSystemTimeZoneFile(String fileName) {
+    public static String getSystemTzFile(String fileName) {
         return getEnvironmentPath(ANDROID_ROOT_ENV, "/usr/share/zoneinfo/" + fileName);
     }
 
@@ -100,17 +117,14 @@
 
         // ICU should then look for a mounted time zone module file in /apex. This is used for
         // (optional) time zone data that can be updated with an APEX file.
-        String timeZoneModuleIcuDataPath = getTimeZoneModuleFile("icu/");
-        if (timeZoneModuleIcuDataPath != null) {
-            paths.add(timeZoneModuleIcuDataPath);
-        }
+        String timeZoneModuleIcuDataPath = getTimeZoneModuleIcuFile("");
+        paths.add(timeZoneModuleIcuDataPath);
 
         // ICU should always look in the runtime module path as this is where most of the data
         // can be found.
-        String runtimeModuleIcuDataPath = getRuntimeModuleFile("icu/");
-        if (runtimeModuleIcuDataPath != null) {
-            paths.add(runtimeModuleIcuDataPath);
-        }
+        String runtimeModuleIcuDataPath = getRuntimeModuleIcuFile("");
+        paths.add(runtimeModuleIcuDataPath);
+
         return String.join(":", paths);
     }
 
diff --git a/luni/src/main/java/libcore/timezone/TimeZoneFinder.java b/luni/src/main/java/libcore/timezone/TimeZoneFinder.java
index eebe8cc..f243a4d 100644
--- a/luni/src/main/java/libcore/timezone/TimeZoneFinder.java
+++ b/luni/src/main/java/libcore/timezone/TimeZoneFinder.java
@@ -48,7 +48,8 @@
 @libcore.api.CorePlatformApi
 public final class TimeZoneFinder {
 
-    private static final String TZLOOKUP_FILE_NAME = "tzlookup.xml";
+    // VisibleForTesting
+    public static final String TZLOOKUP_FILE_NAME = "tzlookup.xml";
 
     // Root element. e.g. <timezones ianaversion="2017b">
     private static final String TIMEZONES_ELEMENT = "timezones";
diff --git a/luni/src/main/java/libcore/timezone/ZoneInfoDB.java b/luni/src/main/java/libcore/timezone/ZoneInfoDB.java
index ca29319..3e2e60e 100644
--- a/luni/src/main/java/libcore/timezone/ZoneInfoDB.java
+++ b/luni/src/main/java/libcore/timezone/ZoneInfoDB.java
@@ -19,8 +19,6 @@
 import android.system.ErrnoException;
 import dalvik.annotation.optimization.ReachabilitySensitive;
 
-import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
@@ -43,10 +41,10 @@
 public final class ZoneInfoDB {
 
   // VisibleForTesting
-  public static final String TZDATA_FILE = "tzdata";
+  public static final String TZDATA_FILE_NAME = "tzdata";
 
   private static final TzData DATA =
-          TzData.loadTzDataWithFallback(TimeZoneDataFiles.getTimeZoneFilePaths(TZDATA_FILE));
+          TzData.loadTzDataWithFallback(TimeZoneDataFiles.getTimeZoneFilePaths(TZDATA_FILE_NAME));
 
   /** @hide */
   @libcore.api.CorePlatformApi
@@ -125,7 +123,7 @@
       // We didn't find any usable tzdata on disk, so let's just hard-code knowledge of "GMT".
       // This is actually implemented in TimeZone itself, so if this is the only time zone
       // we report, we won't be asked any more questions.
-      System.logE("Couldn't find any " + TZDATA_FILE + " file!");
+      System.logE("Couldn't find any " + TZDATA_FILE_NAME + " file!");
       return TzData.createFallback();
     }
 
@@ -195,7 +193,7 @@
 
         // Something's wrong with the file.
         // Log the problem and return false so we try the next choice.
-        System.logE(TZDATA_FILE + " file \"" + path + "\" was present but invalid!", ex);
+        System.logE(TZDATA_FILE_NAME + " file \"" + path + "\" was present but invalid!", ex);
         return false;
       }
     }
diff --git a/luni/src/main/java/libcore/util/CoreLibraryDebug.java b/luni/src/main/java/libcore/util/CoreLibraryDebug.java
index c39017f..809ad30 100644
--- a/luni/src/main/java/libcore/util/CoreLibraryDebug.java
+++ b/luni/src/main/java/libcore/util/CoreLibraryDebug.java
@@ -59,31 +59,31 @@
         // Time zone module tz data set.
         {
             String tzDataModulePrefix = debugKeyPrefix + "tzdata_module_";
-            String versionFileName = TimeZoneDataFiles.getTimeZoneModuleFile(
-                    "tz/" + TzDataSetVersion.DEFAULT_FILE_NAME);
-            addTzDataSetVersionDebugInfo(versionFileName, tzDataModulePrefix, debugInfo);
+            String versionFile =
+                    TimeZoneDataFiles.getTimeZoneModuleTzFile(TzDataSetVersion.DEFAULT_FILE_NAME);
+            addTzDataSetVersionDebugInfo(versionFile, tzDataModulePrefix, debugInfo);
         }
 
         // Runtime module tz data set.
         {
             String runtimeModulePrefix = debugKeyPrefix + "runtime_module_";
-            String versionFileName = TimeZoneDataFiles.getRuntimeModuleFile(
-                    "tz/" + TzDataSetVersion.DEFAULT_FILE_NAME);
-            addTzDataSetVersionDebugInfo(versionFileName, runtimeModulePrefix, debugInfo);
+            String versionFile =
+                    TimeZoneDataFiles.getRuntimeModuleTzFile(TzDataSetVersion.DEFAULT_FILE_NAME);
+            addTzDataSetVersionDebugInfo(versionFile, runtimeModulePrefix, debugInfo);
         }
 
         // /system tz data set.
         {
             String systemDirPrefix = debugKeyPrefix + "system_";
-            String versionFileName =
-                    TimeZoneDataFiles.getSystemTimeZoneFile(TzDataSetVersion.DEFAULT_FILE_NAME);
-            addTzDataSetVersionDebugInfo(versionFileName, systemDirPrefix, debugInfo);
+            String versionFile =
+                    TimeZoneDataFiles.getSystemTzFile(TzDataSetVersion.DEFAULT_FILE_NAME);
+            addTzDataSetVersionDebugInfo(versionFile, systemDirPrefix, debugInfo);
         }
     }
 
-    private static void addTzDataSetVersionDebugInfo(String tzDataSetVersionFileName,
+    private static void addTzDataSetVersionDebugInfo(String tzDataSetVersionFile,
             String debugKeyPrefix, DebugInfo debugInfo) {
-        File file = new File(tzDataSetVersionFileName);
+        File file = new File(tzDataSetVersionFile);
         String statusKey = debugKeyPrefix + "status";
         if (file.exists()) {
             try {
diff --git a/luni/src/test/java/libcore/libcore/icu/TimeZoneIntegrationTest.java b/luni/src/test/java/libcore/libcore/icu/TimeZoneIntegrationTest.java
index 32dbb1c..ccc2119 100644
--- a/luni/src/test/java/libcore/libcore/icu/TimeZoneIntegrationTest.java
+++ b/luni/src/test/java/libcore/libcore/icu/TimeZoneIntegrationTest.java
@@ -226,10 +226,8 @@
      */
     @Test
     public void testTzDataSetVersions() throws Exception {
-        String moduleTzVersionFile = "tz/" + TzDataSetVersion.DEFAULT_FILE_NAME;
-
         String timeZoneModuleVersionFile =
-                TimeZoneDataFiles.getTimeZoneModuleFile(moduleTzVersionFile);
+                TimeZoneDataFiles.getTimeZoneModuleTzFile(TzDataSetVersion.DEFAULT_FILE_NAME);
         // We currently treat the time zone APEX as optional in code. Its is also not present on ART
         // host environments.
         if (fileExists(timeZoneModuleVersionFile)) {
@@ -237,7 +235,7 @@
         }
 
         String runtimeModuleVersionFile =
-                TimeZoneDataFiles.getRuntimeModuleFile(moduleTzVersionFile);
+                TimeZoneDataFiles.getRuntimeModuleTzFile(TzDataSetVersion.DEFAULT_FILE_NAME);
         assertTzDataSetVersionIsCompatible(runtimeModuleVersionFile);
 
         // Check getRuntimeModuleTzVersionFile() is doing the right thing.
@@ -248,12 +246,11 @@
         // TODO: Remove this once the /system copy of time zone files have gone away. See also
         // testTimeZoneDebugInfo().
         assertTzDataSetVersionIsCompatible(
-                TimeZoneDataFiles.getSystemTimeZoneFile(TzDataSetVersion.DEFAULT_FILE_NAME));
+                TimeZoneDataFiles.getSystemTzFile(TzDataSetVersion.DEFAULT_FILE_NAME));
     }
 
     private static void assertTzDataSetVersionIsCompatible(String versionFile) throws Exception {
-        TzDataSetVersion actualVersion =
-                TzDataSetVersion.readFromFile(new File(versionFile));
+        TzDataSetVersion actualVersion = TzDataSetVersion.readFromFile(new File(versionFile));
         assertEquals(
                 TzDataSetVersion.currentFormatMajorVersion(),
                 actualVersion.formatMajorVersion);
@@ -278,8 +275,8 @@
                 "core_library.timezone.source.tzdata_module_status");
         String apexRootDir = TimeZoneDataFiles.getTimeZoneModuleFile("");
         List<String> dataModuleFiles =
-                createModuleTzFileNames(TimeZoneDataFiles::getTimeZoneModuleFile);
-        String icuOverlayFile = TimeZoneDataFiles.getTimeZoneModuleFile("icu/icu_tzdata.dat");
+                createModuleTzFiles(TimeZoneDataFiles::getTimeZoneModuleTzFile);
+        String icuOverlayFile = TimeZoneDataFiles.getTimeZoneModuleIcuFile("icu_tzdata.dat");
         if (fileExists(apexRootDir)) {
             assertEquals("OK", tzModuleStatus);
             dataModuleFiles.forEach(TimeZoneIntegrationTest::assertFileExists);
@@ -295,14 +292,14 @@
         // updated when we update the runtime module. Host ART should match device.
         assertEquals("OK", getDebugStringValue(debugInfo,
                 "core_library.timezone.source.runtime_module_status"));
-        assertFileExists(TimeZoneDataFiles.getRuntimeModuleFile(""));
-        List<String> runtimeModuleFiles =
-                createModuleTzFileNames(TimeZoneDataFiles::getRuntimeModuleFile);
-        runtimeModuleFiles.forEach(TimeZoneIntegrationTest::assertFileExists);
+        assertFileExists(
+                TimeZoneDataFiles.getRuntimeModuleTzFile(TzDataSetVersion.DEFAULT_FILE_NAME));
+        List<String> runtimeModuleTzFiles =
+                createModuleTzFiles(TimeZoneDataFiles::getRuntimeModuleTzFile);
+        runtimeModuleTzFiles.forEach(TimeZoneIntegrationTest::assertFileExists);
 
         String icuDatFileName = "icudt" + VersionInfo.ICU_VERSION.getMajor() + "l.dat";
-        String runtimeModuleIcuData =
-                TimeZoneDataFiles.getRuntimeModuleFile("icu/" + icuDatFileName);
+        String runtimeModuleIcuData = TimeZoneDataFiles.getRuntimeModuleIcuFile(icuDatFileName);
         assertFileExists(runtimeModuleIcuData);
 
         // Devices currently have a subset of the time zone files in /system. These are going away
@@ -310,10 +307,11 @@
         assertEquals("OK", getDebugStringValue(debugInfo,
                 "core_library.timezone.source.system_status"));
         assertFileExists(
-                TimeZoneDataFiles.getSystemTimeZoneFile(TzDataSetVersion.DEFAULT_FILE_NAME));
-        assertFileExists(TimeZoneDataFiles.getSystemTimeZoneFile("tzdata"));
+                TimeZoneDataFiles.getSystemTzFile(TzDataSetVersion.DEFAULT_FILE_NAME));
+        assertFileExists(TimeZoneDataFiles.getSystemTzFile(ZoneInfoDB.TZDATA_FILE_NAME));
         // The following files once existed in /system but have been removed as part of APEX work.
-        assertFileDoesNotExist(TimeZoneDataFiles.getSystemTimeZoneFile("tzlookup.xml"));
+        assertFileDoesNotExist(
+                TimeZoneDataFiles.getSystemTzFile(TimeZoneFinder.TZLOOKUP_FILE_NAME));
 
         // It's hard to assert much about this file as there is a symlink in /system on device for
         // app compatibility (b/122985829) but it doesn't exist in host environments. If the file
@@ -324,12 +322,12 @@
         }
     }
 
-    private static List<String> createModuleTzFileNames(
+    private static List<String> createModuleTzFiles(
             Function<String, String> pathCreationFunction) {
         List<String> relativePaths = Arrays.asList(
-                "tz/" + TzDataSetVersion.DEFAULT_FILE_NAME,
-                "tz/tzdata",
-                "tz/tzlookup.xml");
+                TzDataSetVersion.DEFAULT_FILE_NAME,
+                ZoneInfoDB.TZDATA_FILE_NAME,
+                TimeZoneFinder.TZLOOKUP_FILE_NAME);
         return relativePaths.stream().map(pathCreationFunction).collect(Collectors.toList());
     }
 
diff --git a/luni/src/test/java/libcore/libcore/timezone/ZoneInfoDBTest.java b/luni/src/test/java/libcore/libcore/timezone/ZoneInfoDBTest.java
index ef70236..ffa62f6 100644
--- a/luni/src/test/java/libcore/libcore/timezone/ZoneInfoDBTest.java
+++ b/luni/src/test/java/libcore/libcore/timezone/ZoneInfoDBTest.java
@@ -30,15 +30,15 @@
 public class ZoneInfoDBTest extends junit.framework.TestCase {
 
   // The base tzdata file, always present on a device.
-  private static final String SYSTEM_TZDATA_FILE =
-      TimeZoneDataFiles.getSystemTimeZoneFile(ZoneInfoDB.TZDATA_FILE);
+  private static final String TZDATA_FILE =
+          TimeZoneDataFiles.getRuntimeModuleTzFile(ZoneInfoDB.TZDATA_FILE_NAME);
 
   // An empty override file should fall back to the default file.
   public void testLoadTzDataWithFallback_emptyOverrideFile() throws Exception {
     String emptyFilePath = makeEmptyFile().getPath();
-    try (ZoneInfoDB.TzData data = ZoneInfoDB.TzData.loadTzData(SYSTEM_TZDATA_FILE);
+    try (ZoneInfoDB.TzData data = ZoneInfoDB.TzData.loadTzData(TZDATA_FILE);
          ZoneInfoDB.TzData dataWithEmptyOverride =
-                 ZoneInfoDB.TzData.loadTzDataWithFallback(emptyFilePath, SYSTEM_TZDATA_FILE)) {
+                 ZoneInfoDB.TzData.loadTzDataWithFallback(emptyFilePath, TZDATA_FILE)) {
       assertEquals(data.getVersion(), dataWithEmptyOverride.getVersion());
       assertEquals(data.getAvailableIDs().length, dataWithEmptyOverride.getAvailableIDs().length);
     }
@@ -47,9 +47,9 @@
   // A corrupt override file should fall back to the default file.
   public void testLoadTzDataWithFallback_corruptOverrideFile() throws Exception {
     String corruptFilePath = makeCorruptFile().getPath();
-    try (ZoneInfoDB.TzData data = ZoneInfoDB.TzData.loadTzData(SYSTEM_TZDATA_FILE);
+    try (ZoneInfoDB.TzData data = ZoneInfoDB.TzData.loadTzData(TZDATA_FILE);
          ZoneInfoDB.TzData dataWithCorruptOverride =
-                 ZoneInfoDB.TzData.loadTzDataWithFallback(corruptFilePath, SYSTEM_TZDATA_FILE)) {
+                 ZoneInfoDB.TzData.loadTzDataWithFallback(corruptFilePath, TZDATA_FILE)) {
       assertEquals(data.getVersion(), dataWithCorruptOverride.getVersion());
       assertEquals(data.getAvailableIDs().length, dataWithCorruptOverride.getAvailableIDs().length);
     }
@@ -67,7 +67,7 @@
 
   // Given a valid override file, we should find ourselves using that.
   public void testLoadTzDataWithFallback_goodOverrideFile() throws Exception {
-    RandomAccessFile in = new RandomAccessFile(SYSTEM_TZDATA_FILE, "r");
+    RandomAccessFile in = new RandomAccessFile(TZDATA_FILE, "r");
     byte[] content = new byte[(int) in.length()];
     in.readFully(content);
     in.close();
@@ -81,8 +81,8 @@
 
     File goodFile = makeTemporaryFile(content);
     try (ZoneInfoDB.TzData dataWithOverride =
-              ZoneInfoDB.TzData.loadTzDataWithFallback(goodFile.getPath(), SYSTEM_TZDATA_FILE);
-         ZoneInfoDB.TzData data = ZoneInfoDB.TzData.loadTzData(SYSTEM_TZDATA_FILE)) {
+              ZoneInfoDB.TzData.loadTzDataWithFallback(goodFile.getPath(), TZDATA_FILE);
+         ZoneInfoDB.TzData data = ZoneInfoDB.TzData.loadTzData(TZDATA_FILE)) {
 
       assertEquals("9999z", dataWithOverride.getVersion());
       assertEquals(data.getAvailableIDs().length, dataWithOverride.getAvailableIDs().length);
@@ -92,7 +92,7 @@
   }
 
   public void testLoadTzData_badHeader() throws Exception {
-    RandomAccessFile in = new RandomAccessFile(SYSTEM_TZDATA_FILE, "r");
+    RandomAccessFile in = new RandomAccessFile(TZDATA_FILE, "r");
     byte[] content = new byte[(int) in.length()];
     in.readFully(content);
     in.close();
@@ -215,7 +215,7 @@
 
   // Confirms any caching that exists correctly handles TimeZone mutability.
   public void testMakeTimeZone_timeZoneMutability() throws Exception {
-    try (ZoneInfoDB.TzData data = ZoneInfoDB.TzData.loadTzData(SYSTEM_TZDATA_FILE)) {
+    try (ZoneInfoDB.TzData data = ZoneInfoDB.TzData.loadTzData(TZDATA_FILE)) {
       String tzId = "Europe/London";
       ZoneInfo first = data.makeTimeZone(tzId);
       ZoneInfo second = data.makeTimeZone(tzId);
@@ -233,14 +233,14 @@
   }
 
   public void testMakeTimeZone_notFound() throws Exception {
-    try (ZoneInfoDB.TzData data = ZoneInfoDB.TzData.loadTzData(SYSTEM_TZDATA_FILE)) {
+    try (ZoneInfoDB.TzData data = ZoneInfoDB.TzData.loadTzData(TZDATA_FILE)) {
       assertNull(data.makeTimeZone("THIS_TZ_DOES_NOT_EXIST"));
       assertFalse(data.hasTimeZone("THIS_TZ_DOES_NOT_EXIST"));
     }
   }
 
   public void testMakeTimeZone_found() throws Exception {
-    try (ZoneInfoDB.TzData data = ZoneInfoDB.TzData.loadTzData(SYSTEM_TZDATA_FILE)) {
+    try (ZoneInfoDB.TzData data = ZoneInfoDB.TzData.loadTzData(TZDATA_FILE)) {
       assertNotNull(data.makeTimeZone("Europe/London"));
       assertTrue(data.hasTimeZone("Europe/London"));
     }