001/** 002 * Copyright 2005-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krad.util; 017 018import org.apache.commons.beanutils.NestedNullException; 019import org.junit.Test; 020import org.kuali.rice.coreservice.impl.parameter.ParameterBo; 021import org.kuali.rice.kns.util.FieldUtils; 022import org.kuali.rice.kns.web.struts.form.pojo.PojoPlugin; 023import org.kuali.rice.kns.web.struts.form.pojo.PojoPropertyUtilsBean; 024import org.kuali.rice.krad.bo.BusinessObject; 025import org.kuali.rice.krad.bo.DocumentAttachment; 026import org.kuali.rice.krad.bo.MultiDocumentAttachment; 027import org.kuali.rice.krad.datadictionary.AttributeDefinition; 028import org.kuali.rice.krad.datadictionary.DataDictionaryEntryBase; 029import org.kuali.rice.krad.maintenance.MaintenanceDocumentBase; 030import org.kuali.rice.krad.service.KRADServiceLocatorWeb; 031import org.kuali.rice.krad.test.document.BOContainingPerson; 032import org.kuali.rice.krad.test.KRADTestCase; 033 034import java.util.ArrayList; 035import java.util.HashMap; 036import java.util.Map; 037 038import static org.junit.Assert.*; 039 040/** 041 * ObjectUtilsTest tests {@link ObjectUtils} 042 * 043 * @author Kuali Rice Team (rice.collab@kuali.org) 044 */ 045public class ObjectUtilsTest extends KRADTestCase { 046 @Test 047 /** 048 * tests {@link ObjectUtils#equalByKeys(org.kuali.rice.krad.bo.PersistableBusinessObject, org.kuali.rice.krad.bo.PersistableBusinessObject)} 049 */ 050 public void testObjectUtils_equalsByKey() throws Exception { 051 ParameterBo parameterInDB = new ParameterBo(); 052 parameterInDB.setNamespaceCode("KR-NS"); 053 parameterInDB.setName("OBJ_UTIL_TEST"); 054 055 ParameterBo parameterNew = new ParameterBo(); 056 parameterNew.setNamespaceCode("KR-NS"); 057 parameterInDB.setName(null); 058 059 boolean equalsResult = false; 060 equalsResult = ObjectUtils.equalByKeys(parameterInDB, parameterNew); 061 assertFalse(equalsResult); 062 } 063 064/* @Test 065 public void testGetFormatterWithDataDictionary() throws Exception { 066 // test formatter getting correctly pulled from data dictionary 067 TravelAccountUseRate useRate = new TravelAccountUseRate(); 068 Formatter formatter = ObjectUtils.getFormatterWithDataDictionary(useRate, "active"); 069 assertTrue("Incorrect formatter returned for active property", formatter instanceof BooleanFormatter); 070 071 changeAttributeDefinitionFormatter(useRate.getClass(), "active", IntegerFormatter.class); 072 formatter = ObjectUtils.getFormatterWithDataDictionary(useRate, "active"); 073 assertTrue("Incorrect formatter returned for active property", formatter instanceof IntegerFormatter); 074 075 // test formatter getting correctly pulled by data type 076 formatter = ObjectUtils.getFormatterWithDataDictionary(useRate, "activeFromDate"); 077 assertTrue("Incorrect formatter returned for date type", formatter instanceof DateFormatter); 078 079 formatter = ObjectUtils.getFormatterWithDataDictionary(useRate, "rate"); 080 assertTrue("Incorrect formatter returned for percent type", formatter instanceof PercentageFormatter); 081 082 formatter = ObjectUtils.getFormatterWithDataDictionary(useRate, "number"); 083 assertTrue("Incorrect formatter returned for string type", formatter.getClass().getName().equals("org.kuali.rice.core.web.format.Formatter")); 084 } 085*/ 086 private void changeAttributeDefinitionFormatter(Class boClass, String attributeName, Class formatterClass) { 087 DataDictionaryEntryBase entry = (DataDictionaryEntryBase) KRADServiceLocatorWeb.getDataDictionaryService() 088 .getDataDictionary().getDictionaryObjectEntry(boClass.getName()); 089 if (entry != null) { 090 AttributeDefinition attributeDefinition = entry.getAttributeDefinition(attributeName); 091 attributeDefinition.setFormatterClass(formatterClass.getName()); 092 } 093 } 094 095 @Test 096 public void testMissingNestedObjectCreation() throws Exception { 097 PojoPlugin.initBeanUtils(); 098 MaintenanceDocumentBase m = new MaintenanceDocumentBase(); 099 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}