XPathIgnoreNsRegExpValidator.java :  » Testing » MActor » org » mactor » extensions » xml » Java Open Source

Java Open Source » Testing » MActor 
MActor » org » mactor » extensions » xml » XPathIgnoreNsRegExpValidator.java
/******************************************************************************
 * Copyright (C) Lars Ivar Almli. All rights reserved.                        *
 * ---------------------------------------------------------------------------*
 * This file is part of MActor.                                               *
 *                                                                            *
 * MActor is free software; you can redistribute it and/or modify             *
 * it under the terms of the GNU General Public License as published by       *
 * the Free Software Foundation; either version 2 of the License, or          *
 * (at your option) any later version.                                        *
 *                                                                            *
 * MActor is distributed in the hope that it will be useful,                  *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU General Public License for more details.                               *
 *                                                                            *
 * You should have received a copy of the GNU General Public License          *
 * along with MActor; if not, write to the Free Software                      *
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
 ******************************************************************************/
package org.mactor.extensions.xml;

import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.XPath;
import org.mactor.framework.MactorException;
import org.mactor.framework.TestContext;
import org.mactor.framework.extensioninterface.ActionCommand;

/**
 * Validates that a selected field from the last received message matches a specifed value.
 * <br/>
 * The field to select and the value it must be equal to is specified as a single parameter on the form:<br/>
 * [XPath expression that selects the single attribute or element ]==[a regular expression that the field must match]
 * <br>
 * Namespace information in the evaluated messages is ignored, so the XPath expressions must not include namespace prefixes
 * @author Lars Ivar Almli
 */
public class XPathIgnoreNsRegExpValidator implements ActionCommand {
  public void perform(TestContext context, List<String> params) throws MactorException {
    StringBuffer sb = new StringBuffer();
    Document doc = context.getLastIncomingMessage().getContentDocumentNoNs();
    HashMap<XPath, String> map = ParamUtil.parseXPathParams(params);
    for (Entry<XPath, String> e : map.entrySet()) {
      Node n = e.getKey().selectSingleNode(doc);
      String v = n == null ? null : n.getText();
      if (!ParamUtil.isEqual(v, e.getValue()))
        sb.append("Validation of '" + e.getKey().getText() + "' failed. Expected something matching'" + e.getValue() + "', but got '" + v + "'");
    }
    if (sb.length() != 0)
      throw new MactorException(sb.toString());
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.