1   package test.net.sourceforge.pmd.jsp.ast;
2   
3   import net.sourceforge.pmd.jsp.ast.ASTElExpression;
4   import net.sourceforge.pmd.jsp.ast.ASTJspComment;
5   import net.sourceforge.pmd.jsp.ast.ASTJspDeclaration;
6   import net.sourceforge.pmd.jsp.ast.ASTJspDirective;
7   import net.sourceforge.pmd.jsp.ast.ASTJspDirectiveAttribute;
8   import net.sourceforge.pmd.jsp.ast.ASTJspExpression;
9   import net.sourceforge.pmd.jsp.ast.ASTJspExpressionInAttribute;
10  import net.sourceforge.pmd.jsp.ast.ASTJspScriptlet;
11  import net.sourceforge.pmd.jsp.ast.ASTValueBinding;
12  
13  import java.util.ArrayList;
14  import java.util.Collections;
15  import java.util.Comparator;
16  import java.util.List;
17  import java.util.Set;
18  
19  public class JspPageStyleTest extends AbstractJspNodesTst {
20  
21      /***
22       * Test parsing of a JSP comment.
23       */
24      public void testComment() {
25          Set comments = getNodes(ASTJspComment.class, JSP_COMMENT);
26          assertEquals("One comment expected!", 1, comments.size());
27          ASTJspComment comment = (ASTJspComment) comments.iterator().next();
28          assertEquals("Correct comment content expected!", "some comment", comment.getImage());
29      }
30  
31      /***
32       * Test parsing a JSP directive.
33       */
34      public void testDirective() {
35          Set nodes = getNodes(null, JSP_DIRECTIVE);
36  
37          Set directives = getNodesOfType(ASTJspDirective.class, nodes);
38          assertEquals("One directive expected!", 1, directives.size());
39          ASTJspDirective directive = (ASTJspDirective) directives.iterator().next();
40          assertEquals("Correct directive name expected!",
41                  "page", directive.getName());
42  
43          Set directiveAttrs = getNodesOfType(ASTJspDirectiveAttribute.class, nodes);
44          assertEquals("Two directive attributes expected!", 2, directiveAttrs.size());
45  
46          List attrsList = new ArrayList(directiveAttrs);
47          Collections.sort(attrsList, new Comparator() {
48              public int compare(Object arg0, Object arg1) {
49                  return ((ASTJspDirectiveAttribute) arg0).getName().compareTo(((ASTJspDirectiveAttribute) arg1).getName());
50              }
51          });
52  
53          ASTJspDirectiveAttribute attr = (ASTJspDirectiveAttribute) attrsList.get(0);
54          assertEquals("Correct directive attribute name expected!",
55                  "language", attr.getName());
56          assertEquals("Correct directive attribute value expected!",
57                  "java", attr.getValue());
58  
59          attr = (ASTJspDirectiveAttribute) attrsList.get(1);
60          assertEquals("Correct directive attribute name expected!",
61                  "session", attr.getName());
62          assertEquals("Correct directive attribute value expected!",
63                  "true", attr.getValue());
64  
65  
66      }
67  
68      /***
69       * Test parsing of a JSP declaration.
70       */
71      public void testDeclaration() {
72          Set declarations = getNodes(ASTJspDeclaration.class, JSP_DECLARATION);
73          assertEquals("One declaration expected!", 1, declarations.size());
74          ASTJspDeclaration declaration = (ASTJspDeclaration) declarations.iterator().next();
75          assertEquals("Correct declaration content expected!",
76                  "String someString = \"s\";", declaration.getImage());
77      }
78  
79      /***
80       * Test parsing of a JSP scriptlet.
81       */
82      public void testScriptlet() {
83          Set scriptlets = getNodes(ASTJspScriptlet.class, JSP_SCRIPTLET);
84          assertEquals("One scriptlet expected!", 1, scriptlets.size());
85          ASTJspScriptlet scriptlet = (ASTJspScriptlet) scriptlets.iterator().next();
86          assertEquals("Correct scriptlet content expected!",
87                  "someString = someString + \"suffix\";", scriptlet.getImage());
88      }
89  
90      /***
91       * Test parsing of a JSP expression.
92       */
93      public void testExpression() {
94          Set expressions = getNodes(ASTJspExpression.class, JSP_EXPRESSION);
95          assertEquals("One expression expected!", 1, expressions.size());
96          ASTJspExpression expression = (ASTJspExpression) expressions.iterator().next();
97          assertEquals("Correct expression content expected!",
98                  "someString", expression.getImage());
99      }
100 
101     /***
102      * Test parsing of a JSP expression in an attribute.
103      */
104     public void testExpressionInAttribute() {
105         Set expressions = getNodes(ASTJspExpressionInAttribute.class,
106                 JSP_EXPRESSION_IN_ATTRIBUTE);
107         assertEquals("One expression expected!", 1, expressions.size());
108         ASTJspExpressionInAttribute expression = (ASTJspExpressionInAttribute) expressions.iterator().next();
109         assertEquals("Correct expression content expected!",
110                 "style.getClass()", expression.getImage());
111     }
112 
113     /***
114      * Test parsing of a EL expression.
115      */
116     public void testElExpression() {
117         Set expressions = getNodes(ASTElExpression.class, JSP_EL_EXPRESSION);
118         assertEquals("One expression expected!", 1, expressions.size());
119         ASTElExpression expression = (ASTElExpression) expressions.iterator().next();
120         assertEquals("Correct expression content expected!",
121                 "myBean.get(\"${ World }\")", expression.getImage());
122     }
123 
124     /***
125      * Test parsing of a EL expression in an attribute.
126      */
127     public void testElExpressionInAttribute() {
128         Set expressions = getNodes(ASTElExpression.class, JSP_EL_EXPRESSION_IN_ATTRIBUTE);
129         assertEquals("One expression expected!", 1, expressions.size());
130         ASTElExpression expression = (ASTElExpression) expressions.iterator().next();
131         assertEquals("Correct expression content expected!",
132                 "myValidator.find(\"'jsp'\")", expression.getImage());
133     }
134 
135     /***
136      * Test parsing of a EL expression in an attribute.
137      */
138     public void testJsfValueBinding() {
139         Set valueBindings = getNodes(ASTValueBinding.class, JSF_VALUE_BINDING);
140         assertEquals("One value binding expected!", 1, valueBindings.size());
141         ASTValueBinding valueBinding = (ASTValueBinding) valueBindings.iterator().next();
142         assertEquals("Correct expression content expected!",
143                 "myValidator.find(\"'jsf'\")", valueBinding.getImage());
144     }
145 
146     private static final String JSP_COMMENT
147             = "<html> <%-- some comment --%> </html>";
148 
149     private static final String JSP_DIRECTIVE
150             = "<html> <%@ page language=\"java\" session='true'%> </html>";
151 
152     private static final String JSP_DECLARATION
153             = "<html><%! String someString = \"s\"; %></html>";
154 
155     private static final String JSP_SCRIPTLET
156             = "<html> <% someString = someString + \"suffix\"; %> </html>";
157 
158     private static final String JSP_EXPRESSION
159             = "<html><head><title> <%= someString %> </title></head></html>";
160 
161     private static final String JSP_EXPRESSION_IN_ATTRIBUTE
162             = "<html> <body> <p class='<%= style.getClass() %>'> Hello </p> </body> </html>";
163 
164     private static final String JSP_EL_EXPRESSION
165             = "<html><title>Hello ${myBean.get(\"${ World }\") } .jsp</title></html>";
166 
167     private static final String JSP_EL_EXPRESSION_IN_ATTRIBUTE
168             = "<html> <f:validator type=\"get('type').${myValidator.find(\"'jsp'\")}\" /> </html>";
169 
170     private static final String JSF_VALUE_BINDING
171             = "<html> <body> <p class='#{myValidator.find(\"'jsf'\")}'> Hello </p> </body> </html>";
172 }