From 76fa43737656d859f0943f5326cf2b8565c4b032 Mon Sep 17 00:00:00 2001 From: emopers Date: Sat, 21 Nov 2015 03:47:03 -0600 Subject: [PATCH] Fixed bug in ZoneInfoCompiler.java and added a test --- .../org/joda/time/tz/ZoneInfoCompiler.java | 3 +++ .../java/org/joda/time/tz/TestCompiler.java | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/main/java/org/joda/time/tz/ZoneInfoCompiler.java b/src/main/java/org/joda/time/tz/ZoneInfoCompiler.java index 72ffcb866..e3c509e55 100644 --- a/src/main/java/org/joda/time/tz/ZoneInfoCompiler.java +++ b/src/main/java/org/joda/time/tz/ZoneInfoCompiler.java @@ -528,6 +528,9 @@ public void parseDataFile(BufferedReader in, boolean backward) throws IOExceptio rs.addRule(r); } } else if (token.equalsIgnoreCase("Zone")) { + if (st.countTokens() < 4) { + throw new IllegalArgumentException("Attempting to create a Zone from an incomplete tokenizer"); + } zone = new Zone(st); } else if (token.equalsIgnoreCase("Link")) { String real = st.nextToken(); diff --git a/src/test/java/org/joda/time/tz/TestCompiler.java b/src/test/java/org/joda/time/tz/TestCompiler.java index 8eec2bc49..ac7936383 100644 --- a/src/test/java/org/joda/time/tz/TestCompiler.java +++ b/src/test/java/org/joda/time/tz/TestCompiler.java @@ -76,6 +76,14 @@ public static TestSuite suite() { "Rule US 1918 1919 - Mar lastSun 2:00 1:00 D\n" + "Rule \n" ; // this line is intentionally incomplete + static final String BROKEN_TIMEZONE_FILE_2 = + "# Incomplete Zone for building America/Los_Angeles time zone.\n" + + "\n" + + "Rule CA 1948 only - Mar 14 2:00 1:00 D\n" + + "Rule CA 1949 only - Jan 1 2:00 0 S\n" + + "\n" + + "Zone "; // this line is intentionally left incomplete + private DateTimeZone originalDateTimeZone = null; public TestCompiler(String name) { @@ -136,6 +144,17 @@ public void testCompileOnBrokenTimeZoneFile() throws Exception { assertEquals("Attempting to create a Rule from an incomplete tokenizer", iae.getMessage()); } } + public void testCompileOnBrokenTimeZoneFile_2() throws Exception { + try { + Provider provider = compileAndLoad(BROKEN_TIMEZONE_FILE_2); + fail(); + } catch(NoSuchElementException nsee) { + // This thrown from the Zone constructor + fail("NoSuchElementException was thrown; broken timezone file?"); + } catch(IllegalArgumentException iae) { + assertEquals("Attempting to create a Zone from an incomplete tokenizer", iae.getMessage()); + } + } private Provider compileAndLoad(String data) throws Exception { File tempDir = createDataFile(data);