1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.util;
17
18 import org.apache.commons.beanutils.NestedNullException;
19 import org.junit.Test;
20 import org.kuali.rice.coreservice.impl.parameter.ParameterBo;
21 import org.kuali.rice.kns.util.FieldUtils;
22 import org.kuali.rice.kns.web.struts.form.pojo.PojoPlugin;
23 import org.kuali.rice.kns.web.struts.form.pojo.PojoPropertyUtilsBean;
24 import org.kuali.rice.krad.bo.BusinessObject;
25 import org.kuali.rice.krad.bo.DocumentAttachment;
26 import org.kuali.rice.krad.bo.MultiDocumentAttachment;
27 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
28 import org.kuali.rice.krad.datadictionary.DataDictionaryEntryBase;
29 import org.kuali.rice.krad.maintenance.MaintenanceDocumentBase;
30 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
31 import org.kuali.rice.krad.test.document.BOContainingPerson;
32 import org.kuali.rice.krad.test.KRADTestCase;
33
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.Map;
37
38 import static org.junit.Assert.*;
39
40
41
42
43
44
45 public class ObjectUtilsTest extends KRADTestCase {
46 @Test
47
48
49
50 public void testObjectUtils_equalsByKey() throws Exception {
51 ParameterBo parameterInDB = new ParameterBo();
52 parameterInDB.setNamespaceCode("KR-NS");
53 parameterInDB.setName("OBJ_UTIL_TEST");
54
55 ParameterBo parameterNew = new ParameterBo();
56 parameterNew.setNamespaceCode("KR-NS");
57 parameterInDB.setName(null);
58
59 boolean equalsResult = false;
60 equalsResult = ObjectUtils.equalByKeys(parameterInDB, parameterNew);
61 assertFalse(equalsResult);
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 private void changeAttributeDefinitionFormatter(Class boClass, String attributeName, Class formatterClass) {
87 DataDictionaryEntryBase entry = (DataDictionaryEntryBase) KRADServiceLocatorWeb.getDataDictionaryService()
88 .getDataDictionary().getDictionaryObjectEntry(boClass.getName());
89 if (entry != null) {
90 AttributeDefinition attributeDefinition = entry.getAttributeDefinition(attributeName);
91 attributeDefinition.setFormatterClass(formatterClass.getName());
92 }
93 }
94
95 @Test
96 public void testMissingNestedObjectCreation() throws Exception {
97 PojoPlugin.initBeanUtils();
98 MaintenanceDocumentBase m = new MaintenanceDocumentBase();
99 m.setAttachments(new ArrayList<MultiDocumentAttachment>());
100 assertNotNull(m.getAttachments());
101 Object o = ObjectUtils.getPropertyValue(m, "attachments[0]");
102 assertNotNull(o);
103 assertTrue(o instanceof MultiDocumentAttachment);
104 }
105
106 @Test
107 public void testInvalidOJBCollection() {
108
109 assertNull(new PojoPropertyUtilsBean.PersistenceStructureServiceProvider().getCollectionItemClass(new MaintenanceDocumentBase(), "abcd"));
110
111 assertNull(new PojoPropertyUtilsBean.PersistenceStructureServiceProvider().getCollectionItemClass(new MaintenanceDocumentBase(), "attachment"));
112
113 assertNull(new PojoPropertyUtilsBean.PersistenceStructureServiceProvider().getCollectionItemClass(new DocumentAttachment(), "attachmentContent"));
114 }
115
116
117 @Test
118 public void testPopulateBusinessObjectFromMap() {
119 PojoPlugin.initBeanUtils();
120
121 NestedBo nestedBo = new NestedBo();
122
123 Map<String, Object> values = new HashMap<String, Object>();
124 values.put("nestedImpl.value", "value");
125
126 FieldUtils.populateBusinessObjectFromMap(nestedBo, values);
127
128 assertNotNull(nestedBo.nested);
129
130 nestedBo.nested = null;
131 values.clear();
132 values.put("nestedIntf.value", "value");
133
134 try {
135 FieldUtils.populateBusinessObjectFromMap(nestedBo, values);
136 fail("Expected to throw NestedNullException due to attempt to instantiate interface");
137 } catch (NestedNullException nne) {
138
139 }
140
141 BOContainingPerson bo = new BOContainingPerson();
142 values.clear();
143 values.put("person.name", "value");
144 FieldUtils.populateBusinessObjectFromMap(bo, values);
145
146 assertNotNull(bo.getPerson());
147 assertEquals("value", bo.getPerson().getName());
148 }
149
150 public static interface ValueHolder {
151 public void setValue(String value);
152 public String getValue();
153 }
154
155 public static class NestedBo implements BusinessObject, ValueHolder {
156 public String value = "foo";
157 public NestedBo nested = null;
158
159 public void refresh() {}
160
161 public void setNestedImpl(NestedBo nested) {
162 this.nested = nested;
163 }
164
165 public NestedBo getNestedImpl() {
166 return nested;
167 }
168
169 public void setNestedIntf(ValueHolder refreshable) {
170 nested = (NestedBo) refreshable;
171 }
172
173 public ValueHolder getNestedIntf() {
174 return nested;
175 }
176
177 public void setValue(String value) {
178 this.value = value;
179 }
180
181 public String getValue() {
182 return value;
183 }
184 }
185
186 }