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 org.junit.Assert;
7   import org.junit.Test;
8   
9   /**
10   * Unit tests for {@link ParserOptions}.
11   */
12  public class ParserOptionsTest {
13  
14      /**
15       * SuppressMarker should be initially null and changeable.
16       */
17      @Test
18      public void testSuppressMarker() {
19          ParserOptions parserOptions = new ParserOptions();
20          Assert.assertNull(parserOptions.getSuppressMarker());
21          parserOptions.setSuppressMarker("foo");
22          Assert.assertEquals("foo", parserOptions.getSuppressMarker());
23      }
24  
25      /**
26       * Verify that the equals and hashCode methods work as expected.
27       */
28      @Test
29      public void testEqualsHashcode() {
30          ParserOptions options1 = new ParserOptions();
31          options1.setSuppressMarker("foo");
32          ParserOptions options2 = new ParserOptions();
33          options2.setSuppressMarker("bar");
34          ParserOptions options3 = new ParserOptions();
35          options3.setSuppressMarker("foo");
36          ParserOptions options4 = new ParserOptions();
37          options4.setSuppressMarker("bar");
38          verifyOptionsEqualsHashcode(options1, options2, options3, options4);
39      }
40  
41      /**
42       * Verify equals and hashCode for 4 {@link ParserOptions} instances.
43       * The given options should be as follows: 1 and 3 are equals, as are 2 and 4.
44       * 
45       * @param options1 first option instance - equals third
46       * @param options2 second option instance - equals fourth
47       * @param options3 third option instance - equals first
48       * @param options4 fourth option instance - equals second
49       */
50      public static void verifyOptionsEqualsHashcode(ParserOptions options1, ParserOptions options2,
51              ParserOptions options3, ParserOptions options4) {
52          // Objects should be different
53          Assert.assertNotSame(options1, options2);
54          Assert.assertNotSame(options1, options2);
55          Assert.assertNotSame(options1, options3);
56          Assert.assertNotSame(options2, options3);
57          Assert.assertNotSame(options2, options4);
58          Assert.assertNotSame(options3, options4);
59  
60          // Check all 16 equality combinations
61          Assert.assertEquals(options1, options1);
62          Assert.assertFalse(options1.equals(options2));
63          Assert.assertEquals(options1, options3);
64          Assert.assertFalse(options1.equals(options4));
65  
66          Assert.assertFalse(options2.equals(options1));
67          Assert.assertEquals(options2, options2);
68          Assert.assertFalse(options2.equals(options3));
69          Assert.assertEquals(options2, options4);
70  
71          Assert.assertEquals(options3, options1);
72          Assert.assertFalse(options3.equals(options2));
73          Assert.assertEquals(options3, options3);
74          Assert.assertFalse(options3.equals(options4));
75  
76          Assert.assertFalse(options4.equals(options1));
77          Assert.assertEquals(options4, options2);
78          Assert.assertFalse(options4.equals(options3));
79          Assert.assertEquals(options4, options4);
80  
81          // Hashcodes should match up
82          Assert.assertNotEquals(options1.hashCode(), options2.hashCode());
83          Assert.assertEquals(options1.hashCode(), options3.hashCode());
84          Assert.assertNotEquals(options1.hashCode(), options4.hashCode());
85          Assert.assertNotEquals(options2.hashCode(), options3.hashCode());
86          Assert.assertEquals(options2.hashCode(), options4.hashCode());
87          Assert.assertNotEquals(options3.hashCode(), options4.hashCode());
88      }
89  }