01: /******************************************************************************
02: * Copyright (C) Lars Ivar Almli. All rights reserved. *
03: * ---------------------------------------------------------------------------*
04: * This file is part of MActor. *
05: * *
06: * MActor is free software; you can redistribute it and/or modify *
07: * it under the terms of the GNU General Public License as published by *
08: * the Free Software Foundation; either version 2 of the License, or *
09: * (at your option) any later version. *
10: * *
11: * MActor is distributed in the hope that it will be useful, *
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14: * GNU General Public License for more details. *
15: * *
16: * You should have received a copy of the GNU General Public License *
17: * along with MActor; if not, write to the Free Software *
18: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
19: ******************************************************************************/package org.mactor.extensions.xml;
20:
21: import java.util.HashMap;
22: import java.util.List;
23: import java.util.Map.Entry;
24: import org.dom4j.Document;
25: import org.dom4j.Node;
26: import org.dom4j.XPath;
27: import org.mactor.framework.MactorException;
28: import org.mactor.framework.TestContext;
29: import org.mactor.framework.extensioninterface.ActionCommand;
30:
31: /**
32: * Validates that a selected field from the last received message matches a specifed value.
33: * <br/>
34: * The field to select and the value it must be equal to is specified as a single parameter on the form:<br/>
35: * [XPath expression that selects the single attribute or element ]==[value that the selected field must be equal to]
36: * <br>
37: * Namespace information in the evaluated messages is ignored, so the XPath expressions must not include namespace prefixes
38: * @author Lars Ivar Almli
39: */
40: public class XPathIgnoreNsValidator implements ActionCommand {
41: public void perform(TestContext context, List<String> params)
42: throws MactorException {
43: StringBuffer sb = new StringBuffer();
44: Document doc = context.getLastIncomingMessage()
45: .getContentDocumentNoNs();
46: HashMap<XPath, String> map = ParamUtil.parseXPathParams(params);
47: for (Entry<XPath, String> e : map.entrySet()) {
48: Node n = e.getKey().selectSingleNode(doc);
49: String v = n == null ? null : n.getText();
50: if (!ParamUtil.isMatch(v, e.getValue()))
51: sb.append("Validation of '" + e.getKey().getText()
52: + "' failed. Expected '" + e.getValue()
53: + "', but got '" + v + "'");
54: }
55: if (sb.length() != 0)
56: throw new MactorException(sb.toString());
57: }
58: }
|