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 assertNotNull(nestedBo.nested);
128
129 nestedBo.nested = null;
130 values.clear();
131 values.put("nestedIntf.value", "value");
132
133 FieldUtils.populateBusinessObjectFromMap(nestedBo, values);
134 assertNull(nestedBo.nested);
135
136 BOContainingPerson bo = new BOContainingPerson();
137 values.clear();
138 values.put("person.name", "value");
139 FieldUtils.populateBusinessObjectFromMap(bo, values);
140
141 assertNotNull(bo.getPerson());
142 assertEquals("value", bo.getPerson().getName());
143 }
144
145 public static interface ValueHolder {
146 public void setValue(String value);
147 public String getValue();
148 }
149
150 public static class NestedBo implements BusinessObject, ValueHolder {
151 public String value = "foo";
152 public NestedBo nested = null;
153
154 public void refresh() {}
155
156 public void setNestedImpl(NestedBo nested) {
157 this.nested = nested;
158 }
159
160 public NestedBo getNestedImpl() {
161 return nested;
162 }
163
164 public void setNestedIntf(ValueHolder refreshable) {
165 nested = (NestedBo) refreshable;
166 }
167
168 public ValueHolder getNestedIntf() {
169 return nested;
170 }
171
172 public void setValue(String value) {
173 this.value = value;
174 }
175
176 public String getValue() {
177 return value;
178 }
179 }
180
181 }