diff --git a/.travis.yml b/.travis.yml index 73cc301..b410cb5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,4 +23,4 @@ install: - cp -v .travis.ant.local ant.local script: -- ant compile test jdoc +- ant travis diff --git a/build.xml b/build.xml index 8adaa32..d2249b4 100644 --- a/build.xml +++ b/build.xml @@ -22,4 +22,6 @@ support a course on "Distributed Systems" + + diff --git a/src/main/java/pityoulish/i18n/CatalogHelper.java b/src/main/java/pityoulish/i18n/CatalogHelper.java index b8a5010..a030866 100644 --- a/src/main/java/pityoulish/i18n/CatalogHelper.java +++ b/src/main/java/pityoulish/i18n/CatalogHelper.java @@ -58,22 +58,24 @@ public static final String format(TextRef tref, Object... params) ResourceBundle bundle = getBundle(tref); String pattern = bundle.getString(tref.getKey()); - // This call parses the pattern. If patterns are used frequently, they - // should be parsed only once and the resulting MessageFormat cached. - // That's not necessary for the programming exercises though. - result = MessageFormat.format(pattern, params); + if (pattern.length() > 0) + { + // This call parses the pattern. If patterns are used frequently, they + // should be parsed only once and the resulting MessageFormat cached. + // That's not necessary for the programming exercises though. + result = MessageFormat.format(pattern, params); + } + else + { + // an empty pattern is an invalid catalog entry, provide a fallback + result = fallbackFormat(tref, params); + } } catch (MissingResourceException mre) { // the lookup failed, provide a fallback - - StringBuilder sb = new StringBuilder(80); - sb.append(tref.getBundleName()).append("::").append(tref.getKey()); - for(Object param : params) - sb.append(" \"").append(param).append("\""); - - result = sb.toString(); + result = fallbackFormat(tref, params); } - // ClassCastException intentionally not handled here, + // ClassCastException from bundle.getString intentionally not handled here, // because TextRef should always refer to a text. // IllegalArgumentException intentionally not handled here, // because it indicates a bad resource or a mismatch @@ -83,6 +85,25 @@ public static final String format(TextRef tref, Object... params) } + /** + * Formats parameters in case a pattern is missing. + * + * @param tref text ref of the missing pattern + * @param params parameters to format + * + * @return the arguments in fallback format + */ + protected static final String fallbackFormat(TextRef tref, Object... params) + { + StringBuilder sb = new StringBuilder(80); + sb.append(tref.getBundleName()).append("::").append(tref.getKey()); + for(Object param : params) + sb.append(" \"").append(param).append("\""); + + return sb.toString(); + } + + /** * Looks up a text in the default locale. * @@ -104,7 +125,7 @@ public static final String lookup(TextRef tref) // the lookup failed, provide a fallback result = tref.getBundleName()+"::"+tref.getKey(); } - // ClassCastException intentionally not handled here, + // ClassCastException from bundle.getString intentionally not handled here, // because TextRef should always refer to a text return result; diff --git a/src/test/java/pityoulish/i18n/CatalogHelperTest.java b/src/test/java/pityoulish/i18n/CatalogHelperTest.java index 00bf470..e85ce2c 100644 --- a/src/test/java/pityoulish/i18n/CatalogHelperTest.java +++ b/src/test/java/pityoulish/i18n/CatalogHelperTest.java @@ -53,6 +53,27 @@ public class CatalogHelperTest assertEquals("wrong text", UnitTestCatalogData.textB, text); } + @Test public void lookup_EMPTY() + throws Exception + { + String text = UnitTestCatalog.EMPTY.lookup(); + assertEquals("wrong text", "", text); + } + + @Test public void lookup_EMPTY_0() + throws Exception + { + String text = UnitTestCatalog.EMPTY_0.lookup(); + assertEquals("wrong text", "", text); + } + + @Test public void lookup_EMPTY_1() + throws Exception + { + String text = UnitTestCatalog.EMPTY_1.lookup(); + assertEquals("wrong text", "", text); + } + @Test public void lookup_OBJECT() throws Exception { @@ -130,6 +151,28 @@ public class CatalogHelperTest } + @Test public void format_EMPTY_0() + throws Exception + { + // expect fallback behavior when the format is empty + String text = UnitTestCatalog.EMPTY_0.format(); + assertEquals("wrong text", + "pityoulish.i18n.UnitTestCatalogData::EMPTY_0", text); + } + + + @Test public void format_EMPTY_1() + throws Exception + { + // expect fallback behavior when the format is empty + String text = UnitTestCatalog.EMPTY_1.format("ArgUment"); + assertTrue("missing entry name", + text.indexOf("UnitTestCatalogData::EMPTY_1") >= 0); + assertTrue("missing parameter", + text.indexOf("ArgUment") >= 0); + } + + @Test public void getNumericSuffix_null() throws Exception { diff --git a/src/test/java/pityoulish/i18n/UnitTestCatalog.java b/src/test/java/pityoulish/i18n/UnitTestCatalog.java index eafb4d6..6969c88 100644 --- a/src/test/java/pityoulish/i18n/UnitTestCatalog.java +++ b/src/test/java/pityoulish/i18n/UnitTestCatalog.java @@ -16,6 +16,9 @@ public enum UnitTestCatalog implements TextEntry PATTERN_2, TEXT_A, TEXT_B, + EMPTY, + EMPTY_0, + EMPTY_1, OBJECT; private final static String diff --git a/src/test/java/pityoulish/i18n/UnitTestCatalogData.java b/src/test/java/pityoulish/i18n/UnitTestCatalogData.java index f54f68f..880599e 100644 --- a/src/test/java/pityoulish/i18n/UnitTestCatalogData.java +++ b/src/test/java/pityoulish/i18n/UnitTestCatalogData.java @@ -28,9 +28,13 @@ public class UnitTestCatalogData extends ListResourceBundle public final static String textB = "Boo!"; + public final static String empty = + ""; + public final static Object obj = new Object(); + protected Object[][] getContents() { return new Object[][] { @@ -40,6 +44,9 @@ protected Object[][] getContents() { "PATTERN_2", pattern2 }, { "TEXT_A", textA }, { "TEXT_B", textB }, + { "EMPTY", empty }, + { "EMPTY_0", empty }, + { "EMPTY_1", empty }, { "OBJECT", obj } }; }