View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.messages;
17  
18  import org.junit.Test;
19  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
20  import org.kuali.rice.test.data.PerSuiteUnitTestData;
21  import org.kuali.rice.test.data.UnitTestData;
22  import org.kuali.rice.test.data.UnitTestFile;
23  import org.kuali.rice.krad.test.KRADTestCase;
24  
25  import static org.junit.Assert.assertEquals;
26  
27  /**
28   * Test cases for {@link org.kuali.rice.krad.messages.MessageService}
29   *
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  @PerSuiteUnitTestData(
33          value = @UnitTestData(
34                  order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES},
35                  sqlFiles = {@UnitTestFile(filename = "classpath:testExternalMessages.sql", delimiter = ";")}))
36  public class MessageServiceTest extends KRADTestCase {
37  
38      private MessageService messageService;
39  
40      @Override
41      public void setUp() throws Exception {
42          super.setUp();
43  
44          messageService = KRADServiceLocatorWeb.getMessageService();
45      }
46  
47      /**
48       * Test retrieval of a message from resources by key only (default namespace)
49       */
50      @Test
51      public void testRetrieveMessage_ResourceByKey() throws Exception {
52          String messageText = messageService.getMessageText("validation.test.error");
53  
54          assertEquals("Validation message text not correct", "App Error found for {0}", messageText);
55      }
56  
57      /**
58       * Test retrieval of a message from resources by namespace and key
59       */
60      @Test
61      public void testRetrieveMessage_ResourceByNamespaceKey() throws Exception {
62          String messageText = messageService.getMessageText("TEST", "", "validation.test.error");
63  
64          assertEquals("Validation message text not correct", "Error found for {0}", messageText);
65      }
66  
67      /**
68       * Test retrieval of a message from resources by namespace, component, and key
69       */
70      @Test
71      public void testRetrieveMessage_ResourceByNamespaceComponentKey() throws Exception {
72          String messageText = messageService.getMessageText("TEST", "TestComponent", "validation.test.error");
73  
74          assertEquals("Component validation message text not correct", "Component Error found", messageText);
75      }
76  
77      /**
78       * Test retrieval of a message from the database by key only (default namespace)
79       */
80      @Test
81      public void testRetrieveMessage_DatabaseByKey() throws Exception {
82          String messageText = messageService.getMessageText("validation.test2.error");
83  
84          assertEquals("Validation message text not correct", "App Error found for {0}", messageText);
85      }
86  
87      /**
88       * Test retrieval of a message from the database by namespace and key
89       */
90      @Test
91      public void testRetrieveMessage_DatabaseByNamespaceKey() throws Exception {
92          String messageText = messageService.getMessageText("TEST", "", "validation.test2.error");
93  
94          assertEquals("Validation message text not correct", "Error found for {0}", messageText);
95      }
96  
97      /**
98       * Test retrieval of a message from the database by namespace, component, and key
99       */
100     @Test
101     public void testRetrieveMessage_DatabaseByNamespaceComponentKey() throws Exception {
102         String messageText = messageService.getMessageText("TEST", "TestComponent", "validation.test2.error");
103 
104         assertEquals("Validation message text not correct", "Component Error found", messageText);
105     }
106 
107     /**
108      * Test retrieval of messages with different locales
109      */
110     @Test
111     public void testRetrieveMessage_Locale() throws Exception {
112         String messageText = messageService.getMessageText("TEST", "TestLocales", "message.key", "en-US");
113         assertEquals("Message text not correct for English US locale", "English US Message", messageText);
114 
115         messageText = messageService.getMessageText("TEST", "TestLocales", "message.key", "fr-CA");
116         assertEquals("Message text not correct for French Canada locale", "French CA Message", messageText);
117 
118         messageText = messageService.getMessageText("TEST", "TestLocales", "message.key", "de-AT");
119         assertEquals("Message text not correct for German locale", "German Message", messageText);
120 
121         messageText = messageService.getMessageText("TEST", "TestLocales", "message.key", "de-DE");
122         assertEquals("Message text not correct for German locale", "German Message", messageText);
123 
124         messageText = messageService.getMessageText("TEST", "TestLocales", "message.key2", "en-US");
125         assertEquals("Message text not correct for message with language only and language/country locale",
126                 "English US Message", messageText);
127     }
128 }