001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestRifeConfig.java 3643 2007-01-12 15:29:45Z gbevin $
007: */
008: package com.uwyn.rife.config;
009:
010: import com.uwyn.rife.config.exceptions.DateFormatInitializationException;
011: import com.uwyn.rife.ioc.HierarchicalProperties;
012: import com.uwyn.rife.rep.Participant;
013: import com.uwyn.rife.rep.Rep;
014: import com.uwyn.rife.rep.Repository;
015: import com.uwyn.rife.rep.SingleObjectParticipant;
016: import java.lang.reflect.Constructor;
017: import java.lang.reflect.InvocationTargetException;
018: import java.text.DateFormat;
019: import java.util.Collection;
020: import java.util.GregorianCalendar;
021: import java.util.HashMap;
022: import java.util.Map;
023: import junit.framework.TestCase;
024:
025: public class TestRifeConfig extends TestCase {
026: private Repository mDefaultRep = null;
027:
028: public TestRifeConfig(String name) {
029: super (name);
030: }
031:
032: public void setUp() {
033: mDefaultRep = Rep.getDefaultRepository();
034: }
035:
036: public void tearDown() {
037: Rep.setDefaultRepository(mDefaultRep);
038: }
039:
040: public void testShortDateFormat() throws Exception {
041: switchLocale(USLocaleConfig.class);
042:
043: DateFormat sf = RifeConfig.Tools.getDefaultShortDateFormat();
044: String formatted = sf.format(new GregorianCalendar(2004, 7, 31,
045: 15, 53).getTime());
046:
047: assertEquals(formatted, "8/31/04");
048:
049: switchLocale(BelgiumNLLocaleConfig.class);
050:
051: sf = RifeConfig.Tools.getDefaultShortDateFormat();
052: formatted = sf
053: .format(new GregorianCalendar(2004, 7, 31, 15, 53)
054: .getTime());
055:
056: assertEquals(formatted, "31/08/04");
057:
058: switchLocale(GeneralESLocaleConfig.class);
059:
060: sf = RifeConfig.Tools.getDefaultShortDateFormat();
061: formatted = sf
062: .format(new GregorianCalendar(2004, 7, 31, 15, 53)
063: .getTime());
064:
065: assertEquals(formatted, "31/08/04");
066:
067: switchLocale(FormattedDateFormatConfig.class);
068:
069: sf = RifeConfig.Tools.getDefaultShortDateFormat();
070: formatted = sf
071: .format(new GregorianCalendar(2004, 7, 31, 15, 53)
072: .getTime());
073:
074: assertEquals(formatted, "Tue, Aug 31, 2004");
075:
076: try {
077: switchLocale(BadlyFormattedDateFormatConfig.class);
078:
079: sf = RifeConfig.Tools.getDefaultShortDateFormat();
080: formatted = sf.format(new GregorianCalendar(2004, 7, 31,
081: 15, 53).getTime());
082: assertFalse(true);
083: } catch (DateFormatInitializationException e) {
084: assertTrue(true);
085: }
086: }
087:
088: public void testLongDateFormat() throws IllegalAccessException,
089: InstantiationException, NoSuchMethodException,
090: InvocationTargetException {
091: switchLocale(USLocaleConfig.class);
092:
093: DateFormat sf = RifeConfig.Tools.getDefaultLongDateFormat();
094: String formatted = sf.format(new GregorianCalendar(2004, 7, 31,
095: 15, 53).getTime());
096:
097: assertEquals(formatted, "Aug 31, 2004 3:53 PM");
098:
099: switchLocale(BelgiumNLLocaleConfig.class);
100:
101: sf = RifeConfig.Tools.getDefaultLongDateFormat();
102: formatted = sf
103: .format(new GregorianCalendar(2004, 7, 31, 15, 53)
104: .getTime());
105:
106: assertEquals(formatted, "31-aug-2004 15:53");
107:
108: switchLocale(GeneralESLocaleConfig.class);
109:
110: sf = RifeConfig.Tools.getDefaultLongDateFormat();
111: formatted = sf
112: .format(new GregorianCalendar(2004, 7, 31, 15, 53)
113: .getTime());
114:
115: assertEquals(formatted, "31-ago-2004 15:53");
116:
117: switchLocale(FormattedDateFormatConfig.class);
118:
119: sf = RifeConfig.Tools.getDefaultLongDateFormat();
120: formatted = sf
121: .format(new GregorianCalendar(2004, 7, 31, 15, 53)
122: .getTime());
123:
124: assertEquals(formatted, "Tue, 31 Aug 2004 15:53:00");
125:
126: try {
127: switchLocale(BadlyFormattedDateFormatConfig.class);
128:
129: sf = RifeConfig.Tools.getDefaultLongDateFormat();
130: formatted = sf.format(new GregorianCalendar(2004, 7, 31,
131: 15, 53).getTime());
132: assertFalse(true);
133: } catch (DateFormatInitializationException e) {
134: assertTrue(true);
135: }
136: }
137:
138: private void switchLocale(Class klazz)
139: throws IllegalAccessException, InstantiationException,
140: NoSuchMethodException, InvocationTargetException {
141: MockRepository rep = new MockRepository();
142: rep.addParticipant("ParticipantConfig", klazz);
143: Rep.setDefaultRepository(rep);
144: }
145:
146: public class USLocaleConfig extends SingleObjectParticipant {
147: public Object getObject() {
148: Config config = new Config();
149: config.setParameter(
150: RifeConfig.Tools.PARAM_L10N_DEFAULT_COUNTRY, "US");
151: config.setParameter(
152: RifeConfig.Tools.PARAM_L10N_DEFAULT_LANGUAGE, "EN");
153:
154: return config;
155: }
156: }
157:
158: public class BelgiumNLLocaleConfig extends SingleObjectParticipant {
159: public Object getObject() {
160: Config config = new Config();
161: config.setParameter(
162: RifeConfig.Tools.PARAM_L10N_DEFAULT_COUNTRY, "BE");
163: config.setParameter(
164: RifeConfig.Tools.PARAM_L10N_DEFAULT_LANGUAGE, "NL");
165:
166: return config;
167: }
168: }
169:
170: public class GeneralESLocaleConfig extends SingleObjectParticipant {
171: public Object getObject() {
172: Config config = new Config();
173: config.setParameter(
174: RifeConfig.Tools.PARAM_L10N_DEFAULT_LANGUAGE, "ES");
175:
176: return config;
177: }
178: }
179:
180: public class FormattedDateFormatConfig extends
181: SingleObjectParticipant {
182: public Object getObject() {
183: Config config = new Config();
184: config
185: .setParameter(
186: RifeConfig.Tools.PARAM_L10N_DEFAULT_SHORT_DATEFORMAT,
187: "EEE, MMM d, yyyy");
188: config
189: .setParameter(
190: RifeConfig.Tools.PARAM_L10N_DEFAULT_LONG_DATEFORMAT,
191: "EEE, d MMM yyyy HH:mm:ss");
192:
193: return config;
194: }
195: }
196:
197: public class BadlyFormattedDateFormatConfig extends
198: SingleObjectParticipant {
199: public Object getObject() {
200: Config config = new Config();
201: config
202: .setParameter(
203: RifeConfig.Tools.PARAM_L10N_DEFAULT_SHORT_DATEFORMAT,
204: "wwww 999 uuuu");
205: config
206: .setParameter(
207: RifeConfig.Tools.PARAM_L10N_DEFAULT_LONG_DATEFORMAT,
208: "vvvv, 82.2 cccc");
209:
210: return config;
211: }
212: }
213:
214: public class MockRepository implements Repository {
215: private Map mParticipants = new HashMap();
216:
217: public MockRepository() {
218: }
219:
220: public void addParticipant(String name, Class klazz)
221: throws IllegalAccessException, InstantiationException,
222: NoSuchMethodException, InvocationTargetException {
223: Constructor localeConstructor = klazz
224: .getDeclaredConstructor(new Class[] { TestRifeConfig.class });
225: TestRifeConfig t = new TestRifeConfig(
226: "Workaround for inner class instanciation");
227:
228: mParticipants.put(name, localeConstructor
229: .newInstance(new Object[] { t }));
230: }
231:
232: public boolean hasParticipant(String name) {
233: return mParticipants.containsKey(name);
234: }
235:
236: public Participant getParticipant(String name) {
237: return (Participant) mParticipants.get(name);
238: }
239:
240: public Collection<? extends Participant> getParticipants(
241: String name) {
242: return mParticipants.values();
243: }
244:
245: public boolean isFinished() {
246: return true;
247: }
248:
249: public void cleanup() {
250: }
251:
252: public HierarchicalProperties getProperties() {
253: HierarchicalProperties properties = new HierarchicalProperties();
254: for (Map.Entry property : System.getProperties().entrySet()) {
255: properties.put((String) property.getKey(), property
256: .getValue());
257: }
258: return properties;
259: }
260:
261: public Object getContext() {
262: return null;
263: }
264: }
265: }
|