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.jsl;
17  
18  import java.io.FileInputStream;
19  import java.io.InputStream;
20  
21  import junit.framework.Test;
22  import junit.framework.TestCase;
23  import junit.framework.TestSuite;
24  import junit.textui.TestRunner;
25  
26  import org.apache.commons.jelly.JellyContext;
27  import org.apache.commons.jelly.Script;
28  import org.apache.commons.jelly.XMLOutput;
29  import org.apache.commons.jelly.parser.XMLParser;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.dom4j.Document;
33  import org.dom4j.Element;
34  import org.dom4j.io.SAXContentHandler;
35  
36  /***
37   * Tests the JSL tags.
38   * Note this test harness could be written in Jelly script
39   * if we had the junit tag library!
40   *
41   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
42   * @version $Revision: 155420 $
43   */
44  public class TestJSL extends TestCase {
45  
46      /*** The Log to which logging calls will be made. */
47      private static final Log log = LogFactory.getLog(TestJSL.class);
48  
49      public static void main(String[] args) {
50          TestRunner.run(suite());
51      }
52  
53      public static Test suite() {
54          return new TestSuite(TestJSL.class);
55      }
56  
57      public TestJSL(String testName) {
58          super(testName);
59      }
60  
61      public void testExample1() throws Exception {
62          Document document = runScript( "src/test/org/apache/commons/jelly/jsl/example.jelly" );
63          Element small = (Element) document.selectSingleNode("/html/body/small");
64  
65          assertTrue( "<small> starts with 'James Elson'", small.getText().startsWith("James Elson") );
66          assertEquals( "I am a title!", small.valueOf( "h2" ).trim() );
67          assertEquals( "Twas a dark, rainy night...", small.valueOf( "small" ).trim() );
68          assertEquals( "dfjsdfjsdf", small.valueOf( "p" ).trim() );
69      }
70  
71  
72      protected Document runScript(String fileName) throws Exception {
73          InputStream in = new FileInputStream(fileName);
74          XMLParser parser = new XMLParser();
75          Script script = parser.parse(in);
76          script = script.compile();
77          JellyContext context = parser.getContext();
78  
79          SAXContentHandler contentHandler = new SAXContentHandler();
80          XMLOutput output = new XMLOutput( contentHandler );
81  
82          contentHandler.startDocument();
83          script.run(context, output);
84          contentHandler.endDocument();
85  
86          return contentHandler.getDocument();
87      }
88  }