1   /*
2    * Copyright 1999-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.jetty;
17  
18  import java.io.File;
19  import java.io.StringWriter;
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.XMLOutput;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  /*** Tests the parser, the engine and the XML tags
32    *
33    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
34    * @version $Revision: 155420 $
35    */
36  public class TestJettyHttpServerTags extends TestCase {
37  
38      /*** The Log to which logging calls will be made. */
39      private static final Log log = LogFactory.getLog(TestJettyHttpServerTags.class);
40  
41      public static void main(String[] args) {
42          TestRunner.run(suite());
43      }
44  
45      public static Test suite() {
46          return new TestSuite(TestJettyHttpServerTags.class);
47      }
48  
49      public TestJettyHttpServerTags(String testName) {
50          super(testName);
51      }
52  
53      public void testDefaultServer() throws Exception {
54          String text = evaluteScriptAsText(
55              "src/test/org/apache/commons/jelly/jetty/defaultServer.jelly"
56          );
57          assertEquals("Produces the correct output", "It works!", text);
58      }
59  
60      public void testJettyLogFile() throws Exception {
61          File logFile = new File("src/test/org/apache/commons/jelly/jetty/JellyLogFileTest.log");
62          if (logFile.exists()) {
63              logFile.delete();
64          }
65          assertTrue("Logfile does not exist", !logFile.exists());
66          String text = evaluteScriptAsText(
67              "src/test/org/apache/commons/jelly/jetty/jettyLogFile.jelly"
68          );
69          assertEquals("Produces the correct output", "It works!", text);
70          assertTrue("Logfile exists", logFile.exists());
71      }
72  
73      public void testSocketListener() throws Exception {
74          String text = evaluteScriptAsText(
75              "src/test/org/apache/commons/jelly/jetty/socketListener.jelly"
76          );
77          assertEquals("Produces the correct output", "It works!", text);
78      }
79  
80      public void testHttpContext() throws Exception {
81          String text = evaluteScriptAsText(
82              "src/test/org/apache/commons/jelly/jetty/httpContext.jelly"
83          );
84          assertEquals("Produces the correct output", "It works!", text);
85      }
86  
87      public void testResourceHandler() throws Exception {
88          String text = evaluteScriptAsText(
89              "src/test/org/apache/commons/jelly/jetty/resourceHandler.jelly"
90          );
91          assertEquals("Produces the correct output", "It works!", text);
92      }
93  
94      public void testSecurityHandler() throws Exception {
95          String text = evaluteScriptAsText(
96              "src/test/org/apache/commons/jelly/jetty/securityHandlerForbidden.jelly"
97          );
98          assertEquals("Forbidden test produces the correct output", "It works!", text);
99  
100         text = evaluteScriptAsText(
101             "src/test/org/apache/commons/jelly/jetty/securityHandlerUnauthorized.jelly"
102         );
103         assertEquals("Unauthorized produces the correct output", "It works!", text);
104     }
105 
106     public void testJellyResourceHandler() throws Exception {
107         String text = evaluteScriptAsText(
108             "src/test/org/apache/commons/jelly/jetty/jellyResourceHandler.jelly"
109         );
110         assertEquals("jellyResourceHandler produces the correct output", "It works!", text);
111 
112         text = evaluteScriptAsText(
113             "src/test/org/apache/commons/jelly/jetty/jellyResourceHandlerRequestBody.jelly"
114         );
115         assertEquals("jellyResourceHandlerRequestBody produces the correct output", "It works!", text);
116     }
117 
118     /***
119      * Evaluates the script by the given file name and
120      * returns the whitespace trimmed output as text
121      */
122     protected String evaluteScriptAsText(String fileName) throws Exception {
123         JellyContext context = new JellyContext();
124 
125         // allow scripts to refer to any resource inside this project
126         // using an absolute URI like /src/test/org/apache/foo.xml
127         context.setRootURL(new File(".").toURL());
128 
129         // cature the output
130         StringWriter buffer = new StringWriter();
131         XMLOutput output = XMLOutput.createXMLOutput(buffer);
132 
133         context.runScript( new File(fileName), output );
134         String text = buffer.toString().trim();
135         if (log.isDebugEnabled()) {
136             log.debug("Evaluated script as...");
137             log.debug(text);
138         }
139         return text;
140     }
141 }