1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.util;
16
17 import static org.junit.Assert.*;
18
19 import java.io.CharArrayWriter;
20 import java.util.HashMap;
21 import java.util.Locale;
22 import java.util.Map;
23
24 import javax.annotation.Resource;
25 import javax.servlet.jsp.PageContext;
26
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.kuali.mobility.l10n.service.LocalisationService;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.beans.factory.annotation.Qualifier;
32 import org.springframework.context.MessageSource;
33 import org.springframework.mock.web.MockPageContext;
34 import org.springframework.test.annotation.DirtiesContext;
35 import org.springframework.test.context.ContextConfiguration;
36 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
37 import org.springframework.web.servlet.tags.MessageTag;
38
39
40
41
42
43
44 @RunWith(SpringJUnit4ClassRunner.class)
45 @DirtiesContext
46 @ContextConfiguration("classpath:/LocalisationSpringBeans.xml")
47 public class KMEMessageSourceTest {
48
49 private static final Locale LOCALE_EN = new Locale("en");
50 private static final Locale LOCALE_AF = new Locale("af");
51
52 private static final String AF = "af";
53 private static final String EN = "en";
54
55 private static final String HOME_CODE = "test.home";
56 private static final String HOME_EN = "Home";
57 private static final String HOME2_EN = "Home Page";
58 private static final String HOME2_AF = "Tuis Bladsy";
59
60
61
62
63 @Resource(name="messageSource")
64 private MessageSource messageSource;
65
66
67
68
69 @Autowired
70 @Qualifier("localisationService")
71 private LocalisationService localisationService;
72
73
74
75
76 @Test
77 public void testGetMessage() {
78 String message = messageSource.getMessage(HOME_CODE, null, LOCALE_EN);
79
80
81 assertNotNull(message);
82
83
84 assertEquals(HOME_EN, message);
85 }
86
87
88
89
90
91 @Test
92 @DirtiesContext
93 public void testUpdateMessageToDB(){
94
95 localisationService.saveLocalisedString(HOME_CODE, getNewHomeStrings());
96
97 String message;
98
99 message = messageSource.getMessage(HOME_CODE, null, LOCALE_EN);
100 assertEquals(HOME2_EN, message);
101
102
103 message = messageSource.getMessage(HOME_CODE, null, LOCALE_AF);
104 assertEquals(HOME2_AF, message);
105 }
106
107
108
109
110
111 private Map<String, String> getNewHomeStrings(){
112 Map<String, String> localisedStrings = new HashMap<String, String>();
113 localisedStrings.put(EN, HOME2_EN);
114 localisedStrings.put(AF, HOME2_AF);
115 return localisedStrings;
116 }
117
118
119 public void test(){
120 CharArrayWriter caw = new CharArrayWriter();
121 MockPageContext mpc = new MockPageContext();
122 MessageTag messageTag = new MessageTag();
123 messageTag.setPageContext(mpc);
124 messageTag.setCode(HOME_CODE);
125
126 }
127
128 }