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.IOException;
019:
020: import javax.xml.xpath.XPath;
021: import javax.xml.xpath.XPathConstants;
022: import javax.xml.xpath.XPathExpressionException;
023:
024: import org.kuali.kfs.context.KualiTestBase;
025: import org.kuali.test.ConfigureContext;
026: import org.kuali.workflow.KualiWorkflowUtils;
027:
028: import edu.iu.uis.eden.exception.InvalidXmlException;
029: import edu.iu.uis.eden.routeheader.DocumentContent;
030:
031: /**
032: * This class...
033: */
034: @ConfigureContext
035: public class KualiAttributeXPathTest extends KualiTestBase {
036:
037: private static final String KUALI_SUBFUND_GROUP_ATTRIBUTE_SOURCE = "//org.kuali.kfs.bo.SourceAccountingLine/account/subFundGroupCode";
038: private static final String KUALI_SUBFUND_GROUP_ATTRIBUTE_TARGET = "//org.kuali.kfs.bo.TargetAccountingLine/account/subFundGroupCode";
039: private static final String KUALI_SUBFUND_GROUP_ATTRIBUTE = KUALI_SUBFUND_GROUP_ATTRIBUTE_SOURCE
040: + " or " + KUALI_SUBFUND_GROUP_ATTRIBUTE_TARGET;
041: private static final String KUALI_SUBFUND_GROUP_ATTRIBUTE_XSTREAMSAFE = "wf:xstreamsafe('"
042: + KUALI_SUBFUND_GROUP_ATTRIBUTE_SOURCE
043: + "') or wf:xstreamsafe('"
044: + KUALI_SUBFUND_GROUP_ATTRIBUTE_TARGET + "')";
045: private static final String KUALI_SUBFUND_GROUP_ATTRIBUTE_SOURCE_XSTREAMSAFE = "wf:xstreamsafe('//org.kuali.kfs.bo.SourceAccountingLine/account/subFundGroupCode')";
046: private static final String KUALI_SUBFUND_GROUP_ATTRIBUTE_TARGET_XSTREAMSAFE = "wf:xstreamsafe('//org.kuali.kfs.bo.TargetAccountingLine/account/subFundGroupCode')";
047: private static final String KUALI_CAMPUS_TYPE_ACTIVE_INDICATOR_XSTREAMSAFE = KualiWorkflowUtils.XSTREAM_SAFE_PREFIX
048: + KualiWorkflowUtils.XSTREAM_MATCH_ANYWHERE_PREFIX
049: + "campus/campusType/dataObjectMaintenanceCodeActiveIndicator"
050: + KualiWorkflowUtils.XSTREAM_SAFE_SUFFIX;
051: private static final String KUALI_INITIATOR_UNIVERSAL_USER_STUDENT_INDICATOR_XSTREAMSAFE = KualiWorkflowUtils.XSTREAM_SAFE_PREFIX
052: + KualiWorkflowUtils.XSTREAM_MATCH_ANYWHERE_PREFIX
053: + "kualiTransactionalDocumentInformation/documentInitiator/universalUser/student"
054: + KualiWorkflowUtils.XSTREAM_SAFE_SUFFIX;
055:
056: public void testKualiSubFundGroupAttribute_TransferOfFunds1()
057: throws IOException, InvalidXmlException,
058: XPathExpressionException {
059:
060: DocumentContent docContent = KualiAttributeTestUtil
061: .getDocumentContentFromXmlFile(
062: "TransferOfFunds_FEMPSubcode_OneLiner.xml",
063: "TransferOfFundsDocument");
064: XPath xpath = KualiWorkflowUtils.getXPath(docContent
065: .getDocument());
066:
067: String xpathResult;
068:
069: // make sure the OR statement works
070: xpathResult = (String) xpath.evaluate(
071: KUALI_SUBFUND_GROUP_ATTRIBUTE,
072: docContent.getDocument(), XPathConstants.STRING);
073: assertEquals("true", xpathResult);
074:
075: // make sure the source FEMP exists
076: xpathResult = (String) xpath.evaluate(
077: KUALI_SUBFUND_GROUP_ATTRIBUTE_SOURCE, docContent
078: .getDocument(), XPathConstants.STRING);
079: assertEquals("FEMP", xpathResult);
080:
081: // make sure the target FEMP exists
082: xpathResult = (String) xpath.evaluate(
083: KUALI_SUBFUND_GROUP_ATTRIBUTE_TARGET, docContent
084: .getDocument(), XPathConstants.STRING);
085: assertEquals("FEMP", xpathResult);
086:
087: // make sure the OR statement works with xstreamsafe
088: xpathResult = (String) xpath.evaluate(
089: KUALI_SUBFUND_GROUP_ATTRIBUTE_XSTREAMSAFE, docContent
090: .getDocument(), XPathConstants.STRING);
091: assertEquals("true", xpathResult);
092:
093: // make sure the source FEMP exists with xstreamsafe
094: xpathResult = (String) xpath.evaluate(
095: KUALI_SUBFUND_GROUP_ATTRIBUTE_SOURCE_XSTREAMSAFE,
096: docContent.getDocument(), XPathConstants.STRING);
097: assertEquals("FEMP", xpathResult);
098:
099: // make sure the target FEMP exists with xstreamsafe
100: xpathResult = (String) xpath.evaluate(
101: KUALI_SUBFUND_GROUP_ATTRIBUTE_TARGET_XSTREAMSAFE,
102: docContent.getDocument(), XPathConstants.STRING);
103: assertEquals("FEMP", xpathResult);
104:
105: }
106:
107: public void testKualiIndicatorTranslationAttributeXPath()
108: throws IOException, InvalidXmlException,
109: XPathExpressionException {
110: DocumentContent docContent = KualiAttributeTestUtil
111: .getDocumentContentFromXmlFileAndPath(
112: KualiAttributeTestUtil.PURCHASE_ORDER_DOCUMENT,
113: KualiAttributeTestUtil.RELATIVE_PATH_IN_PROJECT_WORKFLOW,
114: "PurchaseOrderDocument");
115: XPath xpath = KualiWorkflowUtils.getXPath(docContent
116: .getDocument());
117:
118: String valueForTrue = "Yes";
119: String valueForFalse = "No";
120:
121: // test campus active indicator field translation to 'Yes'
122: String xpathConditionStatement = KUALI_CAMPUS_TYPE_ACTIVE_INDICATOR_XSTREAMSAFE
123: + " = 'true'";
124: String xpathExpression = constructXpathExpression(valueForTrue,
125: valueForFalse, xpathConditionStatement);
126: String xpathResult = (String) xpath.evaluate(xpathExpression,
127: docContent.getDocument(), XPathConstants.STRING);
128: assertEquals("Using translated xpath expression '"
129: + xpathExpression + "'", valueForTrue, xpathResult);
130:
131: // test user student indicator translation to 'No'
132: xpathConditionStatement = KUALI_INITIATOR_UNIVERSAL_USER_STUDENT_INDICATOR_XSTREAMSAFE
133: + " = 'true'";
134: xpathExpression = constructXpathExpression(valueForTrue,
135: valueForFalse, xpathConditionStatement);
136: xpathResult = (String) xpath.evaluate(xpathExpression,
137: docContent.getDocument(), XPathConstants.STRING);
138: assertEquals("Using translated xpath expression '"
139: + xpathExpression + "'", valueForFalse, xpathResult);
140:
141: // test filled in date field translates to 'Yes'
142: String expression = KualiWorkflowUtils.XSTREAM_SAFE_PREFIX
143: + KualiWorkflowUtils.XSTREAM_MATCH_ANYWHERE_PREFIX
144: + "document/purchaseOrderCreateDate"
145: + KualiWorkflowUtils.XSTREAM_SAFE_SUFFIX;
146: xpathConditionStatement = "boolean(" + expression
147: + ") and not(" + expression + " = '')";
148: xpathExpression = constructXpathExpression(valueForTrue,
149: valueForFalse, xpathConditionStatement);
150: xpathResult = (String) xpath.evaluate(xpathExpression,
151: docContent.getDocument(), XPathConstants.STRING);
152: assertEquals("Using translated xpath expression '"
153: + xpathExpression + "'", valueForTrue, xpathResult);
154:
155: // test empty date field translates to 'No'
156: expression = KualiWorkflowUtils.XSTREAM_SAFE_PREFIX
157: + KualiWorkflowUtils.XSTREAM_MATCH_ANYWHERE_PREFIX
158: + "document/oldPurchaseOrderCreateDate"
159: + KualiWorkflowUtils.XSTREAM_SAFE_SUFFIX;
160: xpathConditionStatement = "boolean(" + expression
161: + ") and not(" + expression + " = '')";
162: xpathExpression = constructXpathExpression(valueForTrue,
163: valueForFalse, xpathConditionStatement);
164: xpathResult = (String) xpath.evaluate(xpathExpression,
165: docContent.getDocument(), XPathConstants.STRING);
166: assertEquals("Using translated xpath expression '"
167: + xpathExpression + "'", valueForFalse, xpathResult);
168:
169: // test non-existant date field translates to 'No'
170: expression = KualiWorkflowUtils.XSTREAM_SAFE_PREFIX
171: + KualiWorkflowUtils.XSTREAM_MATCH_ANYWHERE_PREFIX
172: + "document/newPurchaseOrderCreateDate"
173: + KualiWorkflowUtils.XSTREAM_SAFE_SUFFIX;
174: xpathConditionStatement = "boolean(" + expression
175: + ") and not(" + expression + " = '')";
176: xpathExpression = constructXpathExpression(valueForTrue,
177: valueForFalse, xpathConditionStatement);
178: xpathResult = (String) xpath.evaluate(xpathExpression,
179: docContent.getDocument(), XPathConstants.STRING);
180: assertEquals("Using translated xpath expression '"
181: + xpathExpression + "'", valueForFalse, xpathResult);
182: }
183:
184: private String constructXpathExpression(String valueForTrue,
185: String valueForFalse, String booleanXPathExpression) {
186: String[] xpathElementsToInsert = new String[3];
187: xpathElementsToInsert[0] = "concat( substring('" + valueForTrue
188: + "', number(not(";
189: xpathElementsToInsert[1] = "))*string-length('" + valueForTrue
190: + "')+1), substring('" + valueForFalse + "', number(";
191: xpathElementsToInsert[2] = ")*string-length('" + valueForFalse
192: + "')+1))";
193:
194: StringBuffer returnableString = new StringBuffer();
195: for (int i = 0; i < xpathElementsToInsert.length; i++) {
196: String newXpathElement = xpathElementsToInsert[i];
197: returnableString.append(newXpathElement);
198:
199: /*
200: * Append the given xpath expression onto the end of the stringbuffer only in the following cases - if there is only one
201: * element in the string array - if there is more than one element in the string array and if the current element is not
202: * the last element
203: */
204: if (((i + 1) != xpathElementsToInsert.length)
205: || (xpathElementsToInsert.length == 1)) {
206: returnableString.append(booleanXPathExpression);
207: }
208: }
209: return returnableString.toString();
210:
211: }
212:
213: public void testConcatFunctionWithNonExistantNode()
214: throws IOException, InvalidXmlException,
215: XPathExpressionException {
216: DocumentContent docContent = KualiAttributeTestUtil
217: .getDocumentContentFromXmlFileAndPath(
218: KualiAttributeTestUtil.PURCHASE_ORDER_DOCUMENT,
219: KualiAttributeTestUtil.RELATIVE_PATH_IN_PROJECT_WORKFLOW,
220: "PurchaseOrderDocument");
221: XPath xpath = KualiWorkflowUtils.getXPath(docContent
222: .getDocument());
223:
224: String tempXpathNugget = KualiWorkflowUtils.XSTREAM_MATCH_ANYWHERE_PREFIX
225: + "campus/campusType/dataObjectMaintenanceCodeActiveIndicator";
226: String xpathExistingNodeStatement = "("
227: + KualiWorkflowUtils.XSTREAM_SAFE_PREFIX
228: + tempXpathNugget
229: + KualiWorkflowUtils.XSTREAM_SAFE_SUFFIX + ")";
230: String xpathNonExistingNodeStatement = "("
231: + KualiWorkflowUtils.XSTREAM_SAFE_PREFIX
232: + tempXpathNugget + "/dummystuff"
233: + KualiWorkflowUtils.XSTREAM_SAFE_SUFFIX + ")";
234: String xpathExpression = "concat(" + xpathExistingNodeStatement
235: + ", " + xpathNonExistingNodeStatement + ")";
236: String xpathResult = (String) xpath.evaluate(xpathExpression,
237: docContent.getDocument(), XPathConstants.STRING);
238: assertEquals("true", xpathResult);
239:
240: xpathExpression = "concat(" + xpathNonExistingNodeStatement
241: + ", " + xpathExistingNodeStatement + ")";
242: xpathResult = (String) xpath.evaluate(xpathExpression,
243: docContent.getDocument(), XPathConstants.STRING);
244: assertEquals("true", xpathResult);
245:
246: xpathExpression = "concat(" + xpathNonExistingNodeStatement
247: + ", " + xpathNonExistingNodeStatement + ")";
248: xpathResult = (String) xpath.evaluate(xpathExpression,
249: docContent.getDocument(), XPathConstants.STRING);
250: assertEquals("", xpathResult);
251: }
252: }
|