View Javadoc
1   /**
2    * Copyright 2005-2014 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.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   * ObjectUtilsTest tests {@link ObjectUtils}
42   * 
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   */
45  public class ObjectUtilsTest extends KRADTestCase {
46      @Test
47      /**
48       * tests {@link ObjectUtils#equalByKeys(org.kuali.rice.krad.bo.PersistableBusinessObject, org.kuali.rice.krad.bo.PersistableBusinessObject)}
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  /*	@Test
65  	public void testGetFormatterWithDataDictionary() throws Exception {
66  		// test formatter getting correctly pulled from data dictionary
67  		TravelAccountUseRate useRate = new TravelAccountUseRate();
68  		Formatter formatter = ObjectUtils.getFormatterWithDataDictionary(useRate, "active");
69  		assertTrue("Incorrect formatter returned for active property", formatter instanceof BooleanFormatter);
70  
71  		changeAttributeDefinitionFormatter(useRate.getClass(), "active", IntegerFormatter.class);
72  		formatter = ObjectUtils.getFormatterWithDataDictionary(useRate, "active");
73  		assertTrue("Incorrect formatter returned for active property", formatter instanceof IntegerFormatter);
74  		
75  		// test formatter getting correctly pulled by data type
76  		formatter = ObjectUtils.getFormatterWithDataDictionary(useRate, "activeFromDate");
77  		assertTrue("Incorrect formatter returned for date type", formatter instanceof DateFormatter);
78  		
79  		formatter = ObjectUtils.getFormatterWithDataDictionary(useRate, "rate");
80  		assertTrue("Incorrect formatter returned for percent type", formatter instanceof PercentageFormatter);
81  		
82  		formatter = ObjectUtils.getFormatterWithDataDictionary(useRate, "number");
83  		assertTrue("Incorrect formatter returned for string type", formatter.getClass().getName().equals("org.kuali.rice.core.web.format.Formatter"));
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         // abcd is not a collection (or any other) property
109         assertNull(new PojoPropertyUtilsBean.PersistenceStructureServiceProvider().getCollectionItemClass(new MaintenanceDocumentBase(), "abcd"));
110         // attachment is a valid property, but not a collection
111         assertNull(new PojoPropertyUtilsBean.PersistenceStructureServiceProvider().getCollectionItemClass(new MaintenanceDocumentBase(), "attachment"));
112         // attachmentContent is an array
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 }