001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.test.web.framework.actions;
018:
019: import java.util.regex.Matcher;
020: import java.util.regex.Pattern;
021:
022: import junit.framework.Assert;
023:
024: import org.w3c.dom.Node;
025:
026: import edu.iu.uis.eden.test.web.framework.Property;
027: import edu.iu.uis.eden.test.web.framework.PropertyScheme;
028: import edu.iu.uis.eden.test.web.framework.Script;
029: import edu.iu.uis.eden.test.web.framework.Util;
030:
031: /**
032: * ScriptAction that asserts an expected property value
033: * <pre>
034: * <assert actual=".." ( regex="..." | expected="..." ) />
035: * <assert actual="..">...</assert>
036: * </pre>
037: * The 'actual', 'regex' and 'expected' attributes are resolved via {@link edu.iu.uis.eden.test.web.framework.Util#getResolvableAttribute(Node, String, PropertyScheme)}.
038: * The 'actual' attribute defautls to variable scheme.
039: * The 'regex' and 'expected' attributes default to literal scheme.
040: * @author Aaron Hamid (arh14 at cornell dot edu)
041: */
042: public class AssertAction extends BaseScriptAction {
043: private static final String[] NAMES = { "assert" };
044:
045: public String[] getNames() {
046: return NAMES;
047: }
048:
049: public void process(Script script, Node node) {
050: Property actualProp = Util.getResolvableAttribute(node,
051: "actual", PropertyScheme.VARIABLE_SCHEME);
052: if (actualProp == null) {
053: String message = "'actual' attribute must be specified for 'assert' element";
054: log.error(message);
055: throw new RuntimeException(message);
056: }
057:
058: Object actualValue = script.getState().retrieveProperty(
059: actualProp);
060: //LOG.debug("Actual value of '" + actualProp + "': " + actualValue);
061: //LOG.debug("Value of CONTEXT.postProcessorListenerCallbacks: " + retreiveProperty(variables, "CONTEXT.postProcessorListenerCallbacks"));
062:
063: Property regexProp = Util.getResolvableAttribute(node, "regex",
064: PropertyScheme.LITERAL_SCHEME);
065: if (regexProp != null) {
066: Object regexValue = script.getState().retrieveProperty(
067: regexProp);
068: if (regexValue == null) {
069: String message = "Could not load regex property: "
070: + regexProp;
071: log.error(message);
072: throw new RuntimeException(message);
073: }
074: log.info("Checking '" + actualValue + "' against regex: '"
075: + regexValue + "'");
076: Matcher matcher = Pattern.compile(regexValue.toString())
077: .matcher((CharSequence) actualValue);
078: Assert.assertTrue("Value does not match regex '"
079: + regexValue + "'", matcher.matches());
080: return;
081: }
082:
083: Property expectedProp = Util.getResolvableAttribute(node,
084: "expected", PropertyScheme.LITERAL_SCHEME);
085: Object expectedValue = null;
086: if (expectedProp == null) {
087: expectedValue = Util.getContent(node);
088: if (expectedValue == null) {
089: log
090: .warn("Neither expected property nor content was specified");
091: }
092: } else {
093: expectedValue = script.getState().retrieveProperty(
094: expectedProp);
095: }
096:
097: log.debug("Checking expected " + expectedProp + " '"
098: + expectedValue + "' against " + actualProp + " '"
099: + actualValue + "'");
100: if (expectedValue instanceof String
101: && actualValue instanceof String) {
102: // do this so JUnit can display diff pane
103: Assert.assertEquals((String) expectedValue,
104: (String) actualValue);
105: } else {
106: Assert.assertEquals(expectedValue, actualValue);
107: }
108: }
109: }
|