I looked over the source for JWebUnit, and it appears that the best solution is
to write a single resource bundle class that merges several bundles, which I
have written (see attached files). All you need to do is subclass
MergeResourceBundle and pass the list of resource base names to the
MergeResourceBundle constructor (see TheResource.java for an example). In
JWebUnit, you simple call
getTestContext().setResourceBundleName("JWebUnit.TheResources");
This requires no changes to JWebUnit. Of course, it could be integrated...
I have also included my JUnit test for MergeResourceBundle.java (see
MergeResourceBundleTest.java and Three.java).
Comments?
----- Original Message ----
From: Julien HENRY <[EMAIL PROTECTED]>
To: Usage problems for JWebUnit <jwebunit-users@lists.sourceforge.net>
Sent: Friday, May 25, 2007 1:58:50 AM
Subject: [JWebUnit-users] Re : Multiple Property Files
Hi,
I think it's not supported. Feele free to open a feature request, and better
provide a patch + test cases.
++
Julien
----- Message d'origine ----
De : Ed Smith <[EMAIL PROTECTED]>
À : jwebunit-users@lists.sourceforge.net
Envoyé le : Jeudi, 24 Mai 2007, 21h03mn 16s
Objet : [JWebUnit-users] Multiple Property Files
How do I load multiple property files in JWebUnit? I know about
getTestContext().setResourceBundleName("Resource1");
but if it's called multiple times with different property files, only the last
property file is used.
Thanks in advance!
____________________________________________________________________________________Be
a better Heartthrob. Get better relationship answers from someone who knows.
Yahoo! Answers - Check it out.
http://answers.yahoo.com/dir/?link=list&sid=396545433
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
JWebUnit-users mailing list
JWebUnit-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jwebunit-users
_____________________________________________________________________________
Ne gardez plus qu'une seule adresse mail ! Copiez vos mails vers Yahoo! Mail
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
JWebUnit-users mailing list
JWebUnit-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jwebunit-users
____________________________________________________________________________________
Moody friends. Drama queens. Your life? Nope! - their life, your story. Play
Sims Stories at Yahoo! Games.
http://sims.yahoo.com/
package bundles;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import java.awt.Point;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
/**
* Unit test for MergerResourceBundle class. Creates test property files: one_<lang>.properties,
* one.properties, two.properties, and empty.properties. Files + resource class are included in
* varying combinations to test handling of keys, duplicate keys, non-existent
* keys, and parent bundles. Requires JUnit 4.
*/
@RunWith(Parameterized.class)
public class MergeResourceBundleTest {
// current test definition for parameterized test
private String[] baseNames;
private Map<String, Object> map;
// current bundle for parameterized test
private MergeResourceBundle bundle;
// character encoding according to Java API (see
// java.util.Properties)
private static final String PROPFILEENCODING = "ISO-8859-1";
/**
* Creates 4 test property files
*
* @throws IOException
* if I/O problem in file creation
*/
@BeforeClass
public static void setUpClass() throws IOException {
// create and populate file one_<lang>.properties
PrintWriter w = new PrintWriter("one_"
+ Locale.getDefault().getLanguage() + ".properties",
PROPFILEENCODING);
w.println("a=onea");
w.close();
// create and populate file one.properties
w = new PrintWriter("one.properties", PROPFILEENCODING);
w.println("b=oneb");
w.close();
// create and populate file two.properties
w = new PrintWriter("two.properties", PROPFILEENCODING);
w.println("b=twob");
w.println("c=twoc");
w.close();
// create and populate file empty.properties
w = new PrintWriter("empty.properties", PROPFILEENCODING);
w.close();
}
/**
* Create list of test definitions as parameters to Unit tests
*
* @return collection of test definitions
*/
@Parameters
public static Collection<Object[]> data() {
// Collection of test definitions. Each element is an array, as required
// by parameterized testing for JUnit 4; however, each array is only one
// element in length
Collection<Object[]> tests = new LinkedList<Object[]>();
// Map of key/value pairs for particular test
HashMap<String, Object> map = new HashMap<String, Object>();
// test definition: no resource bundles
tests.add(new Object[] { new String[] {}, map.clone() });
// test definition: empty bundle
tests.add(new Object[] {new String[] { "empty" }, map.clone() });
// test definition: a resource bundle with parent bundle
map.put("a", "onea");
map.put("b", "oneb");
tests.add(new Object[] { new String[] { "one" }, map.clone() });
// test definition: two resource bundles with overlapping keys and parent bundle
map.put("c", "twoc");
tests.add(new Object[] { new String[] { "one", "two" }, map.clone() });
// test definition: same as previous tests with order reversed
map.put("b", "twob"); // Just change key b
tests.add(new Object[] { new String[] { "two", "one" }, map.clone() });
// test definition: two resource bundles and resource class
map.put("d", new Point(2, 3));
tests.add(new Object[] { new String[] { "two", "one", "bundles.Three" }, map.clone() });
return tests;
}
/**
* Unit test constructor to set up parameterized test definition
*
* @param testDef current test definition to execute over all tests
*/
public MergeResourceBundleTest(final String[] baseNames, final Map<String, Object> map ) {
this.baseNames = baseNames;
this.map = map;
}
/**
* Unit test set up routine
*/
@Before
public void setUp() {
bundle = new MergeResourceBundle(baseNames);
}
/**
* Test getKeys() method for bundles and parent bundles
*/
@Test
public void testGetKeys() {
Set<String> actual = keySet();
Set<String> expected = map.keySet();
assertEquals(expected, actual);
}
/**
* Utility method to create key set from enumeration of resource bundle keys
*
* @return set of keys
*/
private Set<String> keySet() {
Set<String> keys = new HashSet<String>();
Enumeration<String> e = bundle.getKeys();
while (e.hasMoreElements()) {
keys.add(e.nextElement());
}
return keys;
}
/**
* Test getGetObject() method for bundles and parent bundles
*/
@Test
public void testGetObject() {
// null key
try {
bundle.handleGetObject(null);
fail("null key");
} catch (NullPointerException e) {
}
// key to value mapping
for (String key : map.keySet()) {
assertEquals(map.get(key), bundle.handleGetObject(key));
}
// non-existent key
assertNull(bundle.handleGetObject("z"));
}
}
package bundles;
import java.awt.Point;
import java.util.ListResourceBundle;
public class Three extends ListResourceBundle {
protected Object[][] getContents() {
return new Object[][] { { "d", new Point(2, 3) } };
}
}
package JWebUnit;
import bundles.MergeResourceBundle;
public class TheResources extends MergeResourceBundle {
public TheResources() {
super("one", "two");
}
}
package bundles;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
/**
* Resource bundle that behaves as a merge of a list of resource bundles. This
* allows you to maintain multiple resource bundle files/classes, but only
* reference a single bundle in your application/test.
*/
public class MergeResourceBundle extends ResourceBundle {
// List of bundles in merge bundle
private List<ResourceBundle> bundles;
/**
* Constructs a resource bundle from a list of other resource bundles. If
* there are duplicate keys, the key from the resource bundle with the
* smallest index takes precedence.
*
* @param baseNameList
* list of bundle base names to search for key/value pairs
*/
public MergeResourceBundle(String... baseNameList) {
bundles = new ArrayList<ResourceBundle>(baseNameList.length);
for (String baseName : baseNameList) {
bundles.add(ResourceBundle.getBundle(baseName));
}
}
/*
* (non-Javadoc)
*
* @see java.util.ResourceBundle#getKeys()
*/
@Override
public Enumeration<String> getKeys() {
return new Enumeration<String>() {
// Coding Comment: I must eliminate duplicate keys from multiple
// bundles so I take the easy way out and create a set of keys over
// all bundles. This is memory-intensive, O(# keys) PER Enumeration!
// We could create a static set of keys for all enumerations and
// refresh on clearCache(). Seems like a bloom filter would be
// better, but...
// Per enumeration list of keys from all bundles
private Set<String> keys = new HashSet<String>();
// Set iterator to simulate enumeration
private Iterator<String> i;
// Constructor
{
for (ResourceBundle b : bundles) {
keys.addAll(b.keySet());
}
i = keys.iterator();
}
public boolean hasMoreElements() {
return i.hasNext();
}
public String nextElement() {
return i.next();
}
};
}
/*
* (non-Javadoc)
*
* @see java.util.ResourceBundle#handleGetObject(java.lang.String)
*/
@Override
protected Object handleGetObject(String key) {
if (key == null) {
throw new NullPointerException();
}
// Search for first resource bundle containing the specified key
for (ResourceBundle b : bundles) {
try {
return b.getObject(key);
} catch (MissingResourceException mre) {
}
}
return null;
}
}
package JWebUnit;
import bundles.MergeResourceBundle;
public class TheResources extends MergeResourceBundle {
public TheResources() {
super("one", "two");
}
}
package bundles;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
/**
* Resource bundle that behaves as a merge of a list of resource bundles. This
* allows you to maintain multiple resource bundle files/classes, but only
* reference a single bundle in your application/test.
*/
public class MergeResourceBundle extends ResourceBundle {
// List of bundles in merge bundle
private List<ResourceBundle> bundles;
/**
* Constructs a resource bundle from a list of other resource bundles. If
* there are duplicate keys, the key from the resource bundle with the
* smallest index takes precedence.
*
* @param baseNameList
* list of bundle base names to search for key/value pairs
*/
public MergeResourceBundle(String... baseNameList) {
bundles = new ArrayList<ResourceBundle>(baseNameList.length);
for (String baseName : baseNameList) {
bundles.add(ResourceBundle.getBundle(baseName));
}
}
/*
* (non-Javadoc)
*
* @see java.util.ResourceBundle#getKeys()
*/
@Override
public Enumeration<String> getKeys() {
return new Enumeration<String>() {
// Coding Comment: I must eliminate duplicate keys from multiple
// bundles so I take the easy way out and create a set of keys over
// all bundles. This is memory-intensive, O(# keys) PER Enumeration!
// We could create a static set of keys for all enumerations and
// refresh on clearCache(). Seems like a bloom filter would be
// better, but...
// Per enumeration list of keys from all bundles
private Set<String> keys = new HashSet<String>();
// Set iterator to simulate enumeration
private Iterator<String> i;
// Constructor
{
for (ResourceBundle b : bundles) {
keys.addAll(b.keySet());
}
i = keys.iterator();
}
public boolean hasMoreElements() {
return i.hasNext();
}
public String nextElement() {
return i.next();
}
};
}
/*
* (non-Javadoc)
*
* @see java.util.ResourceBundle#handleGetObject(java.lang.String)
*/
@Override
protected Object handleGetObject(String key) {
if (key == null) {
throw new NullPointerException();
}
// Search for first resource bundle containing the specified key
for (ResourceBundle b : bundles) {
try {
return b.getObject(key);
} catch (MissingResourceException mre) {
}
}
return null;
}
}
package JWebUnit;
import bundles.MergeResourceBundle;
public class TheResources extends MergeResourceBundle {
public TheResources() {
super("one", "two");
}
}
package bundles;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
/**
* Resource bundle that behaves as a merge of a list of resource bundles. This
* allows you to maintain multiple resource bundle files/classes, but only
* reference a single bundle in your application/test.
*/
public class MergeResourceBundle extends ResourceBundle {
// List of bundles in merge bundle
private List<ResourceBundle> bundles;
/**
* Constructs a resource bundle from a list of other resource bundles. If
* there are duplicate keys, the key from the resource bundle with the
* smallest index takes precedence.
*
* @param baseNameList
* list of bundle base names to search for key/value pairs
*/
public MergeResourceBundle(String... baseNameList) {
bundles = new ArrayList<ResourceBundle>(baseNameList.length);
for (String baseName : baseNameList) {
bundles.add(ResourceBundle.getBundle(baseName));
}
}
/*
* (non-Javadoc)
*
* @see java.util.ResourceBundle#getKeys()
*/
@Override
public Enumeration<String> getKeys() {
return new Enumeration<String>() {
// Coding Comment: I must eliminate duplicate keys from multiple
// bundles so I take the easy way out and create a set of keys over
// all bundles. This is memory-intensive, O(# keys) PER Enumeration!
// We could create a static set of keys for all enumerations and
// refresh on clearCache(). Seems like a bloom filter would be
// better, but...
// Per enumeration list of keys from all bundles
private Set<String> keys = new HashSet<String>();
// Set iterator to simulate enumeration
private Iterator<String> i;
// Constructor
{
for (ResourceBundle b : bundles) {
keys.addAll(b.keySet());
}
i = keys.iterator();
}
public boolean hasMoreElements() {
return i.hasNext();
}
public String nextElement() {
return i.next();
}
};
}
/*
* (non-Javadoc)
*
* @see java.util.ResourceBundle#handleGetObject(java.lang.String)
*/
@Override
protected Object handleGetObject(String key) {
if (key == null) {
throw new NullPointerException();
}
// Search for first resource bundle containing the specified key
for (ResourceBundle b : bundles) {
try {
return b.getObject(key);
} catch (MissingResourceException mre) {
}
}
return null;
}
}
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
JWebUnit-users mailing list
JWebUnit-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jwebunit-users