You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by aj...@apache.org on 2008/09/18 19:20:04 UTC

svn commit: r696723 - in /incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes: AbstractNode.java AllTests.java Node.java StripesJspTransformer.java StripesJspTransformerTest.java Tag.java TagTest.java

Author: ajaquith
Date: Thu Sep 18 10:20:04 2008
New Revision: 696723

URL: http://svn.apache.org/viewvc?rev=696723&view=rev
Log:
Fixed failing unit tests.

Added:
    incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/TagTest.java
Modified:
    incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/AbstractNode.java
    incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/AllTests.java
    incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/Node.java
    incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/StripesJspTransformer.java
    incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/StripesJspTransformerTest.java
    incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/Tag.java

Modified: incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/AbstractNode.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/AbstractNode.java?rev=696723&r1=696722&r2=696723&view=diff
==============================================================================
--- incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/AbstractNode.java (original)
+++ incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/AbstractNode.java Thu Sep 18 10:20:04 2008
@@ -46,7 +46,7 @@
     {
         // Set parent/child relationships
         node.setParent( this );
-        
+
         // Add the node
         m_children.add( node );
     }
@@ -58,11 +58,29 @@
     {
         // Set parent/child relationships
         node.setParent( this );
-        
+
         // Add the node
         m_children.add( index, node );
     }
 
+    public void addSibling( Node node )
+    {
+        if( m_parent == null )
+        {
+            throw new IllegalStateException( "This node does not have a parent." );
+        }
+        List<Node> siblings = m_parent.getChildren();
+        int pos = siblings.indexOf( this );
+        if( pos == siblings.size() - 1 )
+        {
+            m_parent.addChild( node );
+        }
+        else
+        {
+            m_parent.addChild( node, pos + 1 );
+        }
+    }
+
     /**
      * @see com.ecyrd.jspwiki.ui.stripes.Node#getChildren()
      */
@@ -93,6 +111,11 @@
         return m_end;
     }
 
+    public JspDocument getJspDocument()
+    {
+        return m_doc;
+    }
+
     /*
      * (non-Javadoc)
      * 
@@ -186,6 +209,21 @@
     /*
      * (non-Javadoc)
      * 
+     * @see com.ecyrd.jspwiki.ui.stripes.Node#getValue()
+     */
+    public String getValue()
+    {
+        StringBuilder builder = new StringBuilder();
+        for( Node child : m_children )
+        {
+            builder.append( child.toString() );
+        }
+        return builder.toString();
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
      * @see com.ecyrd.jspwiki.ui.stripes.Node#isHtmlNode()
      */
     public boolean isHtmlNode()
@@ -258,6 +296,7 @@
 
     /**
      * Sets the name value of the Node to a supplied string. This method
+     * 
      * @see com.ecyrd.jspwiki.ui.stripes.Node#setName(Node)
      */
     public void setName( String name )
@@ -295,29 +334,9 @@
         m_type = type;
     }
 
-    public JspDocument getJspDocument()
-    {
-        return m_doc;
-    }
-    
-    
-    /*
-     * (non-Javadoc)
-     * 
-     * @see com.ecyrd.jspwiki.ui.stripes.Node#getValue()
-     */
-    public String getValue()
-    {
-        StringBuilder builder = new StringBuilder();
-        for ( Node child : m_children )
-        {
-            builder.append( child.toString() );
-        }
-        return builder.toString();
-    }
-
     /**
      * Replaces all children of the Tag with a single Text node.
+     * 
      * @param value the string to set
      */
     public void setValue( String value )

Modified: incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/AllTests.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/AllTests.java?rev=696723&r1=696722&r2=696723&view=diff
==============================================================================
--- incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/AllTests.java (original)
+++ incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/AllTests.java Thu Sep 18 10:20:04 2008
@@ -13,6 +13,7 @@
         suite.addTest( JspDocumentTest.suite() );
         suite.addTest( JSPWikiJspTransformerTest.suite() );
         suite.addTest( StripesJspTransformerTest.suite() );
+        suite.addTest( TagTest.suite() );
         return suite;
     }
 

Modified: incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/Node.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/Node.java?rev=696723&r1=696722&r2=696723&view=diff
==============================================================================
--- incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/Node.java (original)
+++ incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/Node.java Thu Sep 18 10:20:04 2008
@@ -29,6 +29,13 @@
      * @param index the position to insert the Node into
      */
     public abstract void addChild( Node node, int index );
+    
+    /**
+     * Adds a Node as a sibling to the current Node.  The supplied sibling
+     * will be added just after the current Node. 
+     * @param node the node to insert
+     */
+    public abstract void addSibling( Node node );
 
     /**
      * Returns the child nodes of this node, as a defensive copy of the

Modified: incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/StripesJspTransformer.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/StripesJspTransformer.java?rev=696723&r1=696722&r2=696723&view=diff
==============================================================================
--- incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/StripesJspTransformer.java (original)
+++ incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/StripesJspTransformer.java Thu Sep 18 10:20:04 2008
@@ -11,17 +11,47 @@
 
     public void transform( Map<String, Object> sharedState, JspDocument doc )
     {
+        // Add Stripes taglib entry
+        verifyStripesTaglib( doc );
+        
+        // Process HTML nodes
+        List<Node> nodes = doc.getNodes();
+        for( Node node : nodes )
+        {
+            // For all HTML tags...
+            if( node.isHtmlNode() )
+            {
+                Tag tag = (Tag)node;
+                
+                // Change <form> to <stripes:form>
+                if( "form".equals( tag.getName() ) )
+                {
+                    migrateFormTag( tag );
+                }
+
+                // Change <input type="*"> tags to <stripes:*>
+                else if( "input".equals( tag.getName() ) )
+                {
+                    migrateInputTag( tag );
+                }
+            }
+        }
+    }
 
+    private void verifyStripesTaglib( JspDocument doc )
+    {
         // Add the Stripes taglib declaration if it's not there already
         List<Node> nodes = doc.getNodes( NodeType.JSP_DIRECTIVE );
         boolean declaresStripesTaglib = false;
+        Node lastTaglib = null;
         for ( Node node : nodes )
         {
             Tag tag = (Tag)node;
             if ( "taglib".equals( node.getName() ) )
             {
+                lastTaglib = node;
                 Attribute attribute = tag.getAttribute( "prefix" );
-                if ( attribute != null )
+                if ( attribute != null && "stripes".equals( attribute.getValue() ) )
                 {
                     declaresStripesTaglib = true;
                     break;
@@ -40,32 +70,17 @@
             attribute.setName( "prefix" );
             attribute.setValue( "stripes" );
             tag.addAttribute( attribute );
-            doc.getRoot().addChild( tag );
-            message( doc.getRoot(), "Added Stripes taglib directive." );
-        }
-        
-        // Process HTML nodes
-        nodes = doc.getNodes();
-        for( Node node : nodes )
-        {
-            // For all HTML tags...
-            if( node.isHtmlNode() )
+            if ( lastTaglib == null )
             {
-                Tag tag = (Tag)node;
-                
-                // Change <form> to <stripes:form>
-                if( "form".equals( tag.getName() ) )
-                {
-                    migrateFormTag( tag );
-                }
-
-                // Change <input type="*"> tags to <stripes:*>
-                else if( "input".equals( tag.getName() ) )
-                {
-                    migrateInputTag( tag );
-                }
+                doc.getRoot().addChild( tag, 0 );
+            }
+            else
+            {
+                lastTaglib.addSibling( tag );
             }
+            message( doc.getRoot(), "Added Stripes taglib directive." );
         }
+        
     }
 
     /**

Modified: incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/StripesJspTransformerTest.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/StripesJspTransformerTest.java?rev=696723&r1=696722&r2=696723&view=diff
==============================================================================
--- incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/StripesJspTransformerTest.java (original)
+++ incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/StripesJspTransformerTest.java Thu Sep 18 10:20:04 2008
@@ -25,8 +25,8 @@
         JspDocument doc = new JspParser().parse( s );
         m_transformer.transform( m_sharedState, doc );
         
-        assertEquals( 3, doc.getNodes().size() );
-        Node node = doc.getNodes().get( 0 );
+        assertEquals( 4, doc.getNodes().size() );
+        Node node = doc.getNodes().get( 1 );            // First line is the injected Stripes taglib declaration
         assertEquals( "stripes:form", node.getName() );
         assertEquals( 1, ((Tag)node).getAttributes().size() );
         Node attribute = ((Tag)node).getAttributes().get( 0 );
@@ -39,8 +39,8 @@
         JspDocument doc = new JspParser().parse( s );
         m_transformer.transform( m_sharedState, doc );
 
-        assertEquals( 1, doc.getNodes().size() );
-        Node node = doc.getNodes().get( 0 );
+        assertEquals( 2, doc.getNodes().size() );       // Added Stripes taglib
+        Node node = doc.getNodes().get( 1 );            // Node 0 is injected Stripes taglib
         assertEquals( "stripes:form", node.getName() );
         
         assertEquals( 2, ((Tag)node).getAttributes().size() );
@@ -61,8 +61,8 @@
         m_transformer.transform( m_sharedState, doc );
 
         // After transformation, the "type" attribute is deleted
-        assertEquals( 1, doc.getNodes().size() );
-        node = doc.getNodes().get( 0 );
+        assertEquals( 2, doc.getNodes().size() );           // Added Stripes taglib
+        node = doc.getNodes().get( 1 );
         assertEquals( NodeType.HTML_COMBINED_TAG, node.getType() );
         assertEquals( "stripes:password", node.getName() );
         assertEquals( 4, ((Tag)node).getAttributes().size() );
@@ -77,8 +77,8 @@
         m_transformer.transform( m_sharedState, doc );
 
         // After transformation, the "type" and "value" attributes are deleted; value becomes child node
-        assertEquals( 3, doc.getNodes().size() );
-        Node node = doc.getNodes().get( 0 );
+        assertEquals( 4, doc.getNodes().size() );           // Added Stripes taglib
+        Node node = doc.getNodes().get( 1 );
         assertEquals( NodeType.HTML_START_TAG, node.getType() );
         assertEquals( "stripes:password", node.getName() );
         assertEquals( 3, ((Tag)node).getAttributes().size() );

Modified: incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/Tag.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/Tag.java?rev=696723&r1=696722&r2=696723&view=diff
==============================================================================
--- incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/Tag.java (original)
+++ incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/Tag.java Thu Sep 18 10:20:04 2008
@@ -142,24 +142,20 @@
      */
     public void addChild( Node node, int index )
     {
-        if( m_parent == null )
-        {
-            throw new IllegalStateException( "Node does not have a parent!" );
-        }
-
         // If this node is a "combined node," split it into two
         if( m_type == NodeType.HTML_COMBINED_TAG )
         {
+            if( m_parent == null )
+            {
+                throw new IllegalStateException( "Node does not have a parent!" );
+            }
+
             // Change node type to start tag
             m_type = NodeType.HTML_START_TAG;
 
             // Build new end tag & set its parent
             Tag endNode = new Tag( m_doc, NodeType.HTML_END_TAG );
             endNode.setName( m_name );
-
-            // FIXME
-            // endNode.setText( NodeType.HTML_END_TAG.getTagEnd() + m_name +
-            // NodeType.HTML_END_TAG.getTagEnd() );
             endNode.setParent( m_parent );
 
             // Insert as sibling of this node
@@ -174,10 +170,9 @@
                 m_parent.addChild( endNode, startTagPos + 1 );
             }
         }
-        else
-        {
-            super.addChild( node, index );
-        }
+        
+        // Finally add the child to the parent
+        super.addChild( node, index );
     }
 
     public String getValue()

Added: incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/TagTest.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/TagTest.java?rev=696723&view=auto
==============================================================================
--- incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/TagTest.java (added)
+++ incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/TagTest.java Thu Sep 18 10:20:04 2008
@@ -0,0 +1,84 @@
+package com.ecyrd.jspwiki.ui.stripes;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class TagTest extends TestCase
+{
+    public void testAddChildCombined()
+    {
+        JspDocument doc = new JspDocument();
+        
+        // Set up tag <foo/>
+        Tag parentTag = new Tag( doc, NodeType.HTML_COMBINED_TAG );
+        parentTag.setName( "foo" );
+        doc.getRoot().addChild( parentTag );
+        assertEquals( "<foo/>", parentTag.toString() );
+        assertEquals( 1, doc.getRoot().getChildren().size() );
+        
+        // Add child tag <bar/>
+        Tag childTag = new Tag( doc, NodeType.HTML_COMBINED_TAG );
+        childTag.setName( "bar" );
+        parentTag.addChild( childTag );
+        
+        // Should result in <foo><bar/></foo>
+        assertEquals( "<foo><bar/></foo>", doc.toString() );
+        assertEquals( 2, doc.getRoot().getChildren().size() );
+    }
+
+    public void testAddChild()
+    {
+        JspDocument doc = new JspDocument();
+        
+        // Set up tag <foo>
+        Tag parentTag = new Tag( doc, NodeType.HTML_START_TAG);
+        parentTag.setName( "foo" );
+        doc.getRoot().addChild( parentTag );
+        assertEquals( "<foo>", parentTag.toString() );
+        assertEquals( 1, doc.getRoot().getChildren().size() );
+        
+        // Add child tag <bar/>
+        Tag childTag = new Tag( doc, NodeType.HTML_COMBINED_TAG );
+        childTag.setName( "bar" );
+        parentTag.addChild( childTag );
+        
+        // Should result in <foo><bar/> (with bar a child of foo)
+        assertEquals( "<foo><bar/>", doc.toString() );
+        assertEquals( 1, doc.getRoot().getChildren().size() );
+    }
+
+    public void testAddChildAtIndex()
+    {
+        JspDocument doc = new JspDocument();
+        
+        // Set up tag <foo>
+        Tag parentTag = new Tag( doc, NodeType.HTML_START_TAG);
+        parentTag.setName( "foo" );
+        doc.getRoot().addChild( parentTag );
+        assertEquals( "<foo>", parentTag.toString() );
+        assertEquals( 1, doc.getRoot().getChildren().size() );
+        
+        // Add child tag <bar/>
+        Tag childTag = new Tag( doc, NodeType.HTML_COMBINED_TAG );
+        childTag.setName( "bar" );
+        parentTag.addChild( childTag );
+        assertEquals( 1, doc.getRoot().getChildren().size() );
+        
+        // Add a second child <golf/>  before <bar>
+        childTag = new Tag( doc, NodeType.HTML_COMBINED_TAG );
+        childTag.setName( "golf" );
+        parentTag.addChild( childTag, 0 );
+        
+        // Should result in <foo><golf/><bar/> (with golf and bar children of foo)
+        assertEquals( "<foo><golf/><bar/>", doc.toString() );
+        assertEquals( 1, doc.getRoot().getChildren().size() );
+        assertEquals( 2, doc.getRoot().getChildren().get( 0 ).getChildren().size() );
+    }
+    
+    public static Test suite()
+    {
+        return new TestSuite( TagTest.class );
+    }
+
+}