View Javadoc

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