001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013: package org.rapla.components.xmlbundle.tests;
014:
015: import java.util.Locale;
016:
017: import junit.framework.Test;
018: import junit.framework.TestSuite;
019:
020: import org.apache.avalon.framework.configuration.Configuration;
021: import org.apache.avalon.framework.configuration.DefaultConfiguration;
022: import org.apache.avalon.framework.logger.ConsoleLogger;
023: import org.apache.avalon.framework.logger.Logger;
024: import org.rapla.components.xmlbundle.I18nBundle;
025: import org.rapla.components.xmlbundle.LocaleSelector;
026: import org.rapla.components.xmlbundle.impl.I18nBundleImpl;
027: import org.rapla.components.xmlbundle.impl.LocaleSelectorImpl;
028: import org.rapla.framework.RaplaDefaultContext;
029:
030: public class I18nBundleImplTest extends AbstractI18nTest {
031: I18nBundleImpl i18n;
032: boolean useFile;
033: LocaleSelector localeSelector;
034: Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_WARN);
035:
036: public I18nBundleImplTest(String name) {
037: this (name, true);
038: }
039:
040: public I18nBundleImplTest(String name, boolean useFile) {
041: super (name);
042: this .useFile = useFile;
043: }
044:
045: protected void setUp() throws Exception {
046: DefaultConfiguration config = new DefaultConfiguration("i18n",
047: this .toString());
048: if (this .useFile) {
049: DefaultConfiguration child = new DefaultConfiguration(
050: "file", this .toString());
051: child.setValue("src/org/rapla/RaplaResources.xml");
052: config.addChild(child);
053: } else {
054: config.setAttribute("id", "org.rapla.RaplaResources");
055: }
056: i18n = create(config);
057: }
058:
059: private I18nBundleImpl create(Configuration config)
060: throws Exception {
061: I18nBundleImpl i18n;
062: RaplaDefaultContext context = new RaplaDefaultContext();
063: localeSelector = new LocaleSelectorImpl();
064: context.put(LocaleSelector.ROLE, localeSelector);
065: i18n = new I18nBundleImpl(context, config, new ConsoleLogger());
066: return i18n;
067: }
068:
069: protected void tearDown() {
070: i18n.dispose();
071: }
072:
073: public I18nBundle getI18n() {
074: return i18n;
075: }
076:
077: public static Test suite() {
078: TestSuite suite = new TestSuite();
079: // The first four test only succeed if the Resource Bundles are build.
080: suite
081: .addTest(new I18nBundleImplTest("testLocaleChanged",
082: false));
083: suite.addTest(new I18nBundleImplTest("testGetIcon", false));
084: suite.addTest(new I18nBundleImplTest("testGetString", false));
085: suite.addTest(new I18nBundleImplTest("testLocale", false));
086: /*
087: */
088: suite
089: .addTest(new I18nBundleImplTest("testInvalidConfig",
090: true));
091: suite
092: .addTest(new I18nBundleImplTest("testLocaleChanged",
093: true));
094: suite.addTest(new I18nBundleImplTest("testGetIcon", true));
095: suite.addTest(new I18nBundleImplTest("testGetString", true));
096: suite.addTest(new I18nBundleImplTest("testLocale", true));
097:
098: return suite;
099: }
100:
101: public void testLocaleChanged() {
102: localeSelector.setLocale(new Locale("de", "DE"));
103: assertEquals(getI18n().getString("cancel"), "Abbrechen");
104: localeSelector.setLocale(new Locale("en", "DE"));
105: assertEquals(getI18n().getString("cancel"), "cancel");
106: }
107:
108: public void testInvalidConfig() throws Exception {
109: if (!this .useFile)
110: return;
111: DefaultConfiguration config = new DefaultConfiguration("i18n",
112: this .toString());
113: try {
114: create(config);
115: assertTrue("id is missing should be reported", true);
116: } catch (Exception ex) {
117: }
118: config.setAttribute("id", "org.rapla.RaplaResources");
119: DefaultConfiguration child = new DefaultConfiguration("file",
120: this .toString());
121: child.setValue("./src/org/rapla/RaplaResou");
122: config.addChild(child);
123: try {
124: create(config);
125: assertTrue("file ./src/org/rapla/RaplaResou should fail",
126: true);
127: } catch (Exception ex) {
128: }
129: }
130: }
|