1   /*
2    *  soapui, copyright (C) 2005 Ole Matzura / eviware.com 
3    *
4    *  SoapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
7    *
8    *  SoapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.support;
14  
15  import junit.framework.TestCase;
16  
17  import org.w3c.dom.Document;
18  import org.w3c.dom.Element;
19  import org.w3c.dom.NodeList;
20  
21  public class XmlUtilsTestCase extends TestCase
22  {
23  	public void testGetElementIndex() throws Exception
24  	{
25  		Document dom = XmlUtils.parseXml( "<h1><p>p1</p><h2>lkj</h2><p>p2</p></h1>");
26  		NodeList nl = dom.getDocumentElement().getElementsByTagName( "p" );
27  		
28  		assertEquals( 1, XmlUtils.getElementIndex( (Element) nl.item(0)));
29  		assertEquals( 2, XmlUtils.getElementIndex( (Element) nl.item(1)));
30  	}
31  	
32  	public void testGetElementPath() throws Exception
33  	{
34  		Document dom = XmlUtils.parseXml( "<h1><p>p1</p><h2>lkj</h2><p>p2</p></h1>");
35  		NodeList nl = dom.getDocumentElement().getElementsByTagName( "p" );
36  		
37  		assertEquals( "/h1[1]/p[1]", XmlUtils.getElementPath( (Element) nl.item(0)) );
38  		assertEquals( "/h1[1]/p[2]", XmlUtils.getElementPath( (Element) nl.item(1)) );
39  	}
40  	
41  	public void testTransferValues() throws Exception
42  	{
43  		String doc1 = "<h1><p>p1</p><h2 test=\"bil\">lkj</h2></h1>";
44  		String doc2 = "<h1><p>string</p><h2>string</h2><p>p2</p></h1>";
45  		
46  		String result = XmlUtils.transferValues( doc1, doc2 );
47  		assertEquals( "<h1><p>p1</p><h2 test=\"bil\">lkj</h2><p>p2</p></h1>", result );
48  	}
49  	
50  	public void testTransferValuesNS() throws Exception
51  	{
52  		String doc1 = "<ns:h1 xmlns:ns=\"test\"><ns:p>p1</ns:p><ns:h2 test=\"bil\">lkj</ns:h2></ns:h1>";
53  		String doc2 = "<ns:h1 xmlns:ns=\"test\"><ns:p>string</ns:p><ns:h2>string</ns:h2><ns:p>p2</ns:p></ns:h1>";
54  		
55  		String result = XmlUtils.transferValues( doc1, doc2 );
56  		assertEquals( "<ns:h1 xmlns:ns=\"test\"><ns:p>p1</ns:p><ns:h2 test=\"bil\">lkj</ns:h2><ns:p>p2</ns:p></ns:h1>", result );
57  	}
58  }