summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/libcore/util/ZoneInfoDBTest.java
blob: a90bb8e2313f451123573bdca1f8fc9fd8e8b40c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package libcore.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.util.TimeZone;

public class ZoneInfoDBTest extends junit.framework.TestCase {

  // The base tzdata file, always present on a device.
  private static final String TZDATA_IN_ROOT =
      System.getenv("ANDROID_ROOT") + "/usr/share/zoneinfo/tzdata";

  // An empty override file should fall back to the default file.
  public void testEmptyOverrideFile() throws Exception {
    ZoneInfoDB.TzData data = new ZoneInfoDB.TzData(TZDATA_IN_ROOT);
    ZoneInfoDB.TzData dataWithEmptyOverride =
        new ZoneInfoDB.TzData(makeEmptyFile(), TZDATA_IN_ROOT);
    assertEquals(data.getVersion(), dataWithEmptyOverride.getVersion());
    assertEquals(data.getAvailableIDs().length, dataWithEmptyOverride.getAvailableIDs().length);
  }

  // A corrupt override file should fall back to the default file.
  public void testCorruptOverrideFile() throws Exception {
    ZoneInfoDB.TzData data = new ZoneInfoDB.TzData(TZDATA_IN_ROOT);
    ZoneInfoDB.TzData dataWithCorruptOverride =
        new ZoneInfoDB.TzData(makeCorruptFile(), TZDATA_IN_ROOT);
    assertEquals(data.getVersion(), dataWithCorruptOverride.getVersion());
    assertEquals(data.getAvailableIDs().length, dataWithCorruptOverride.getAvailableIDs().length);
  }

  // Given no tzdata files we can use, we should fall back to built-in "GMT".
  public void testNoGoodFile() throws Exception {
    ZoneInfoDB.TzData data = new ZoneInfoDB.TzData(makeEmptyFile());
    assertEquals("missing", data.getVersion());
    assertEquals(1, data.getAvailableIDs().length);
    assertEquals("GMT", data.getAvailableIDs()[0]);
  }

  // Given a valid override file, we should find ourselves using that.
  public void testGoodOverrideFile() throws Exception {
    RandomAccessFile in = new RandomAccessFile(TZDATA_IN_ROOT, "r");
    byte[] content = new byte[(int) in.length()];
    in.readFully(content);
    // Bump the version number to one long past where humans will be extinct.
    content[6] = '9';
    content[7] = '9';
    content[8] = '9';
    content[9] = '9';
    content[10] = 'z';
    in.close();

    ZoneInfoDB.TzData data = new ZoneInfoDB.TzData(TZDATA_IN_ROOT);
    String goodFile = makeTemporaryFile(content);
    try {
      ZoneInfoDB.TzData dataWithOverride = new ZoneInfoDB.TzData(goodFile, TZDATA_IN_ROOT);
      assertEquals("9999z", dataWithOverride.getVersion());
      assertEquals(data.getAvailableIDs().length, dataWithOverride.getAvailableIDs().length);
    } finally {
      new File(goodFile).delete();
    }
  }

  // Confirms any caching that exists correctly handles TimeZone mutability.
  public void testMakeTimeZone_timeZoneMutability() throws Exception {
    ZoneInfoDB.TzData data = new ZoneInfoDB.TzData(TZDATA_IN_ROOT);
    String tzId = "Europe/London";
    ZoneInfo first = data.makeTimeZone(tzId);
    ZoneInfo second = data.makeTimeZone(tzId);
    assertNotSame(first, second);

    assertTrue(first.hasSameRules(second));

    first.setID("Not Europe/London");

    assertFalse(first.getID().equals(second.getID()));

    first.setRawOffset(3600);
    assertFalse(first.getRawOffset() == second.getRawOffset());
  }

  public void testMakeTimeZone_notFound() throws Exception {
    ZoneInfoDB.TzData data = new ZoneInfoDB.TzData(TZDATA_IN_ROOT);
    assertNull(data.makeTimeZone("THIS_TZ_DOES_NOT_EXIST"));
    assertFalse(data.hasTimeZone("THIS_TZ_DOES_NOT_EXIST"));
  }

  public void testMakeTimeZone_found() throws Exception {
    ZoneInfoDB.TzData data = new ZoneInfoDB.TzData(TZDATA_IN_ROOT);
    assertNotNull(data.makeTimeZone("Europe/London"));
    assertTrue(data.hasTimeZone("Europe/London"));
  }

  private static String makeCorruptFile() throws Exception {
    return makeTemporaryFile("invalid content".getBytes());
  }

  private static String makeEmptyFile() throws Exception {
    return makeTemporaryFile(new byte[0]);
  }

  private static String makeTemporaryFile(byte[] content) throws Exception {
    File f = File.createTempFile("temp-", ".txt");
    FileOutputStream fos = new FileOutputStream(f);
    fos.write(content);
    fos.close();
    return f.getPath();
  }
}