001 /****************************************************************************** 002 * Copyright (C) MActor Developers. All rights reserved. * 003 * ---------------------------------------------------------------------------* 004 * This file is part of MActor. * 005 * * 006 * MActor is free software; you can redistribute it and/or modify * 007 * it under the terms of the GNU General Public License as published by * 008 * the Free Software Foundation; either version 2 of the License, or * 009 * (at your option) any later version. * 010 * * 011 * MActor is distributed in the hope that it will be useful, * 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 014 * GNU General Public License for more details. * 015 * * 016 * You should have received a copy of the GNU General Public License * 017 * along with MActor; if not, write to the Free Software * 018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * 019 ******************************************************************************/ 020 package org.mactor.extensions.xml; 021 022 import java.util.HashMap; 023 import java.util.List; 024 import java.util.Map.Entry; 025 026 import org.dom4j.Document; 027 import org.dom4j.Node; 028 import org.dom4j.XPath; 029 import org.mactor.brokers.Message; 030 import org.mactor.framework.MactorException; 031 import org.mactor.framework.TestContext; 032 import org.mactor.framework.extensioninterface.ActionCommand; 033 034 /** 035 * Validates that a selected field from the last received message matches a 036 * specifed value. <br/> The field to select and the value it must be equal to 037 * is specified as a single parameter on the form:<br/> [XPath expression that 038 * selects the single attribute or element ]==[a regular expression that the 039 * field must match] <br> 040 * Namespace information in the evaluated messages is ignored, so the XPath 041 * expressions must not include namespace prefixes 042 * 043 * @author Lars Ivar Almli 044 */ 045 public class XPathIgnoreNsRegExpValidator implements ActionCommand { 046 public void perform(TestContext context, List<String> params) throws MactorException { 047 StringBuffer sb = new StringBuffer(); 048 Message m = context.getLastIncomingMessage(); 049 if (m == null) 050 throw new MactorException("There is no incoming message to validate!"); 051 Document doc = m.getContentDocumentNoNs(); 052 HashMap<XPath, String> map = ParamUtil.parseXPathParams(params); 053 for (Entry<XPath, String> e : map.entrySet()) { 054 Node n = e.getKey().selectSingleNode(doc); 055 String v = n == null ? null : n.getText(); 056 if (!ParamUtil.isEqual(v, e.getValue())) 057 sb.append("Validation of '" + e.getKey().getText() + "' failed. Expected something matching'" + e.getValue() + "', but got '" + v + "'"); 058 } 059 if (sb.length() != 0) 060 throw new MactorException(sb.toString()); 061 } 062 }