--- /dev/null Thu Jul 7 15:45:54 2016 +++ new/test/sun/util/calendar/zi/TestCustomizedTZDBLocation.java Thu Jul 7 15:45:53 2016 @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code 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 General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8153044 + * @summary Allow a property to control location of tzdb.dat file + * @library /lib/testlibrary + * @build jdk.testlibrary.* + * @run main TestCustomizedTZDBLocation + */ + +import java.io.*; +import java.nio.file.*; +import jdk.testlibrary.*; + +import static java.nio.file.StandardCopyOption.*; + +public class TestCustomizedTZDBLocation { + + static final String TESTCLASSES = System.getProperty("test.classes", "."); + static final String TESTDIR = System.getProperty("test.dir", "."); + static final String JAVA_HOME = System.getProperty("java.home"); + + public static void main(String[] args) throws Exception { + if (System.getProperty("tzdbTest") != null) { + customLaunch(System.getProperty("jdk.time.tzdbfile")); + } else { + Path p = Paths.get(TESTDIR, "tzdb.dat"); + try { + Files.copy(Paths.get(JAVA_HOME, "lib", "tzdb.dat"), + p, REPLACE_EXISTING); + + System.out.println("Found version: " + + getTzIDFromTZDB(p.toString())); + + try (RandomAccessFile raf = new RandomAccessFile(p.toFile(), "rw")) { + raf.readByte(); // file format + raf.readUTF(); // group ID + raf.readShort(); // version count + raf.writeUTF("2010z"); // write a dummy test version + } + + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( + "-DtzdbTest", "-Djdk.time.tzdbfile=" + + p.toAbsolutePath(), + "-cp", Paths.get(TESTCLASSES).toAbsolutePath().toString(), + "TestCustomizedTZDBLocation" + ); + OutputAnalyzer output = ProcessTools.executeProcess(pb); + System.out.println(output.getOutput()); + output.shouldContain("2010z"); + output.shouldHaveExitValue(0); + + System.out.println("test a dummy file location.."); + pb = ProcessTools.createJavaProcessBuilder( + "-DtzdbTest", "-Djdk.time.tzdbfile=" + + p.toAbsolutePath() + "dummy", + "-cp", Paths.get(TESTCLASSES).toAbsolutePath().toString(), + "TestCustomizedTZDBLocation" + ); + output = ProcessTools.executeProcess(pb); + System.out.println(output.getOutput()); + output.shouldContain("Error reading tzdata version"); + output.shouldHaveExitValue(1); + } finally { + FileUtils.deleteFileIfExistsWithRetry(p); + } + } + + } + + static void customLaunch(String path) throws IOException { + System.out.println("version is: " + + getTzIDFromTZDB(path)); + } + + static String getTzIDFromTZDB(String tzdbFile) throws IOException { + DataInputStream dis = null; + try { + dis = new DataInputStream( + new BufferedInputStream(new FileInputStream(new File(tzdbFile)))); + return getTzID(dis); + } catch (Exception e) { + throw new RuntimeException("Error reading tzdata version from " + tzdbFile, e); + } finally { + if (dis != null) dis.close(); + } + } + + static String getTzID(DataInputStream dis) throws Exception { + String versionID = ""; + if (dis.readByte() != 1) { + throw new StreamCorruptedException("File format not recognised"); + } + // group + String groupId = dis.readUTF(); + if ("TZDB".equals(groupId) == false) { + throw new StreamCorruptedException("File format not recognised"); + } + // versions + int versionCount = dis.readShort(); + for (int i = 0; i < versionCount; i++) { + versionID = dis.readUTF(); + } + return "tzdata" + versionID; + } +} +