1   /*
2    * Created on Apr 23, 2005
3    */
4   package com.flexiblewebsolutions.xml.util.test;
5   
6   import junit.framework.TestCase;
7   
8   import com.flexiblewebsolutions.xml.util.XMLStringParser;
9   
10  /***
11   * @author Donavon Buss
12   */
13  public class SimpleXMLUtilsTest extends TestCase
14  {
15  
16      private StringBuffer setupTestXML()
17      {
18          StringBuffer testXML = new StringBuffer();
19          testXML.append( "<decorator><name>HELLO</name><decorator><name>Hello2</name>\n" );
20          testXML.append( "<decorator><name>HELLO3</name></decorator>\n" );
21          testXML.append( "</decorator></decorator>" );
22          return testXML;
23      }
24  
25      public void testGetNestedTagValues()
26      {
27          StringBuffer testXML = new StringBuffer();
28          testXML.append( "<test>ONE</test>" );
29          testXML.append( "<test2><test>TWO</test></test2>" );
30          testXML.append( "<test>TWO</test>" );
31  
32          String[] tests = XMLStringParser.getMatchingNestedTags(
33                  testXML.toString(), "test" );
34          assertEquals( tests.length, 2 );
35      }
36  
37      public void testGetValueForTag2()
38      {
39          String classname = "com.flexiblewebsolutionstest.extensions.test.TestTest";
40          StringBuffer test = new StringBuffer();
41          test.append( "<testconfig>\n" );
42          test.append( "<classes>\n" );
43          test.append( "<class>\n" );
44          test.append( "<classname>" );
45          test.append( classname );
46          test.append( "</classname>\n" );
47          test.append( "<testOptionsFileName>test/LoadTestOptions.xml</testOptionsFileName>\n" );
48          test.append( "<methods>\n" );
49          test.append( "<decorator>\n" );
50          test.append( "<decoratorclass>com.flexiblewebsolutionstest.extensions.test.Test1TestDecorator</decoratorclass>\n" );
51          test.append( "<parameterdata></parameterdata>\n" );
52          test.append( "<decorator>\n" );
53          test.append( "<decoratorclass>com.flexiblewebsolutionstest.extensions.test.Test2TestDecorator</decoratorclass>\n" );
54          test.append( "<parameterdata></parameterdata>\n" );
55          test.append( "<method><inputFileName></inputFileName>\n" );
56          test.append( "<methodname>testStuff</methodname>\n" );
57          test.append( "</method>\n" );
58          test.append( "</decorator>\n" );
59          test.append( "</decorator>\n" );
60          test.append( "</methods>\n" );
61          test.append( "</class>\n" );
62          test.append( "</classes>\n" );
63          test.append( "</testconfig>\n" );
64  
65          String xmlClass = XMLStringParser.getValueForTag( test, "classname" );
66          assertEquals( xmlClass, classname );
67      }
68  
69      public void testEmbeddedTags()
70      {
71          StringBuffer testXML = setupTestXML();
72  
73          String decTag = XMLStringParser.getEntireTag( testXML, 0,
74                  "</decorator>", "<decorator>" );
75          //		System.err.println( decTag );
76          assertEquals( decTag, testXML.toString() );
77      }
78  
79      public void testGetAllTagValsWEmbedded()
80      {
81          StringBuffer testXML = setupTestXML();
82          String[] allVals = XMLStringParser.getMatchingNestedTags(
83                  testXML.toString(), "decorator" );
84          assertEquals( allVals.length, 1 );
85      }
86  
87      public void testGetValueForTag()
88      {
89          StringBuffer valXML = new StringBuffer();
90          valXML.append( "<potatoe>yummy</potatoe>" );
91          String result = XMLStringParser.getValueForTag( valXML, "potatoe" );
92          assertEquals( result, "yummy" );
93  
94          StringBuffer newValXML = setupTestXML();
95          String newResult = XMLStringParser.getValueForTag( newValXML,
96                  "decorator" );
97          newValXML.delete( 0, "<decorator>".length() );
98          newValXML.delete( newValXML.length() - "</decorator>".length(),
99                  newValXML.length() );
100         assertEquals( newValXML.toString(), newResult );
101     }
102 }