View Javadoc

    1   package net.sourceforge.pmd.lang;
    2
    3   import static org.junit.Assert.assertEquals;
    4   import static org.junit.Assert.assertFalse;
    5   import static org.junit.Assert.assertNotSame;
    6   import static org.junit.Assert.assertNull;
    7   import static org.junit.Assert.assertTrue;
    8   import net.sourceforge.pmd.lang.ParserOptions;
    9
    10  import org.junit.Test;
    11
    12  public class ParserOptionsTest {
    13
    14      @Test
    15      public void testSuppressMarker() throws Exception {
    16  	ParserOptions parserOptions = new ParserOptions();
    17  	assertNull(parserOptions.getSuppressMarker());
    18  	parserOptions.setSuppressMarker("foo");
    19  	assertEquals("foo", parserOptions.getSuppressMarker());
    20      }
    21
    22      @Test
    23      public void testEqualsHashcode() throws Exception {
    24  	ParserOptions options1 = new ParserOptions();
    25  	options1.setSuppressMarker("foo");
    26  	ParserOptions options2 = new ParserOptions();
    27  	options2.setSuppressMarker("bar");
    28  	ParserOptions options3 = new ParserOptions();
    29  	options3.setSuppressMarker("foo");
    30  	ParserOptions options4 = new ParserOptions();
    31  	options4.setSuppressMarker("bar");
    32  	verifyOptionsEqualsHashcode(options1, options2, options3, options4);
    33      }
    34
    35      // 1 and 3 are equals, as are 2 and 4.
    36      @SuppressWarnings("PMD.UseAssertSameInsteadOfAssertTrue")
    37      public static void verifyOptionsEqualsHashcode(ParserOptions options1, ParserOptions options2,
    38  	    ParserOptions options3, ParserOptions options4) {
    39  	// Objects should be different
    40  	assertNotSame(options1, options2);
    41  	assertNotSame(options1, options2);
    42  	assertNotSame(options1, options3);
    43  	assertNotSame(options2, options3);
    44  	assertNotSame(options2, options4);
    45  	assertNotSame(options3, options4);
    46
    47  	// Check all 16 equality combinations
    48  	assertEquals(options1, options1);
    49  	assertFalse(options1.equals(options2));
    50  	assertEquals(options1, options3);
    51  	assertFalse(options1.equals(options4));
    52
    53  	assertFalse(options2.equals(options1));
    54  	assertEquals(options2, options2);
    55  	assertFalse(options2.equals(options3));
    56  	assertEquals(options2, options4);
    57
    58  	assertEquals(options3, options1);
    59  	assertFalse(options3.equals(options2));
    60  	assertEquals(options3, options3);
    61  	assertFalse(options3.equals(options4));
    62
    63  	assertFalse(options4.equals(options1));
    64  	assertEquals(options4, options2);
    65  	assertFalse(options4.equals(options3));
    66  	assertEquals(options4, options4);
    67
    68  	// Hashcodes should match up
    69  	assertFalse(options1.hashCode() == options2.hashCode());
    70  	assertTrue(options1.hashCode() == options3.hashCode());
    71  	assertFalse(options1.hashCode() == options4.hashCode());
    72  	assertFalse(options2.hashCode() == options3.hashCode());
    73  	assertTrue(options2.hashCode() == options4.hashCode());
    74  	assertFalse(options3.hashCode() == options4.hashCode());
    75      }
    76
    77      public static junit.framework.Test suite() {
    78  	return new junit.framework.JUnit4TestAdapter(ParserOptionsTest.class);
    79      }
    80  }