Author: dflorey Date: Mon Apr 30 03:55:31 2007 New Revision: 533694 URL: http://svn.apache.org/viewvc?view=rev&rev=533694 Log: Applied patches from Mattias Jiderhamn. Thanks!
Added: jakarta/commons/sandbox/i18n/trunk/src/test/org/apache/commons/i18n/MessageProviderTestBase.java Modified: jakarta/commons/sandbox/i18n/trunk/project.xml jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/XMLMessageProvider.java jakarta/commons/sandbox/i18n/trunk/src/resources/i18n-messages.properties jakarta/commons/sandbox/i18n/trunk/src/resources/i18n-messages_de.properties jakarta/commons/sandbox/i18n/trunk/src/test/messageBundle.properties jakarta/commons/sandbox/i18n/trunk/src/test/messageBundle_de.properties jakarta/commons/sandbox/i18n/trunk/src/test/org/apache/commons/i18n/JdbcMessageProviderTest.java jakarta/commons/sandbox/i18n/trunk/src/test/org/apache/commons/i18n/ResourceBundleMessageProviderTest.java jakarta/commons/sandbox/i18n/trunk/src/test/org/apache/commons/i18n/XMLMessageProviderTest.java jakarta/commons/sandbox/i18n/trunk/src/test/testMessages.xml jakarta/commons/sandbox/i18n/trunk/xdocs/examples.xml Modified: jakarta/commons/sandbox/i18n/trunk/project.xml URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/i18n/trunk/project.xml?view=diff&rev=533694&r1=533693&r2=533694 ============================================================================== --- jakarta/commons/sandbox/i18n/trunk/project.xml (original) +++ jakarta/commons/sandbox/i18n/trunk/project.xml Mon Apr 30 03:55:31 2007 @@ -75,6 +75,10 @@ </contributors> <dependencies> + <dependency> + <id>commons-resources</id> + <version>0.1-dev</version> + </dependency> <!-- Please note, these depenencies are needed for unit tests only! --> <dependency> <id>hsqldb</id> Modified: jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/XMLMessageProvider.java URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/XMLMessageProvider.java?view=diff&rev=533694&r1=533693&r2=533694 ============================================================================== --- jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/XMLMessageProvider.java (original) +++ jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/XMLMessageProvider.java Mon Apr 30 03:55:31 2007 @@ -56,9 +56,9 @@ applicationMessages.putAll(parsedMessages); messages.putAll(applicationMessages); } catch (Exception exception) { - // TODO: Fix message without ID - logger.log(Level.SEVERE,I18nUtils.INTERNAL_MESSAGES.getString(I18nUtils.MESSAGE_PARSING_ERROR),exception); - // TODO: Consider throwing exception + String msg = I18nUtils.INTERNAL_MESSAGES.getString(I18nUtils.MESSAGE_PARSING_ERROR); + logger.log(Level.SEVERE,msg,exception); + throw new RuntimeException(msg, exception); } } @@ -85,7 +85,7 @@ } if (message == null ) throw new MessageNotFoundException( MessageFormat.format( - I18nUtils.INTERNAL_MESSAGES.getString(I18nUtils.MESSAGE_NOT_FOUND), + I18nUtils.INTERNAL_MESSAGES.getString(I18nUtils.NO_MESSAGE_ENTRIES_FOUND), new String[] { id })); return message; } Modified: jakarta/commons/sandbox/i18n/trunk/src/resources/i18n-messages.properties URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/i18n/trunk/src/resources/i18n-messages.properties?view=diff&rev=533694&r1=533693&r2=533694 ============================================================================== --- jakarta/commons/sandbox/i18n/trunk/src/resources/i18n-messages.properties (original) +++ jakarta/commons/sandbox/i18n/trunk/src/resources/i18n-messages.properties Mon Apr 30 03:55:31 2007 @@ -1,5 +1,4 @@ -messageNotFound=Message with key {0} not found noMessageEntriesFound=No message entries found for bundle with key {0} messageEntryNotFound=Message bundle with key {0} does not contain an entry with key {1} resourceBundleNotFound=Could not find resource bundle with base name {0}, uninstalling it -messageParsingError=Error while parsing message file with id {0} \ No newline at end of file +messageParsingError=Error while parsing message file Modified: jakarta/commons/sandbox/i18n/trunk/src/resources/i18n-messages_de.properties URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/i18n/trunk/src/resources/i18n-messages_de.properties?view=diff&rev=533694&r1=533693&r2=533694 ============================================================================== --- jakarta/commons/sandbox/i18n/trunk/src/resources/i18n-messages_de.properties (original) +++ jakarta/commons/sandbox/i18n/trunk/src/resources/i18n-messages_de.properties Mon Apr 30 03:55:31 2007 @@ -1,5 +1,4 @@ -messageNotFound=Keine Nachricht mit Schlüssel {0} gefunden noMessageEntriesFound=Keine Einträge für die Nachricht mit Schlüssel {0} gefunden messageEntryNotFound=Die Nachricht mit Schlüssel {0} enthält keinen Eintrag mit Schlüssel {1} resourceBundleNotFound=Übersetzungsdatei mit dem Namen {0} wurde nicht gefunden und wird daher deinstalliert -messageParsingError=Fehler beim Einlesen der Übersetzungsdatei mit der ID {0} \ No newline at end of file +messageParsingError=Fehler beim Einlesen der Übersetzungsdatei Modified: jakarta/commons/sandbox/i18n/trunk/src/test/messageBundle.properties URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/i18n/trunk/src/test/messageBundle.properties?view=diff&rev=533694&r1=533693&r2=533694 ============================================================================== --- jakarta/commons/sandbox/i18n/trunk/src/test/messageBundle.properties (original) +++ jakarta/commons/sandbox/i18n/trunk/src/test/messageBundle.properties Mon Apr 30 03:55:31 2007 @@ -1,4 +1,4 @@ # Example for using message bundles using a flat properties-file helloWorld.title=Hello World -helloWorld.text=I wish you a merry christmas! +helloWorld.text=Hello World, we are in {0}. helloWorld.notTranslated=This entry is not translated to any other languages Modified: jakarta/commons/sandbox/i18n/trunk/src/test/messageBundle_de.properties URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/i18n/trunk/src/test/messageBundle_de.properties?view=diff&rev=533694&r1=533693&r2=533694 ============================================================================== --- jakarta/commons/sandbox/i18n/trunk/src/test/messageBundle_de.properties (original) +++ jakarta/commons/sandbox/i18n/trunk/src/test/messageBundle_de.properties Mon Apr 30 03:55:31 2007 @@ -1,3 +1,3 @@ # Example for using message bundles using a flat properties-file helloWorld.title=Hallo Welt -helloWorld.text=Ich wünsche Dir alles Gute und ein frohes Fest! \ No newline at end of file +helloWorld.text=Hallo Welt, wir sind in {0}. Modified: jakarta/commons/sandbox/i18n/trunk/src/test/org/apache/commons/i18n/JdbcMessageProviderTest.java URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/i18n/trunk/src/test/org/apache/commons/i18n/JdbcMessageProviderTest.java?view=diff&rev=533694&r1=533693&r2=533694 ============================================================================== --- jakarta/commons/sandbox/i18n/trunk/src/test/org/apache/commons/i18n/JdbcMessageProviderTest.java (original) +++ jakarta/commons/sandbox/i18n/trunk/src/test/org/apache/commons/i18n/JdbcMessageProviderTest.java Mon Apr 30 03:55:31 2007 @@ -1,9 +1,24 @@ -package org.apache.commons.i18n; +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ -import junit.framework.TestCase; +package org.apache.commons.i18n; -import java.sql.DriverManager; import java.sql.Connection; +import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.Locale; @@ -15,15 +30,14 @@ /** * @author Mattias Jiderhamn */ -public class JdbcMessageProviderTest extends TestCase { +public class JdbcMessageProviderTest extends MessageProviderTestBase { private static Connection getNewConnection() throws SQLException { return DriverManager.getConnection("jdbc:hsqldb:.", "sa", ""); // Connect to in-memory database } public void setUp() throws Exception { - /* Make sure en_US is the default Locale for tests */ - Locale.setDefault(Locale.US); + super.setUp(); Class.forName("org.hsqldb.jdbcDriver"); // Load HSQLDB database driver Connection conn = getNewConnection(); @@ -44,7 +58,7 @@ stmt.execute( "INSERT INTO messages VALUES (" + " 'helloWorld', 'de', " + - " 'Hallo Welt', 'Ich wünsche Dir alles Gute und ein frohes Fest!'" + + " 'Hallo Welt', 'Ich wünsche Dir alles Gute und ein frohes Fest!'" + ")" ); stmt.close(); @@ -52,6 +66,7 @@ } public void tearDown() throws Exception { + super.tearDown(); Connection conn = getNewConnection(); conn.createStatement().execute( "DROP TABLE messages" @@ -73,7 +88,7 @@ dataSource.setPassword(""); jdbcMessageProvider = new JdbcMessageProvider(dataSource, "messages", "id", "language"); assertEquals("Hello World", jdbcMessageProvider.getText("helloWorld", "title", Locale.ENGLISH)); - + // Map/Properties constructor Properties props = new Properties(); props.setProperty("jdbc.connect.driver", "org.hsqldb.jdbcDriver"); @@ -111,16 +126,16 @@ assertEquals("I wish you a merry christmas!", jdbcMessageProvider.getText("helloWorld", "text", scottish)); assertEquals("Hallo Welt", jdbcMessageProvider.getText("helloWorld", "title", Locale.GERMAN)); - assertEquals("Ich wünsche Dir alles Gute und ein frohes Fest!", jdbcMessageProvider.getText("helloWorld", "text", Locale.GERMAN)); + assertEquals("Ich wünsche Dir alles Gute und ein frohes Fest!", jdbcMessageProvider.getText("helloWorld", "text", Locale.GERMAN)); // Default locale with country assertEquals("Hallo Welt", jdbcMessageProvider.getText("helloWorld", "title", Locale.GERMANY)); - assertEquals("Ich wünsche Dir alles Gute und ein frohes Fest!", jdbcMessageProvider.getText("helloWorld", "text", Locale.GERMANY)); + assertEquals("Ich wünsche Dir alles Gute und ein frohes Fest!", jdbcMessageProvider.getText("helloWorld", "text", Locale.GERMANY)); // Test use of defaule assertEquals("Hello World", jdbcMessageProvider.getText("helloWorld", "title", Locale.JAPANESE)); assertEquals("I wish you a merry christmas!", jdbcMessageProvider.getText("helloWorld", "text", Locale.JAPANESE)); - + // Test non-existent assertNull(jdbcMessageProvider.getText("helloWorld", "foobar", Locale.ENGLISH)); assertNull(jdbcMessageProvider.getText("foo", "bar", Locale.ENGLISH)); @@ -153,13 +168,13 @@ entries = jdbcMessageProvider.getEntries("helloWorld", Locale.GERMAN); assertEquals("No of entries", 2, entries.size()); assertEquals("Hallo Welt", (String)entries.get("title")); - assertEquals("Ich wünsche Dir alles Gute und ein frohes Fest!", (String)entries.get("text")); + assertEquals("Ich wünsche Dir alles Gute und ein frohes Fest!", (String)entries.get("text")); // Default locale with country entries = jdbcMessageProvider.getEntries("helloWorld", Locale.GERMANY); assertEquals("No of entries", 2, entries.size()); assertEquals("Hallo Welt", (String)entries.get("title")); - assertEquals("Ich wünsche Dir alles Gute und ein frohes Fest!", (String)entries.get("text")); + assertEquals("Ich wünsche Dir alles Gute und ein frohes Fest!", (String)entries.get("text")); // Test use of defaule entries = jdbcMessageProvider.getEntries("helloWorld", Locale.JAPANESE); Added: jakarta/commons/sandbox/i18n/trunk/src/test/org/apache/commons/i18n/MessageProviderTestBase.java URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/i18n/trunk/src/test/org/apache/commons/i18n/MessageProviderTestBase.java?view=auto&rev=533694 ============================================================================== --- jakarta/commons/sandbox/i18n/trunk/src/test/org/apache/commons/i18n/MessageProviderTestBase.java (added) +++ jakarta/commons/sandbox/i18n/trunk/src/test/org/apache/commons/i18n/MessageProviderTestBase.java Mon Apr 30 03:55:31 2007 @@ -0,0 +1,222 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.commons.i18n; + +import junit.framework.TestCase; + +import java.util.Locale; +import java.util.Map; + +/** + * This class tests that the behaviour of a <code>MessageProvider</code> implementation is correct. + * If creating new implementations, consider subclassing this class to test the behaviour of that implementation. + * The tests assume the provided <code>MessageProvider</code> + * contains the following entries: + * <p /> + * <table> + * <tr> + * <th>Language/<code>Locale</code></th> + * <th>ID</th> + * <th>Entry</th> + * <th>Message</th> + * </tr> + * <tr> + * <td>English (en)</td> + * <td>helloWorld</td> + * <td>title</td> + * <td>Hello World</td> + * <tr> + * <tr> + * <td>English (en)</td> + * <td>helloWorld</td> + * <td>text</td> + * <td>Hello World, we are in {0}.</td> + * <tr> + * <tr> + * <td>German (de)</td> + * <td>helloWorld</td> + * <td>title</td> + * <td>Hallo Welt</td> + * <tr> + * <tr> + * <td>German (de)</td> + * <td>helloWorld</td> + * <td>text</td> + * <td>Hallo Welt, wir sind in {0}.</td> + * <tr> + * <tr> + * <td colspan="4"><i>The entry below is used only if possible. If not possible, set + * <code>hasNonTranslatedEntry</code> to false.</i></td> + * <tr> + * <tr> + * <td>English (en)</td> + * <td>helloWorld</td> + * <td>helloWorld</td> + * <td>This entry is not translated to any other languages</td> + * <tr> + * </table> + * <p /> + * Specifically, the ID <code>nonExistentId</code> and the entry <code>nonExistentEntry</code> of ID + * <code>helloWorld</code> must NOT be existent. + */ +public abstract class MessageProviderTestBase extends TestCase { + + protected MessageProviderTestBase() { + } + + protected MessageProviderTestBase(String testName) { + super(testName); + } + + /** + * Set English as default Locale. + * If overridden, please remember to call <code>super.setUp()</code> + * @throws Exception No exception is thrown, but allow for overriding methods + * to throw exceptions. + */ + public void setUp() throws Exception { + /* Make sure en_US is the default Locale for tests */ + Locale.setDefault(Locale.ENGLISH); + } + + public void tearDown() throws Exception { + /* Uninstall resource bundles after every test */ + MessageManager.clearMessageProviders(); + } + + /** + * Test functionality of getText() method, which should be common for all implementations of the + * <code>MessageProvider</code> interface. + * @param messageProvider + */ + protected void testGetText(MessageProvider messageProvider) { + // Explicit default locale + assertEnglishTexts(messageProvider, Locale.ENGLISH); + + // Default locale with country + assertEnglishTexts(messageProvider, Locale.US); + + // Default locale with variant + assertEnglishTexts(messageProvider, new Locale("en", "", "scottish")); + + // Default locale with country and variant + assertEnglishTexts(messageProvider, new Locale("en", "GB", "scottish")); + + // Non-default locale + assertGermanTexts(messageProvider, Locale.GERMAN); + + // Non-default locale with country + assertGermanTexts(messageProvider, Locale.GERMANY); + + // Non-default locale with variant + assertGermanTexts(messageProvider, new Locale("de", "", "foo")); + + // Non-default locale with country and variant + assertGermanTexts(messageProvider, new Locale("de", "CH", "foo")); + + // Fallback to default + assertEnglishTexts(messageProvider, Locale.JAPANESE); + + // Non-existent entry + // try { + assertNull(messageProvider.getText("helloWorld", "nonExistentEntry", Locale.ENGLISH)); + // } + // catch(MessageNotFoundException mnfex) { + // assertEquals("Message bundle with key helloWorld does not contain an entry with key nonExistentEntry", mnfex.getMessage()); + // } + + // Non-existent id + try { + assertNull(messageProvider.getText("nonExistentId", "foo", Locale.ENGLISH)); + } + catch(MessageNotFoundException mnfex) { + assertEquals("No message entries found for bundle with key nonExistentId", mnfex.getMessage()); // TODO: JDBC + } + } + + private void assertEnglishTexts(MessageProvider messageProvider, Locale locale) { + assertEquals("Locale = " + locale + ", title", + "Hello World", messageProvider.getText("helloWorld", "title", locale)); + assertEquals("Locale = " + locale + ", text", + "Hello World, we are in {0}.", messageProvider.getText("helloWorld", "text", locale)); + } + + private void assertGermanTexts(MessageProvider messageProvider, Locale locale) { + assertEquals("Locale = " + locale + ", title", + "Hallo Welt", messageProvider.getText("helloWorld", "title", locale)); + assertEquals("Locale = " + locale + ", text", + "Hallo Welt, wir sind in {0}.", messageProvider.getText("helloWorld", "text", locale)); + } + + // TODO: Document + protected void testGetEntries(MessageProvider messageProvider, boolean hasNonTranslatedEntry) { + // TODO: Complete from above + + // Explicit default locale + assertEnglishEntries(messageProvider, Locale.ENGLISH, hasNonTranslatedEntry); + + // Default locale with country + assertEnglishEntries(messageProvider, Locale.US, hasNonTranslatedEntry); + + // Default locale with country and variant + assertEnglishEntries(messageProvider, new Locale("en", "", "scottish"), hasNonTranslatedEntry); + + assertGermanEntries(messageProvider, Locale.GERMAN); + + // Default locale with country + assertGermanEntries(messageProvider, Locale.GERMANY); + + // Test use of defaule + assertEnglishEntries(messageProvider, Locale.JAPANESE, hasNonTranslatedEntry); + + // Non-existent id + try { + messageProvider.getEntries("nonExistentId", Locale.ENGLISH); + fail("Non-existent ID should cause exception"); + } + catch(MessageNotFoundException mnfex) { + assertEquals("No message entries found for bundle with key nonExistentId", mnfex.getMessage()); + } + } + + private static void assertEnglishEntries(MessageProvider messageProvider, Locale locale, boolean hasNonTranslatedEntry) { + Map entries = messageProvider.getEntries("helloWorld", locale); + if(hasNonTranslatedEntry) { + assertEquals("Locale = " + locale + ", No of entries", 3, entries.size()); + assertEquals("Locale = " + locale, "This entry is not translated to any other languages", + entries.get("notTranslated")); + } + else + assertEquals("Locale = " + locale + ", No of entries", 2, entries.size()); + assertEquals("Locale = " + locale, "Hello World", (String)entries.get("title")); + assertEquals("Locale = " + locale, "Hello World, we are in {0}.", entries.get("text")); + } + + private static void assertGermanEntries(MessageProvider messageProvider, Locale locale) { + Map entries = messageProvider.getEntries("helloWorld", locale); + + // TODO: Consider whether all MessageProviders must behave the same way + if(entries.size() == 3) // If non-translated entries included + assertEquals("This entry is not translated to any other languages", entries.get("notTranslated")); + else if(entries.size() != 2) + fail("No of entries should be 2 or 3!"); + + assertEquals("Hallo Welt", (String)entries.get("title")); + assertEquals("Hallo Welt, wir sind in {0}.", entries.get("text")); + } +} Modified: jakarta/commons/sandbox/i18n/trunk/src/test/org/apache/commons/i18n/ResourceBundleMessageProviderTest.java URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/i18n/trunk/src/test/org/apache/commons/i18n/ResourceBundleMessageProviderTest.java?view=diff&rev=533694&r1=533693&r2=533694 ============================================================================== --- jakarta/commons/sandbox/i18n/trunk/src/test/org/apache/commons/i18n/ResourceBundleMessageProviderTest.java (original) +++ jakarta/commons/sandbox/i18n/trunk/src/test/org/apache/commons/i18n/ResourceBundleMessageProviderTest.java Mon Apr 30 03:55:31 2007 @@ -23,29 +23,17 @@ import java.util.Locale; import java.util.Map; -import junit.framework.TestCase; - import org.apache.commons.i18n.bundles.MessageBundle; /** * @author Daniel Florey * */ -public class ResourceBundleMessageProviderTest extends TestCase { +public class ResourceBundleMessageProviderTest extends MessageProviderTestBase { public ResourceBundleMessageProviderTest(String testName) { super(testName); } - public void setUp() { - /* Make sure en_US is the default Locale for tests */ - Locale.setDefault(Locale.US); - } - - public void tearDown() { - /* Uninstall resource bundles after every test */ - MessageManager.clearMessageProviders(); - } - public void testInstallResourceBundle() { MessageBundle testMessage = new MessageBundle("helloWorld"); @@ -75,23 +63,17 @@ public void testGetText() { ResourceBundleMessageProvider rbmp = new ResourceBundleMessageProvider("messageBundle"); - assertEquals("Default locale", "Hello World", rbmp.getText("helloWorld", "title", Locale.US)); - assertEquals("Additional locale", "Hallo Welt", rbmp.getText("helloWorld", "title", Locale.GERMAN)); - assertEquals("Fallback locale", "Hello World", rbmp.getText("helloWorld", "title", Locale.FRENCH)); + super.testGetText(rbmp); + + // TODO: Wait for Daniels reply on whether this is intended assertEquals("Fallback when only in default", "This entry is not translated to any other languages", rbmp.getText("helloWorld", "notTranslated", Locale.GERMAN)); // Test with list resource bundle -// ResourceBundleMessageProvider.uninstall("messageBundle"); // Remove -// ResourceBundleMessageProvider.install("org.apache.commons.i18n.MyListResourceBundle"); // Install ListResourceBundle ResourceBundleMessageProvider listResourceBundleProvider = new ResourceBundleMessageProvider("org.apache.commons.i18n.MyListResourceBundle"); // Install ListResourceBundle assertEquals("Value from ListResourceBundle", "listResourceValue", listResourceBundleProvider.getText("helloWorld", "title", Locale.US)); assertEquals("Value from ListResourceBundle", "1", listResourceBundleProvider.getText("helloWorld", "text", Locale.US)); - - assertNull(rbmp.getText("nonExistentId", "nonExistentEntry", Locale.US)); - - assertNull(rbmp.getText("helloWorld", "nonExistentEntry", Locale.US)); try { new ResourceBundleMessageProvider("nonExistentBundle"); Modified: jakarta/commons/sandbox/i18n/trunk/src/test/org/apache/commons/i18n/XMLMessageProviderTest.java URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/i18n/trunk/src/test/org/apache/commons/i18n/XMLMessageProviderTest.java?view=diff&rev=533694&r1=533693&r2=533694 ============================================================================== --- jakarta/commons/sandbox/i18n/trunk/src/test/org/apache/commons/i18n/XMLMessageProviderTest.java (original) +++ jakarta/commons/sandbox/i18n/trunk/src/test/org/apache/commons/i18n/XMLMessageProviderTest.java Mon Apr 30 03:55:31 2007 @@ -21,27 +21,14 @@ package org.apache.commons.i18n; import java.util.Locale; -import java.util.Map; import org.apache.commons.i18n.bundles.MessageBundle; -import junit.framework.TestCase; - /** * @author Daniel Florey * */ -public class XMLMessageProviderTest extends TestCase { - - public void setUp() { - /* Make sure en_US is the default Locale for tests */ - Locale.setDefault(Locale.US); - } - - public void tearDown() { - /* Uninstall resource bundles after every test */ - MessageManager.clearMessageProviders(); - } +public class XMLMessageProviderTest extends MessageProviderTestBase { public void testInstallResourceBundle() { MessageBundle testMessage = new MessageBundle("helloWorld"); @@ -70,38 +57,26 @@ } // Try to parse non-XML file - new XMLMessageProvider(Thread.currentThread().getContextClassLoader().getResourceAsStream("messageBundle.properties")); + try { + new XMLMessageProvider( + Thread.currentThread().getContextClassLoader().getResourceAsStream("messageBundle.properties")); + fail("Parsing non-XML file should fail"); + } + catch(RuntimeException rtex) { + assertEquals("Error while parsing message file", rtex.getMessage()); + } } public void testGetText() { -// XMLMessageProvider.install("org.apache.commons-i18n.test", -// Thread.currentThread().getContextClassLoader().getResourceAsStream("testMessages.xml")); XMLMessageProvider xmlmp = new XMLMessageProvider( Thread.currentThread().getContextClassLoader().getResourceAsStream("testMessages.xml")); - assertEquals("Default locale", "hello world", xmlmp.getText("helloWorld", "title", Locale.US)); - assertEquals("Default locale", "hello world", xmlmp.getText("helloWorld", "title", Locale.UK)); - assertEquals("Additional locale", "Hallo Welt", xmlmp.getText("helloWorld", "title", Locale.GERMAN)); - assertEquals("Language and country using parent", "Hallo Welt", xmlmp.getText("helloWorld", "title", - new Locale("de", "CH"))); - assertEquals("Language, country and variant using parent", "Hallo Welt", xmlmp.getText("helloWorld", "title", - new Locale("de", "CH", "foo"))); - assertEquals("Fallback locale", "hello world", xmlmp.getText("helloWorld", "title", Locale.JAPANESE)); + super.testGetText(xmlmp); + // TODO: Wait for Daniels reply on whether this is intended // assertEquals("Fallback when only in default", "This entry is not translated to any other languages", // xmlmp.getText("helloWorld", "notTranslated", Locale.GERMAN)); -// ResourceBundleMessageProvider.install("messageBundle2"); // Install another bundle -// assertEquals("This message exists in another resource bundle", xmlmp.getText("onlyInSecond", "title", Locale.US)); - - try { - xmlmp.getText("nonExistentId", "nonExistentEntry", Locale.US); - fail("ID does not exist, should throw exception"); - } - catch(MessageNotFoundException mnfex) { - assertEquals("Message with key nonExistentId not found", mnfex.getMessage()); - } - // TODO: Wait for Daniels reply on whether this is intended /* try { @@ -115,8 +90,6 @@ } public void testGetTextVariants() { -// XMLMessageProvider.install("org.apache.commons-i18n.variants", -// Thread.currentThread().getContextClassLoader().getResourceAsStream("variantTestMessages.xml")); XMLMessageProvider xmlmp = new XMLMessageProvider( Thread.currentThread().getContextClassLoader().getResourceAsStream("variantTestMessages.xml")); @@ -127,48 +100,10 @@ } public void testGetEntries() { -// XMLMessageProvider.install("org.apache.commons-i18n.test", -// Thread.currentThread().getContextClassLoader().getResourceAsStream("testMessages.xml")); - Map usEntries = new XMLMessageProvider( - Thread.currentThread().getContextClassLoader().getResourceAsStream("testMessages.xml")). - getEntries("helloWorld", Locale.US); - assertEquals("Default locale, no of entries", 5, usEntries.size()); - assertEquals("Default locale, titel", "hello world", usEntries.get("title")); - assertEquals("Default locale, text", "hello world, we are in {0}.", usEntries.get("text")); - assertEquals("Default locale, text", - "sample summary to test english messages. Country = {0}, language = {1} and variant = {2}.", - usEntries.get("summary")); - assertEquals("Default locale, text", - "sample deatils to test english messages. Country = {0}, language = {1} and variant = {2}.", - usEntries.get("details")); - assertEquals("This entry is not translated to any other languages (XML)", usEntries.get("notTranslated")); - - Map germanEntries = new XMLMessageProvider( - Thread.currentThread().getContextClassLoader().getResourceAsStream("testMessages.xml")). - getEntries("helloWorld", Locale.GERMAN); - assertEquals("No of entries", 4, germanEntries.size()); - assertEquals("Hallo Welt", germanEntries.get("title")); - assertEquals("Wir sind in {0}.", germanEntries.get("text")); - assertEquals("sample summary to test german messages. Country = {0}, language = {1} and variant = {2}.", - germanEntries.get("summary")); - assertEquals("sample deatils to test german messages. Country = {0}, language = {1} and variant = {2}.", - germanEntries.get("details")); -// assertEquals("This entry is not translated to any other languages", germanEntries.get("notTranslated")); - - Map japaneseEntries = new XMLMessageProvider( - Thread.currentThread().getContextClassLoader().getResourceAsStream("testMessages.xml")). - getEntries("helloWorld", Locale.JAPANESE); - assertEquals("Fallback locale, no of entries", 5, japaneseEntries.size()); - - assertEquals("Fallback locale, titel", "hello world", usEntries.get("title")); - assertEquals("Fallback locale, text", "hello world, we are in {0}.", japaneseEntries.get("text")); - assertEquals("Fallback locale, text", - "sample summary to test english messages. Country = {0}, language = {1} and variant = {2}.", - japaneseEntries.get("summary")); - assertEquals("Fallback locale, text", - "sample deatils to test english messages. Country = {0}, language = {1} and variant = {2}.", - japaneseEntries.get("details")); - assertEquals("This entry is not translated to any other languages (XML)", japaneseEntries.get("notTranslated")); + final XMLMessageProvider xmlMessageProvider = new XMLMessageProvider( + Thread.currentThread().getContextClassLoader().getResourceAsStream("testMessages.xml")); + + super.testGetEntries(xmlMessageProvider, true); } /** Modified: jakarta/commons/sandbox/i18n/trunk/src/test/testMessages.xml URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/i18n/trunk/src/test/testMessages.xml?view=diff&rev=533694&r1=533693&r2=533694 ============================================================================== --- jakarta/commons/sandbox/i18n/trunk/src/test/testMessages.xml (original) +++ jakarta/commons/sandbox/i18n/trunk/src/test/testMessages.xml Mon Apr 30 03:55:31 2007 @@ -3,47 +3,47 @@ <!-- Feel free to insert your locale or correct mispellings here --> <message id="helloWorld"> <locale language="en"> - <entry key="title">hello world</entry> - <entry key="text">hello world, we are in {0}.</entry> - <entry key="summary">sample summary to test english messages. Country = {0}, language = {1} and variant = {2}.</entry> - <entry key="details">sample deatils to test english messages. Country = {0}, language = {1} and variant = {2}.</entry> - <entry key="notTranslated">This entry is not translated to any other languages (XML)</entry> + <entry key="title">Hello World</entry> + <entry key="text">Hello World, we are in {0}.</entry> +<!-- <entry key="summary">sample summary to test english messages. Country = {0}, language = {1} and variant = {2}.</entry>--> +<!-- <entry key="details">sample deatils to test english messages. Country = {0}, language = {1} and variant = {2}.</entry>--> + <entry key="notTranslated">This entry is not translated to any other languages</entry> </locale> <locale language="pt" country="br"> <entry key="title">olo mundo</entry> <entry key="text">hello mundo, nós estamos no {0}.</entry> - <entry key="summary">sample summary to test portuguese messages. Country = {0}, language = {1} and variant = {2}.</entry> - <entry key="details">sample deatils to test portuguese messages. Country = {0}, language = {1} and variant = {2}.</entry> +<!-- <entry key="summary">sample summary to test portuguese messages. Country = {0}, language = {1} and variant = {2}.</entry>--> +<!-- <entry key="details">sample deatils to test portuguese messages. Country = {0}, language = {1} and variant = {2}.</entry>--> </locale> <locale language="es"> <entry key="title">hola mundo</entry> <entry key="text">hola mundo, estamos em los {0}.</entry> - <entry key="summary">sample summary to test spanish messages. Country = {0}, language = {1} and variant = {2}.</entry> - <entry key="details">sample deatils to test spanish messages. Country = {0}, language = {1} and variant = {2}.</entry> +<!-- <entry key="summary">sample summary to test spanish messages. Country = {0}, language = {1} and variant = {2}.</entry>--> +<!-- <entry key="details">sample deatils to test spanish messages. Country = {0}, language = {1} and variant = {2}.</entry>--> </locale> <locale language="it"> <entry key="title">ciao mondo</entry> <entry key="text">ciao mondo, siamo negli {0}.</entry> - <entry key="summary">sample summary to test italian messages. Country = {0}, language = {1} and variant = {2}.</entry> - <entry key="details">sample deatils to test italian messages. Country = {0}, language = {1} and variant = {2}.</entry> +<!-- <entry key="summary">sample summary to test italian messages. Country = {0}, language = {1} and variant = {2}.</entry>--> +<!-- <entry key="details">sample deatils to test italian messages. Country = {0}, language = {1} and variant = {2}.</entry>--> </locale> <locale language="fr"> <entry key="title">bonjour monde</entry> <entry key="text">bonjour monde, nous sommes aux {0}.</entry> - <entry key="summary">sample summary to test francais messages. Country = {0}, language = {1} and variant = {2}.</entry> - <entry key="details">sample deatils to test francais messages. Country = {0}, language = {1} and variant = {2}.</entry> +<!-- <entry key="summary">sample summary to test francais messages. Country = {0}, language = {1} and variant = {2}.</entry>--> +<!-- <entry key="details">sample deatils to test francais messages. Country = {0}, language = {1} and variant = {2}.</entry>--> </locale> <locale language="du"> <entry key="title">hello wereld</entry> <entry key="text">hello wereld, zijn wij in de {0}.</entry> - <entry key="summary">sample summary to test dutch messages. Country = {0}, language = {1} and variant = {2}.</entry> - <entry key="details">sample deatils to test dutch messages. Country = {0}, language = {1} and variant = {2}.</entry> +<!-- <entry key="summary">sample summary to test dutch messages. Country = {0}, language = {1} and variant = {2}.</entry>--> +<!-- <entry key="details">sample deatils to test dutch messages. Country = {0}, language = {1} and variant = {2}.</entry>--> </locale> <locale language="de"> <entry key="title">Hallo Welt</entry> - <entry key="text">Wir sind in {0}.</entry> - <entry key="summary">sample summary to test german messages. Country = {0}, language = {1} and variant = {2}.</entry> - <entry key="details">sample deatils to test german messages. Country = {0}, language = {1} and variant = {2}.</entry> + <entry key="text">Hallo Welt, wir sind in {0}.</entry> +<!-- <entry key="summary">sample summary to test german messages. Country = {0}, language = {1} and variant = {2}.</entry>--> +<!-- <entry key="details">sample deatils to test german messages. Country = {0}, language = {1} and variant = {2}.</entry>--> </locale> </message> </messages> Modified: jakarta/commons/sandbox/i18n/trunk/xdocs/examples.xml URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/i18n/trunk/xdocs/examples.xml?view=diff&rev=533694&r1=533693&r2=533694 ============================================================================== --- jakarta/commons/sandbox/i18n/trunk/xdocs/examples.xml (original) +++ jakarta/commons/sandbox/i18n/trunk/xdocs/examples.xml Mon Apr 30 03:55:31 2007 @@ -176,8 +176,8 @@ ")"); stmt.execute("INSERT INTO messages VALUES ('enterFirstName', 'en', 'Please enter your first name')"); stmt.execute("INSERT INTO messages VALUES ('enterLastName', 'en', 'Please enter your last name')"); - stmt.execute("INSERT INTO messages VALUES ('enterFirstName', 'sv', 'Vänligen ange ditt förnamn')"); - stmt.execute("INSERT INTO messages VALUES ('enterLastName', 'sv', 'Vänligen ange ditt efternamn')"); + stmt.execute("INSERT INTO messages VALUES ('enterFirstName', 'sv', 'Vänligen ange ditt förnamn')"); + stmt.execute("INSERT INTO messages VALUES ('enterLastName', 'sv', 'Vänligen ange ditt efternamn')"); stmt.execute( "CREATE TABLE errors ( " + " 'id' VARCHAR(30), " + @@ -191,7 +191,7 @@ ")"); stmt.execute("INSERT INTO errors VALUES (" + " 'identicalNames', 'sv', " + - " 'Fel! Identiska namn.', 'Du angav samma namn som bÃ¥de förnamn och efternamn'" + + " 'Fel! Identiska namn.', 'Du angav samma namn som både förnamn och efternamn'" + ")"); stmt.close(); conn.close(); --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]