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.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 tests the KualiXMLBooleanTranslatorSearchableAttributeImpl class
033: */
034: @ConfigureContext
035: public class KualiXMLBooleanTranslatorSearchableAttributeImplTest
036: extends KualiTestBase {
037:
038: private static final String KUALI_CAMPUS_TYPE_ACTIVE_INDICATOR_XSTREAMSAFE = KualiWorkflowUtils.XSTREAM_SAFE_PREFIX
039: + KualiWorkflowUtils.XSTREAM_MATCH_ANYWHERE_PREFIX
040: + "campus/campusType/dataObjectMaintenanceCodeActiveIndicator"
041: + KualiWorkflowUtils.XSTREAM_SAFE_SUFFIX;
042: private static final String KUALI_INITIATOR_UNIVERSAL_USER_STUDENT_INDICATOR_XSTREAMSAFE = KualiWorkflowUtils.XSTREAM_SAFE_PREFIX
043: + KualiWorkflowUtils.XSTREAM_MATCH_ANYWHERE_PREFIX
044: + "kualiTransactionalDocumentInformation/documentInitiator/universalUser/student"
045: + KualiWorkflowUtils.XSTREAM_SAFE_SUFFIX;
046:
047: public void testKualiIndicatorTranslationAttributeXPath()
048: throws IOException, InvalidXmlException,
049: XPathExpressionException {
050: DocumentContent docContent = KualiAttributeTestUtil
051: .getDocumentContentFromXmlFileAndPath(
052: KualiAttributeTestUtil.PURCHASE_ORDER_DOCUMENT,
053: KualiAttributeTestUtil.RELATIVE_PATH_IN_PROJECT_WORKFLOW,
054: "PurchaseOrderDocument");
055: XPath xpath = KualiWorkflowUtils.getXPath(docContent
056: .getDocument());
057:
058: String valueForTrue = "Yes";
059: String valueForFalse = "No";
060:
061: // test campus active indicator field translation to 'Yes'
062: String xpathConditionStatement = KUALI_CAMPUS_TYPE_ACTIVE_INDICATOR_XSTREAMSAFE
063: + " = 'true'";
064: String xpathExpression = constructXpathExpression(valueForTrue,
065: valueForFalse, xpathConditionStatement);
066: String xpathResult = (String) xpath.evaluate(xpathExpression,
067: docContent.getDocument(), XPathConstants.STRING);
068: assertEquals("Using translated xpath expression '"
069: + xpathExpression + "'", valueForTrue, xpathResult);
070:
071: // test user student indicator translation to 'No'
072: xpathConditionStatement = KUALI_INITIATOR_UNIVERSAL_USER_STUDENT_INDICATOR_XSTREAMSAFE
073: + " = 'true'";
074: xpathExpression = constructXpathExpression(valueForTrue,
075: valueForFalse, xpathConditionStatement);
076: xpathResult = (String) xpath.evaluate(xpathExpression,
077: docContent.getDocument(), XPathConstants.STRING);
078: assertEquals("Using translated xpath expression '"
079: + xpathExpression + "'", valueForFalse, xpathResult);
080:
081: // test filled in date field translates to 'Yes'
082: String expression = KualiWorkflowUtils.XSTREAM_SAFE_PREFIX
083: + KualiWorkflowUtils.XSTREAM_MATCH_ANYWHERE_PREFIX
084: + "document/purchaseOrderCreateDate"
085: + KualiWorkflowUtils.XSTREAM_SAFE_SUFFIX;
086: xpathConditionStatement = "boolean(" + expression
087: + ") and not(" + expression + " = '')";
088: xpathExpression = constructXpathExpression(valueForTrue,
089: valueForFalse, xpathConditionStatement);
090: xpathResult = (String) xpath.evaluate(xpathExpression,
091: docContent.getDocument(), XPathConstants.STRING);
092: assertEquals("Using translated xpath expression '"
093: + xpathExpression + "'", valueForTrue, xpathResult);
094:
095: // test empty date field translates to 'No'
096: expression = KualiWorkflowUtils.XSTREAM_SAFE_PREFIX
097: + KualiWorkflowUtils.XSTREAM_MATCH_ANYWHERE_PREFIX
098: + "document/oldPurchaseOrderCreateDate"
099: + KualiWorkflowUtils.XSTREAM_SAFE_SUFFIX;
100: xpathConditionStatement = "boolean(" + expression
101: + ") and not(" + expression + " = '')";
102: xpathExpression = constructXpathExpression(valueForTrue,
103: valueForFalse, xpathConditionStatement);
104: xpathResult = (String) xpath.evaluate(xpathExpression,
105: docContent.getDocument(), XPathConstants.STRING);
106: assertEquals("Using translated xpath expression '"
107: + xpathExpression + "'", valueForFalse, xpathResult);
108:
109: // test non-existant date field translates to 'No'
110: expression = KualiWorkflowUtils.XSTREAM_SAFE_PREFIX
111: + KualiWorkflowUtils.XSTREAM_MATCH_ANYWHERE_PREFIX
112: + "document/newPurchaseOrderCreateDate"
113: + KualiWorkflowUtils.XSTREAM_SAFE_SUFFIX;
114: xpathConditionStatement = "boolean(" + expression
115: + ") and not(" + expression + " = '')";
116: xpathExpression = constructXpathExpression(valueForTrue,
117: valueForFalse, xpathConditionStatement);
118: xpathResult = (String) xpath.evaluate(xpathExpression,
119: docContent.getDocument(), XPathConstants.STRING);
120: assertEquals("Using translated xpath expression '"
121: + xpathExpression + "'", valueForFalse, xpathResult);
122: }
123:
124: private String constructXpathExpression(String valueForTrue,
125: String valueForFalse, String booleanXPathExpression) {
126: String[] xpathElementsToInsert = new String[3];
127: xpathElementsToInsert[0] = "concat( substring('" + valueForTrue
128: + "', number(not(";
129: xpathElementsToInsert[1] = "))*string-length('" + valueForTrue
130: + "')+1), substring('" + valueForFalse + "', number(";
131: xpathElementsToInsert[2] = ")*string-length('" + valueForFalse
132: + "')+1))";
133:
134: StringBuffer returnableString = new StringBuffer();
135: for (int i = 0; i < xpathElementsToInsert.length; i++) {
136: String newXpathElement = xpathElementsToInsert[i];
137: returnableString.append(newXpathElement);
138:
139: /*
140: * Append the given xpath expression onto the end of the stringbuffer only in the following cases - if there is only one
141: * element in the string array - if there is more than one element in the string array and if the current element is not
142: * the last element
143: */
144: if (((i + 1) != xpathElementsToInsert.length)
145: || (xpathElementsToInsert.length == 1)) {
146: returnableString.append(booleanXPathExpression);
147: }
148: }
149: return returnableString.toString();
150:
151: }
152:
153: }
|