1 package test.net.sourceforge.pmd.jsp.ast;
2
3 import net.sourceforge.pmd.jsp.ast.ASTAttribute;
4 import net.sourceforge.pmd.jsp.ast.ASTAttributeValue;
5 import net.sourceforge.pmd.jsp.ast.ASTCData;
6 import net.sourceforge.pmd.jsp.ast.ASTCommentTag;
7 import net.sourceforge.pmd.jsp.ast.ASTDoctypeDeclaration;
8 import net.sourceforge.pmd.jsp.ast.ASTDoctypeExternalId;
9 import net.sourceforge.pmd.jsp.ast.ASTElement;
10
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Comparator;
14 import java.util.List;
15 import java.util.Set;
16
17 /***
18 * Test parsing of a JSP in document style, by checking the generated AST.
19 *
20 * @author pieter_van_raemdonck - Application Engineers NV/SA - www.ae.be
21 *
22 */
23 public class JspDocStyleTest extends AbstractJspNodesTst {
24
25 /***
26 * Smoke test for JSP parser.
27 *
28 * @throws Throwable
29 */
30 public void testSimplestJsp() throws Throwable {
31 assertNumberOfNodes(ASTElement.class, TEST_SIMPLEST_HTML, 1);
32 }
33
34 /***
35 * Test the information on a Element and Attribute.
36 *
37 * @throws Throwable
38 */
39 public void testElementAttributeAndNamespace() throws Throwable {
40 Set nodes = getNodes(null, TEST_ELEMENT_AND_NAMESPACE);
41
42 Set elementNodes = getNodesOfType(ASTElement.class, nodes);
43 assertEquals("One element node expected!", 1, elementNodes.size());
44 ASTElement element = (ASTElement) elementNodes.iterator().next();
45 assertEquals("Correct name expected!", "h:html", element.getName());
46 assertEquals("Has namespace prefix!", true, element.isHasNamespacePrefix());
47 assertEquals("Element is empty!", true, element.isEmpty());
48 assertEquals("Correct namespace prefix of element expected!", "h", element
49 .getNamespacePrefix());
50 assertEquals("Correct local name of element expected!", "html", element
51 .getLocalName());
52
53 Set attributeNodes = getNodesOfType(ASTAttribute.class, nodes);
54 assertEquals("One attribute node expected!", 1, attributeNodes.size());
55 ASTAttribute attribute = (ASTAttribute) attributeNodes.iterator().next();
56 assertEquals("Correct name expected!", "MyNsPrefix:MyAttr", attribute
57 .getName());
58 assertEquals("Has namespace prefix!", true, attribute.isHasNamespacePrefix());
59 assertEquals("Correct namespace prefix of element expected!", "MyNsPrefix",
60 attribute.getNamespacePrefix());
61 assertEquals("Correct local name of element expected!", "MyAttr", attribute
62 .getLocalName());
63
64 }
65
66 /***
67 * Test exposing a bug of parsing error when having a hash as last character
68 * in an attribute value.
69 *
70 */
71 public void testAttributeValueContainingHash()
72 {
73 Set nodes = getNodes(null, TEST_ATTRIBUTE_VALUE_CONTAINING_HASH);
74
75 Set attributes = getNodesOfType(ASTAttribute.class, nodes);
76 assertEquals("Three attributes expected!", 3, attributes.size());
77
78 List attrsList = new ArrayList(attributes);
79 Collections.sort(attrsList, new Comparator() {
80 public int compare(Object arg0, Object arg1) {
81 return ((ASTAttribute)arg0).getName().compareTo(
82 ((ASTAttribute)arg1).getName() );
83 }
84 });
85
86 ASTAttribute attr = (ASTAttribute) attrsList.get(0);
87 assertEquals("Correct attribute name expected!",
88 "foo", attr.getName());
89 assertEquals("Correct attribute value expected!",
90 "CREATE", ((ASTAttributeValue) attr.getFirstChildOfType(ASTAttributeValue.class)).getImage());
91
92 attr = (ASTAttribute) attrsList.get(1);
93 assertEquals("Correct attribute name expected!",
94 "href", attr.getName());
95 assertEquals("Correct attribute value expected!",
96 "#", ((ASTAttributeValue) attr.getFirstChildOfType(ASTAttributeValue.class)).getImage());
97
98 attr = (ASTAttribute) attrsList.get(2);
99 assertEquals("Correct attribute name expected!",
100 "something", attr.getName());
101 assertEquals("Correct attribute value expected!",
102 "#yes#", ((ASTAttributeValue) attr.getFirstChildOfType(ASTAttributeValue.class)).getImage());
103 }
104
105 /***
106 * Test correct parsing of CDATA.
107 */
108 public void testCData() {
109 Set cdataNodes = getNodes(ASTCData.class, TEST_CDATA);
110
111 assertEquals("One CDATA node expected!", 1, cdataNodes.size());
112 ASTCData cdata = (ASTCData) cdataNodes.iterator().next();
113 assertEquals("Content incorrectly parsed!", " some <cdata> ]] ]> ", cdata
114 .getImage());
115 }
116
117 /***
118 * Test parsing of Doctype declaration.
119 */
120 public void testDoctype() {
121 Set nodes = getNodes(null, TEST_DOCTYPE);
122
123 Set docTypeDeclarations = getNodesOfType(ASTDoctypeDeclaration.class, nodes);
124 assertEquals("One doctype declaration expected!", 1, docTypeDeclarations
125 .size());
126 ASTDoctypeDeclaration docTypeDecl = (ASTDoctypeDeclaration) docTypeDeclarations
127 .iterator().next();
128 assertEquals("Correct doctype-name expected!", "html", docTypeDecl.getName());
129
130 Set externalIds = getNodesOfType(ASTDoctypeExternalId.class, nodes);
131 assertEquals("One doctype external id expected!", 1, externalIds
132 .size());
133 ASTDoctypeExternalId externalId = (ASTDoctypeExternalId) externalIds
134 .iterator().next();
135 assertEquals("Correct external public id expected!", "-//W3C//DTD XHTML 1.1//EN",
136 externalId.getPublicId());
137 assertEquals("Correct external uri expected!", "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd",
138 externalId.getUri());
139
140 }
141
142 /***
143 * Test parsing of a XML comment.
144 *
145 */
146 public void testComment() {
147 Set comments = getNodes(ASTCommentTag.class, TEST_COMMENT);
148 assertEquals("One comment expected!", 1, comments.size());
149 ASTCommentTag comment = (ASTCommentTag) comments.iterator().next();
150 assertEquals("Correct comment content expected!", "comment", comment.getImage());
151 }
152
153 private static final String TEST_SIMPLEST_HTML = "<html/>";
154
155 private static final String TEST_ELEMENT_AND_NAMESPACE = "<h:html MyNsPrefix:MyAttr='MyValue'/>";
156
157 private static final String TEST_CDATA = "<html><![CDATA[ some <cdata> ]] ]> ]]></html>";
158
159 private static final String TEST_DOCTYPE = "<?xml version=\"1.0\" standalone='yes'?>\n"
160 + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" "
161 + "\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"
162 + "<greeting>Hello, world!</greeting>";
163
164 private static final String TEST_COMMENT = "<html><!-- comment --></html>";
165
166 private static final String TEST_ATTRIBUTE_VALUE_CONTAINING_HASH =
167 "<tag:if something=\"#yes#\" foo=\"CREATE\"> <a href=\"#\">foo</a> </tag:if>";
168 }