001: package test.net.sourceforge.pmd.jsp.ast;
002:
003: import static org.junit.Assert.assertEquals;
004: import net.sourceforge.pmd.jsp.ast.ASTElExpression;
005: import net.sourceforge.pmd.jsp.ast.ASTJspComment;
006: import net.sourceforge.pmd.jsp.ast.ASTJspDeclaration;
007: import net.sourceforge.pmd.jsp.ast.ASTJspDirective;
008: import net.sourceforge.pmd.jsp.ast.ASTJspDirectiveAttribute;
009: import net.sourceforge.pmd.jsp.ast.ASTJspExpression;
010: import net.sourceforge.pmd.jsp.ast.ASTJspExpressionInAttribute;
011: import net.sourceforge.pmd.jsp.ast.ASTJspScriptlet;
012: import net.sourceforge.pmd.jsp.ast.ASTValueBinding;
013:
014: import org.junit.Test;
015:
016: import java.util.ArrayList;
017: import java.util.Collections;
018: import java.util.Comparator;
019: import java.util.List;
020: import java.util.Set;
021:
022: public class JspPageStyleTest extends AbstractJspNodesTst {
023:
024: /**
025: * Test parsing of a JSP comment.
026: */
027: @Test
028: public void testComment() {
029: Set comments = getNodes(ASTJspComment.class, JSP_COMMENT);
030: assertEquals("One comment expected!", 1, comments.size());
031: ASTJspComment comment = (ASTJspComment) comments.iterator()
032: .next();
033: assertEquals("Correct comment content expected!",
034: "some comment", comment.getImage());
035: }
036:
037: /**
038: * Test parsing a JSP directive.
039: */
040: @Test
041: public void testDirective() {
042: Set nodes = getNodes(null, JSP_DIRECTIVE);
043:
044: Set<ASTJspDirective> directives = getNodesOfType(
045: ASTJspDirective.class, nodes);
046: assertEquals("One directive expected!", 1, directives.size());
047: ASTJspDirective directive = directives.iterator().next();
048: assertEquals("Correct directive name expected!", "page",
049: directive.getName());
050:
051: Set<ASTJspDirectiveAttribute> directiveAttrs = getNodesOfType(
052: ASTJspDirectiveAttribute.class, nodes);
053: assertEquals("Two directive attributes expected!", 2,
054: directiveAttrs.size());
055:
056: List<ASTJspDirectiveAttribute> attrsList = new ArrayList<ASTJspDirectiveAttribute>(
057: directiveAttrs);
058: Collections.sort(attrsList,
059: new Comparator<ASTJspDirectiveAttribute>() {
060: public int compare(ASTJspDirectiveAttribute arg0,
061: ASTJspDirectiveAttribute arg1) {
062: return arg0.getName().compareTo(arg1.getName());
063: }
064: });
065:
066: ASTJspDirectiveAttribute attr = attrsList.get(0);
067: assertEquals("Correct directive attribute name expected!",
068: "language", attr.getName());
069: assertEquals("Correct directive attribute value expected!",
070: "java", attr.getValue());
071:
072: attr = attrsList.get(1);
073: assertEquals("Correct directive attribute name expected!",
074: "session", attr.getName());
075: assertEquals("Correct directive attribute value expected!",
076: "true", attr.getValue());
077:
078: }
079:
080: /**
081: * Test parsing of a JSP declaration.
082: */
083: @Test
084: public void testDeclaration() {
085: Set declarations = getNodes(ASTJspDeclaration.class,
086: JSP_DECLARATION);
087: assertEquals("One declaration expected!", 1, declarations
088: .size());
089: ASTJspDeclaration declaration = (ASTJspDeclaration) declarations
090: .iterator().next();
091: assertEquals("Correct declaration content expected!",
092: "String someString = \"s\";", declaration.getImage());
093: }
094:
095: /**
096: * Test parsing of a JSP scriptlet.
097: */
098: @Test
099: public void testScriptlet() {
100: Set scriptlets = getNodes(ASTJspScriptlet.class, JSP_SCRIPTLET);
101: assertEquals("One scriptlet expected!", 1, scriptlets.size());
102: ASTJspScriptlet scriptlet = (ASTJspScriptlet) scriptlets
103: .iterator().next();
104: assertEquals("Correct scriptlet content expected!",
105: "someString = someString + \"suffix\";", scriptlet
106: .getImage());
107: }
108:
109: /**
110: * Test parsing of a JSP expression.
111: */
112: @Test
113: public void testExpression() {
114: Set expressions = getNodes(ASTJspExpression.class,
115: JSP_EXPRESSION);
116: assertEquals("One expression expected!", 1, expressions.size());
117: ASTJspExpression expression = (ASTJspExpression) expressions
118: .iterator().next();
119: assertEquals("Correct expression content expected!",
120: "someString", expression.getImage());
121: }
122:
123: /**
124: * Test parsing of a JSP expression in an attribute.
125: */
126: @Test
127: public void testExpressionInAttribute() {
128: Set expressions = getNodes(ASTJspExpressionInAttribute.class,
129: JSP_EXPRESSION_IN_ATTRIBUTE);
130: assertEquals("One expression expected!", 1, expressions.size());
131: ASTJspExpressionInAttribute expression = (ASTJspExpressionInAttribute) expressions
132: .iterator().next();
133: assertEquals("Correct expression content expected!",
134: "style.getClass()", expression.getImage());
135: }
136:
137: /**
138: * Test parsing of a EL expression.
139: */
140: @Test
141: public void testElExpression() {
142: Set expressions = getNodes(ASTElExpression.class,
143: JSP_EL_EXPRESSION);
144: assertEquals("One expression expected!", 1, expressions.size());
145: ASTElExpression expression = (ASTElExpression) expressions
146: .iterator().next();
147: assertEquals("Correct expression content expected!",
148: "myBean.get(\"${ World }\")", expression.getImage());
149: }
150:
151: /**
152: * Test parsing of a EL expression in an attribute.
153: */
154: @Test
155: public void testElExpressionInAttribute() {
156: Set expressions = getNodes(ASTElExpression.class,
157: JSP_EL_EXPRESSION_IN_ATTRIBUTE);
158: assertEquals("One expression expected!", 1, expressions.size());
159: ASTElExpression expression = (ASTElExpression) expressions
160: .iterator().next();
161: assertEquals("Correct expression content expected!",
162: "myValidator.find(\"'jsp'\")", expression.getImage());
163: }
164:
165: /**
166: * Test parsing of a EL expression in an attribute.
167: */
168: @Test
169: public void testJsfValueBinding() {
170: Set valueBindings = getNodes(ASTValueBinding.class,
171: JSF_VALUE_BINDING);
172: assertEquals("One value binding expected!", 1, valueBindings
173: .size());
174: ASTValueBinding valueBinding = (ASTValueBinding) valueBindings
175: .iterator().next();
176: assertEquals("Correct expression content expected!",
177: "myValidator.find(\"'jsf'\")", valueBinding.getImage());
178: }
179:
180: private static final String JSP_COMMENT = "<html> <%-- some comment --%> </html>";
181:
182: private static final String JSP_DIRECTIVE = "<html> <%@ page language=\"java\" session='true'%> </html>";
183:
184: private static final String JSP_DECLARATION = "<html><%! String someString = \"s\"; %></html>";
185:
186: private static final String JSP_SCRIPTLET = "<html> <% someString = someString + \"suffix\"; %> </html>";
187:
188: private static final String JSP_EXPRESSION = "<html><head><title> <%= someString %> </title></head></html>";
189:
190: private static final String JSP_EXPRESSION_IN_ATTRIBUTE = "<html> <body> <p class='<%= style.getClass() %>'> Hello </p> </body> </html>";
191:
192: private static final String JSP_EL_EXPRESSION = "<html><title>Hello ${myBean.get(\"${ World }\") } .jsp</title></html>";
193:
194: private static final String JSP_EL_EXPRESSION_IN_ATTRIBUTE = "<html> <f:validator type=\"get('type').${myValidator.find(\"'jsp'\")}\" /> </html>";
195:
196: private static final String JSF_VALUE_BINDING = "<html> <body> <p class='#{myValidator.find(\"'jsf'\")}'> Hello </p> </body> </html>";
197:
198: public static junit.framework.Test suite() {
199: return new junit.framework.JUnit4TestAdapter(
200: JspPageStyleTest.class);
201: }
202: }
|