1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.tags.xml;
17  
18  import java.io.FileInputStream;
19  import java.io.InputStream;
20  import java.util.Iterator;
21  
22  import junit.framework.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  import junit.textui.TestRunner;
26  
27  import org.apache.commons.jelly.Script;
28  import org.apache.commons.jelly.Tag;
29  import org.apache.commons.jelly.impl.ScriptBlock;
30  import org.apache.commons.jelly.impl.TagScript;
31  import org.apache.commons.jelly.parser.XMLParser;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  /*** Tests the core tags
36    *
37    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
38    * @version $Revision: 1.5 $
39    */
40  public class TestParser extends TestCase {
41  
42      /*** The Log to which logging calls will be made. */
43      private static final Log log = LogFactory.getLog(TestParser.class);
44  
45      public static void main(String[] args) {
46          TestRunner.run(suite());
47      }
48  
49      public static Test suite() {
50          return new TestSuite(TestParser.class);
51      }
52  
53      public TestParser(String testName) {
54          super(testName);
55      }
56  
57      /***
58       * Tests that parsing an example script correctly creates the parent
59       * relationships
60       */
61      public void testParser() throws Exception {
62          InputStream in = new FileInputStream("src/test/org/apache/commons/jelly/tags/xml/example2.jelly");
63          XMLParser parser = new XMLParser();
64          Script script = parser.parse(in);
65          script = script.compile();
66  
67          log.debug("Found: " + script);
68  
69          assertTagsHaveParent( script, null );
70      }
71  
72      /***
73       * Tests that the Tag in the TagScript has the given parent and then
74       * recurse to check its children has the correct parent and so forth.
75       */
76      protected void assertTagsHaveParent(Script script, Tag parent) throws Exception {
77          if ( script instanceof TagScript ) {
78              TagScript tagScript = (TagScript) script;
79              Tag tag = tagScript.getTag();
80  
81              assertEquals( "Tag: " + tag + " has the incorrect parent", parent, tag.getParent() );
82  
83              assertTagsHaveParent( tag.getBody(), tag );
84          }
85          else if ( script instanceof ScriptBlock ) {
86              ScriptBlock block = (ScriptBlock) script;
87              for ( Iterator iter = block.getScriptList().iterator(); iter.hasNext(); ) {
88                  assertTagsHaveParent( (Script) iter.next(), parent );
89              }
90          }
91      }
92  }