1   package org.paneris.wvm.test;
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   
10  import net.sourceforge.jwebunit.junit.WebTestCase;
11  
12  /**
13   * @author timp
14   * @since 30 Aug 2007
15   * 
16   */
17  public class WebIntegrationTest extends WebTestCase {
18  
19    private static Server server;
20    private static boolean started = false;
21    private static String contextName = "wvm";
22    
23    /**
24     * Default constructor.
25     */
26    public WebIntegrationTest() {
27    }
28    
29    private static void startServer(int port) throws Exception { 
30      if (!started) { 
31        // Port 0 means "assign arbitrarily port number"
32        server = new org.mortbay.jetty.Server(port);
33        WebAppContext wac = new WebAppContext(
34            "src/main/webapp", "/" + contextName);
35        org.mortbay.resource.FileResource.setCheckAliases(false); 
36        server.addHandler(wac);
37        server.start();
38  
39        started = true;
40      }
41      
42    }
43    
44    protected void setUp() throws Exception {
45      startServer(8083);
46      // getLocalPort returns the port that was actually assigned
47      int actualPort = server.getConnectors()[0].getLocalPort();
48      getTestContext().setBaseUrl("http://localhost:" + actualPort + "/");
49    }
50  
51    protected void tearDown() throws Exception {
52      super.tearDown();
53    }
54    
55     /**
56     * {@inheritDoc}
57     * @see net.sourceforge.jwebunit.junit.WebTestCase#beginAt(java.lang.String)
58     */
59    public void beginAt(String url) { 
60      super.beginAt(contextUrl(url));
61    }
62    /**
63     * {@inheritDoc}
64     * @see net.sourceforge.jwebunit.junit.WebTestCase#gotoPage(java.lang.String)
65     */
66    public void gotoPage(String url) { 
67      super.gotoPage(contextUrl(url));
68    }
69    protected String contextUrl(String url) { 
70      return "/" + contextName  + url;
71    }
72  
73    private String getSTATIC_ROOT() {
74      return "/dist/wvm/www";
75    }
76    private boolean generateCached() { 
77      return false;
78    }
79    /**
80     * If you don't know by now.
81     * @param args
82     * @throws Exception
83     */
84    public static void main(String[] args) throws Exception {
85      startServer(8080);
86    }
87  
88    /**
89     * Test.
90     */
91    public void testIndex() {
92      beginAt("/index.jsp");
93      assertTextPresent("Home");
94    }
95    
96    
97      
98    /**
99     * Test Home.
100    */
101   public void testHome() throws Exception {
102     beginAt("/Display/page/Home.html");
103     String generated = getTester().getPageSource();
104     File cachedFile = new File(getSTATIC_ROOT(), "Home.html");
105     if (cachedFile.exists() && ! generateCached()) {
106       FileInputStream file = new FileInputStream (cachedFile);
107       byte[] b = new byte[file.available()];
108       file.read(b);
109       file.close ();
110       String cached = new String(b);
111       assertEquals(cached, generated);
112     } else { 
113       FileOutputStream file = new FileOutputStream(cachedFile);
114       file.write(generated.getBytes());
115     }
116   }
117   
118   /**
119    * Test AboutUs.
120    */
121   public void testAboutUs() throws Exception {
122     beginAt("/Display/page/About_Us.html");
123     String generated = getTester().getPageSource();
124     File cachedFile = new File(getSTATIC_ROOT(), "About_Us.html");
125     if (cachedFile.exists() && ! generateCached()) {
126       FileInputStream file = new FileInputStream (cachedFile);
127       byte[] b = new byte[file.available()];
128       file.read(b);
129       file.close ();
130       String cached = new String(b);
131       assertEquals(cached, generated);
132     } else { 
133       FileOutputStream file = new FileOutputStream(cachedFile);
134       file.write(generated.getBytes());
135     }
136   }
137   
138   /**
139    * Test TheMalts.
140    */
141   public void testTheMalts() throws Exception {
142     beginAt("/Display/page/The_Malts.html");
143     String generated = getTester().getPageSource();
144     File cachedFile = new File(getSTATIC_ROOT(), "The_Malts.html");
145     if (cachedFile.exists() && ! generateCached()) {
146       FileInputStream file = new FileInputStream (cachedFile);
147       byte[] b = new byte[file.available()];
148       file.read(b);
149       file.close ();
150       String cached = new String(b);
151       assertEquals(cached, generated);
152     } else { 
153       FileOutputStream file = new FileOutputStream(cachedFile);
154       file.write(generated.getBytes());
155     }
156   }
157   
158   /**
159    * Test TheRegions.
160    */
161   public void testTheRegions() throws Exception {
162     beginAt("/Display/page/The_Regions.html");
163     String generated = getTester().getPageSource();
164     File cachedFile = new File(getSTATIC_ROOT(), "The_Regions.html");
165     if (cachedFile.exists() && ! generateCached()) {
166       FileInputStream file = new FileInputStream (cachedFile);
167       byte[] b = new byte[file.available()];
168       file.read(b);
169       file.close ();
170       String cached = new String(b);
171       assertEquals(cached, generated);
172     } else { 
173       FileOutputStream file = new FileOutputStream(cachedFile);
174       file.write(generated.getBytes());
175     }
176   }
177   
178   /**
179    * Test News.
180    */
181   public void testNews() throws Exception {
182     beginAt("/Display/page/News.html");
183     String generated = getTester().getPageSource();
184     File cachedFile = new File(getSTATIC_ROOT(), "News.html");
185     if (cachedFile.exists() && ! generateCached()) {
186       FileInputStream file = new FileInputStream (cachedFile);
187       byte[] b = new byte[file.available()];
188       file.read(b);
189       file.close ();
190       String cached = new String(b);
191       assertEquals(cached, generated);
192     } else { 
193       FileOutputStream file = new FileOutputStream(cachedFile);
194       file.write(generated.getBytes());
195     }
196   }
197   
198   /**
199    * Test Sales.
200    */
201   public void testSales() throws Exception {
202     beginAt("/Display/page/Sales.html");
203     String generated = getTester().getPageSource();
204     File cachedFile = new File(getSTATIC_ROOT(), "Sales.html");
205     if (cachedFile.exists() && ! generateCached()) {
206       FileInputStream file = new FileInputStream (cachedFile);
207       byte[] b = new byte[file.available()];
208       file.read(b);
209       file.close ();
210       String cached = new String(b);
211       assertEquals(cached, generated);
212     } else { 
213       FileOutputStream file = new FileOutputStream(cachedFile);
214       file.write(generated.getBytes());
215     }
216   }
217   
218   /**
219    * Test ContactUs.
220    */
221   public void testContactUs() throws Exception {
222     beginAt("/Display/page/Contact_Us.html");
223     String generated = getTester().getPageSource();
224     File cachedFile = new File(getSTATIC_ROOT(), "Contact_Us.html");
225     if (cachedFile.exists() && ! generateCached()) {
226       FileInputStream file = new FileInputStream (cachedFile);
227       byte[] b = new byte[file.available()];
228       file.read(b);
229       file.close ();
230       String cached = new String(b);
231       assertEquals(cached, generated);
232     } else { 
233       FileOutputStream file = new FileOutputStream(cachedFile);
234       file.write(generated.getBytes());
235     }
236   }
237   
238   /**
239    * Test TradeInfo.
240    */
241   public void testTradeInfo() throws Exception {
242     beginAt("/Display/page/Trade_Info.html");
243     String generated = getTester().getPageSource();
244     File cachedFile = new File(getSTATIC_ROOT(), "Trade_Info.html");
245     if (cachedFile.exists() && ! generateCached()) {
246       FileInputStream file = new FileInputStream (cachedFile);
247       byte[] b = new byte[file.available()];
248       file.read(b);
249       file.close ();
250       String cached = new String(b);
251       assertEquals(cached, generated);
252     } else { 
253       FileOutputStream file = new FileOutputStream(cachedFile);
254       file.write(generated.getBytes());
255     }
256   }
257   
258   /**
259    * Test SingleCasks.
260    */
261   public void testSingleCasks() throws Exception {
262     beginAt("/Display/page/Single_Casks.html");
263     String generated = getTester().getPageSource();
264     File cachedFile = new File(getSTATIC_ROOT(), "Single_Casks.html");
265     if (cachedFile.exists() && ! generateCached()) {
266       FileInputStream file = new FileInputStream (cachedFile);
267       byte[] b = new byte[file.available()];
268       file.read(b);
269       file.close ();
270       String cached = new String(b);
271       assertEquals(cached, generated);
272     } else { 
273       FileOutputStream file = new FileOutputStream(cachedFile);
274       file.write(generated.getBytes());
275     }
276   }
277   
278   /**
279    * Test BlendedMalts.
280    */
281   public void testBlendedMalts() throws Exception {
282     beginAt("/Display/page/Blended_Malts.html");
283     String generated = getTester().getPageSource();
284     File cachedFile = new File(getSTATIC_ROOT(), "Blended_Malts.html");
285     if (cachedFile.exists() && ! generateCached()) {
286       FileInputStream file = new FileInputStream (cachedFile);
287       byte[] b = new byte[file.available()];
288       file.read(b);
289       file.close ();
290       String cached = new String(b);
291       assertEquals(cached, generated);
292     } else { 
293       FileOutputStream file = new FileOutputStream(cachedFile);
294       file.write(generated.getBytes());
295     }
296   }
297   
298   /**
299    * Test PressReviews.
300    */
301   public void testPressReviews() throws Exception {
302     beginAt("/Display/page/Press_Reviews.html");
303     String generated = getTester().getPageSource();
304     File cachedFile = new File(getSTATIC_ROOT(), "Press_Reviews.html");
305     if (cachedFile.exists() && ! generateCached()) {
306       FileInputStream file = new FileInputStream (cachedFile);
307       byte[] b = new byte[file.available()];
308       file.read(b);
309       file.close ();
310       String cached = new String(b);
311       assertEquals(cached, generated);
312     } else { 
313       FileOutputStream file = new FileOutputStream(cachedFile);
314       file.write(generated.getBytes());
315     }
316   }
317   
318 }