View Javadoc

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