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.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.List;
021: import java.util.Map;
022: import java.util.Properties;
023:
024: import org.apache.commons.lang.StringUtils;
025: import org.kuali.module.gl.bo.OriginEntryGroup;
026: import org.kuali.module.labor.bo.LaborOriginEntry;
027:
028: /**
029: * provide with a set of utilities that can be used to prepare test data for unit testing. The core idea is to convert Java
030: * properties into a list of specified business objects or search criteria.
031: */
032: public class TestDataPreparator {
033: public static final String DEFAULT_FIELD_NAMES = "fieldNames";
034: public static final String DEFAULT_DELIMINATOR = "deliminator";
035:
036: /**
037: * build a list of objects of type "clazz" from the test data provided by the given properties. The default fieldNames and
038: * deliminator are used.
039: *
040: * @param clazz the the specified object type
041: * @param properties the given properties that contain the test data
042: * @param propertyKeyPrefix the test data with the given key prefix can be used to construct the return objects
043: * @param numberOfData the number of test data matching the search criteria
044: * @return a list of objects of type "clazz" from the test data provided by the given properties
045: */
046: public static List buildTestDataList(Class clazz,
047: Properties properties, String propertyKeyPrefix,
048: int numberOfData) {
049: String fieldNames = properties.getProperty(DEFAULT_FIELD_NAMES);
050: String deliminator = properties
051: .getProperty(DEFAULT_DELIMINATOR);
052: return buildTestDataList(clazz, properties, propertyKeyPrefix,
053: fieldNames, deliminator, numberOfData);
054: }
055:
056: /**
057: * build a list of objects of type "clazz" from the test data provided by the given properties
058: *
059: * @param clazz the the specified object type
060: * @param properties the given properties that contain the test data
061: * @param propertyKeyPrefix the test data with the given key prefix can be used to construct the return objects
062: * @param fieldNames the field names of the test data columns
063: * @param deliminator the deliminator that is used to separate the field from each other
064: * @param numberOfData the number of test data matching the search criteria
065: * @return a list of objects of type "clazz" from the test data provided by the given properties
066: */
067: public static List buildTestDataList(Class clazz,
068: Properties properties, String propertyKeyPrefix,
069: String fieldNames, String deliminator, int numberOfData) {
070: List testDataList = new ArrayList();
071: for (int i = 1; i <= numberOfData; i++) {
072: String propertyKey = propertyKeyPrefix + i;
073: try {
074: Object testData = clazz.newInstance();
075: ObjectUtil.populateBusinessObject(testData, properties,
076: propertyKey, fieldNames, deliminator);
077: testDataList.add(testData);
078: } catch (Exception e) {
079: e.printStackTrace();
080: }
081: }
082: return testDataList;
083: }
084:
085: /**
086: * build a list of objects of type "clazz" from the expected results provided by the given properties. The default fieldNames
087: * and deliminator are used.
088: *
089: * @param clazz the the specified object type. The instance of this type should be comparable through overriding Object.equals()
090: * @param properties the given properties that contain the expected results
091: * @param propertyKeyPrefix the expected results with the given key prefix can be used to construct the return objects
092: * @param numberOfData the number of the expected results matching the search criteria
093: * @return a list of objects of type "clazz" from the expected results provided by the given properties
094: */
095: public static List buildExpectedValueList(Class clazz,
096: Properties properties, String propertyKeyPrefix,
097: int numberOfData) {
098: String fieldNames = properties.getProperty(DEFAULT_FIELD_NAMES);
099: String deliminator = properties
100: .getProperty(DEFAULT_DELIMINATOR);
101: return buildExpectedValueList(clazz, properties,
102: propertyKeyPrefix, fieldNames, deliminator,
103: numberOfData);
104: }
105:
106: /**
107: * build a list of objects of type "clazz" from the expected results provided by the given properties
108: *
109: * @param clazz the the specified object type. The instance of this type should be comparable through overriding Object.equals()
110: * @param properties the given properties that contain the expected results
111: * @param propertyKeyPrefix the expected results with the given key prefix can be used to construct the return objects
112: * @param fieldNames the field names of the expected results columns
113: * @param deliminator the deliminator that is used to separate the field from each other
114: * @param numberOfData the number of the expected results matching the search criteria
115: * @return a list of objects of type "clazz" from the expected results provided by the given properties
116: */
117: public static List buildExpectedValueList(Class clazz,
118: Properties properties, String propertyKeyPrefix,
119: String fieldNames, String deliminator, int numberOfData) {
120: List expectedDataList = new ArrayList();
121: for (int i = 1; i <= numberOfData; i++) {
122: String propertyKey = propertyKeyPrefix + i;
123: try {
124: Object expectedData = clazz.newInstance();
125: ObjectUtil.populateBusinessObject(expectedData,
126: properties, propertyKey, fieldNames,
127: deliminator);
128:
129: if (!expectedDataList.contains(expectedData)) {
130: expectedDataList.add(expectedData);
131: }
132: } catch (Exception e) {
133: e.printStackTrace();
134: }
135: }
136: return expectedDataList;
137: }
138:
139: /**
140: * build the cleanup criteria for "clazz" from the given properties. The default fieldNames and deliminator are used.
141: *
142: * @param clazz the the specified object type.
143: * @param properties the given properties that contain the cleanup criteria fields and values
144: * @param propertyKey the given property whose value provides the cleanup criteria values
145: * @return the cleanup criteria for "clazz" from the given properties
146: */
147: public static Map<String, Object> buildCleanupCriteria(Class clazz,
148: Properties properties, String propertyKey) {
149: String fieldNames = properties.getProperty(DEFAULT_FIELD_NAMES);
150: String deliminator = properties
151: .getProperty(DEFAULT_DELIMINATOR);
152: return buildCleanupCriteria(clazz, properties, propertyKey,
153: fieldNames, deliminator);
154: }
155:
156: /**
157: * build the cleanup criteria for "clazz" from the given properties.
158: *
159: * @param clazz the the specified object type.
160: * @param properties the given properties that contain the cleanup criteria fields and values
161: * @param propertyKey the given property whose value provides the cleanup criteria values
162: * @param fieldNames the field names of the cleanup columns
163: * @param deliminator the deliminator that is used to separate the field from each other
164: * @return the cleanup criteria for "clazz" from the given properties
165: */
166: public static Map<String, Object> buildCleanupCriteria(Class clazz,
167: Properties properties, String propertyKey,
168: String fieldNames, String deliminator) {
169: Object instanceOfClazz = null;
170: try {
171: instanceOfClazz = clazz.newInstance();
172: } catch (Exception e) {
173: e.printStackTrace();
174: return null;
175: }
176:
177: ObjectUtil.populateBusinessObject(instanceOfClazz, properties,
178: propertyKey, fieldNames, deliminator);
179: return ObjectUtil.buildPropertyMap(instanceOfClazz, Arrays
180: .asList(StringUtils.split(fieldNames, deliminator)));
181: }
182:
183: /**
184: * build a list of LaborOriginEntry objects from the given properties. The default fieldNames and deliminator are used.
185: *
186: * @param properties the given properties that contain the data that can be used to populate LaborOriginEntry objects
187: * @param propertyKeyPrefix the data with the given key prefix can be used to construct the return objects
188: * @param fieldNames the field names of the data columns
189: * @param deliminator the deliminator that is used to separate the field from each other
190: * @param numberOfData the number of the data matching the search criteria
191: * @return a list of LaborOriginEntry objects from the given properties
192: */
193: public static List<LaborOriginEntry> getLaborOriginEntryList(
194: Properties properties, String propertyKeyPrefix,
195: int numberOfInputData, OriginEntryGroup group) {
196: String fieldNames = properties.getProperty(DEFAULT_FIELD_NAMES);
197: String deliminator = properties
198: .getProperty(DEFAULT_DELIMINATOR);
199: return getLaborOriginEntryList(properties, propertyKeyPrefix,
200: fieldNames, deliminator, numberOfInputData, group);
201: }
202:
203: /**
204: * build a list of LaborOriginEntry objects from the given properties
205: *
206: * @param properties the given properties that contain the data that can be used to populate LaborOriginEntry objects
207: * @param propertyKeyPrefix the data with the given key prefix can be used to construct the return objects
208: * @param fieldNames the field names of the data columns
209: * @param deliminator the deliminator that is used to separate the field from each other
210: * @param numberOfData the number of the data matching the search criteria
211: * @return a list of LaborOriginEntry objects from the given properties
212: */
213: public static List<LaborOriginEntry> getLaborOriginEntryList(
214: Properties properties, String propertyKeyPrefix,
215: String fieldNames, String deliminator,
216: int numberOfInputData, OriginEntryGroup group) {
217: List<LaborOriginEntry> inputDataList = new ArrayList<LaborOriginEntry>();
218: for (int i = 1; i <= numberOfInputData; i++) {
219: String propertyKey = propertyKeyPrefix + i;
220: LaborOriginEntry inputData = new LaborOriginEntry();
221: ObjectUtil.populateBusinessObject(inputData, properties,
222: propertyKey, fieldNames, deliminator);
223: inputData.setEntryGroupId(group.getId());
224: inputDataList.add(inputData);
225: }
226: return inputDataList;
227: }
228: }
|