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