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.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 public void testObjectUtils_equalsByKey() throws Exception {
48 ParameterBo parameterInDB = new ParameterBo();
49 parameterInDB.setNamespaceCode("KR-NS");
50 parameterInDB.setName("OBJ_UTIL_TEST");
51
52 ParameterBo parameterNew = new ParameterBo();
53 parameterNew.setNamespaceCode("KR-NS");
54 parameterInDB.setName(null);
55
56 boolean equalsResult = false;
57 equalsResult = ObjectUtils.equalByKeys(parameterInDB, parameterNew);
58 assertFalse(equalsResult);
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 private void changeAttributeDefinitionFormatter(Class boClass, String attributeName, Class formatterClass) {
84 DataDictionaryEntryBase entry = (DataDictionaryEntryBase) KRADServiceLocatorWeb.getDataDictionaryService()
85 .getDataDictionary().getDictionaryObjectEntry(boClass.getName());
86 if (entry != null) {
87 AttributeDefinition attributeDefinition = entry.getAttributeDefinition(attributeName);
88 attributeDefinition.setFormatterClass(formatterClass.getName());
89 }
90 }
91
92 @Test
93 public void testMissingNestedObjectCreation() throws Exception {
94 PojoPlugin.initBeanUtils();
95 MaintenanceDocumentBase m = new MaintenanceDocumentBase();
96 m.setAttachments(new ArrayList<MultiDocumentAttachment>());
97 assertNotNull(m.getAttachments());
98 Object o = ObjectUtils.getPropertyValue(m, "attachments[0]");
99 assertNotNull(o);
100 assertTrue(o instanceof MultiDocumentAttachment);
101 }
102
103 @Test
104 public void testInvalidOJBCollection() {
105
106 assertNull(new PojoPropertyUtilsBean.PersistenceStructureServiceProvider().getCollectionItemClass(new MaintenanceDocumentBase(), "abcd"));
107
108 assertNull(new PojoPropertyUtilsBean.PersistenceStructureServiceProvider().getCollectionItemClass(new MaintenanceDocumentBase(), "attachment"));
109
110 assertNull(new PojoPropertyUtilsBean.PersistenceStructureServiceProvider().getCollectionItemClass(new DocumentAttachment(), "attachmentContent"));
111 }
112
113
114 @Test
115 public void testPopulateBusinessObjectFromMap() {
116 PojoPlugin.initBeanUtils();
117
118 NestedBo nestedBo = new NestedBo();
119
120 Map<String, Object> values = new HashMap<String, Object>();
121 values.put("nestedImpl.value", "value");
122
123 FieldUtils.populateBusinessObjectFromMap(nestedBo, values);
124
125 assertNotNull(nestedBo.nested);
126
127 nestedBo.nested = null;
128 values.clear();
129 values.put("nestedIntf.value", "value");
130
131 try {
132 FieldUtils.populateBusinessObjectFromMap(nestedBo, values);
133 fail("Expected to throw NestedNullException due to attempt to instantiate interface");
134 } catch (NestedNullException nne) {
135
136 }
137
138 BOContainingPerson bo = new BOContainingPerson();
139 values.clear();
140 values.put("person.name", "value");
141 FieldUtils.populateBusinessObjectFromMap(bo, values);
142
143 assertNotNull(bo.getPerson());
144 assertEquals("value", bo.getPerson().getName());
145 }
146
147 public static interface ValueHolder {
148 public void setValue(String value);
149 public String getValue();
150 }
151
152 public static class NestedBo implements BusinessObject, ValueHolder {
153 public String value = "foo";
154 public NestedBo nested = null;
155
156 public void refresh() {}
157
158 public void setNestedImpl(NestedBo nested) {
159 this.nested = nested;
160 }
161
162 public NestedBo getNestedImpl() {
163 return nested;
164 }
165
166 public void setNestedIntf(ValueHolder refreshable) {
167 nested = (NestedBo) refreshable;
168 }
169
170 public ValueHolder getNestedIntf() {
171 return nested;
172 }
173
174 public void setValue(String value) {
175 this.value = value;
176 }
177
178 public String getValue() {
179 return value;
180 }
181 }
182
183 }