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