001: package test.net.sourceforge.pmd.jsp.ast;
002:
003: import static org.junit.Assert.assertEquals;
004: import net.sourceforge.pmd.jsp.ast.ASTAttribute;
005: import net.sourceforge.pmd.jsp.ast.ASTAttributeValue;
006: import net.sourceforge.pmd.jsp.ast.ASTCData;
007: import net.sourceforge.pmd.jsp.ast.ASTCommentTag;
008: import net.sourceforge.pmd.jsp.ast.ASTDoctypeDeclaration;
009: import net.sourceforge.pmd.jsp.ast.ASTDoctypeExternalId;
010: import net.sourceforge.pmd.jsp.ast.ASTElement;
011:
012: import org.junit.Test;
013:
014: import java.util.ArrayList;
015: import java.util.Collections;
016: import java.util.Comparator;
017: import java.util.List;
018: import java.util.Set;
019:
020: /**
021: * Test parsing of a JSP in document style, by checking the generated AST.
022: *
023: * @author pieter_van_raemdonck - Application Engineers NV/SA - www.ae.be
024: *
025: */
026: public class JspDocStyleTest extends AbstractJspNodesTst {
027:
028: /**
029: * Smoke test for JSP parser.
030: *
031: * @throws Throwable
032: */
033: @Test
034: public void testSimplestJsp() throws Throwable {
035: assertNumberOfNodes(ASTElement.class, TEST_SIMPLEST_HTML, 1);
036: }
037:
038: /**
039: * Test the information on a Element and Attribute.
040: *
041: * @throws Throwable
042: */
043: @Test
044: public void testElementAttributeAndNamespace() throws Throwable {
045: Set nodes = getNodes(null, TEST_ELEMENT_AND_NAMESPACE);
046:
047: Set<ASTElement> elementNodes = getNodesOfType(ASTElement.class,
048: nodes);
049: assertEquals("One element node expected!", 1, elementNodes
050: .size());
051: ASTElement element = elementNodes.iterator().next();
052: assertEquals("Correct name expected!", "h:html", element
053: .getName());
054: assertEquals("Has namespace prefix!", true, element
055: .isHasNamespacePrefix());
056: assertEquals("Element is empty!", true, element.isEmpty());
057: assertEquals("Correct namespace prefix of element expected!",
058: "h", element.getNamespacePrefix());
059: assertEquals("Correct local name of element expected!", "html",
060: element.getLocalName());
061:
062: Set attributeNodes = getNodesOfType(ASTAttribute.class, nodes);
063: assertEquals("One attribute node expected!", 1, attributeNodes
064: .size());
065: ASTAttribute attribute = (ASTAttribute) attributeNodes
066: .iterator().next();
067: assertEquals("Correct name expected!", "MyNsPrefix:MyAttr",
068: attribute.getName());
069: assertEquals("Has namespace prefix!", true, attribute
070: .isHasNamespacePrefix());
071: assertEquals("Correct namespace prefix of element expected!",
072: "MyNsPrefix", attribute.getNamespacePrefix());
073: assertEquals("Correct local name of element expected!",
074: "MyAttr", attribute.getLocalName());
075:
076: }
077:
078: /**
079: * Test exposing a bug of parsing error when having a hash as last character
080: * in an attribute value.
081: *
082: */
083: @Test
084: public void testAttributeValueContainingHash() {
085: Set nodes = getNodes(null, TEST_ATTRIBUTE_VALUE_CONTAINING_HASH);
086:
087: Set<ASTAttribute> attributes = getNodesOfType(
088: ASTAttribute.class, nodes);
089: assertEquals("Three attributes expected!", 3, attributes.size());
090:
091: List<ASTAttribute> attrsList = new ArrayList<ASTAttribute>(
092: attributes);
093: Collections.sort(attrsList, new Comparator<ASTAttribute>() {
094: public int compare(ASTAttribute arg0, ASTAttribute arg1) {
095: return arg0.getName().compareTo(arg1.getName());
096: }
097: });
098:
099: ASTAttribute attr = attrsList.get(0);
100: assertEquals("Correct attribute name expected!", "foo", attr
101: .getName());
102: assertEquals("Correct attribute value expected!", "CREATE",
103: attr.getFirstChildOfType(ASTAttributeValue.class)
104: .getImage());
105:
106: attr = attrsList.get(1);
107: assertEquals("Correct attribute name expected!", "href", attr
108: .getName());
109: assertEquals("Correct attribute value expected!", "#", attr
110: .getFirstChildOfType(ASTAttributeValue.class)
111: .getImage());
112:
113: attr = attrsList.get(2);
114: assertEquals("Correct attribute name expected!", "something",
115: attr.getName());
116: assertEquals("Correct attribute value expected!", "#yes#", attr
117: .getFirstChildOfType(ASTAttributeValue.class)
118: .getImage());
119: }
120:
121: /**
122: * Test correct parsing of CDATA.
123: */
124: @Test
125: public void testCData() {
126: Set cdataNodes = getNodes(ASTCData.class, TEST_CDATA);
127:
128: assertEquals("One CDATA node expected!", 1, cdataNodes.size());
129: ASTCData cdata = (ASTCData) cdataNodes.iterator().next();
130: assertEquals("Content incorrectly parsed!",
131: " some <cdata> ]] ]> ", cdata.getImage());
132: }
133:
134: /**
135: * Test parsing of Doctype declaration.
136: */
137: @Test
138: public void testDoctype() {
139: Set nodes = getNodes(null, TEST_DOCTYPE);
140:
141: Set<ASTDoctypeDeclaration> docTypeDeclarations = getNodesOfType(
142: ASTDoctypeDeclaration.class, nodes);
143: assertEquals("One doctype declaration expected!", 1,
144: docTypeDeclarations.size());
145: ASTDoctypeDeclaration docTypeDecl = docTypeDeclarations
146: .iterator().next();
147: assertEquals("Correct doctype-name expected!", "html",
148: docTypeDecl.getName());
149:
150: Set externalIds = getNodesOfType(ASTDoctypeExternalId.class,
151: nodes);
152: assertEquals("One doctype external id expected!", 1,
153: externalIds.size());
154: ASTDoctypeExternalId externalId = (ASTDoctypeExternalId) externalIds
155: .iterator().next();
156: assertEquals("Correct external public id expected!",
157: "-//W3C//DTD XHTML 1.1//EN", externalId.getPublicId());
158: assertEquals("Correct external uri expected!",
159: "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd",
160: externalId.getUri());
161:
162: }
163:
164: /**
165: * Test parsing of a XML comment.
166: *
167: */
168: @Test
169: public void testComment() {
170: Set comments = getNodes(ASTCommentTag.class, TEST_COMMENT);
171: assertEquals("One comment expected!", 1, comments.size());
172: ASTCommentTag comment = (ASTCommentTag) comments.iterator()
173: .next();
174: assertEquals("Correct comment content expected!", "comment",
175: comment.getImage());
176: }
177:
178: private static final String TEST_SIMPLEST_HTML = "<html/>";
179:
180: private static final String TEST_ELEMENT_AND_NAMESPACE = "<h:html MyNsPrefix:MyAttr='MyValue'/>";
181:
182: private static final String TEST_CDATA = "<html><![CDATA[ some <cdata> ]] ]> ]]></html>";
183:
184: private static final String TEST_DOCTYPE = "<?xml version=\"1.0\" standalone='yes'?>\n"
185: + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" "
186: + "\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"
187: + "<greeting>Hello, world!</greeting>";
188:
189: private static final String TEST_COMMENT = "<html><!-- comment --></html>";
190:
191: private static final String TEST_ATTRIBUTE_VALUE_CONTAINING_HASH = "<tag:if something=\"#yes#\" foo=\"CREATE\"> <a href=\"#\">foo</a> </tag:if>";
192:
193: public static junit.framework.Test suite() {
194: return new junit.framework.JUnit4TestAdapter(
195: JspDocStyleTest.class);
196: }
197: }
|