Issue 184: assertXPath() does not work when passing in an XML object
Status:  Fixed
Owner:
Closed:  Apr 2010
Reported by petermac...@gmail.com, Apr 24, 2010
What steps will reproduce the problem?

Using:

<cfset node = assertXPath('root', xml) />

Where `xml` is an CFML XML object that was gotten via XmlParse.

What is the expected output? What do you see instead?

Nothing, the assert never fails and always passed.  Also, assertXpath()
does not return any results (even if the XPath is the root of the XML object)

Please provide any additional information below.

Using MXUnit 1.0.8 (latest stable as of this ticket date).

I snooped the code for XPathAssert.cfc and there is a logic error where no
body of the assertXpath() method is run due to this statement:

<cfif NOT isXMLDoc(arguments.data)>

The fix is to do this by checking if the passed in data is already an XML
document and if not then use the buildXmlDom() method as the else.

This method fixes the issue:

  <cffunction name="assertXpath" access="public" returntype="any">
    <cfargument name="xpath" type="String" hint="string representing an
xpath expression" required="true" />
    <cfargument name="data" hint="String or URL to search" required="true"
type="any"  />
    <!--- To Do: Maybe TEXT can also accepts regular expressions? --->
    <cfargument name="text" type="string" required="false" default=""
hint="The text to match against the xpath expression. If omitted, this
assertion returns true if any elements of the xpath epxression are found." />
    <cfargument name="message" type="string" required="false" hint="The
mssage to display when this assertion fails" default="The XPath expression,
#arguments.xpath#, did not match the data." />
    <cfset var dom = javacast("null","java.lang.Object") />
    <cfset var isUrl= "" />
    <cfset var results = javacast("null","java.lang.Object") />
      
       <cftry>
        <!---
         Note:
         SSL Not supported. Workaround is to use another http client
         and pass in a string.

         To Do: allow pass in of org.xml.sax.InputSource
        --->

        <cfset isUrl = refindNoCase("^(http[s]*|file)://.+",data)>
	<cfif isXMLDoc(arguments.data)>
		<cfset dom = arguments.data />
	<cfelse>
	        <cfset dom = buildXmlDom(arguments.data,isUrl) />
	</cfif>
        <cfset results = xmlSearch(dom,arguments.xpath)>
        <cfif len(arguments.text) gt 0>
          <cfset assertEquals(arguments.text, results[1].xmlText, message) />
        </cfif>
        <cfif arrayLen(results) lt 1>
          <cfset fail(message) />
        </cfif>
        <cfreturn results />
       <cfcatch type="any">
        <cfthrow object="#cfcatch#">
       </cfcatch>
      </cftry>
  </cffunction>
Apr 24, 2010
Project Member #1 marc.es...@gmail.com
patch applied; test written; all is right with the world

thanks Peter!
Status: Fixed
Owner: marc.esher