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.workflow.attribute;
017:
018: import java.io.BufferedReader;
019: import java.io.File;
020: import java.io.FileReader;
021:
022: import javax.xml.xpath.XPath;
023: import javax.xml.xpath.XPathConstants;
024:
025: import org.apache.commons.lang.StringUtils;
026: import org.apache.log4j.Logger;
027: import org.kuali.kfs.KFSConstants;
028: import org.kuali.kfs.context.KualiTestBase;
029: import org.kuali.test.ConfigureContext;
030: import org.kuali.workflow.KualiWorkflowUtils;
031: import org.w3c.dom.Node;
032: import org.w3c.dom.NodeList;
033:
034: @ConfigureContext
035: public class KualiOrgReviewAttributeTest extends KualiTestBase {
036: private static final Logger LOG = Logger
037: .getLogger(KualiOrgReviewAttributeTest.class);
038:
039: private static final String ORG_CD_1 = "PSY";
040: private static final String ORG_CHART_1 = "BL";
041:
042: public void testGetChartOrgCode() throws Exception {
043: assertEquals(
044: "BL-PSY",
045: getChartOrg(
046: "DisbursementVoucherDocument",
047: getDocumentContent("./test/src/org/kuali/workflow/attribute/DVDocContent.xml")));
048: assertEquals(
049: "BL-PSY",
050: getChartOrg(
051: "TransferOfFundsDocument",
052: getDocumentContent("./test/src/org/kuali/workflow/attribute/TOFDocContent.xml")));
053: }
054:
055: private String getChartOrg(String docTypeName,
056: String documentContent) throws Exception {
057: XPath xpath = KualiWorkflowUtils.getXPath(KualiWorkflowUtils
058: .getDocument(documentContent));
059: StringBuffer expression = new StringBuffer(
060: KualiWorkflowUtils.XSTREAM_SAFE_PREFIX);
061: expression
062: .append(KualiWorkflowUtils.XSTREAM_MATCH_ANYWHERE_PREFIX);
063: expression.append(KualiWorkflowUtils
064: .getSourceAccountingLineClassName(docTypeName));
065: expression.append("/account");
066: expression.append(KualiWorkflowUtils.XSTREAM_SAFE_SUFFIX)
067: .toString();
068: NodeList nodes = (NodeList) xpath.evaluate(expression
069: .toString(), documentContent, XPathConstants.NODESET);
070: for (int i = 0; i < nodes.getLength(); i++) {
071: Node accountingLineNode = nodes.item(i);
072: return xpath
073: .evaluate(
074: KualiWorkflowUtils.XSTREAM_MATCH_RELATIVE_PREFIX
075: + KFSConstants.CHART_OF_ACCOUNTS_CODE_PROPERTY_NAME,
076: accountingLineNode)
077: + "-"
078: + xpath
079: .evaluate(
080: KualiWorkflowUtils.XSTREAM_MATCH_RELATIVE_PREFIX
081: + KFSConstants.ORGANIZATION_CODE_PROPERTY_NAME,
082: accountingLineNode);
083: }
084: return null;
085: }
086:
087: public String getDocumentContent(String fileName) throws Exception {
088: File file = new File(".");
089: StringBuffer fileBuffer = new StringBuffer();
090: FileReader fileReader = null;
091: BufferedReader bufferedReader = null;
092: try {
093: fileReader = new FileReader(fileName);
094: bufferedReader = new BufferedReader(fileReader);
095: String l;
096: while ((l = bufferedReader.readLine()) != null) {
097: fileBuffer.append(l);
098: }
099: } finally {
100: if (fileReader != null) {
101: fileReader.close();
102: }
103: if (bufferedReader != null) {
104: bufferedReader.close();
105: }
106: }
107: return fileBuffer.toString();
108: }
109:
110: public void testGetRowVsUtilsBuildTextRow_Org() {
111: KualiOrgReviewAttribute orgReviewAttribute = new KualiOrgReviewAttribute(
112: ORG_CHART_1, ORG_CD_1);
113: edu.iu.uis.eden.lookupable.Row manualOrgRow = orgReviewAttribute
114: .getOrgRow();
115: // edu.iu.uis.eden.lookupable.Row utilsOrgRow = KualiWorkflowUtils.buildTextRowWithLookup(Org.class,
116: // KFSConstants.ORGANIZATION_CODE_PROPERTY_NAME, KualiOrgReviewAttribute.ORG_CD_KEY);
117: edu.iu.uis.eden.lookupable.Row utilsOrgRow = orgReviewAttribute
118: .getOrgRowUsingKualiWorkflowUtils();
119: assertTrue(areEdenRowsEqual(manualOrgRow, utilsOrgRow));
120: }
121:
122: public void testGetRowVsUtilsBuildTextRow_Chart() {
123: KualiOrgReviewAttribute orgReviewAttribute = new KualiOrgReviewAttribute(
124: ORG_CHART_1, ORG_CD_1);
125: edu.iu.uis.eden.lookupable.Row manualChartRow = orgReviewAttribute
126: .getChartRow();
127: // edu.iu.uis.eden.lookupable.Row utilsChartRow = KualiWorkflowUtils.buildTextRowWithLookup(Chart.class,
128: // KFSConstants.CHART_OF_ACCOUNTS_CODE_PROPERTY_NAME, KualiOrgReviewAttribute.FIN_COA_CD_KEY);
129: edu.iu.uis.eden.lookupable.Row utilsChartRow = orgReviewAttribute
130: .getChartRowUsingKualiWorkflowUtils();
131: assertTrue(areEdenRowsEqual(manualChartRow, utilsChartRow));
132: }
133:
134: public void testGetRowVsUtilsBuildTextRow_MismatchedOrgChart() {
135: KualiOrgReviewAttribute orgReviewAttribute = new KualiOrgReviewAttribute(
136: ORG_CHART_1, ORG_CD_1);
137: edu.iu.uis.eden.lookupable.Row manualChartRow = orgReviewAttribute
138: .getChartRow();
139: // edu.iu.uis.eden.lookupable.Row utilsChartRow = KualiWorkflowUtils.buildTextRowWithLookup(Org.class,
140: // KFSConstants.ORGANIZATION_CODE_PROPERTY_NAME, KualiOrgReviewAttribute.ORG_CD_KEY);
141: edu.iu.uis.eden.lookupable.Row utilsChartRow = orgReviewAttribute
142: .getOrgRowUsingKualiWorkflowUtils();
143: assertFalse(areEdenRowsEqual(manualChartRow, utilsChartRow));
144: }
145:
146: public void testAreEdenRowsEqual() {
147: KualiOrgReviewAttribute orgReviewAttribute = new KualiOrgReviewAttribute(
148: ORG_CHART_1, ORG_CD_1);
149: edu.iu.uis.eden.lookupable.Row manualChartRow = orgReviewAttribute
150: .getChartRow();
151: // edu.iu.uis.eden.lookupable.Row utilsChartRow = KualiWorkflowUtils.buildTextRowWithLookup(Chart.class,
152: // KFSConstants.CHART_OF_ACCOUNTS_CODE_PROPERTY_NAME, KualiOrgReviewAttribute.FIN_COA_CD_KEY);
153: edu.iu.uis.eden.lookupable.Row utilsChartRow = orgReviewAttribute
154: .getChartRowUsingKualiWorkflowUtils();
155:
156: assertTrue(areEdenRowsEqual(manualChartRow, manualChartRow));
157: assertTrue(areEdenRowsEqual(utilsChartRow, utilsChartRow));
158: }
159:
160: private boolean areEdenRowsEqual(
161: edu.iu.uis.eden.lookupable.Row row1,
162: edu.iu.uis.eden.lookupable.Row row2) {
163: // null handling
164: if ((row1 == null && row2 != null)
165: || (row1 != null && row2 == null)) {
166: LOG.info("One of row1 and row2 are null.");
167: return false;
168: } else if (row1 == null && row2 == null) {
169: LOG.info("row1 and row2 are both null.");
170: return true;
171: }
172:
173: if (!StringUtils.equals(row1.getRowsGroupLabel(), row2
174: .getRowsGroupLabel())) {
175: LOG.info("getRowsGroupLabel failed test");
176: return false;
177: }
178: if (row1.getNumberOfGroupRows() != row2.getNumberOfGroupRows()) {
179: LOG.info("number of Group Rows failed test");
180: return false;
181: }
182:
183: // field counts
184: if ((row1.getFields() == null && row2.getFields() != null)
185: || (row1.getFields() != null && row2.getFields() == null)) {
186: LOG
187: .info("One of the getFields() is null while the other isnt.");
188: return false;
189: } else if (row1.getFields() != null && row2.getFields() != null) {
190: if (row1.getFields().size() != row2.getFields().size()) {
191: LOG
192: .info("The two rows have different number of fields.");
193: return false;
194: }
195: edu.iu.uis.eden.lookupable.Field field1;
196: edu.iu.uis.eden.lookupable.Field field2;
197: for (int i = 0; i < row1.getFields().size(); i++) {
198: field1 = row1.getField(i);
199: field2 = row2.getField(i);
200: if (!areEdenFieldsEqual(field1, field2)) {
201: LOG
202: .info("Field #"
203: + i
204: + " isnt the same between field1 and field2.");
205: return false;
206: }
207: }
208: }
209: return true;
210: }
211:
212: private boolean areEdenFieldsEqual(
213: edu.iu.uis.eden.lookupable.Field field1,
214: edu.iu.uis.eden.lookupable.Field field2) {
215: // null handling
216: if ((field1 == null && field2 != null)
217: || (field1 != null && field2 == null)) {
218: LOG
219: .info("One of field1 and field2 is null, but the other isnt.");
220: return false;
221: } else if (field1 == null && field2 == null) {
222: LOG.info("Both field1 and field2 are null.");
223: return true;
224: }
225:
226: if (!StringUtils.equals(field1.getDefaultLookupableName(),
227: field2.getDefaultLookupableName())) {
228: LOG
229: .info("getDefaultLookupableName has failed equality test.");
230: return false;
231: }
232: if (!StringUtils.equals(field1.getFieldHelpUrl(), field2
233: .getFieldHelpUrl())) {
234: LOG.info("getFieldHelpUrl has failed equality test.");
235: return false;
236: }
237: if (!StringUtils.equals(field1.getFieldLabel(), field2
238: .getFieldLabel())) {
239: LOG.info("getFieldLabel has failed equality test.");
240: return false;
241: }
242: if (!StringUtils.equals(field1.getFieldType(), field2
243: .getFieldType())) {
244: LOG.info("getFieldType has failed equality test.");
245: return false;
246: }
247: if (!StringUtils.equals(field1.getPropertyName(), field2
248: .getPropertyName())) {
249: LOG.info("getPropertyName has failed equality test.");
250: return false;
251: }
252: if (!StringUtils.equals(field1.getPropertyValue(), field2
253: .getPropertyValue())) {
254: LOG.info("getPropertyValue has failed equality test.");
255: return false;
256: }
257: if (!StringUtils.equals(field1.getQuickFinderClassNameImpl(),
258: field2.getQuickFinderClassNameImpl())) {
259: LOG
260: .info("getQuickFinderClassNameImpl has failed equality test.");
261: return false;
262: }
263: if (field1.isColumnVisible() != field2.isColumnVisible()) {
264: LOG.info("isColumnVisible has failed equality test.");
265: return false;
266: }
267: if (field1.isHasLookupable() != field2.isHasLookupable()) {
268: LOG.info("isHasLookupable has failed equality test.");
269: return false;
270: }
271:
272: if ((field1.getFieldValidValues() == null && field2
273: .getFieldValidValues() != null)
274: || (field1.getFieldValidValues() != null && field2
275: .getFieldValidValues() == null)) {
276: LOG
277: .info("One of the getFieldValidValues is null while the other isnt.");
278: return false;
279: } else if (field1.getFieldValidValues() != null
280: && field2.getFieldValidValues() != null) {
281: if (field1.getFieldValidValues().size() != field2
282: .getFieldValidValues().size()) {
283: LOG
284: .info("The two fields have FieldValidValues with different number of elements.");
285: return false;
286: }
287: }
288: return true;
289: }
290:
291: }
|