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