dion        02/05/21 23:55:28

  Modified:    src/java/org/apache/maven/struts Struts10WarValidator.java
                        Struts10WarFile.java FormBean.java
               src/test/org/apache/maven/struts Struts10WarFileTest.java
  Added:       src/templates/build/plugins/struts build.xml
                        default.properties Control.vm
  Log:
  no message
  
  Revision  Changes    Path
  1.3       +33 -7     
jakarta-turbine-maven/src/java/org/apache/maven/struts/Struts10WarValidator.java
  
  Index: Struts10WarValidator.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/struts/Struts10WarValidator.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Struts10WarValidator.java 21 May 2002 00:03:30 -0000      1.2
  +++ Struts10WarValidator.java 22 May 2002 06:55:28 -0000      1.3
  @@ -55,9 +55,12 @@
    */
   
   import java.io.IOException;
  +import java.util.List;
  +import java.util.Iterator;
   
  -import org.apache.maven.j2ee.WarValidator;
   import org.apache.maven.j2ee.ValidationEvent;
  +import org.apache.maven.j2ee.WarValidator;
  +import org.apache.maven.j2ee.WarClassLoader;
   
   /**
    * A class that validates a Struts 1.0 War File.
  @@ -68,7 +71,7 @@
    * </ol>
    *
    * @author  dion
  - * @version $Id: Struts10WarValidator.java,v 1.2 2002/05/21 00:03:30 dion Exp $
  + * @version $Id: Struts10WarValidator.java,v 1.3 2002/05/22 06:55:28 dion Exp $
    */
   public class Struts10WarValidator extends WarValidator
   {
  @@ -93,12 +96,12 @@
               Struts10WarFile strutsWar = new Struts10WarFile(getWarFileName());
               strutsWar.setConfig(getConfig());
               validateStrutsConfig(strutsWar);
  +            validateFormBeans(strutsWar);
           }
           catch (IOException ioe)
           {
               ioe.printStackTrace();
  -            getBroadcaster().fireErrorEvent(new ValidationEvent(this, 
  -                getWarFileName(), "Error reading struts war file"));
  +            error("Error reading struts war file");
           }
       }
       
  @@ -124,11 +127,34 @@
        */
       private void validateStrutsConfig(Struts10WarFile strutsWar)
       {
  +        info("validating Struts Configuration");
           if (strutsWar.getStrutsConfigEntry() == null)
           {
  -            getBroadcaster().fireErrorEvent(new ValidationEvent(this, 
  -                getWarFileName(), "Struts Configuration: '" + 
  -                strutsWar.getConfig() + "' not found in the war file"));
  +            error("Struts Configuration: '" + strutsWar.getConfig() + 
  +                "' not found in the war file");
  +        }
  +    }
  +    
  +    /** validations for the form beans in the config
  +     * @param strutsWar - the struts web app being validated
  +     */
  +    private void validateFormBeans(Struts10WarFile strutsWar) throws IOException
  +    {
  +        info("validating Struts Form Beans");
  +        List formBeans = strutsWar.getFormBeans();
  +        FormBean bean = null;
  +        ClassLoader loader = new WarClassLoader(strutsWar, 
  +            getClass().getClassLoader());
  +        for (Iterator beans = formBeans.iterator(); beans.hasNext();)
  +        {
  +            bean = (FormBean) beans.next();
  +            info("validating form bean: '" + bean.getName() + "', class: '" +
  +                bean.getType() + "'");
  +            validateClass(bean.getType(), loader);
  +            if (bean.getClassName() != null)
  +            {
  +                validateClass(bean.getClassName(), loader);
  +            }
           }
       }
   }
  
  
  
  1.3       +63 -2     
jakarta-turbine-maven/src/java/org/apache/maven/struts/Struts10WarFile.java
  
  Index: Struts10WarFile.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/struts/Struts10WarFile.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Struts10WarFile.java      21 May 2002 01:18:57 -0000      1.2
  +++ Struts10WarFile.java      22 May 2002 06:55:28 -0000      1.3
  @@ -55,16 +55,26 @@
    */
   
   import java.io.File;
  +import java.io.InputStream;
   import java.io.IOException;
  +import java.util.ArrayList;
  +import java.util.Iterator;
  +import java.util.List;
   import java.util.jar.JarEntry;
   
   import org.apache.maven.j2ee.WarFile;
   
  +import org.dom4j.Document;
  +import org.dom4j.DocumentException;
  +import org.dom4j.Element;
  +import org.dom4j.Node;
  +import org.dom4j.io.SAXReader;
  +
   /**
    * Encapsulates a Struts 1.0 War File. Holds functionality to access Struts
    * specific resources and data in the war.
    * @author  dion
  - * @version $Id: Struts10WarFile.java,v 1.2 2002/05/21 01:18:57 dion Exp $
  + * @version $Id: Struts10WarFile.java,v 1.3 2002/05/22 06:55:28 dion Exp $
    */
   public class Struts10WarFile extends WarFile
   {
  @@ -158,4 +168,55 @@
           return getJarEntry(getConfig());
       }
    
  -}
  +   /** Get the Struts configuration back as a dom4j Document, for easier 
  +     * processing
  +     * @return a {@link Document} representing the web.xml
  +     * @throws IOException if there are any issues reading the web.xml
  +     * or producing the xml document
  +     */
  +    private Document getStrutsConfig() throws IOException
  +    {
  +        if (getWebXmlEntry() == null)
  +        {
  +            throw new IOException("Attempted to get non-existent web.xml");
  +        }
  +        try 
  +        {
  +            SAXReader xmlReader = new SAXReader(false);
  +            xmlReader.setEntityResolver(new StrutsEntityResolver());
  +            InputStream configStream = getInputStream(getStrutsConfigEntry());
  +            Document configXml = xmlReader.read(configStream);
  +            return configXml;
  +        }
  +        catch (DocumentException de)
  +        {
  +            de.printStackTrace();
  +            throw new IOException(de.getMessage());
  +        }
  +    }
  +    
  +    /** retrieves the form beans defined in the struts configuration file
  +     * @return a collection of {@link FormBean form beans}
  +     * @throws IOException when there are problems reading from the war
  +     */
  +    public List getFormBeans() throws IOException
  +    {
  +        List formBeans = new ArrayList();
  +        Document config = getStrutsConfig();
  +        List formBeanNodes = config.selectNodes(
  +            "/struts-config/form-beans/form-bean");
  +        Element formBeanNode = null;
  +        FormBean formBean = null;
  +        for (Iterator nodes = formBeanNodes.iterator(); nodes.hasNext();)
  +        {
  +            formBeanNode = (Element) nodes.next();
  +            formBean = new FormBean();
  +            formBean.setClassName(formBeanNode.attributeValue("className"));
  +            formBean.setName(formBeanNode.attributeValue("name"));
  +            formBean.setType(formBeanNode.attributeValue("type"));
  +            formBeans.add(formBean);
  +        }
  +        
  +        return formBeans;
  +    }
  + }
  
  
  
  1.2       +39 -1     
jakarta-turbine-maven/src/java/org/apache/maven/struts/FormBean.java
  
  Index: FormBean.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/struts/FormBean.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- FormBean.java     22 May 2002 01:41:33 -0000      1.1
  +++ FormBean.java     22 May 2002 06:55:28 -0000      1.2
  @@ -57,7 +57,7 @@
   /**
    * A class to hold data about a Struts form bean as found in the configuration
    * @author  dion
  - * @version $Id: FormBean.java,v 1.1 2002/05/22 01:41:33 dion Exp $
  + * @version $Id: FormBean.java,v 1.2 2002/05/22 06:55:28 dion Exp $
    */
   public class FormBean
   {
  @@ -121,5 +121,43 @@
       public void setType(String type) 
       {
           this.type = type;
  +    }
  +    
  +    /** whether the passed object is the same as this one. In the case of a form
  +     * bean, the name is the unique qualifier. So two form beans are equal if
  +     * they have equal names
  +     * @param o any object
  +     * @return true if o is the same as this object, false otherwise
  +     */
  +    public boolean equals(Object o)
  +    {
  +        if (o == null)
  +        {
  +            return false;
  +        }
  +        
  +        if (getClass() != o.getClass())
  +        {
  +            return false;
  +        }
  +        
  +        return getName().equals(((FormBean) o).getName());
  +    }
  +    
  +    /** provides the hashCode of this object, which is determined by simply
  +     * delegating the responsibility to the name property
  +     * @return the hashCode of the name if not null, otherwise delegate to the
  +     * parent class
  +     */
  +    public int hashCode()
  +    {
  +        if (getName() != null)
  +        {
  +            return getName().hashCode();
  +        }
  +        else
  +        {
  +            return super.hashCode();
  +        }
       }
   }
  
  
  
  1.1                  
jakarta-turbine-maven/src/templates/build/plugins/struts/build.xml
  
  Index: build.xml
  ===================================================================
  <?xml version='1.0' encoding='UTF-8' ?>
  
  <project name="$project.id" default="war" basedir="$antBasedir">
  
  #parse("build.init.target")
  
      <!-- ================================================================== -->
      <!-- L O C A L   I N I T                                                -->
      <!-- ================================================================== -->
  
      <target name="local-init" depends="init">
          <!-- Pick up tool specific defaults -->
          <property file="${maven.home}/plugins/$plugin/default.properties"/>
      </target>
    
      <target name="validate-struts-war" depends="local-init">
          <taskdef
            name="struts10warvalidator"
            classname="org.apache.maven.struts.Struts10WarValidator">
            <classpath refid="maven-classpath"/>
            <classpath refid="maven.dependency.classpath"/>
          </taskdef>
          
          <struts10warvalidator
              warFileName="${maven.build.dir}/${maven.j2ee.war.name}.war">
              <formatter type="plain" usefile="false"/>
          </struts10warvalidator>
  
      </target>    
  </project>
  
  
  
  1.1                  
jakarta-turbine-maven/src/templates/build/plugins/struts/default.properties
  
  Index: default.properties
  ===================================================================
  # properties file for struts plugin build.xml
  
  
  
  1.1                  
jakarta-turbine-maven/src/templates/build/plugins/struts/Control.vm
  
  Index: Control.vm
  ===================================================================
  ## -------------------------------------------------------
  ## Control file for Maven j2ee plug-in
  ## -------------------------------------------------------
  
  ## -------------------------------------------------------
  ## Add build files to the list
  ## -------------------------------------------------------
  $buildElements.add("plugins/$plugin/build.xml")
  $buildElements.add("plugins/$plugin/default.properties")
  
  ## -------------------------------------------------------
  ## Make the list of build-j2ee.xml delegators
  ## -------------------------------------------------------
  $delegators.put("validate-struts-war", "plugins/$plugin/build.xml")
  
  ## -------------------------------------------------------
  ## Make the list of build-j2ee.xml callbacks
  ## -------------------------------------------------------
  $callbacks.put( "$plugin", [ "pre-validate-struts-war", "post-validate-struts-war"] )
  
  
  
  1.2       +85 -3     
jakarta-turbine-maven/src/test/org/apache/maven/struts/Struts10WarFileTest.java
  
  Index: Struts10WarFileTest.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-maven/src/test/org/apache/maven/struts/Struts10WarFileTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Struts10WarFileTest.java  21 May 2002 00:04:10 -0000      1.1
  +++ Struts10WarFileTest.java  22 May 2002 06:55:28 -0000      1.2
  @@ -54,26 +54,108 @@
    * <http://www.apache.org/>.
    */
   
  +import java.io.IOException;
  +import java.util.List;
  +
   import junit.framework.TestCase;
   
   /**
    * Unit tests for {@link Struts10WarFile}
    *
    * @author dion
  - * @version $Id: Struts10WarFileTest.java,v 1.1 2002/05/21 00:04:10 dion Exp $
  + * @version $Id: Struts10WarFileTest.java,v 1.2 2002/05/22 06:55:28 dion Exp $
    */
   public class Struts10WarFileTest extends TestCase {
   
  +    /** instance to be tested */
  +    private Struts10WarFile instance;
  +    /** test war file */
  +    private String testWarFileName;
  +    
       /** Creates a new instance of Struts10WarFileTest 
        * @param testName the name of the test
        */
       public Struts10WarFileTest(java.lang.String testName) {
           super(testName);
       }
  +
  +    /**
  +     * Initialize per test data
  +     * @throws Exception when there is an unexpected problem
  +     */
  +    public void setUp() throws Exception
  +    {
  +        String baseDir = System.getProperty("basedir");
  +        assertNotNull("The system property basedir was not defined.", baseDir);
  +        String fs = System.getProperty("file.separator");
  +        assertNotNull("The system property file.separator was not defined.", 
  +            fs);
  +        testWarFileName = baseDir + fs + "src/test-struts/struts-example.war";
  +    }
  +    
  +    /** Test that the constructor succeeds
  +     * @throws Exception when there is an unexpected problem
  +     */
  +    public void testConstructor() throws Exception
  +    {
  +        try 
  +        {
  +            instance = new Struts10WarFile(testWarFileName);
  +        }
  +        catch (IOException ioe)
  +        {
  +            fail("There was an error reading the test war file");
  +        }
  +    }
  +    
  +    /** test that the config is defaulted correctly
  +     * @throws Exception when there is an unexpected problem
  +     */
  +    public void testDefaultConfig() throws Exception
  +    {
  +        testConstructor();
  +        assertEquals("Default config isn't WEB-INF/struts-config.xml", 
  +            Struts10WarFile.DEFAULT_CONFIG,
  +            instance.getConfig());
  +    }
       
  -    /** dummy method to allow tests to pass */
  -    public void testNothing() throws Exception
  +    /** test the the config property works
  +     * @throws Exception when there is an unexpected problem
  +     */
  +    public void testConfig() throws Exception
       {
  +        testDefaultConfig();
  +        String dummyConfig = "WEB-INF/struts.xml";
  +        instance.setConfig(dummyConfig);
  +        assertEquals("Set or get for config failed", dummyConfig,
  +            instance.getConfig());
       }
       
  +    /** test retrieving the struts config as a JarEntry
  +     * @throws Exception when there is an unexpected problem
  +     */
  +    public void testConfigEntry() throws Exception
  +    {
  +        testConstructor();
  +        assertNotNull("config entry was null", instance.getStrutsConfigEntry());
  +    }
  +    
  +    /** test the retrieval of form beans
  +     * @throws Exception when there is an unexpected problem
  +     */
  +    public void testFormBeans() throws Exception
  +    {
  +        testConstructor();
  +        assertEquals("Number of form beans returned was wrong", 3, 
  +            instance.getFormBeans().size());
  +        List formBeans = instance.getFormBeans();
  +        assertEquals("first form bean isn't logonForm", "logonForm",
  +            ((FormBean)formBeans.get(0)).getName());
  +        assertEquals("second form bean isn't registrationForm", 
  +            "registrationForm",
  +            ((FormBean)formBeans.get(1)).getName());
  +        assertEquals("third form bean isn't subscriptionForm", 
  +            "subscriptionForm",
  +            ((FormBean)formBeans.get(2)).getName());
  +    }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to