View Javadoc
1   /**
2    * Copyright 2005-2014 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.util;
17  
18  import static org.junit.Assert.assertEquals;
19  
20  import java.util.List;
21  
22  import org.junit.Test;
23  import org.kuali.rice.krad.test.KRADTestCase;
24  import org.kuali.rice.test.data.PerSuiteUnitTestData;
25  import org.kuali.rice.test.data.UnitTestData;
26  import org.kuali.rice.test.data.UnitTestFile;
27  
28  /**
29   * Test retrieval of validation messages that have been added to a {@link MessageMap}
30   *
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  @PerSuiteUnitTestData(
34          value = @UnitTestData(
35                  order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES},
36                  sqlFiles = {@UnitTestFile(filename = "classpath:testValidationMessages.sql", delimiter = ";")}))
37  public class ValidationMessageRetrievalTest extends KRADTestCase {
38  
39      private MessageMap messageMap;
40  
41      @Override
42      public void setUp() throws Exception {
43          super.setUp();
44  
45          messageMap = new MessageMap();
46      }
47  
48      /**
49       * Test that message text is correctly retrieved for a validation message specified with only the
50       * message key
51       */
52      @Test
53      public void testRetrieveMessage_keyOnly() throws Exception {
54          messageMap.putError("field1", "testErrorKey");
55  
56          List<ErrorMessage> fieldErrors = messageMap.getErrorMessagesForProperty("field1");
57          assertEquals("Incorrect number of messages for field1", 1, fieldErrors.size());
58  
59          ErrorMessage message = fieldErrors.get(0);
60          String messageText = KRADUtils.getMessageText(message, true);
61          assertEquals("Message for field1 is not correct", "Error on field1", messageText);
62      }
63  
64      /**
65       * Test that message text is correctly retrieved for a validation message specified by namespace and
66       * message key
67       */
68      @Test
69      public void testRetrieveMessage_namespaceKey() throws Exception {
70          ErrorMessage errorMessage = new ErrorMessage();
71          errorMessage.setNamespaceCode("KR-NS");
72          errorMessage.setErrorKey("testErrorKey");
73  
74          messageMap.putError("field1", errorMessage);
75  
76          List<ErrorMessage> fieldErrors = messageMap.getErrorMessagesForProperty("field1");
77          assertEquals("Incorrect number of messages for field1", 1, fieldErrors.size());
78  
79          ErrorMessage message = fieldErrors.get(0);
80          String messageText = KRADUtils.getMessageText(message, true);
81          assertEquals("Message for field1 is not correct", "Error on field1", messageText);
82      }
83  
84      /**
85       * Test that message text is correctly retrieved for a validation message specified by namespace,
86       * component, and message key
87       */
88      @Test
89      public void testRetrieveMessage_componentKey() throws Exception {
90          ErrorMessage errorMessage = new ErrorMessage();
91          errorMessage.setNamespaceCode("KR-NS");
92          errorMessage.setComponentCode("GeneralGroup");
93          errorMessage.setErrorKey("testErrorKey");
94  
95          messageMap.putError("field1", errorMessage);
96  
97          List<ErrorMessage> fieldErrors = messageMap.getErrorMessagesForProperty("field1");
98          assertEquals("Incorrect number of messages for field1", 1, fieldErrors.size());
99  
100         ErrorMessage message = fieldErrors.get(0);
101         String messageText = KRADUtils.getMessageText(message, true);
102         assertEquals("Message for field1 is not correct", "Error on field1", messageText);
103     }
104 }