This is really just a tighter solution, one's that working right now in production code, to what was done in previous resource-bundle notes.
package com.windofkeltia.configuration;
import java.util.Enumeration;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import static java.util.Objects.isNull;
import com.windofkeltia.utilities.StringUtilities;
public class ConfigurationBundleTest
{
private static final String BUNDLENAME = "application";
public static void main( String[] args )
{
ResourceBundle bundle = null;
try
{
bundle = ResourceBundle.getBundle( BUNDLENAME );
}
catch( MissingResourceException e )
{
System.out.println( "No bundle \"" + BUNDLENAME + "\" was found.");
}
if( isNull( bundle ) )
{
System.out.println( "No properties read." );
return;
}
System.out.println( "Bundle resources name is: \"" + bundle.getBaseBundleName() + "\"" );
Map< String, String > properties = new HashMap<>();
Enumeration< String > keys = bundle.getKeys();
int maxKeyWidth = 0;
while( keys.hasMoreElements() )
{
String key = keys.nextElement();
String value = bundle.getString( key );
properties.put( key, value );
maxKeyWidth = Math.max( maxKeyWidth, key.length() );
}
for( Map.Entry< String, String > property : properties.entrySet() )
{
String key = property.getKey();
String value = property.getValue();
System.out.println( " " + StringUtilities.padStringLeft( key, maxKeyWidth ) + " = " + value );
}
}
}
Here's the output from the test above. What was in simple-resource.txt? Exactly what you see here.
Bundle resources name is: "application" disableSsl = false timeout = 20 doInput = true accept = application/xml pathname = ixmlgenerator mode = development hostname = localhost doOutput = true port = 7070 useCache = false readTimeout = 20
Last, where is the file put? That depends on just what the application is. In this case, I created it for a server that I'm running in Tomcat.
This is where the maven-war-plugin puts the properties file originally located on path application/src/main/resources/application.properties:
PATH = "/opt/tomcat/webapps/application/WEB-INFO/classes/" + BUNDLENAME + ".properties";
If this is for some other form of Java output, perhaps a JAR file, I would have to look for it.
If this copy is missing, JUnit will grab the production one, but this is a way to have different properties when running JUnit tests. The file is kept on the path application/src/test/resources/application.properties.