1   package org.melati;
2   
3   import org.mortbay.jetty.Server;
4   import org.mortbay.jetty.webapp.WebAppContext;
5   import org.mortbay.resource.FileResource;
6   
7   import net.sourceforge.jwebunit.junit.WebTestCase;
8   
9   /**
10   *
11   * Much thanks to 
12   * http://today.java.net/pub/a/today/2007/04/12/embedded-integration-testing-of-web-applications.html
13   * 
14   * @author timp
15   * @since 2008/01/01
16   * 
17   */
18  public class JettyWebTestCase extends WebTestCase {
19  
20    private static Server server;
21    private static boolean started = false;
22    protected static String contextName = "melatitest";
23    protected static String webAppDirName = "src/test/webapp";
24  
25    /**
26     * Default constructor.
27     */
28    public JettyWebTestCase() {
29      super();
30    }
31  
32    /**
33     * Constructor, with name.
34     * @param name
35     */
36    public JettyWebTestCase(String name) {
37      super(name);
38    }
39  
40    protected void setUp() throws Exception {
41      // Port 0 means "assign arbitrarily port number"
42      startServer(8083);
43    
44      // getLocalPort returns the port that was actually assigned
45      int actualPort = server.getConnectors()[0].getLocalPort();
46      getTestContext().setBaseUrl("http://localhost:" + actualPort + "/" );
47    }
48  
49    protected void tearDown() throws Exception {
50      super.tearDown();
51    }
52    
53    /**
54     * If you don't know by now.
55     * @param args
56     * @throws Exception
57     */
58    public static void main(String[] args) throws Exception {
59      startServer(8080);
60    }
61  
62    protected static void startServer(int port) throws Exception {
63      if (!started) { 
64        server = new Server(port);
65        WebAppContext wac = new WebAppContext(
66                getWebAppDirName(), "/" + getContextName());
67        FileResource.setCheckAliases(false); 
68        server.addHandler(wac);
69        server.start();
70        wac.dumpUrl();
71        started = true;
72      }
73      
74    }
75    
76    /**
77     * Just to say hello.
78     */
79    public void testIndex() {
80      beginAt("/index.html");
81      assertTextPresent("Hello World!");
82    }
83    
84     /**
85     * {@inheritDoc}
86     * @see net.sourceforge.jwebunit.junit.WebTestCase#beginAt(java.lang.String)
87     */
88    public void beginAt(String url) { 
89      super.beginAt(contextUrl(url));
90    }
91    /**
92     * {@inheritDoc}
93     * @see net.sourceforge.jwebunit.junit.WebTestCase#gotoPage(java.lang.String)
94     */
95    public void gotoPage(String url) { 
96      super.gotoPage(contextUrl(url));
97    }
98    protected String contextUrl(String url) { 
99      return "/" + getContextName()  + url;
100   }
101 
102   /**
103    * @return the contextName
104    */
105   public static String getContextName() {
106     return contextName;
107   }
108   
109   /**
110    * @return relative path of webapp dir
111    */
112   public static String getWebAppDirName() {
113     return webAppDirName;
114   }
115   
116 }