1   package org.apache.maven.j2ee;
2   
3   /* ====================================================================
4    * The Apache Software License, Version 1.1
5    *
6    * Copyright (c) 2002 The Apache Software Foundation.  All rights
7    * reserved.
8    *
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   * 1. Redistributions of source code must retain the above copyright
14   *    notice, this list of conditions and the following disclaimer.
15   *
16   * 2. Redistributions in binary form must reproduce the above copyright
17   *    notice, this list of conditions and the following disclaimer in
18   *    the documentation and/or other materials provided with the
19   *    distribution.
20   *
21   * 3. The end-user documentation included with the redistribution,
22   *    if any, must include the following acknowledgment:
23   *       "This product includes software developed by the
24   *        Apache Software Foundation (http://www.apache.org/)."
25   *    Alternately, this acknowledgment may appear in the software itself,
26   *    if and wherever such third-party acknowledgments normally appear.
27   *
28   * 4. The names "Apache" and "Apache Software Foundation" and
29   *    "Apache Maven" must not be used to endorse or promote products
30   *    derived from this software without prior written permission. For
31   *    written permission, please contact apache@apache.org.
32   *
33   * 5. Products derived from this software may not be called "Apache",
34   *    "Apache Maven", nor may "Apache" appear in their name, without
35   *    prior written permission of the Apache Software Foundation.
36   *
37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48   * SUCH DAMAGE.
49   * ====================================================================
50   *
51   * This software consists of voluntary contributions made by many
52   * individuals on behalf of the Apache Software Foundation.  For more
53   * information on the Apache Software Foundation, please see
54   * <http://www.apache.org/>.
55   */
56  
57  import junit.framework.TestCase;
58  
59  import org.apache.maven.TestConstants;
60  
61  /**
62   * Unit test for <code>WarValidator</code>.
63   *
64   * @author <a href="mailto:dion@multitask.com.au">dIon Gillard</a>
65   * @version $Id: WarValidatorTest.java 170200 2005-05-15 06:24:19Z brett $
66   */
67  public class WarValidatorTest extends TestCase
68  {
69  
70      /** the validator being tested */
71      private WarValidator validator = null;
72      /** file name for the empty war*/
73      private String emptyWarFile = null;
74      /** file name for manifest-only war */
75      private String manifestOnlyWarFile = null;
76      /** file name for simple war (manifest & web.xml)*/
77      private String simpleWarFile = null;
78      /** file name for dummy war */
79      private String dummyWarFile = null;
80      /** file name for dummy jsp war (dummy war + a jsp-file entry) */
81      private String dummyJspWarFile;
82      /** file name for dummy taglib war (dummy war + 2 taglibs) */
83      private String dummyTaglibWarFile;
84      /** file name for error pages war (dummy war + 2 error pages) */
85      private String errorPagesWarFile;
86      /** file name for form login config war (dummy war + login config) */
87      private String loginPagesWarFile;
88  
89      /**
90       * Creates a new instance of WarValidatorTest
91       * @param testName the name of the test
92       */
93      public WarValidatorTest(String testName)
94      {
95          super(testName);
96      }
97  
98      /**
99       * Initialize per test data
100      * @throws Exception when there is an unexpected problem
101      */
102     public void setUp() throws Exception
103     {
104         validator = new WarValidator();
105         String baseDir = System.getProperty("basedir");
106         assertNotNull("The system property basedir was not defined.", baseDir);
107         String j2eeDir = baseDir + TestConstants.J2EE_DIR;
108         emptyWarFile = j2eeDir + "empty.war";
109         manifestOnlyWarFile = j2eeDir + "manifest-only.war";
110         simpleWarFile = j2eeDir + "simple.war";
111         dummyWarFile = j2eeDir + "dummy-servlet.war";
112         dummyJspWarFile = j2eeDir + "dummy-jsp.war";
113         dummyTaglibWarFile = j2eeDir + "dummy-taglib.war";
114         errorPagesWarFile = j2eeDir + "error-pages.war";
115         loginPagesWarFile = j2eeDir + "login-pages.war";
116     }
117 
118     /**
119      * test that the warFileName property is mandatory for validation
120      * @throws Exception when there is an unexpected problem
121      */
122     public void testWarFileMandatory() throws Exception
123     {
124         try
125         {
126             validator.execute();
127             fail("war validator executed without a war file");
128         }
129         catch (NullPointerException e)
130         {
131             // this is the expected behaviour
132         }
133     }
134 
135     /**
136      * test that an empty war file produces an error
137      * @throws Exception when there is an unexpected problem
138      */
139     public void testEmptyWar() throws Exception
140     {
141         try
142         {
143             validator.setFailOnError(true);
144             validator.setWarFileName(emptyWarFile);
145             validator.addFormatter(new ValidationFormatter());
146             validator.execute();
147             fail("Empty war file (with no META-INF, or WEB-INF/web.xml) has"
148                 + " succeeded");
149         }
150         catch (IllegalStateException e)
151         {
152             // this is the expected behaviour
153         }
154     }
155 
156     /**
157      * test that the failOnError property when set to false doesn't throw
158      * an exception.
159      * @throws Exception when there is an unexpected problem
160      */
161     public void testFailOnError() throws Exception
162     {
163         try
164         {
165             validator.setFailOnError(false);
166             validator.setWarFileName(emptyWarFile);
167             validator.addFormatter(new ValidationFormatter());
168             validator.execute();
169             assertTrue("Error not fired for an empty war file "
170                 + "(with no META-INF, or WEB-INF/web.xml)",
171                 validator.getStatus().isError());
172         }
173         catch (IllegalStateException e)
174         {
175             fail("Empty war file (with no META-INF, or WEB-INF/web.xml) failed"
176                 + " the build process with failOnError=false");
177         }
178     }
179 
180     /**
181      * test that a manifest only war file produces a warning
182      * @throws Exception when there is an unexpected problem
183      */
184     public void testManifestOnlyWar() throws Exception
185     {
186         validator.setWarFileName(manifestOnlyWarFile);
187         validator.addFormatter(new ValidationFormatter());
188         validator.execute();
189         assertTrue("Manifest only war file (with no WEB-INF/web.xml) has"
190             + " succeeded without a warning", 
191             validator.getStatus().isWarning());
192     }
193 
194     /**
195      * test that a simple war (MANIFEST and web.xml) file produces no warning
196      * @throws Exception when there is an unexpected problem
197      */
198     public void testSimpleWar() throws Exception
199     {
200         validator.setWarFileName(simpleWarFile);
201         validator.addFormatter(new ValidationFormatter());
202         validator.execute();
203         assertTrue("Simple war file has generated a warning",
204             !validator.getStatus().isWarning());
205         assertTrue("Simple war file has generated an error",
206             !validator.getStatus().isError());
207     }
208 
209     /**
210      * test that a dummy war file produces no warning and an error
211      * (as there is an intentionally bad class (java.servlet.Servlet) in
212      * web.xml)
213      * @throws Exception when there is an unexpected problem
214      */
215     public void testDummyWar() throws Exception
216     {
217         validator.setWarFileName(dummyWarFile);
218         validator.addFormatter(new ValidationFormatter());
219         validator.setFailOnError(false);
220         validator.execute();
221         assertTrue("Dummy war file has generated a warning",
222             !validator.getStatus().isWarning());
223         assertTrue("Dummy war file has not generated an error",
224             validator.getStatus().isError());
225     }
226 
227     /**
228      * FIXME: Need to finish coding
229      * 
230      * @throws Exception when there is an unexpected problem
231      */
232     public void testDummyJspWar() throws Exception
233     {
234         validator.setWarFileName(dummyJspWarFile);
235         validator.addFormatter(new ValidationFormatter());
236         validator.setFailOnError(false);
237         validator.execute();
238         assertTrue("Dummy jsp war file has not generated an error",
239             validator.getStatus().isError());
240     }
241 }