01: package com.mockrunner.test.web;
02:
03: import java.util.HashMap;
04: import java.util.Locale;
05: import java.util.Map;
06:
07: import org.apache.struts.util.MessageResourcesFactory;
08:
09: import junit.framework.TestCase;
10:
11: import com.mockrunner.struts.MapMessageResources;
12: import com.mockrunner.struts.MapMessageResourcesFactory;
13:
14: public class MapMessageResourcesTest extends TestCase {
15: private MapMessageResources resources;
16: private Map testMap;
17: private String tempFactoryClass;
18:
19: protected void setUp() throws Exception {
20: super .setUp();
21: tempFactoryClass = MessageResourcesFactory.getFactoryClass();
22: testMap = new HashMap();
23: testMap.put("test.property.one", "TestOne");
24: testMap.put("test.property.two", "TestTwo {0} {1}");
25: testMap.put("test.property.three", "Test{0}Three");
26: resources = new MapMessageResources(testMap);
27: }
28:
29: protected void tearDown() throws Exception {
30: super .tearDown();
31: MessageResourcesFactory.setFactoryClass(tempFactoryClass);
32: }
33:
34: public void testGetMessages() {
35: assertEquals("TestOne", resources
36: .getMessage("test.property.one"));
37: assertEquals("TestTwo {0} {1}", resources
38: .getMessage("test.property.two"));
39: assertEquals("Test{0}Three", resources.getMessage(
40: Locale.ITALIAN, "test.property.three"));
41: assertNull(resources.getMessage("test.property.four"));
42: assertTrue(resources.isPresent("test.property.one"));
43: assertTrue(resources.isPresent(Locale.CHINESE,
44: "test.property.two"));
45: assertFalse(resources.isPresent("test.property.four"));
46: resources.putMessage("test.property.four", "xyz");
47: assertEquals("xyz", resources.getMessage("test.property.four"));
48: assertEquals("TestTwo 0 1", resources.getMessage(
49: "test.property.two", "0", "1"));
50: assertEquals("TestTestThree", resources.getMessage(
51: "test.property.three", "Test"));
52: resources.clear();
53: assertNull(resources.getMessage("test.property.one"));
54: assertFalse(resources.isPresent("test.property.one"));
55: assertFalse(resources.isPresent(Locale.GERMAN,
56: "test.property.one"));
57: }
58:
59: public void testMessageResourcesFactory() {
60: MessageResourcesFactory
61: .setFactoryClass("com.mockrunner.struts.MapMessageResourcesFactory");
62: assertTrue(MessageResourcesFactory.createFactory() instanceof MapMessageResourcesFactory);
63: MapMessageResourcesFactory factory = (MapMessageResourcesFactory) MessageResourcesFactory
64: .createFactory();
65: resources = (MapMessageResources) factory.createResources("");
66: assertFalse(resources.isPresent("test.property.one"));
67: MapMessageResourcesFactory.setMessageMap(testMap);
68: resources = (MapMessageResources) factory.createResources("");
69: assertTrue(resources.isPresent("test.property.one"));
70: MapMessageResourcesFactory.setMessageMap(null);
71: resources = (MapMessageResources) factory.createResources("");
72: assertFalse(resources.isPresent("test.property.one"));
73: }
74:
75: public void testLoadFromFile() {
76: resources
77: .putMessages("src/com/mockrunner/test/web/test.properties");
78: assertEquals("test1", resources.getMessage("test.property1"));
79: assertEquals("test2", resources.getMessage("test.property2"));
80: assertEquals("test3", resources.getMessage("test.property3"));
81: }
82: }
|