View Javadoc

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