View Javadoc

1   /**
2    * Copyright 2011-2013 The Kuali Foundation Licensed under the Educational
3    * Community License, Version 2.0 (the "License"); you may not use this file
4    * except in compliance with the License. You may obtain a copy of the License
5    * at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12   * License for the specific language governing permissions and limitations under
13   * the License.
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   * Unit test for the KME Message Source
41   * @author Kuali Mobility Team (mobility.collab@kuali.org)
42   * @since 
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  	 * A reference to the <code>MessageSource</code>.
62  	 */
63  	@Resource(name="messageSource")
64  	private MessageSource messageSource;
65  	
66  	/**
67  	 * A reference to the <code>LocalisationService</code>.
68  	 */
69  	@Autowired
70  	@Qualifier("localisationService")
71  	private LocalisationService localisationService;
72  	
73  	/**
74  	 * Check that we can atleast get a message from a file with the message source
75  	 */
76  	@Test
77  	public void testGetMessage() {
78  		String message = messageSource.getMessage(HOME_CODE, null, LOCALE_EN);
79  		
80  		// Check that we got atleast something
81  		assertNotNull(message);
82  		
83  		// Check that it is what we expect
84  		assertEquals(HOME_EN, message);
85  	}
86  	
87  	/**
88  	 * Test that when you change strings to the database, the message source]
89  	 * should reflect this change
90  	 */
91  	@Test
92  	@DirtiesContext
93  	public void testUpdateMessageToDB(){
94  		// Replace home string with new values
95  		localisationService.saveLocalisedString(HOME_CODE, getNewHomeStrings());
96  		
97  		String message;
98  		// Check that English is what we expect
99  		message = messageSource.getMessage(HOME_CODE, null, LOCALE_EN);
100 		assertEquals(HOME2_EN, message);
101 		
102 		// Check that Afrikaans is what we expect
103 		message = messageSource.getMessage(HOME_CODE, null, LOCALE_AF);
104 		assertEquals(HOME2_AF, message);
105 	}
106 	
107 	/**
108 	 * Create a map that can be used to replace the home string with new values
109 	 * @return
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 }