Skip to content
This repository has been archived by the owner on Nov 21, 2021. It is now read-only.

Commit

Permalink
Merge pull request #77 from rolandweber/44-catalog-empty-string
Browse files Browse the repository at this point in the history
i18n: fallback when pattern in catalog is empty
  • Loading branch information
rolandweber authored Oct 28, 2018
2 parents 2f17f8c + 1491584 commit 83f8b36
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ install:
- cp -v .travis.ant.local ant.local

script:
- ant compile test jdoc
- ant travis
2 changes: 2 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ support a course on "Distributed Systems"

<target name="default" depends="compile,compile-pitfill" />

<target name="travis" depends="compile,test,compile-pitfill,jdoc" />

</project>
47 changes: 34 additions & 13 deletions src/main/java/pityoulish/i18n/CatalogHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
*
Expand All @@ -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;
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/pityoulish/i18n/CatalogHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/pityoulish/i18n/UnitTestCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/pityoulish/i18n/UnitTestCatalogData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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[][] {
Expand All @@ -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 }
};
}
Expand Down

0 comments on commit 83f8b36

Please sign in to comment.