View Javadoc

1   /**
2    * Copyright 2005-2011 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.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
42   * 
43   * @author Kuali Rice Team (rice.collab@kuali.org)
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  /*	@Test
62  	public void testGetFormatterWithDataDictionary() throws Exception {
63  		// test formatter getting correctly pulled from data dictionary
64  		TravelAccountUseRate useRate = new TravelAccountUseRate();
65  		Formatter formatter = ObjectUtils.getFormatterWithDataDictionary(useRate, "active");
66  		assertTrue("Incorrect formatter returned for active property", formatter instanceof BooleanFormatter);
67  
68  		changeAttributeDefinitionFormatter(useRate.getClass(), "active", IntegerFormatter.class);
69  		formatter = ObjectUtils.getFormatterWithDataDictionary(useRate, "active");
70  		assertTrue("Incorrect formatter returned for active property", formatter instanceof IntegerFormatter);
71  		
72  		// test formatter getting correctly pulled by data type
73  		formatter = ObjectUtils.getFormatterWithDataDictionary(useRate, "activeFromDate");
74  		assertTrue("Incorrect formatter returned for date type", formatter instanceof DateFormatter);
75  		
76  		formatter = ObjectUtils.getFormatterWithDataDictionary(useRate, "rate");
77  		assertTrue("Incorrect formatter returned for percent type", formatter instanceof PercentageFormatter);
78  		
79  		formatter = ObjectUtils.getFormatterWithDataDictionary(useRate, "number");
80  		assertTrue("Incorrect formatter returned for string type", formatter.getClass().getName().equals("org.kuali.rice.core.web.format.Formatter"));
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         // abcd is not a collection (or any other) property
106         assertNull(new PojoPropertyUtilsBean.PersistenceStructureServiceProvider().getCollectionItemClass(new MaintenanceDocumentBase(), "abcd"));
107         // attachment is a valid property, but not a collection
108         assertNull(new PojoPropertyUtilsBean.PersistenceStructureServiceProvider().getCollectionItemClass(new MaintenanceDocumentBase(), "attachment"));
109         // attachmentContent is an array
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             // expected
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 }