1 8 package org.apache.avalon.excalibur.i18n.test; 9 10 import java.util.Map ; 11 import java.util.HashMap ; 12 import java.util.Locale ; 13 14 import org.apache.avalon.excalibur.testcase.ExcaliburTestCase; 15 import org.apache.avalon.excalibur.i18n.BundleInfo; 16 import org.apache.avalon.excalibur.i18n.XmlBundle; 17 import org.apache.avalon.excalibur.i18n.BundleInfoMapper; 18 import org.apache.avalon.framework.configuration.Configuration; 19 import org.apache.avalon.framework.configuration.DefaultConfiguration; 20 import org.xml.sax.InputSource ; 21 22 26 public class XmlBundleTestCase extends ExcaliburTestCase { 27 28 private Map variables = new HashMap (5); 29 private XmlBundle bundle = new XmlBundle(); 30 private XmlBundle bundleWithRoot = new XmlBundle(); 31 32 public XmlBundleTestCase( String name ) { 33 super(name); 34 } 35 36 public void setUp() throws Exception { 37 this.variables.put("value", "a cat"); 38 39 final String bundleFileName = this.getClass().getPackage().getName().replace( '.', '/' ) + "/XmlBundle.xml"; 40 getLogger().debug("Test-bundle file = " + bundleFileName); 41 42 this.bundle.setBundleInfo(new BundleInfo("test", null)); 43 this.bundle.setMapper((BundleInfoMapper) this.manager.lookup(BundleInfoMapper.ROLE)); 44 this.bundle.enableLogging(getLogEnabledLogger()); 45 this.bundle.compose(this.manager); 46 DefaultConfiguration conf = new DefaultConfiguration("bundle-conf", "conf"); 47 conf.setAttribute(XmlBundle.ConfigurationKeys.LOAD_ON_INIT, "true"); 48 conf.setAttribute(XmlBundle.ConfigurationKeys.USE_ROOT, "false"); 49 this.bundle.configure(conf); 50 this.bundle.initialize(new InputSource (this.getClass().getClassLoader().getResource(bundleFileName).openStream())); 51 52 this.bundleWithRoot.setBundleInfo(new BundleInfo("test-with-root", null)); 53 this.bundleWithRoot.setMapper((BundleInfoMapper) this.manager.lookup(BundleInfoMapper.ROLE)); 54 this.bundleWithRoot.enableLogging(getLogEnabledLogger()); 55 this.bundleWithRoot.compose(super.manager); 56 conf = new DefaultConfiguration("bundle-conf", "conf"); 57 conf.setAttribute(XmlBundle.ConfigurationKeys.LOAD_ON_INIT, "true"); 58 conf.setAttribute(XmlBundle.ConfigurationKeys.USE_ROOT, "true"); 59 this.bundleWithRoot.configure(conf); 60 this.bundleWithRoot.initialize(new InputSource (this.getClass().getClassLoader().getResource(bundleFileName).openStream())); 61 } 62 63 public void testGetString() { 64 assertEquals("Text on level3", this.bundle.getString("level1.level2.level3")); 65 assertEquals("Flat level 2", this.bundle.getString("levels/level[@number='2']")); 66 67 assertEquals("Text on level3", this.bundleWithRoot.getString("resources.level1.level2.level3")); 68 assertEquals("Flat level 2", this.bundleWithRoot.getString("resources.levels/level[@number='2']")); 69 } 70 71 public void testGetStringWithVariable() { 72 assertEquals("Here is an example of a cat.", this.bundle.getString("key-with-variable", this.variables)); 73 assertEquals("Here is an example of a cat.", this.bundleWithRoot.getString("resources.key-with-variable", this.variables)); 74 } 75 76 } 77 | Popular Tags |