1 package org.kuali.rice.krad.datadictionary;
2
3 import org.junit.Test;
4 import org.kuali.rice.core.api.util.KeyValue;
5 import org.kuali.rice.krad.messages.MessageService;
6 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
7 import org.kuali.rice.krad.test.TestDictionaryBean;
8 import org.kuali.rice.krad.util.KRADConstants;
9 import org.kuali.rice.test.data.PerSuiteUnitTestData;
10 import org.kuali.rice.test.data.UnitTestData;
11 import org.kuali.rice.test.data.UnitTestFile;
12 import org.kuali.test.KRADTestCase;
13 import org.kuali.test.KRADTestConstants;
14 import org.kuali.test.TestDictionaryConfig;
15
16 import java.util.List;
17
18 import static org.junit.Assert.assertEquals;
19
20
21
22
23
24
25
26
27
28
29
30
31 @PerSuiteUnitTestData(
32 value = @UnitTestData(
33 order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES},
34 sqlFiles = {@UnitTestFile(filename = "classpath:testExternalMessages.sql", delimiter = ";")}))
35 @TestDictionaryConfig(
36 namespaceCode = "TEST",
37 dataDictionaryFiles = "classpath:org/kuali/rice/krad/datadictionary/MessageProcessorTestBeans.xml")
38 public class MessageBeanProcessorTest extends KRADTestCase {
39
40
41
42
43
44
45 @Test
46 public void testMatchBeanIdSimpleKey() throws Exception {
47 TestDictionaryBean tObject = (TestDictionaryBean) getTestDictionaryObject("TestMessagesSimpleProperty");
48
49 assertEquals("Property1 was not overridden with external message", "ext p1 value", tObject.getProperty1());
50 assertEquals("Property2 was not overridden with external message", "ext p2 value", tObject.getProperty2());
51 }
52
53 @Test
54 public void testMatchExplicitKey() throws Exception {
55 TestDictionaryBean tObject = (TestDictionaryBean) getTestDictionaryObject("TestMessagesMessageKey");
56
57 assertEquals("Property1 was not overridden with explicit message key (with default)", "ext key p1 value",
58 tObject.getProperty1());
59 assertEquals("Property2 was not overridden with explicit message key (without default)", "ext key p2 value",
60 tObject.getProperty2());
61 }
62
63 @Test
64 public void testMatchExplicitListKey() throws Exception {
65 TestDictionaryBean tObject = (TestDictionaryBean) getTestDictionaryObject("TestMessagesListMessageKey");
66
67 assertEquals("List1 entry 2 was not overridden with explicit message key", "ext list key value",
68 tObject.getList1().get(1));
69 }
70
71 @Test
72 public void testMatchExplicitMapKey() throws Exception {
73 TestDictionaryBean tObject = (TestDictionaryBean) getTestDictionaryObject("TestMessagesMapMessageKey");
74
75 assertEquals("Map1 key 'k2' was not overridden with explicit message key", "ext map key value",
76 tObject.getMap1().get("k2"));
77 }
78
79 @Test
80 public void testMessageExpressionMerge() throws Exception {
81 TestDictionaryBean tObject = (TestDictionaryBean) getTestDictionaryObject("TestMessagesExpressionMerge");
82
83 assertEquals("Expression for property 1 was not merged", "Value '@{expr1}' is invalid",
84 tObject.getExpressionGraph().get("property1"));
85 assertEquals("Expression for property 2 was not merged", "The @{expr1} code should not equal @{expr2 + 3}",
86 tObject.getExpressionGraph().get("property2"));
87
88 tObject = (TestDictionaryBean) getTestDictionaryObject("TestMessagesKeyExprMerge");
89
90 assertEquals("Expression for property 1 was not merged with explicit message key",
91 "Expr @{expr1} then expr @{expr2}", tObject.getExpressionGraph().get("property1"));
92 }
93
94 @Test
95 public void testMessagesForListBean() throws Exception {
96 List<KeyValue> keyValues = (List<KeyValue>) getTestDictionaryObject("TestMessagesOptions");
97
98 assertEquals("First key value not overridden by message", "Ext Summer", keyValues.get(0).getValue());
99 assertEquals("Second key value was overriden and should not be", "Fall", keyValues.get(1).getValue());
100 assertEquals("Third key value not overridden by message", "Ext Spring", keyValues.get(2).getValue());
101 }
102
103 @Test
104 public void testModuleResourcesMessages() throws Exception {
105 TestDictionaryBean tObject = (TestDictionaryBean) getTestDictionaryObject("TestMessagesResources");
106
107 assertEquals("Property 1 value was not overridden with message resource", "Message for Property 1",
108 tObject.getProperty1());
109 assertEquals("Property 2 value with explicit key was not overridden with message resource",
110 "Message for explicit message key", tObject.getProperty2());
111
112
113 String messageText = getMessageService().getMessageText(KRADTestConstants.TEST_NAMESPACE_CODE, "",
114 "validation.test.error");
115 assertEquals("Validation message text not correct", "Error found for {0}",
116 messageText);
117 }
118
119 @Test
120 public void testApplicationResourcesMessages() throws Exception {
121 TestDictionaryBean tObject = (TestDictionaryBean) getTestDictionaryObject("TestApplicationMessagesResources");
122
123 assertEquals("Property 1 value was not overridden with message resource", "App Message for Property 1",
124 tObject.getProperty1());
125 assertEquals("Property 2 value with explicit key was not overridden with message resource",
126 "App Message for explicit message key", tObject.getProperty2());
127
128
129 String messageText = getMessageService().getMessageText(KRADConstants.DEFAULT_NAMESPACE, "",
130 "validation.test.error");
131 assertEquals("Validation message text not correct", "App Error found for {0}",
132 messageText);
133 }
134
135
136
137
138 @Test
139 public void testExpressionMessages() throws Exception {
140 TestDictionaryBean tObject = (TestDictionaryBean) getTestDictionaryObject("TestExpressionMessages");
141
142 assertEquals("Messages in expression for property 1 not replaced",
143 "@{foo eq '1' ? 'ext key p1 value' : 'ext key p2 value'}", tObject.getExpressionGraph().get(
144 "property1"));
145 }
146
147 public MessageService getMessageService() {
148 return KRADServiceLocatorWeb.getMessageService();
149
150 }
151
152 }