View Javadoc

1   package org.melati;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.FileOutputStream;
6   
7   import org.mortbay.jetty.Server;
8   import org.mortbay.jetty.webapp.WebAppContext;
9   import org.mortbay.resource.FileResource;
10  
11  import net.sourceforge.jwebunit.junit.WebTestCase;
12  
13  /**
14   *
15   * Much thanks to 
16   * http://today.java.net/pub/a/today/2007/04/12/embedded-integration-testing-of-web-applications.html
17   * 
18   * @author timp
19   * @since 2008/01/01
20   * 
21   */
22  public class JettyWebTestCase extends WebTestCase {
23  
24    private static Server server;
25    private static boolean started = false;
26    protected static String contextName = "melatitest";
27    protected static String webAppDirName = "src/test/webapp";
28    protected static String referenceOutputDir = "src/test/resources";
29  
30    protected static int actualPort;  
31    /**
32     * 
33     * Default constructor.
34     */
35    public JettyWebTestCase() {
36      super();
37    }
38  
39    /**
40     * Constructor, with name.
41     * @param name
42     */
43    public JettyWebTestCase(String name) {
44      super(name);
45    }
46  
47    protected void setUp() throws Exception {
48      // Port 0 means "assign arbitrarily port number"
49      actualPort = startServer(0);
50      getTestContext().setBaseUrl("http://localhost:" + actualPort + "/" );
51    }
52  
53    protected void tearDown() throws Exception {
54      super.tearDown();
55    }
56    
57    /**
58     * If you don't know by now.
59     * @param args
60     * @throws Exception
61     */
62    public static void main(String[] args) throws Exception {
63      actualPort = startServer(8080);
64    }
65  
66    protected static int startServer(int port) throws Exception {
67      
68      if (!started) { 
69        server = new Server(port);
70        WebAppContext wac = new WebAppContext(
71                getWebAppDirName(), "/" + getContextName());
72        FileResource.setCheckAliases(false); 
73        server.addHandler(wac);
74        server.start();
75        wac.dumpUrl();
76        started = true;
77      }
78      // getLocalPort returns the port that was actually assigned
79      return server.getConnectors()[0].getLocalPort();
80      
81    }
82    
83    protected int getActualPort() { 
84      return actualPort;
85    }
86    
87    /**
88     * Just to say hello.
89     */
90    public void testIndex() {
91      beginAt("/");
92      assertTextPresent("Hello World!");
93    }
94    
95     /**
96     * {@inheritDoc}
97     * @see net.sourceforge.jwebunit.junit.WebTestCase#beginAt(java.lang.String)
98     */
99    public void beginAt(String url) { 
100     super.beginAt(contextUrl(url));
101   }
102   /**
103    * {@inheritDoc}
104    * @see net.sourceforge.jwebunit.junit.WebTestCase#gotoPage(java.lang.String)
105    */
106   public void gotoPage(String url) { 
107     System.err.println(contextUrl(url));
108     super.gotoPage(contextUrl(url));
109   }
110   protected String contextUrl(String url) { 
111     return "/" + getContextName()  + url;
112   }
113 
114   /**
115    * @return the contextName
116    */
117   public static String getContextName() {
118     return contextName;
119   }
120   
121   /**
122    * @return relative path of webapp dir
123    */
124   public static String getWebAppDirName() {
125     return webAppDirName;
126   }
127 
128   /**
129    * @param contextName the contextName to set
130    */
131   protected static void setContextName(String contextName) {
132     JettyWebTestCase.contextName = contextName;
133   }
134 
135   /**
136    * @param webAppDirName the webAppDirName to set
137    */
138   protected static void setWebAppDirName(String webAppDirName) {
139     JettyWebTestCase.webAppDirName = webAppDirName;
140   }
141 
142   private boolean generateCached() { 
143     return false;
144   }
145   protected void assertPageEqual(String url, String fileName) throws Exception { 
146     beginAt(url);
147     String generated = getTester().getPageSource();
148     File referenceFile = new File(referenceOutputDir, fileName);
149     if (referenceFile.exists() && ! generateCached()) {
150       FileInputStream file = new FileInputStream (referenceFile);
151       byte[] b = new byte[file.available()];
152       file.read(b);
153       file.close ();
154       String cached = new String(b);
155       assertEquals(cached, generated);
156     } else { 
157       FileOutputStream file = new FileOutputStream(referenceFile);
158       file.write(generated.getBytes());
159       fail("Reference output file generated: " + referenceFile.getCanonicalPath() + " modify generateCached and rerun");
160     }
161   }
162   
163 }