View Javadoc

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