View Javadoc

1   /*
2    * Copyright 2007-2008 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.kns.util;
17  
18  import org.junit.Test;
19  import org.kuali.rice.kns.bo.Parameter;
20  import org.kuali.rice.kns.datadictionary.AttributeDefinition;
21  import org.kuali.rice.kns.datadictionary.DataDictionaryEntryBase;
22  import org.kuali.rice.kns.service.KNSServiceLocator;
23  import org.kuali.rice.kns.web.format.BooleanFormatter;
24  import org.kuali.rice.kns.web.format.DateFormatter;
25  import org.kuali.rice.kns.web.format.Formatter;
26  import org.kuali.rice.kns.web.format.IntegerFormatter;
27  import org.kuali.rice.kns.web.format.PercentageFormatter;
28  import org.kuali.test.KNSTestCase;
29  
30  import edu.sampleu.travel.bo.TravelAccountUseRate;
31  
32  /**
33   * ObjectUtilsTest
34   * 
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  public class ObjectUtilsTest extends KNSTestCase {
38      @Test
39      public void testObjectUtils_equalsByKey() throws Exception {
40          Parameter parameterInDB = new Parameter();
41          parameterInDB.setParameterNamespaceCode("KR-NS");
42          parameterInDB.setParameterName("OBJ_UTIL_TEST");
43          
44          Parameter parameterNew = new Parameter();
45          parameterNew.setParameterNamespaceCode("KR-NS");
46          parameterInDB.setParameterName(null);
47          
48          boolean equalsResult = false;
49          equalsResult = ObjectUtils.equalByKeys(parameterInDB, parameterNew);
50          assertFalse(equalsResult);
51      }
52      
53  	@Test
54  	public void testGetFormatterWithDataDictionary() throws Exception {
55  		// test formatter getting correctly pulled from data dictionary
56  		TravelAccountUseRate useRate = new TravelAccountUseRate();
57  		Formatter formatter = ObjectUtils.getFormatterWithDataDictionary(useRate, "active");
58  		assertTrue("Incorrect formatter returned for active property", formatter instanceof BooleanFormatter);
59  
60  		changeAttributeDefinitionFormatter(useRate.getClass(), "active", IntegerFormatter.class);
61  		formatter = ObjectUtils.getFormatterWithDataDictionary(useRate, "active");
62  		assertTrue("Incorrect formatter returned for active property", formatter instanceof IntegerFormatter);
63  		
64  		// test formatter getting correctly pulled by data type
65  		formatter = ObjectUtils.getFormatterWithDataDictionary(useRate, "activeFromDate");
66  		assertTrue("Incorrect formatter returned for date type", formatter instanceof DateFormatter);
67  		
68  		formatter = ObjectUtils.getFormatterWithDataDictionary(useRate, "rate");
69  		assertTrue("Incorrect formatter returned for percent type", formatter instanceof PercentageFormatter);
70  		
71  		formatter = ObjectUtils.getFormatterWithDataDictionary(useRate, "number");
72  		assertTrue("Incorrect formatter returned for string type", formatter.getClass().getName().equals("org.kuali.rice.kns.web.format.Formatter"));
73  	}
74  
75  	private void changeAttributeDefinitionFormatter(Class boClass, String attributeName, Class formatterClass) {
76  		DataDictionaryEntryBase entry = (DataDictionaryEntryBase) KNSServiceLocator.getDataDictionaryService()
77  				.getDataDictionary().getDictionaryObjectEntry(boClass.getName());
78  		if (entry != null) {
79  			AttributeDefinition attributeDefinition = entry.getAttributeDefinition(attributeName);
80  			attributeDefinition.setFormatterClass(formatterClass.getName());
81  		}
82  	}
83  }