Skip to content

Commit

Permalink
Merge pull request #112 from vt-middleware/resource-bundle-support
Browse files Browse the repository at this point in the history
Add support for resource bundles.
  • Loading branch information
serac committed Jan 15, 2020
2 parents 5080f32 + 51eeabb commit cb9f4ca
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 30 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -72,7 +72,7 @@
<testng.dir>${basedir}/src/test/testng</testng.dir>
<assembly.dir>${basedir}/src/main/assembly</assembly.dir>
<japicmp.enabled>false</japicmp.enabled>
<japicmp.oldVersion>1.4.0</japicmp.oldVersion>
<japicmp.oldVersion>1.5.0</japicmp.oldVersion>
</properties>

<dependencies>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/passay/PropertiesMessageResolver.java
Expand Up @@ -15,21 +15,21 @@ public class PropertiesMessageResolver extends AbstractMessageResolver
{

/** Classpath location of default message map. */
public static final String DEFAULT_MESSAGE_PATH = "/messages.properties";
public static final String DEFAULT_MESSAGE_PATH = "/passay.properties";

/** Maps message keys to message strings. */
private final Properties messageProperties;


/** Creates a new message resolver with the default message map. */
/** Creates a new message resolver with the default message properties. See {@link #getDefaultProperties()}. */
public PropertiesMessageResolver()
{
this(getDefaultProperties());
}


/**
* Creates a new message resolver with the supplied message map.
* Creates a new message resolver with the supplied message properties.
*
* @param properties map of keys to messages.
*/
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/org/passay/ResourceBundleMessageResolver.java
@@ -0,0 +1,61 @@
/* See LICENSE for licensing and NOTICE for copyright. */
package org.passay;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
* Resolves messages from rule result details from a resource bundle. Uses {@link ResourceBundle#getBundle(String)} to
* load the default bundle.
*
* @author Middleware Services
*/
public class ResourceBundleMessageResolver extends AbstractMessageResolver
{

/** Maps locale specific message keys to message strings. */
private final ResourceBundle resourceBundle;


/** Creates a new message resolver with the default message map. */
public ResourceBundleMessageResolver()
{
this(getDefaultBundle());
}


/**
* Creates a new message resolver with the supplied resource bundle.
*
* @param bundle locale specific map of keys to messages.
*/
public ResourceBundleMessageResolver(final ResourceBundle bundle)
{
if (bundle == null) {
throw new IllegalArgumentException("Bundle cannot be null.");
}
resourceBundle = bundle;
}


@Override
protected String getMessage(final String key)
{
try {
return resourceBundle.getString(key);
} catch (MissingResourceException e) {
return null;
}
}


/**
* Returns the default resource bundle which is found in passay.properties.
*
* @return default resource bundle.
*/
public static ResourceBundle getDefaultBundle()
{
return ResourceBundle.getBundle("passay");
}
}
File renamed without changes.
30 changes: 4 additions & 26 deletions src/test/java/org/passay/AbstractRuleTest.java
@@ -1,9 +1,8 @@
/* See LICENSE for licensing and NOTICE for copyright. */
package org.passay;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.ResourceBundle;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;

Expand All @@ -16,32 +15,11 @@ public abstract class AbstractRuleTest
{

/** test message resolver. */
protected static final PropertiesMessageResolver TEST_RESOLVER;
protected static final MessageResolver TEST_RESOLVER = new ResourceBundleMessageResolver(
ResourceBundle.getBundle("passay-test"));

/** empty message resolver. */
protected static final PropertiesMessageResolver EMPTY_RESOLVER = new PropertiesMessageResolver(new Properties());


// load the properties for the test resolver
static {
InputStream in = null;
try {
in = AbstractRuleTest.class.getResourceAsStream("/messages-test.properties");
final Properties props = new Properties();
props.load(in);
TEST_RESOLVER = new PropertiesMessageResolver(props);
} catch (Exception e) {
throw new IllegalStateException("Error loading test message properties.", e);
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
protected static final MessageResolver EMPTY_RESOLVER = new PropertiesMessageResolver(new Properties());


/**
Expand Down
File renamed without changes.

0 comments on commit cb9f4ca

Please sign in to comment.