View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.xml;
5   
6   import static net.sourceforge.pmd.lang.ParserOptionsTest.verifyOptionsEqualsHashcode;
7   import static org.junit.Assert.assertEquals;
8   import static org.junit.Assert.assertFalse;
9   import static org.junit.Assert.assertNull;
10  import static org.junit.Assert.assertTrue;
11  import net.sourceforge.pmd.lang.ParserOptions;
12  import net.sourceforge.pmd.lang.rule.properties.BooleanProperty;
13  import net.sourceforge.pmd.lang.xml.rule.AbstractXmlRule;
14  
15  import org.junit.Test;
16  
17  public class XmlParserOptionsTest {
18  
19      @Test
20      public void testDefaults() throws Exception {
21  	XmlParserOptions options = new XmlParserOptions();
22  	assertFalse(options.isCoalescing());
23  	assertTrue(options.isExpandEntityReferences());
24  	assertFalse(options.isIgnoringComments());
25  	assertFalse(options.isIgnoringElementContentWhitespace());
26  	assertTrue(options.isNamespaceAware());
27  	assertFalse(options.isValidating());
28  	assertFalse(options.isXincludeAware());
29  
30  	MyRule rule = new MyRule();
31  	options = (XmlParserOptions) rule.getParserOptions();
32  	assertFalse(options.isCoalescing());
33  	assertTrue(options.isExpandEntityReferences());
34  	assertFalse(options.isIgnoringComments());
35  	assertFalse(options.isIgnoringElementContentWhitespace());
36  	assertTrue(options.isNamespaceAware());
37  	assertFalse(options.isValidating());
38  	assertFalse(options.isXincludeAware());
39      }
40  
41      @Test
42      public void testConstructor() throws Exception {
43  	MyRule rule = new MyRule();
44  
45  	rule.setProperty(XmlParserOptions.COALESCING_DESCRIPTOR, true);
46  	assertTrue(((XmlParserOptions) rule.getParserOptions()).isCoalescing());
47  	rule.setProperty(XmlParserOptions.COALESCING_DESCRIPTOR, false);
48  	assertFalse(((XmlParserOptions) rule.getParserOptions()).isCoalescing());
49  
50  	rule.setProperty(XmlParserOptions.EXPAND_ENTITY_REFERENCES_DESCRIPTOR, true);
51  	assertTrue(((XmlParserOptions) rule.getParserOptions()).isExpandEntityReferences());
52  	rule.setProperty(XmlParserOptions.EXPAND_ENTITY_REFERENCES_DESCRIPTOR, false);
53  	assertFalse(((XmlParserOptions) rule.getParserOptions()).isExpandEntityReferences());
54  
55  	rule.setProperty(XmlParserOptions.IGNORING_COMMENTS_DESCRIPTOR, true);
56  	assertTrue(((XmlParserOptions) rule.getParserOptions()).isIgnoringComments());
57  	rule.setProperty(XmlParserOptions.IGNORING_COMMENTS_DESCRIPTOR, false);
58  	assertFalse(((XmlParserOptions) rule.getParserOptions()).isIgnoringComments());
59  
60  	rule.setProperty(XmlParserOptions.IGNORING_ELEMENT_CONTENT_WHITESPACE_DESCRIPTOR, true);
61  	assertTrue(((XmlParserOptions) rule.getParserOptions()).isIgnoringElementContentWhitespace());
62  	rule.setProperty(XmlParserOptions.IGNORING_ELEMENT_CONTENT_WHITESPACE_DESCRIPTOR, false);
63  	assertFalse(((XmlParserOptions) rule.getParserOptions()).isIgnoringElementContentWhitespace());
64  
65  	rule.setProperty(XmlParserOptions.NAMESPACE_AWARE_DESCRIPTOR, true);
66  	assertTrue(((XmlParserOptions) rule.getParserOptions()).isNamespaceAware());
67  	rule.setProperty(XmlParserOptions.NAMESPACE_AWARE_DESCRIPTOR, false);
68  	assertFalse(((XmlParserOptions) rule.getParserOptions()).isNamespaceAware());
69  
70  	rule.setProperty(XmlParserOptions.VALIDATING_DESCRIPTOR, true);
71  	assertTrue(((XmlParserOptions) rule.getParserOptions()).isValidating());
72  	rule.setProperty(XmlParserOptions.VALIDATING_DESCRIPTOR, false);
73  	assertFalse(((XmlParserOptions) rule.getParserOptions()).isValidating());
74  
75  	rule.setProperty(XmlParserOptions.XINCLUDE_AWARE_DESCRIPTOR, true);
76  	assertTrue(((XmlParserOptions) rule.getParserOptions()).isXincludeAware());
77  	rule.setProperty(XmlParserOptions.XINCLUDE_AWARE_DESCRIPTOR, false);
78  	assertFalse(((XmlParserOptions) rule.getParserOptions()).isXincludeAware());
79      }
80  
81      @Test
82      public void testSetters() {
83  	XmlParserOptions options = new XmlParserOptions();
84  
85  	options.setSuppressMarker("foo");
86  	assertEquals("foo", options.getSuppressMarker());
87  	options.setSuppressMarker(null);
88  	assertNull(options.getSuppressMarker());
89  
90  	options.setCoalescing(true);
91  	assertTrue(options.isCoalescing());
92  	options.setCoalescing(false);
93  	assertFalse(options.isCoalescing());
94  
95  	options.setExpandEntityReferences(true);
96  	assertTrue(options.isExpandEntityReferences());
97  	options.setExpandEntityReferences(false);
98  	assertFalse(options.isExpandEntityReferences());
99  
100 	options.setIgnoringComments(true);
101 	assertTrue(options.isIgnoringComments());
102 	options.setIgnoringComments(false);
103 	assertFalse(options.isIgnoringComments());
104 
105 	options.setIgnoringElementContentWhitespace(true);
106 	assertTrue(options.isIgnoringElementContentWhitespace());
107 	options.setIgnoringElementContentWhitespace(false);
108 	assertFalse(options.isIgnoringElementContentWhitespace());
109 
110 	options.setNamespaceAware(true);
111 	assertTrue(options.isNamespaceAware());
112 	options.setNamespaceAware(false);
113 	assertFalse(options.isNamespaceAware());
114 
115 	options.setValidating(true);
116 	assertTrue(options.isValidating());
117 	options.setValidating(false);
118 	assertFalse(options.isValidating());
119 
120 	options.setXincludeAware(true);
121 	assertTrue(options.isXincludeAware());
122 	options.setXincludeAware(false);
123 	assertFalse(options.isXincludeAware());
124     }
125 
126     @Test
127     public void testEqualsHashcode() throws Exception {
128 	BooleanProperty[] properties = new BooleanProperty[] { XmlParserOptions.COALESCING_DESCRIPTOR,
129 		XmlParserOptions.EXPAND_ENTITY_REFERENCES_DESCRIPTOR, XmlParserOptions.IGNORING_COMMENTS_DESCRIPTOR,
130 		XmlParserOptions.IGNORING_ELEMENT_CONTENT_WHITESPACE_DESCRIPTOR,
131 		XmlParserOptions.NAMESPACE_AWARE_DESCRIPTOR, XmlParserOptions.VALIDATING_DESCRIPTOR,
132 		XmlParserOptions.XINCLUDE_AWARE_DESCRIPTOR, };
133 
134 	for (int i = 0; i < properties.length; i++) {
135 	    BooleanProperty property = properties[i];
136 
137 	    MyRule rule = new MyRule();
138 	    rule.setProperty(property, true);
139 	    ParserOptions options1 = rule.getParserOptions();
140 	    rule.setProperty(property, false);
141 	    ParserOptions options2 = rule.getParserOptions();
142 	    rule.setProperty(property, true);
143 	    ParserOptions options3 = rule.getParserOptions();
144 	    rule.setProperty(property, false);
145 	    ParserOptions options4 = rule.getParserOptions();
146 	    verifyOptionsEqualsHashcode(options1, options2, options3, options4);
147 	}
148 
149 	XmlParserOptions options1 = new XmlParserOptions();
150 	options1.setSuppressMarker("foo");
151 	XmlParserOptions options2 = new XmlParserOptions();
152 	options2.setSuppressMarker("bar");
153 	XmlParserOptions options3 = new XmlParserOptions();
154 	options3.setSuppressMarker("foo");
155 	XmlParserOptions options4 = new XmlParserOptions();
156 	options4.setSuppressMarker("bar");
157 	verifyOptionsEqualsHashcode(options1, options2, options3, options4);
158     }
159 
160     private static final class MyRule extends AbstractXmlRule {
161     }
162 
163     public static junit.framework.Test suite() {
164 	return new junit.framework.JUnit4TestAdapter(XmlParserOptionsTest.class);
165     }
166 }