neeme 01/10/31 11:56:47 Modified: src/scratchpad/org/apache/avalon/excalibur/i18n AbstractBundle.java Bundle.java BundleSelector.java DefaultBundleLoader.java DefaultMapper.java DirectoryMapper.java XmlBundle.java src/scratchpad/org/apache/avalon/excalibur/i18n/test AbstractBundleTestCase.java Added: src/scratchpad/org/apache/avalon/excalibur/i18n/test DefaultBundleLoaderTestCase.java DefaultBundleLoaderTestCase.xtest XmlBundle.xml XmlBundleTestCase.java XmlBundleTestCase.xtest Log: first round of bugfixing and testing the rewritten i18n support. no major changes. Revision Changes Path 1.4 +3 -1 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/AbstractBundle.java Index: AbstractBundle.java =================================================================== RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/AbstractBundle.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- AbstractBundle.java 2001/10/31 19:39:05 1.3 +++ AbstractBundle.java 2001/10/31 19:56:47 1.4 @@ -86,7 +86,7 @@ * @exception MissingResourceException if value was not found */ public String getString(String key, Map variables) { - return convertKey(substitute(getString(key), variables)); + return substitute(getString(key), variables); } /** @@ -113,6 +113,7 @@ */ public String substitute(String value, Map values) { if (value == null || values == null) return value; + if (getLogger().isDebugEnabled()) getLogger().debug("Substituting value: " + value); StringBuffer result = new StringBuffer(value.length()); int startPos = 0; @@ -149,6 +150,7 @@ endPos++; } } + if (getLogger().isDebugEnabled()) getLogger().debug("Returning substituted value: " + result); return result.toString(); } 1.2 +8 -4 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/Bundle.java Index: Bundle.java =================================================================== RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/Bundle.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- Bundle.java 2001/10/30 21:38:56 1.1 +++ Bundle.java 2001/10/31 19:56:47 1.2 @@ -14,17 +14,21 @@ * This is the interface of the ResourceBundle, for used for i18n support. * * @author <a href="mailto:[EMAIL PROTECTED]">Neeme Praks</a> - * @version CVS $Revision: 1.1 $ $Date: 2001/10/30 21:38:56 $ $Author: neeme $ + * @version CVS $Revision: 1.2 $ $Date: 2001/10/31 19:56:47 $ $Author: neeme $ */ public interface Bundle { /** * Initalize the bundle * - * @param fileName name of the XML source file - * @param cacheAtStartup cache all the keys when constructing? + * @param uri URI of the XML source */ - public void init(String fileName, boolean cacheAtStartup) throws Exception; + public void init(String uri) throws Exception; + + /** + * Load all the keys when initializing? + */ + public void setLoadOnInit(boolean loadOnInit); /** * Get the bundle info. 1.2 +10 -3 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/BundleSelector.java Index: BundleSelector.java =================================================================== RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/BundleSelector.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- BundleSelector.java 2001/10/30 21:38:56 1.1 +++ BundleSelector.java 2001/10/31 19:56:47 1.2 @@ -30,7 +30,7 @@ * * @author <a href="mailto:[EMAIL PROTECTED]">Neeme Praks</a> * @author <a href="mailto:[EMAIL PROTECTED]">Mike Engelhart</a> - * @version $Id: BundleSelector.java,v 1.1 2001/10/30 21:38:56 neeme Exp $ + * @version $Id: BundleSelector.java,v 1.2 2001/10/31 19:56:47 neeme Exp $ */ public class BundleSelector @@ -41,7 +41,7 @@ /** * The role implemented by an <code>BundleSelector</code>. */ - String ROLE = "org.apache.avalon.excalibur.i18n.BundleSelector"; + public static String ROLE = "org.apache.avalon.excalibur.i18n.BundleSelector"; /** Component Manager */ protected ComponentManager manager = null; @@ -83,6 +83,13 @@ Configuration[] loaderConfs = configuration.getChild("loaders").getChildren("loader"); for (int i = 0; i < loaderConfs.length; i++) { BundleLoader loader = (BundleLoader) getInstance(loaderConfs[i].getAttribute("class")); + if (loader instanceof Loggable) ((Loggable)loader).setLogger(logger); + try { + if (loader instanceof Composable) ((Composable)loader).compose(this.manager); + } + catch (ComponentException e) { + throw new ConfigurationException("unable to compose loader", e); + } loader.configure(loaderConfs[i]); loader.setBundleSelector(this); this.loaders.put(loaderConfs[i].getAttribute("type-name"), loader); @@ -156,9 +163,9 @@ BundleInfo parentBundleInfo = bundleInfo.getParent(); while (bundle == null && parentBundleInfo != null) { if (logger.isDebugEnabled()) logger.debug("synchronized: still not found, trying parent: " + parentBundleInfo); - parentBundleInfo = bundleInfo.getParent(); bundle = loadBundle(parentBundleInfo); updateCache(parentBundleInfo, bundle); + parentBundleInfo = parentBundleInfo.getParent(); } updateCache(bundleInfo, bundle); } 1.2 +4 -3 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/DefaultBundleLoader.java Index: DefaultBundleLoader.java =================================================================== RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/DefaultBundleLoader.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- DefaultBundleLoader.java 2001/10/30 21:38:56 1.1 +++ DefaultBundleLoader.java 2001/10/31 19:56:47 1.2 @@ -21,7 +21,7 @@ * Used to map locale information to URI space, to find the relevant bundle. * * @author <a href="mailto:[EMAIL PROTECTED]">Neeme Praks</a> - * @version CVS $Revision: 1.1 $ $Date: 2001/10/30 21:38:56 $ $Author: neeme $ + * @version CVS $Revision: 1.2 $ $Date: 2001/10/31 19:56:47 $ $Author: neeme $ */ public class DefaultBundleLoader implements BundleLoader, Loggable, Composable { @@ -74,7 +74,7 @@ /** * Set the bundle selector. * - * @param logger the selector + * @param selector the selector */ public void setBundleSelector(BundleSelector selector) { this.selector = selector; @@ -190,7 +190,8 @@ bundle.setBundleInfo(bundleInfo); bundle.setParent(parentBundle); if (bundle instanceof Composable) ((Composable)bundle).compose(this.manager); - bundle.init(uri, cacheAllValues()); + bundle.setLoadOnInit(cacheAllValues()); + bundle.init(uri); bundle.setLastModified(System.currentTimeMillis()); } catch (Exception e) { 1.3 +1 -3 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/DefaultMapper.java Index: DefaultMapper.java =================================================================== RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/DefaultMapper.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- DefaultMapper.java 2001/10/30 21:38:56 1.2 +++ DefaultMapper.java 2001/10/31 19:56:47 1.3 @@ -14,7 +14,7 @@ * file ending with en_US. * * @author <a href="mailto:[EMAIL PROTECTED]">Neeme Praks</a> - * @version CVS $Revision: 1.2 $ $Date: 2001/10/30 21:38:56 $ $Author: neeme $ + * @version CVS $Revision: 1.3 $ $Date: 2001/10/31 19:56:47 $ $Author: neeme $ */ public class DefaultMapper implements BundleInfoMapper { @@ -47,8 +47,6 @@ sb.append(loc.getVariant()); } } - sb.append(".xml"); - String result = sb.toString(); return result; } 1.3 +2 -2 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/DirectoryMapper.java Index: DirectoryMapper.java =================================================================== RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/DirectoryMapper.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- DirectoryMapper.java 2001/10/30 21:38:56 1.2 +++ DirectoryMapper.java 2001/10/31 19:56:47 1.3 @@ -14,7 +14,7 @@ * file ending with en_US. * * @author <a href="mailto:[EMAIL PROTECTED]">Neeme Praks</a> - * @version CVS $Revision: 1.2 $ $Date: 2001/10/30 21:38:56 $ $Author: neeme $ + * @version CVS $Revision: 1.3 $ $Date: 2001/10/31 19:56:47 $ $Author: neeme $ */ public class DirectoryMapper implements BundleInfoMapper { @@ -37,7 +37,7 @@ if (country.length() > 0) sb.append("/").append(country); if (variant.length() > 0) sb.append("/").append(variant); } - sb.append("/").append(name).append(".xml"); + sb.append("/").append(name); String result = sb.toString(); return result; 1.2 +84 -40 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/XmlBundle.java Index: XmlBundle.java =================================================================== RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/XmlBundle.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- XmlBundle.java 2001/10/30 21:38:56 1.1 +++ XmlBundle.java 2001/10/31 19:56:47 1.2 @@ -22,7 +22,9 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.util.HashMap; import java.util.Locale; import java.util.Map; @@ -32,15 +34,10 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Mike Engelhart</a> * @author <a href="mailto:[EMAIL PROTECTED]">Neeme Praks</a> * @author <a href="mailto:[EMAIL PROTECTED]">Oleg Podolsky</a> - * @version $Id: XmlBundle.java,v 1.1 2001/10/30 21:38:56 neeme Exp $ + * @version $Id: XmlBundle.java,v 1.2 2001/10/31 19:56:47 neeme Exp $ */ -public class XmlBundle extends AbstractBundle -implements Disposable, Composable { +public class XmlBundle extends AbstractBundle implements Disposable, Composable { - /** DOM factory */ - private static DocumentBuilderFactory docfactory = - DocumentBuilderFactory.newInstance(); - /** Cache for storing string values for existing XPaths */ private Map cache = new HashMap(); @@ -56,59 +53,104 @@ /** XPath Processor */ private XPathProcessor processor = null; + private boolean loadOnInit = true; + private boolean useRootElement = false; + public void compose(ComponentManager manager) { this.manager = manager; try { this.processor = (XPathProcessor)this.manager.lookup(XPathProcessor.ROLE); } catch (Exception e) { - logger.error("cannot obtain XPathProcessor", e); + getLogger().error("cannot obtain XPathProcessor", e); } } /** + * Load all the keys when initializing? + */ + public void setLoadOnInit(boolean loadOnInit) { + this.loadOnInit = loadOnInit; + } + + /** + * Use the XML root element in key names when pre-loading? + */ + public void setUseRootElement(boolean useRootElement) { + this.useRootElement = useRootElement; + } + + /** * Initalize the bundle * - * @param fileName name of the XML source file - * @param cacheAtStartup cache all the keys when constructing? + * @param uri URI of the XML source * @exception IOException if an IO error occurs while reading the file * @exception ParserConfigurationException if no parser is configured * @exception SAXException if an error occurs while parsing the file */ - public void init(String fileName, boolean cacheAtStartup) - throws Exception { - if (logger.isInfoEnabled()) logger.info("Constructing XMLResourceBundle: " + getBundleInfo()); - this.doc = loadResourceBundle(fileName); - if (cacheAtStartup) cacheAll(doc.getDocumentElement(), ""); + public void init(String uri) throws Exception { + this.init(getDocumentBuilder().parse(uri)); } /** - * Convert the "user view" of the lookup key to the - * "system view". - * Current implementation coverts dots into slashes, so common Java property - * names become XPath paths. - * E.g: this.is.java.property.key --> /this/is/java/property/key + * Initalize the bundle * - * @param key user key - * @return system key + * @param file XML source file + * @exception IOException if an IO error occurs while reading the file + * @exception ParserConfigurationException if no parser is configured + * @exception SAXException if an error occurs while parsing the file */ - public String convertKey(String userKey) { - return '/' + userKey.replace('.', '/'); + public void init(File file) throws Exception { + this.init(getDocumentBuilder().parse(file)); } /** - * Load the DOM tree, based on the file name. + * Initalize the bundle * - * @param fileName name of the XML source file - * @return the DOM tree + * @param inStream XML source stream * @exception IOException if an IO error occurs while reading the file * @exception ParserConfigurationException if no parser is configured * @exception SAXException if an error occurs while parsing the file + */ + public void init(InputStream inStream) throws Exception { + this.init(getDocumentBuilder().parse(inStream)); + } + + /** + * Initalize the bundle + * + * @param document XML source document (DOM) */ - protected static Document loadResourceBundle(String fileName) - throws IOException, ParserConfigurationException, SAXException { + public void init(Document document) { + if (getLogger().isInfoEnabled()) getLogger().info("Constructing XMLResourceBundle: " + getBundleInfo()); + this.doc = document; + if (this.loadOnInit) { + cacheAll(doc.getDocumentElement(), useRootElement ? '/' + doc.getDocumentElement().getTagName() : ""); + } + } + + /** + * Return document builder used to parse the XML document. + * + * @return the document builder + * @exception ParserConfigurationException if no parser is configured + */ + protected DocumentBuilder getDocumentBuilder() throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - DocumentBuilder builder = factory.newDocumentBuilder(); - return builder.parse(fileName); + return factory.newDocumentBuilder(); + } + + /** + * Convert the "user view" of the lookup key to the + * "system view". + * Current implementation coverts dots into slashes, so common Java property + * names become XPath paths. + * E.g: this.is.java.property.key --> /this/is/java/property/key + * + * @param key user key + * @return system key + */ + public String convertKey(String userKey) { + return '/' + userKey.replace('.', '/'); } /** @@ -127,8 +169,9 @@ * @return true if contains, false otherwise */ protected boolean cacheContains(String key) { - if (logger.isDebugEnabled()) logger.debug(getBundleInfo() + ": cache contains: " + key); - return cache.containsKey(key); + boolean result = cache.containsKey(key); + if (getLogger().isDebugEnabled()) getLogger().debug(getBundleInfo() + ": cache contains key '" + key + "': " + result); + return result; } /** @@ -138,8 +181,9 @@ * @return true if contains, false otherwise */ protected boolean cacheNotFoundContains(String key) { - if (logger.isDebugEnabled()) logger.debug(getBundleInfo() + ": cache_not_found contains: " + key); - return cacheNotFound.containsKey(key); + boolean result = cacheNotFound.containsKey(key); + if (getLogger().isDebugEnabled()) getLogger().debug(getBundleInfo() + ": cache_not_found contains key '" + key + "': " + result); + return result; } /** @@ -149,7 +193,7 @@ * @param value the value */ protected void cacheKey(String key, String value) { - if (logger.isDebugEnabled()) logger.debug(getBundleInfo() + ": caching: " + key + " = " + value); + if (getLogger().isDebugEnabled()) getLogger().debug(getBundleInfo() + ": caching: " + key + " = " + value); cache.put(key, value); } @@ -159,7 +203,7 @@ * @param key the key */ protected void cacheNotFoundKey(String key) { - if (logger.isDebugEnabled()) logger.debug(getBundleInfo() + ": caching not_found: " + key); + if (getLogger().isDebugEnabled()) getLogger().debug(getBundleInfo() + ": caching not_found: " + key); cacheNotFound.put(key, ""); } @@ -170,7 +214,7 @@ * @return the value */ protected String getFromCache(String key) { - if (logger.isDebugEnabled()) logger.debug(getBundleInfo() + ": returning from cache: " + key); + if (getLogger().isDebugEnabled()) getLogger().debug(getBundleInfo() + ": returning from cache: " + key); return (String) cache.get(key); } @@ -274,7 +318,7 @@ value = getTextValue(_getNode(node, key)); } catch (Exception e) { - logger.error(getBundleInfo() + ": error while locating resource: " + key, e); + getLogger().error(getBundleInfo() + ": error while locating resource: " + key, e); } return value; } @@ -335,7 +379,7 @@ node = this.processor.selectSingleNode(rootNode, key); } catch (Exception e) { - logger.error("Error while locating resource with key: " + key, e); + getLogger().error("Error while locating resource with key: " + key, e); } return node; } 1.5 +15 -11 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/test/AbstractBundleTestCase.java Index: AbstractBundleTestCase.java =================================================================== RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/test/AbstractBundleTestCase.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- AbstractBundleTestCase.java 2001/10/31 19:43:52 1.4 +++ AbstractBundleTestCase.java 2001/10/31 19:56:47 1.5 @@ -10,20 +10,17 @@ import java.util.Map; import java.util.HashMap; -import junit.framework.TestCase; -import org.apache.log.Hierarchy; -import org.apache.avalon.excalibur.i18n.Bundle; +import org.apache.avalon.excalibur.testcase.ExcaliburTestCase; import org.apache.avalon.excalibur.i18n.AbstractBundle; -import java.io.FileWriter; /** * @author <a href="mailto:[EMAIL PROTECTED]">Neeme Praks</a> - * @version $Id: AbstractBundleTestCase.java,v 1.4 2001/10/31 19:43:52 bloritsch Exp $ + * @version $Id: AbstractBundleTestCase.java,v 1.5 2001/10/31 19:56:47 neeme Exp $ */ -public class AbstractBundleTestCase extends TestCase { +public class AbstractBundleTestCase extends ExcaliburTestCase { private TestBundle bundle = new TestBundle(); - private HashMap variables = new HashMap(5); + private Map variables = new HashMap(5); public AbstractBundleTestCase( String name ) { super(name); @@ -32,8 +29,10 @@ public void setUp() throws Exception { this.variables.put("nice", "not so nice"); this.variables.put("bad", "too bad"); + this.variables.put("value", "a cat"); - this.bundle.setLogger(Hierarchy.getDefaultHierarchy().getLoggerFor("Bundle")); + this.bundle.setLogger(getLogger()); + this.bundle.put("cat", "Here is an example of {value}."); this.bundle.put("nice", "This is a {nice} test!"); this.bundle.put("nice.nice", "This is a {nice}, {nice} test!"); this.bundle.put("nice.bad", "This is a {nice} but not {bad} test!"); @@ -47,17 +46,22 @@ } public void testSubstitute() { - assertEquals("This is a test!", this.bundle.getString("test.plain", variables)); - assertEquals("", this.bundle.getString("test.empty", variables)); assertEquals("This is a not so nice test!", this.bundle.getString("nice", variables)); assertEquals("This is a not so nice, not so nice test!", this.bundle.getString("nice.nice", variables)); assertEquals("This is a not so nice but not too bad test!", this.bundle.getString("nice.bad", variables)); + assertEquals("This is a test!", this.bundle.getString("test.plain", variables)); + assertEquals("", this.bundle.getString("test.empty", variables)); + assertEquals("Here is an example of a cat.", this.bundle.getString("cat", this.variables)); } private static class TestBundle extends AbstractBundle { private Map store = new HashMap(); + + public void setLoadOnInit(boolean set) { + } - public void init(String fileName, boolean cacheAtStartup) throws Exception { + public void init(String fileName) throws Exception { + // do nothing } void put(String key, String value) { 1.1 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/test/DefaultBundleLoaderTestCase.java Index: DefaultBundleLoaderTestCase.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE file. */ package org.apache.avalon.excalibur.i18n.test; import java.util.Map; import java.util.HashMap; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.excalibur.testcase.ExcaliburTestCase; import org.apache.avalon.excalibur.i18n.BundleInfo; import org.apache.avalon.excalibur.i18n.XmlBundle; import org.apache.avalon.excalibur.i18n.BundleSelector; /** * @author <a href="mailto:[EMAIL PROTECTED]">Neeme Praks</a> * @version $Id: DefaultBundleLoaderTestCase.java,v 1.1 2001/10/31 19:56:47 neeme Exp $ */ public class DefaultBundleLoaderTestCase extends ExcaliburTestCase { private BundleSelector bundleSelector; public DefaultBundleLoaderTestCase( String name ) { super(name); } public void setUp() throws Exception { this.bundleSelector = (BundleSelector) manager.lookup(BundleSelector.ROLE); } public void testLoading() throws Exception { this.bundleSelector.select("ee", "uu"); } public static void main(String[] args) { ExcaliburTestCase test = new DefaultBundleLoaderTestCase("test"); test.run(); } } 1.1 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/test/DefaultBundleLoaderTestCase.xtest Index: DefaultBundleLoaderTestCase.xtest =================================================================== <?xml version="1.0" encoding="UTF-8"?> <testcase> <annotation><![CDATA[ <title>DefaultBundleLoader tests</title> <para> Tests the DefaultBundleLoader implementation: loading bundles, mapping correctly, etc. </para> ]]></annotation> <roles> <role name="org.apache.avalon.excalibur.xml.xpath.XPathProcessor" shorthand="xpath-processor" default-class="org.apache.avalon.excalibur.xml.xpath.XPathProcessorImpl"/> <role name="org.apache.avalon.excalibur.i18n.BundleSelector" shorthand="bundle-selector" default-class="org.apache.avalon.excalibur.i18n.BundleSelector"/> </roles> <components> <bundle-selector> <loaders> <loader type-name="xml" bundle="org.apache.avalon.excalibur.i18n.XmlBundle" class="org.apache.avalon.excalibur.i18n.DefaultBundleLoader" mapper="org.apache.avalon.excalibur.i18n.DefaultMapper"> <prefix>file:///root/path/to/xml/files/</prefix> <suffix>.xml</suffix> <load-on-startup> <!-- implementation specific parameters --> <all/> </load-on-startup> </loader> </loaders> <matchers default-type="xml"> <!--matcher class="org.apache.avalon.excalibur.i18n.DefaultMatcher" type="xml"/--> </matchers> </bundle-selector> </components> <configuration> </configuration> </testcase> 1.1 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/test/XmlBundle.xml Index: XmlBundle.xml =================================================================== <?xml version="1.0" encoding="UTF-8"?> <resources> <level1> <level2> <level3>Text on level3</level3> </level2> </level1> <levels> <level number="1">Flat level 1</level> <level number="2">Flat level 2</level> <level number="3">Flat level 3</level> </levels> <key-with-variable>Here is an example of {value}.</key-with-variable> </resources> 1.1 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/test/XmlBundleTestCase.java Index: XmlBundleTestCase.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE file. */ package org.apache.avalon.excalibur.i18n.test; import java.util.Map; import java.util.HashMap; import org.apache.avalon.excalibur.testcase.ExcaliburTestCase; import org.apache.avalon.excalibur.i18n.BundleInfo; import org.apache.avalon.excalibur.i18n.XmlBundle; /** * @author <a href="mailto:[EMAIL PROTECTED]">Neeme Praks</a> * @version $Id: XmlBundleTestCase.java,v 1.1 2001/10/31 19:56:47 neeme Exp $ */ public class XmlBundleTestCase extends ExcaliburTestCase { private Map variables = new HashMap(5); private XmlBundle bundle = new XmlBundle(); private XmlBundle bundleWithRoot = new XmlBundle(); public XmlBundleTestCase( String name ) { super(name); } public void setUp() throws Exception { this.variables.put("value", "a cat"); final String bundleFileName = this.getClass().getPackage().getName().replace( '.', '/' ) + "/XmlBundle.xml"; getLogger().debug("Test-bundle file = " + bundleFileName); this.bundle.setBundleInfo(new BundleInfo("test", null)); this.bundle.setLogger(getLogger()); this.bundle.compose(super.manager); this.bundle.setLoadOnInit(true); this.bundleWithRoot.setUseRootElement(false); this.bundle.init(this.getClass().getClassLoader().getResource(bundleFileName).openStream()); this.bundleWithRoot.setBundleInfo(new BundleInfo("test-with-root", null)); this.bundleWithRoot.setLogger(getLogger()); this.bundleWithRoot.compose(super.manager); this.bundleWithRoot.setLoadOnInit(true); this.bundleWithRoot.setUseRootElement(true); this.bundleWithRoot.init(this.getClass().getClassLoader().getResource(bundleFileName).openStream()); } public void testGetString() { assertEquals("Text on level3", this.bundle.getString("level1.level2.level3")); assertEquals("Flat level 2", this.bundle.getString("levels/[EMAIL PROTECTED]'2']")); assertEquals("Text on level3", this.bundleWithRoot.getString("resources.level1.level2.level3")); assertEquals("Flat level 2", this.bundleWithRoot.getString("resources.levels/[EMAIL PROTECTED]'2']")); } public void testGetStringWithVariable() { assertEquals("Here is an example of a cat.", this.bundle.getString("key-with-variable", this.variables)); assertEquals("Here is an example of a cat.", this.bundleWithRoot.getString("resources.key-with-variable", this.variables)); } } 1.1 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/i18n/test/XmlBundleTestCase.xtest Index: XmlBundleTestCase.xtest =================================================================== <?xml version="1.0" encoding="UTF-8"?> <testcase> <annotation><![CDATA[ <title>XmlResourceBundle tests</title> <para> Tests the XmlResourceBundle implementation: loading from file, returning correct values, substituting correctly, etc. </para> ]]></annotation> <roles> <role name="org.apache.avalon.excalibur.xml.xpath.XPathProcessor" shorthand="xpath-processor" default-class="org.apache.avalon.excalibur.xml.xpath.XPathProcessorImpl"/> </roles> </testcase>
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>