001: /*
002: * Copyright 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.module.labor.util;
017:
018: import java.sql.Date;
019: import java.sql.Timestamp;
020: import java.util.ArrayList;
021: import java.util.List;
022: import java.util.Map;
023:
024: import junit.framework.TestCase;
025:
026: import org.kuali.core.util.KualiDecimal;
027: import org.kuali.module.labor.util.testobject.SimpleAddress;
028:
029: public class ObjectUtilTest extends TestCase {
030:
031: public void testBuildObject() throws Exception {
032: SimpleAddress targetAddress = null;
033: SimpleAddress sourceAddress = new SimpleAddress(
034: "1000 Main Street", "Source City", "Kuali", 10000);
035: List<String> propertyList = new ArrayList<String>();
036:
037: targetAddress = new SimpleAddress(null, null, null, 9999);
038: ObjectUtil.buildObject(targetAddress, sourceAddress);
039: assertTrue(targetAddress.equals(sourceAddress));
040:
041: targetAddress = new SimpleAddress(null, null, null, 9999);
042: propertyList.clear();
043: propertyList.add("street");
044: ObjectUtil.buildObject(targetAddress, sourceAddress,
045: propertyList);
046: assertEquals(sourceAddress.getStreet(), targetAddress
047: .getStreet());
048: assertFalse(targetAddress.equals(sourceAddress));
049:
050: targetAddress = new SimpleAddress(null, null, null, 9999);
051: String[] sourceAddressArray = { "1000 Main Street",
052: "Source City", "Kuali", "10000" };
053: propertyList.clear();
054: propertyList.add("street");
055: propertyList.add("city");
056: propertyList.add("state");
057: ObjectUtil.buildObject(targetAddress, sourceAddressArray,
058: propertyList);
059: assertFalse(targetAddress.equals(sourceAddress));
060:
061: propertyList.add("zip");
062: ObjectUtil.buildObject(targetAddress, sourceAddressArray,
063: propertyList);
064: assertTrue(targetAddress.equals(sourceAddress));
065: }
066:
067: public void testCompareObject() throws Exception {
068: SimpleAddress targetAddress = null;
069: SimpleAddress sourceAddress = new SimpleAddress(
070: "1000 Main Street", "Source City", "Kuali", 10000);
071:
072: List<String> propertyList = new ArrayList<String>();
073: propertyList.add("street");
074: propertyList.add("city");
075: propertyList.add("state");
076: propertyList.add("zip");
077:
078: targetAddress = new SimpleAddress(null, null, null, 9999);
079: assertFalse(ObjectUtil.compareObject(targetAddress,
080: sourceAddress, propertyList));
081:
082: targetAddress = new SimpleAddress("1000 Main Street",
083: "Source City", "Kuali", 10000);
084: assertTrue(ObjectUtil.compareObject(targetAddress,
085: sourceAddress, propertyList));
086:
087: targetAddress = sourceAddress;
088: assertTrue(ObjectUtil.compareObject(targetAddress,
089: sourceAddress, propertyList));
090:
091: targetAddress = new SimpleAddress(null, null, null, 9999);
092: ObjectUtil.buildObject(targetAddress, sourceAddress);
093: assertTrue(ObjectUtil.compareObject(targetAddress,
094: sourceAddress, propertyList));
095:
096: sourceAddress.setZip(2000);
097: assertFalse(ObjectUtil.compareObject(targetAddress,
098: sourceAddress, propertyList));
099:
100: propertyList.remove("zip");
101: assertTrue(ObjectUtil.compareObject(targetAddress,
102: sourceAddress, propertyList));
103: }
104:
105: public void testBuildPropertyMap() throws Exception {
106: SimpleAddress address = new SimpleAddress("1000 Main Street",
107: "Source City", "Kuali", 10000);
108:
109: List<String> propertyList = new ArrayList<String>();
110: propertyList.add("street");
111: propertyList.add("city");
112: propertyList.add("state");
113:
114: Map propertyMap = ObjectUtil.buildPropertyMap(address,
115: propertyList);
116: assertEquals(address.getStreet(), propertyMap.get("street"));
117: assertEquals(address.getCity(), propertyMap.get("city"));
118: assertEquals(address.getState(), propertyMap.get("state"));
119: assertFalse(address.getZip().equals(propertyMap.get("zip")));
120:
121: propertyList.add("zip");
122: propertyMap = ObjectUtil
123: .buildPropertyMap(address, propertyList);
124: assertEquals(address.getZip(), propertyMap.get("zip"));
125: }
126:
127: public void testBuildPropertyMapWithBlankValues() throws Exception {
128: SimpleAddress address = new SimpleAddress("", "", null, 1000);
129:
130: List<String> propertyList = new ArrayList<String>();
131: propertyList.add("street");
132: propertyList.add("city");
133: propertyList.add("state");
134: propertyList.add("zip");
135:
136: Map propertyMap = ObjectUtil.buildPropertyMap(address,
137: propertyList);
138: assertEquals(null, propertyMap.get("street"));
139: assertEquals(null, propertyMap.get("city"));
140: assertEquals(null, propertyMap.get("state"));
141: assertEquals(address.getZip(), propertyMap.get("zip"));
142: assertEquals(1, propertyMap.size());
143: }
144:
145: public void testConvertLineToBusinessObjectBasedOnDeliminatorAndKeyList()
146: throws Exception {
147: String delim = ";";
148: String line = "1000 Main Street" + delim + "Source City"
149: + delim + "Kuali" + delim + "10000";
150:
151: List<String> propertyList = new ArrayList<String>();
152: propertyList.add("street");
153: propertyList.add("city");
154: propertyList.add("state");
155: propertyList.add("zip");
156:
157: SimpleAddress address = new SimpleAddress();
158: ObjectUtil.convertLineToBusinessObject(address, line, delim,
159: propertyList);
160:
161: assertEquals(address.getStreet(), "1000 Main Street");
162: assertEquals(address.getCity(), "Source City");
163: assertEquals(address.getState(), "Kuali");
164: assertEquals(address.getZip(), new Integer(10000));
165: }
166:
167: public void testConvertLineToBusinessObjectBasedOnDeliminatorAndKeyString()
168: throws Exception {
169: String delim = ";";
170: String line = "1000 Main Street" + delim + "Source City"
171: + delim + "Kuali" + delim + "10000" + delim;
172: String fieldNames = "street" + delim + "city" + delim + "state"
173: + delim + "zip" + delim;
174:
175: SimpleAddress address = new SimpleAddress();
176: ObjectUtil.convertLineToBusinessObject(address, line, delim,
177: fieldNames);
178:
179: assertEquals("1000 Main Street", address.getStreet());
180: assertEquals("Source City", address.getCity());
181: assertEquals("Kuali", address.getState());
182: assertEquals(new Integer(10000), address.getZip());
183: }
184:
185: public void testConvertLineToBusinessObjectAtCompressedFormat()
186: throws Exception {
187: String delim = ";";
188: String line = "" + delim + "" + delim + "" + delim + "" + delim;
189: String fieldNames = "street" + delim + "city" + delim + "state"
190: + delim + "zip" + delim;
191:
192: SimpleAddress address = new SimpleAddress("", "", "", null);
193: ObjectUtil.convertLineToBusinessObject(address, line, delim,
194: fieldNames);
195:
196: assertEquals(null, address.getStreet());
197: assertEquals(null, address.getCity());
198: assertEquals(null, address.getState());
199: assertEquals(null, address.getZip());
200: }
201:
202: public void testConvertLineToBusinessObjectBasedOnFieldLength()
203: throws Exception {
204: int[] fieldLength = { 6, 4, 5, 5 };
205: String line = "StreetCityState10000";
206:
207: List<String> propertyList = new ArrayList<String>();
208: propertyList.add("street");
209: propertyList.add("city");
210: propertyList.add("state");
211: propertyList.add("zip");
212:
213: SimpleAddress address = new SimpleAddress();
214: ObjectUtil.convertLineToBusinessObject(address, line,
215: fieldLength, propertyList);
216:
217: assertEquals("Street", address.getStreet());
218: assertEquals("City", address.getCity());
219: assertEquals("State", address.getState());
220: assertEquals(new Integer(10000), address.getZip());
221: }
222:
223: public void testConvertLineToBusinessObjectBasedOnFieldLength_WithWhiteSpace()
224: throws Exception {
225: int[] fieldLength = { 8, 6, 7, 7 };
226: String line = "Street City State 10000 ";
227:
228: List<String> propertyList = new ArrayList<String>();
229: propertyList.add("street");
230: propertyList.add("city");
231: propertyList.add("state");
232: propertyList.add("zip");
233:
234: SimpleAddress address = new SimpleAddress();
235: ObjectUtil.convertLineToBusinessObject(address, line,
236: fieldLength, propertyList);
237:
238: assertEquals("Street", address.getStreet());
239: assertEquals("City", address.getCity());
240: assertEquals("State", address.getState());
241: assertEquals(new Integer(10000), address.getZip());
242: }
243:
244: public void testSplit() throws Exception {
245: String delim = ";";
246: String line = "" + delim + "" + delim + "" + delim + "" + delim;
247:
248: List<String> tokens = ObjectUtil.split(line, delim);
249: assertEquals(4, tokens.size());
250:
251: line = delim + line;
252: tokens = ObjectUtil.split(line, delim);
253: assertEquals(5, tokens.size());
254: }
255:
256: public void testValueOfInteger() throws Exception {
257: String integerType = "Integer";
258: String[] value = { "-100", "0", "100", "", "12.9", "bad value" };
259: String[] expected = { "-100", "0", "100", null, null, null };
260: for (int i = 0; i < value.length; i++) {
261: String tempvalue = expected[i];
262: Integer expectedValue = tempvalue != null ? Integer
263: .valueOf(expected[i]) : null;
264: assertEquals(expectedValue, ObjectUtil.valueOf(integerType,
265: value[i]));
266: }
267: }
268:
269: public void testValueOfKualiDecimal() throws Exception {
270: String type = "KualiDecimal";
271: String[] value = { "-100.00", "0", "100", "100.00", "",
272: "bad value" };
273: String[] expected = { "-100", "0", "100", "100", null, null };
274: for (int i = 0; i < value.length; i++) {
275: String tempvalue = expected[i];
276: KualiDecimal expectedValue = tempvalue != null ? new KualiDecimal(
277: expected[i])
278: : null;
279: assertEquals(expectedValue, ObjectUtil.valueOf(type,
280: value[i]));
281: }
282: }
283:
284: public void testValueOfDate() throws Exception {
285: String type = "Date";
286: String[] value = { "2000-01-31", "2000/1/1", "1/1/2000", "",
287: "bad value" };
288: String[] expected = { "2000-01-31", null, null, null, null };
289: for (int i = 0; i < value.length; i++) {
290: String tempvalue = expected[i];
291: Date expectedValue = tempvalue != null ? Date
292: .valueOf(expected[i]) : null;
293: assertEquals(expectedValue, ObjectUtil.valueOf(type,
294: value[i]));
295: }
296: }
297:
298: public void testValueOfTimestamp() throws Exception {
299: String type = "Timestamp";
300: String[] value = { "2000-01-31 00:12:00.55", "2000-01-31",
301: "2000/1/1", "1/1/2000", "", "bad value" };
302: String[] expected = { "2000-01-31 00:12:00.55",
303: "2000-01-31 00:00:00.0", null, null, null, null };
304: for (int i = 0; i < value.length; i++) {
305: String tempvalue = expected[i];
306: Timestamp expectedValue = tempvalue != null ? Timestamp
307: .valueOf(expected[i]) : null;
308: assertEquals(expectedValue, ObjectUtil.valueOf(type,
309: value[i]));
310: }
311: }
312:
313: public void testHasNullValueField() throws Exception {
314: SimpleAddress sourceAddress = new SimpleAddress(null, null,
315: null, 9999);
316: assertTrue(ObjectUtil.hasNullValueField(sourceAddress));
317:
318: sourceAddress = new SimpleAddress("1000 Main Street",
319: "Source City", "Kuali", 10000,
320: new KualiDecimal(200000), new Date(1000000));
321: assertTrue(!ObjectUtil.hasNullValueField(sourceAddress));
322: }
323: }
|