-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Converge error messages between JSON sockets and servlets
- Loading branch information
Showing
11 changed files
with
172 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package jmri.web.servlet.json; | ||
|
||
import java.util.Locale; | ||
import javax.annotation.CheckReturnValue; | ||
import javax.annotation.CheckForNull; | ||
import javax.annotation.ParametersAreNonnullByDefault; | ||
|
||
@ParametersAreNonnullByDefault | ||
@CheckReturnValue | ||
|
||
@javax.annotation.concurrent.Immutable | ||
|
||
/** | ||
* Provides standard access for resource bundles in a package. | ||
* <p> | ||
* Convention is to provide a subclass of this name in each package, working off | ||
* the local resource bundle name. | ||
* <p> | ||
* This instance is named differently than usual since two Bundle classes exist | ||
* for this package: one that inherits from jmri.web.servlet and one that | ||
* inherits from jmri.server.json | ||
* | ||
* @author Bob Jacobsen Copyright (C) 2012 | ||
* @since 3.3.1 | ||
*/ | ||
public class JsonBundle extends jmri.server.json.Bundle { | ||
|
||
@CheckForNull | ||
private static final String name = null; // NOI18N | ||
|
||
// | ||
// below here is boilerplate to be copied exactly | ||
// | ||
/** | ||
* Provides a translated string for a given key from the package resource | ||
* bundle or parent. | ||
* <p> | ||
* Note that this is intentionally package-local access. | ||
* | ||
* @param key Bundle key to be translated | ||
* @return Internationalized text | ||
*/ | ||
static String getMessage(String key) { | ||
return getBundle().handleGetMessage(key); | ||
} | ||
|
||
/** | ||
* Provides a translated string for a given key in a given locale from the | ||
* package resource bundle or parent. | ||
* <p> | ||
* Note that this is intentionally package-local access. | ||
* | ||
* @param locale The locale to be used | ||
* @param key Bundle key to be translated | ||
* @return Internationalized text | ||
*/ | ||
static String getMessage(Locale locale, String key) { | ||
return getBundle().handleGetMessage(locale, key); | ||
} | ||
|
||
/** | ||
* Merges user data with a translated string for a given key from the | ||
* package resource bundle or parent. | ||
* <p> | ||
* Uses the transformation conventions of the Java MessageFormat utility. | ||
* <p> | ||
* Note that this is intentionally package-local access. | ||
* | ||
* @see java.text.MessageFormat | ||
* @param key Bundle key to be translated | ||
* @param subs One or more objects to be inserted into the message | ||
* @return Internationalized text | ||
*/ | ||
static String getMessage(String key, Object... subs) { | ||
return getBundle().handleGetMessage(key, subs); | ||
} | ||
|
||
/** | ||
* Merges user data with a translated string for a given key in a given | ||
* locale from the package resource bundle or parent. | ||
* <p> | ||
* Uses the transformation conventions of the Java MessageFormat utility. | ||
* <p> | ||
* Note that this is intentionally package-local access. | ||
* | ||
* @see java.text.MessageFormat | ||
* @param locale The locale to be used | ||
* @param key Bundle key to be translated | ||
* @param subs One or more objects to be inserted into the message | ||
* @return Internationalized text | ||
*/ | ||
static String getMessage(Locale locale, String key, Object... subs) { | ||
return getBundle().handleGetMessage(locale, key, subs); | ||
} | ||
|
||
private final static JsonBundle b = new JsonBundle(); | ||
|
||
@Override | ||
@CheckForNull | ||
protected String bundleName() { | ||
return name; | ||
} | ||
|
||
protected static jmri.Bundle getBundle() { | ||
return b; | ||
} | ||
|
||
@Override | ||
protected String retry(Locale locale, String key) { | ||
return super.getBundle().handleGetMessage(locale, key); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package jmri.web.servlet.json; | ||
|
||
import java.util.Locale; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Tests for the JsonBundle class | ||
* | ||
* @author Bob Jacobsen Copyright (C) 2012 | ||
*/ | ||
public class JsonBundleTest { | ||
|
||
@Test | ||
public void testGoodKeyMessage() { | ||
Assert.assertEquals("Turnout", JsonBundle.getMessage("BeanNameTurnout")); | ||
} | ||
|
||
@Test(expected = java.util.MissingResourceException.class) | ||
public void testBadKeyMessage() { | ||
JsonBundle.getMessage("FFFFFTTTTTTT"); | ||
} | ||
|
||
@Test | ||
public void testGoodKeyMessageArg() { | ||
Assert.assertEquals("Turnout", JsonBundle.getMessage("BeanNameTurnout", new Object[]{})); | ||
Assert.assertEquals("About Test", JsonBundle.getMessage("TitleAbout", "Test")); | ||
} | ||
|
||
@Test(expected = java.util.MissingResourceException.class) | ||
public void testBadKeyMessageArg() { | ||
JsonBundle.getMessage("FFFFFTTTTTTT", new Object[]{}); | ||
} | ||
|
||
@Test | ||
public void testLocaleMessage() { | ||
Assert.assertEquals("Scambio", JsonBundle.getMessage(Locale.ITALY, "BeanNameTurnout")); | ||
} | ||
|
||
@Test | ||
public void testLocaleMessageArg() { | ||
Assert.assertEquals("Scambio", JsonBundle.getMessage(Locale.ITALY, "BeanNameTurnout", new Object[]{})); | ||
Assert.assertEquals("Informazioni su Test", JsonBundle.getMessage(Locale.ITALY, "TitleAbout", "Test")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters