001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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: */
016: package org.kuali.core.util;
017:
018: import java.util.HashMap;
019: import java.util.Map;
020:
021: import org.apache.ojb.broker.core.proxy.ProxyHelper;
022: import org.kuali.core.bo.Parameter;
023: import org.kuali.core.bo.PersistableBusinessObject;
024: import org.kuali.kfs.context.KualiTestBase;
025: import org.kuali.kfs.context.SpringContext;
026: import org.kuali.module.chart.bo.Account;
027: import org.kuali.module.chart.bo.AccountGlobalDetail;
028: import org.kuali.module.chart.bo.ObjLevel;
029: import org.kuali.module.chart.bo.ObjectCode;
030: import org.kuali.module.chart.bo.Org;
031: import org.kuali.module.chart.bo.ProjectCode;
032: import org.kuali.module.chart.service.AccountService;
033: import org.kuali.test.ConfigureContext;
034:
035: /**
036: * This class tests the ObjectUtil methods.
037: */
038: @ConfigureContext
039: public class ObjectUtilsTest extends KualiTestBase {
040:
041: public void testNullHandling() {
042: PersistableBusinessObject bo = new Account();
043: Object prop = ObjectUtils.getPropertyValue(bo,
044: "chartOfAccounts");
045: assertNull(prop);
046: }
047:
048: public final void testCreateHybridBusinessObject() throws Exception {
049: Account account = new Account();
050: Map<String, String> template = new HashMap();
051:
052: template.put("accountNumber", "accountNumber");
053: template.put("chartOfAccountsCode", "chartOfAccountsCode");
054:
055: account.setAccountNumber("123");
056: account.setChartOfAccountsCode("XY");
057:
058: AccountGlobalDetail result = (AccountGlobalDetail) ObjectUtils
059: .createHybridBusinessObject(AccountGlobalDetail.class,
060: account, template);
061: assertEquals(result.getAccountNumber(), account
062: .getAccountNumber());
063: assertEquals(result.getChartOfAccountsCode(), account
064: .getChartOfAccountsCode());
065:
066: }
067:
068: public void testDeepCopyOriginal() {
069: Org src = new Org();
070: src.setChartOfAccountsCode("BA");
071: src.setOrganizationCode("AAAI");
072: src.refresh();
073: assertNotNull(src.getChartOfAccounts());
074:
075: Org copy = (Org) ObjectUtils.deepCopy(src);
076: assertNotNull(copy.getChartOfAccounts()
077: .getChartOfAccountsCode());
078: assertTrue(copy.getChartOfAccounts().getChartOfAccountsCode()
079: .equals(
080: src.getChartOfAccounts()
081: .getChartOfAccountsCode()));
082: }
083:
084: public void testDeepCopyWithTransients() {
085: ObjectCode src = new ObjectCode();
086: src.setChartOfAccountsCode(TestConstants.Data4.CHART_CODE);
087: src.setFinancialObjectCode(TestConstants.Data4.OBJECT_CODE);
088: src.refresh();
089: assertNotNull(src.getChartOfAccounts());
090:
091: ObjectCode copy = (ObjectCode) ObjectUtils.deepCopy(src);
092:
093: assertNotNull(copy.getChartOfAccountsCode());
094: assertNull(copy.getChartOfAccounts());
095: copy.refresh(); // refresh to populate the chart object reference
096: assertNotNull(copy.getChartOfAccounts());
097: assertTrue(copy.getChartOfAccounts().getChartOfAccountsCode()
098: .equals(
099: src.getChartOfAccounts()
100: .getChartOfAccountsCode()));
101: }
102:
103: /**
104: * test hash code function on objects with nested objects
105: */
106: public void testHashCode3() throws Exception {
107: ObjectCode objectCode = new ObjectCode();
108: objectCode
109: .setChartOfAccountsCode(TestConstants.Data4.CHART_CODE);
110: objectCode
111: .setFinancialObjectCode(TestConstants.Data4.OBJECT_CODE);
112:
113: ObjLevel objectLevel = new ObjLevel();
114: objectLevel
115: .setChartOfAccountsCode(TestConstants.Data4.CHART_CODE);
116: objectLevel.setFinancialObjectLevelCode("RESA");
117: objectCode.setFinancialObjectLevelCode("RESA");
118:
119: objectCode.refresh();
120: String hashKey1 = ObjectUtils.getMD5Hash(objectCode);
121:
122: ObjectCode objectCode2 = new ObjectCode();
123: objectCode2.setChartOfAccountsCode("BL");
124: objectCode2.setFinancialObjectCode("3000");
125:
126: ObjLevel objectLevel2 = new ObjLevel();
127: objectLevel2.setChartOfAccountsCode("BL");
128: objectLevel2.setFinancialObjectLevelCode("RESA");
129: objectCode2.setFinancialObjectLevelCode("RESA");
130:
131: objectCode2.refresh();
132: String hashKey2 = ObjectUtils.getMD5Hash(objectCode2);
133:
134: assertEquals("Hash keys do not match", hashKey1, hashKey2);
135: }
136:
137: public void testEqualByKeys() throws Exception {
138: Org org1 = new Org();
139: Org org2 = new Org();
140: org1.setChartOfAccountsCode("BA");
141: org1.setOrganizationCode("BOOK");
142: org2.setChartOfAccountsCode("BA");
143: org2.setOrganizationCode("BOOK");
144:
145: assertTrue(ObjectUtils.equalByKeys(org1, org2));
146:
147: org1.setOrganizationCode("BOOK");
148: org2.setOrganizationCode("MOTR");
149:
150: assertFalse(ObjectUtils.equalByKeys(org1, org2));
151:
152: ProjectCode project = new ProjectCode();
153:
154: assertFalse(ObjectUtils.equalByKeys(org1, project));
155:
156: assertFalse(ObjectUtils.equalByKeys(null, project));
157:
158: assertFalse(ObjectUtils.equalByKeys(org1, null));
159:
160: assertTrue(ObjectUtils.equalByKeys(null, null));
161: }
162:
163: // FIXME change this test to use something other than Budget which doesn't exist for Phase 1
164: // public void testSetObjectPropertyDeep() throws Exception {
165: //
166: // // Null BO
167: // Object nullObj = null;
168: // ObjectUtils.setObjectPropertyDeep(nullObj, "someProperty", String.class, "someValue");
169: //
170: // // Property doesn't exist in BO
171: // Budget testBudget = new Budget();
172: // ObjectUtils.setObjectPropertyDeep(testBudget, "nonExistingProperty", String.class, "someValue");
173: //
174: // // Property already == given value
175: // testBudget = new Budget();
176: // testBudget.setDocumentHeaderId("testingDeepSet");
177: // ObjectUtils.setObjectPropertyDeep(testBudget, "documentHeaderId", String.class, "testingDeepSet");
178: //
179: // // Object has property, but of a different type
180: // testBudget = new Budget();
181: // ObjectUtils.setObjectPropertyDeep(testBudget, "documentHeaderId", Integer.class, new Integer(1));
182: //
183: // // Object w/loops
184: // testBudget = new Budget();
185: // testBudget.getTasks().add(testBudget);
186: // ObjectUtils.setObjectPropertyDeep(testBudget, "documentHeaderId", String.class, "testingDeepSet");
187: // assertTrue("testingDeepSet".equals(testBudget.getDocumentHeaderId()));
188: // assertTrue("testingDeepSet".equals(((Budget) testBudget.getTasks().get(0)).getDocumentHeaderId()));
189: //
190: // // Deep object w/nested objects & lists
191: // testBudget = new Budget();
192: // BudgetModular testBudgetModular = new BudgetModular();
193: // testBudget.setModularBudget(testBudgetModular);
194: // BudgetTask testTask = new BudgetTask();
195: // testBudget.getTasks().add(testTask);
196: // BudgetTask testTask2 = new BudgetTask();
197: // testBudget.getTasks().add(testTask2);
198: // ObjectUtils.setObjectPropertyDeep(testBudget, "documentHeaderId", String.class, "testingDeepSet");
199: // assertTrue("testingDeepSet".equals(testBudget.getDocumentHeaderId()));
200: // assertTrue("testingDeepSet".equals(testBudget.getModularBudget().getDocumentHeaderId()));
201: // assertTrue("testingDeepSet".equals(((BudgetTask) testBudget.getTasks().get(0)).getDocumentHeaderId()));
202: // assertTrue("testingDeepSet".equals(((BudgetTask) testBudget.getTasks().get(1)).getDocumentHeaderId()));
203: // }
204:
205: /**
206: * This test exercises a situation encountered with deepCopy, where unexpectedly some fields in non-serializable base classes
207: * wont copy.
208: */
209: public void testDeepCopy_nonSerializableBaseClass() {
210:
211: final String SCRIPT = "SYSTEM";
212: final String NAME = "TEST";
213: final String OBJID = "";
214: final Long VERNBR = new Long(2);
215:
216: Parameter parm1 = new Parameter();
217: parm1.setParameterNamespaceCode(SCRIPT);
218: parm1.setParameterName(NAME);
219: parm1.setObjectId(OBJID);
220: parm1.setVersionNumber(VERNBR);
221:
222: // now do a deepCopy
223: Parameter parm2 = (Parameter) ObjectUtils.deepCopy(parm1);
224:
225: // now lets see if all of the fields were copied over successfully
226: assertEquals(SCRIPT, parm2.getParameterNamespaceCode());
227: assertEquals(NAME, parm2.getParameterName());
228: assertEquals(OBJID, parm2.getObjectId());
229: assertEquals(VERNBR, parm2.getVersionNumber());
230: // the above line fails if PersistableBase does not implement Serializable
231:
232: }
233:
234: public void testMaterializeAllSubObjects_NullParam() {
235: boolean correctErrorThrown = false;
236: try {
237: ObjectUtils.materializeAllSubObjects(null);
238: } catch (IllegalArgumentException e) {
239: correctErrorThrown = true;
240: }
241: assertEquals("The correct error should have been thrown.",
242: true, correctErrorThrown);
243: }
244:
245: public void testMaterializeAllSubObjects_ProxiedParam() {
246:
247: Account account = SpringContext.getBean(AccountService.class)
248: .getByPrimaryId("BL", "1031400");
249: assertTrue(ProxyHelper.isProxy(account.getChartOfAccounts()));
250: assertTrue(ProxyHelper.isProxy(account.getOrganization()));
251:
252: boolean correctErrorThrown = false;
253: try {
254: ObjectUtils.materializeAllSubObjects(account
255: .getChartOfAccounts());
256: } catch (IllegalArgumentException e) {
257: correctErrorThrown = true;
258: }
259: assertEquals("The correct error should have been thrown.",
260: true, correctErrorThrown);
261: }
262:
263: public void testMaterializeAllSubObjects_Test1() {
264:
265: Account account = SpringContext.getBean(AccountService.class)
266: .getByPrimaryId("BL", "1031400");
267: assertTrue(ProxyHelper.isProxy(account.getChartOfAccounts()));
268: assertTrue(ProxyHelper.isProxy(account.getOrganization()));
269: assertFalse(ProxyHelper.isProxy(account
270: .getAccountFiscalOfficerUser()));
271:
272: ObjectUtils.materializeAllSubObjects(account);
273:
274: assertFalse(ProxyHelper.isProxy(account.getChartOfAccounts()));
275: assertFalse(ProxyHelper.isProxy(account.getOrganization()));
276: assertFalse(ProxyHelper.isProxy(account
277: .getAccountFiscalOfficerUser()));
278:
279: }
280:
281: public void testMaterializeSubObjectsToDepth_Test1() {
282: Account account = SpringContext.getBean(AccountService.class)
283: .getByPrimaryId("BL", "1031400");
284: assertTrue(ProxyHelper.isProxy(account.getChartOfAccounts()));
285: assertTrue(ProxyHelper.isProxy(account.getOrganization()));
286: assertFalse(ProxyHelper.isProxy(account
287: .getAccountFiscalOfficerUser()));
288:
289: ObjectUtils.materializeSubObjectsToDepth(account, 1);
290:
291: assertFalse(ProxyHelper.isProxy(account.getChartOfAccounts()));
292: assertFalse(ProxyHelper.isProxy(account.getOrganization()));
293: assertFalse(ProxyHelper.isProxy(account
294: .getAccountFiscalOfficerUser()));
295: }
296:
297: }
|