1   // 
2   //Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   //All rights reserved. 
4   //   
5   //Redistribution and use in source and binary forms, with or without modification,  
6   //are permitted provided that the following conditions are met: 
7   //   
8   //* Redistributions of source code must retain the above copyright notice,  
9   //this list of conditions and the following disclaimer. 
10  //* Redistributions in binary form must reproduce the above copyright notice,  
11  //this list of conditions and the following disclaimer in the documentation  
12  //and/or other materials provided with the distribution. 
13  //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //nor the names of its contributors may be used to endorse or promote products  
15  //derived from this software without specific prior written permission. 
16  // 
17  //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  //POSSIBILITY OF SUCH DAMAGE. 
27  //
28  
29  package org.objectledge.i18n;
30  
31  import java.util.Locale;
32  
33  import javax.xml.parsers.DocumentBuilder;
34  import javax.xml.parsers.DocumentBuilderFactory;
35  
36  import org.apache.log4j.LogManager;
37  import org.apache.log4j.Logger;
38  import org.jcontainer.dna.Configuration;
39  import org.jcontainer.dna.impl.Log4JLogger;
40  import org.objectledge.filesystem.ClasspathFileSystemProvider;
41  import org.objectledge.filesystem.FileSystem;
42  import org.objectledge.filesystem.FileSystemProvider;
43  import org.objectledge.filesystem.LocalFileSystemProvider;
44  import org.objectledge.i18n.xml.XMLI18n;
45  import org.objectledge.logging.LedgeDOMConfigurator;
46  import org.objectledge.utils.LedgeTestCase;
47  import org.objectledge.xml.XMLGrammarCache;
48  import org.objectledge.xml.XMLValidator;
49  import org.w3c.dom.Document;
50  import org.xml.sax.InputSource;
51  
52  /**
53   * @author <a href="mailto:pablo@caltha.pl">Pawel Potempski</a>
54   *
55   */
56  public class XMLI18nTest extends LedgeTestCase
57  {
58  	/*** i18n component */
59  	private XMLI18n i18n;
60  	
61      public void setUp()
62      throws Exception
63      {
64          FileSystemProvider lfs = new LocalFileSystemProvider("local", "src/test/resources");
65          FileSystemProvider cfs = new ClasspathFileSystemProvider("classpath",
66          										 getClass().getClassLoader());
67          FileSystem fs = new FileSystem(new FileSystemProvider[] { lfs, cfs }, 4096, 4096);
68          try
69          {
70              InputSource source = new InputSource(
71              	fs.getInputStream("config/org.objectledge.logging.LoggingConfigurator.xml"));
72              DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
73              Document logConfig = builder.parse(source);
74              LedgeDOMConfigurator configurator = new LedgeDOMConfigurator(fs);
75              configurator.doConfigure(logConfig.getDocumentElement(), 
76                  LogManager.getLoggerRepository());
77  
78              Configuration config = getConfig(fs, "config/org.objectledge.i18n.I18n.xml");
79              Logger logger = Logger.getLogger(XMLI18n.class);
80              XMLValidator validator = new XMLValidator(new XMLGrammarCache());
81              i18n = new XMLI18n(config, new Log4JLogger(logger), fs, validator);
82          }
83          catch (Exception e)
84          {
85              throw new RuntimeException(e);
86          }
87  
88      }
89  
90      public void testInit()
91      {
92      	i18n.reload();
93      }
94  
95  	/*
96  	 * Test for Locale getDefaultLocale()
97  	 */
98  	public void testGetDefaultLocale()
99  	{
100 		Locale plLocale = new Locale("pl","PL");
101 		assertEquals(plLocale, i18n.getDefaultLocale());
102 	}
103 
104     /*
105      * Test for String get(Locale, String)
106      */
107     public void testGetLocaleString()
108     {
109     	Locale plLocale = new Locale("pl","PL");
110 		Locale enLocale = new Locale("en","EN");
111     	assertEquals("foo2",i18n.get(plLocale,"foo2"));
112     	assertEquals("bar",i18n.get(plLocale,"foo.bar.foo"));
113     	assertEquals("bar",i18n.get(enLocale,"foo.bar.foo"));
114 		assertEquals("bar",i18n.get(enLocale,"bar.foo.bar.foo"));
115         assertEquals("foo&bar",i18n.get(plLocale,"bar.foo.foobar"));
116     }
117 
118     /*
119      * Test for String get(Locale, String, String[])
120      */
121     public void testGetLocaleStringStringArray()
122     {
123 		Locale plLocale = new Locale("pl","PL");
124 		String key = "foo_$1_bar_$2";
125 		String[] values = new String[]{"foo","bar"};
126 		assertEquals("foo_foo_bar_bar", i18n.get(plLocale, key, values));
127     }
128 
129     public void testGetTool()
130     {
131 		I18nTool tool = new I18nTool(i18n, i18n.getDefaultLocale(), null);
132     	assertNotNull(tool);
133 		String key = "foo_$1_bar_$2";
134 		String[] values = new String[]{"foo","bar"};
135 		assertEquals("foo_foo_bar_bar", tool.get(key, values));
136     	assertEquals("bar", tool.get("foo.bar.foo"));
137     	tool = tool.usePrefix("foo");
138     	assertEquals("bar", tool.get("bar.foo"));
139 		tool = tool.usePrefix("bar");
140 		assertEquals("bar", tool.get("foo"));
141 		tool = tool.useLocale("en_EN");
142 		assertEquals("bar", tool.get("foo"));
143 		String output = tool.get(key, values);
144 		assertEquals("foo.bar.foo_foo_bar_bar", output);
145 		tool = tool.usePrefix("");
146     }
147 
148     public void testUndefined()
149     {
150         Locale plLocale = new Locale("pl","PL");
151         Locale noLocale = new Locale("no","NO");
152         assertEquals("foo2", i18n.get(noLocale, "foo2"));
153         assertEquals("undefined", i18n.get(plLocale, "undefined"));
154     }
155 }