Skip to content

Commit

Permalink
Add tests to defend against bad TZDB versions
Browse files Browse the repository at this point in the history
Ensure that Oslo, Stockholm and Amsterdam still resolve correctly
Any user picking up an invalid TZDB file should see a failure
  • Loading branch information
jodastephen committed Sep 23, 2021
1 parent 27edfff commit 5a3cb28
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/test/java/org/joda/time/TestDateTimeZone.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@
import java.util.Set;
import java.util.TimeZone;

import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.joda.time.tz.DefaultNameProvider;
import org.joda.time.tz.NameProvider;
import org.joda.time.tz.Provider;
import org.joda.time.tz.UTCProvider;
import org.joda.time.tz.ZoneInfoProvider;

import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
* This class is a JUnit test for DateTimeZone.
*
Expand Down Expand Up @@ -106,13 +106,16 @@ public class TestDateTimeZone extends TestCase {
static {
// don't call Policy.getPolicy()
RESTRICT = new Policy() {
@Override
public PermissionCollection getPermissions(CodeSource codesource) {
Permissions p = new Permissions();
p.add(new AllPermission()); // enable everything
return p;
}
@Override
public void refresh() {
}
@Override
public boolean implies(ProtectionDomain domain, Permission permission) {
if (permission instanceof JodaTimePermission) {
return false;
Expand All @@ -122,11 +125,13 @@ public boolean implies(ProtectionDomain domain, Permission permission) {
}
};
ALLOW = new Policy() {
@Override
public PermissionCollection getPermissions(CodeSource codesource) {
Permissions p = new Permissions();
p.add(new AllPermission()); // enable everything
return p;
}
@Override
public void refresh() {
}
};
Expand All @@ -147,12 +152,14 @@ public TestDateTimeZone(String name) {
super(name);
}

@Override
protected void setUp() throws Exception {
locale = Locale.getDefault();
zone = DateTimeZone.getDefault();
Locale.setDefault(Locale.UK);
}

@Override
protected void tearDown() throws Exception {
Locale.setDefault(locale);
DateTimeZone.setDefault(zone);
Expand Down Expand Up @@ -243,6 +250,12 @@ public void testForID_String() {
} catch (IllegalArgumentException ex) {}
}

public void testForID_ensureTzdb() {
assertEquals("Europe/Oslo", DateTimeZone.forID("Europe/Oslo").getID());
assertEquals("Europe/Stockholm", DateTimeZone.forID("Europe/Stockholm").getID());
assertEquals("Europe/Amsterdam", DateTimeZone.forID("Europe/Amsterdam").getID());
}

public void testForID_String_old() {
Map<String, String> map = new LinkedHashMap<String, String>();
map.put("GMT", "UTC");
Expand Down Expand Up @@ -706,24 +719,31 @@ public void testConstructor() {
assertTrue(Modifier.isProtected(DateTimeZone.class.getDeclaredConstructors()[0].getModifiers()));
try {
new DateTimeZone(null) {
@Override
public String getNameKey(long instant) {
return null;
}
@Override
public int getOffset(long instant) {
return 0;
}
@Override
public int getStandardOffset(long instant) {
return 0;
}
@Override
public boolean isFixed() {
return false;
}
@Override
public long nextTransition(long instant) {
return 0;
}
@Override
public long previousTransition(long instant) {
return 0;
}
@Override
public boolean equals(Object object) {
return false;
}
Expand Down Expand Up @@ -845,24 +865,31 @@ static class MockDateTimeZone extends DateTimeZone {
public MockDateTimeZone(String id) {
super(id);
}
@Override
public String getNameKey(long instant) {
return null; // null
}
@Override
public int getOffset(long instant) {
return 0;
}
@Override
public int getStandardOffset(long instant) {
return 0;
}
@Override
public boolean isFixed() {
return false;
}
@Override
public long nextTransition(long instant) {
return 0;
}
@Override
public long previousTransition(long instant) {
return 0;
}
@Override
public boolean equals(Object object) {
return false;
}
Expand Down

0 comments on commit 5a3cb28

Please sign in to comment.