01: /*
02: * Copyright 2007 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.outerj.daisy.xmlutil.test;
17:
18: import junit.framework.TestCase;
19: import org.outerj.daisy.i18n.impl.AggregateResourceBundle;
20: import org.outerj.daisy.i18n.impl.DResourceBundleFactory;
21: import org.outerj.daisy.i18n.DResourceBundle;
22: import org.xml.sax.InputSource;
23: import org.xml.sax.SAXException;
24:
25: import javax.xml.parsers.ParserConfigurationException;
26: import java.io.ByteArrayInputStream;
27: import java.io.IOException;
28: import java.util.Locale;
29:
30: public class DResourceBundleTest extends TestCase {
31: public void testResourceBundle() throws Exception {
32: // Simple test of fallback between languages and bundles
33: String bundle1En = "<catalogue><message key='hi'>Hi!</message></catalogue>";
34: String bundle1Nl = "<catalogue><message key='hi'>Hallo!</message> <message key='hond'>Hond</message> </catalogue>";
35: String bundle1NlBe = "<catalogue><message key='hi'>Hallo!!</message></catalogue>";
36:
37: String bundle2En = "<catalogue><message key='cow'>Cow</message></catalogue>";
38: String bundle2Nl = "<catalogue><message key='koe'>Koe</message></catalogue>";
39:
40: AggregateResourceBundle arb = new AggregateResourceBundle(
41: new String[] { "bundle1", "bundle2" });
42:
43: arb
44: .addBundle("bundle1", new Locale(""),
45: buildBundle(bundle1En));
46: arb.addBundle("bundle1", new Locale("nl"),
47: buildBundle(bundle1Nl));
48: arb.addBundle("bundle1", new Locale("nl", "BE"),
49: buildBundle(bundle1NlBe));
50: arb
51: .addBundle("bundle2", new Locale(""),
52: buildBundle(bundle2En));
53: arb.addBundle("bundle2", new Locale("nl"),
54: buildBundle(bundle2Nl));
55:
56: // test exact matches
57: assertEquals("Hallo!", arb.get(new Locale("nl"), "hi")
58: .getText());
59: assertEquals("Hallo!!", arb.get(new Locale("nl", "BE"), "hi")
60: .getText());
61:
62: // fallback between locales
63: assertEquals("Hi!", arb.get(new Locale("en"), "hi").getText());
64: assertEquals("Hond", arb.get(new Locale("nl", "BE"), "hond")
65: .getText());
66:
67: // fallback between locales and bundles
68: assertEquals("Cow", arb.get(new Locale("en"), "cow").getText());
69:
70: // test non existing key behaviour
71: assertNull(arb.get(new Locale("en"), "nonExistingKey"));
72: }
73:
74: private DResourceBundle buildBundle(String content)
75: throws IOException, ParserConfigurationException,
76: SAXException {
77: return DResourceBundleFactory.build(new InputSource(
78: new ByteArrayInputStream(content.getBytes("UTF-8"))));
79: }
80: }
|