View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.dfa.report;
5   
6   import static org.junit.Assert.assertFalse;
7   import static org.junit.Assert.assertTrue;
8   import static org.mockito.Mockito.mock;
9   import static org.mockito.Mockito.when;
10  import net.sourceforge.pmd.RuleViolation;
11  
12  import org.junit.Test;
13  
14  /**
15   * @author Philip Graf
16   */
17  public final class ViolationNodeTest {
18      
19      /**
20       * Verifies that two violations nodes with equal {@code filename, beginLine, endLine, beginColumn, endColumn} and
21       * {@code variableName} are equal.
22       */
23      @Test
24      public void testEqualsNodeWithTwoEqualViolations() {
25          final ViolationNode node1 = createViolationNode("Foo.java", 1, 1, 5, 15, "");
26          final ViolationNode node2 = createViolationNode("Foo.java", 1, 1, 5, 15, "");
27          assertTrue("Two equal violations should result in equal nodes", node1.equalsNode(node2));
28      }
29      
30      /**
31       * Verifies that two violations nodes with different {@code filename} are not equal.
32       */
33      @Test
34      public void testEqualsNodeWithTwoDifferentViolationsDifferentFilename() {
35          final ViolationNode node1 = createViolationNode("Foo.java", 1, 1, 5, 15, "");
36          final ViolationNode node2 = createViolationNode("Bar.java", 1, 1, 5, 15, "");
37          assertFalse("Two violations with different filename should result in not equal nodes", node1.equalsNode(node2));
38      }
39      
40      /**
41       * Verifies that two violations nodes with different {@code beginLine} are not equal.
42       */
43      @Test
44      public void testEqualsNodeWithTwoDifferentViolationsDifferentBeginLine() {
45          final ViolationNode node1 = createViolationNode("Foo.java", 1, 2, 5, 15, "");
46          final ViolationNode node2 = createViolationNode("Foo.java", 2, 2, 5, 15, "");
47          assertFalse("Two violations with different beginLine should result in not equal nodes", node1.equalsNode(node2));
48      }
49      
50      /**
51       * Verifies that two violations nodes with different {@code endLine} are not equal.
52       */
53      @Test
54      public void testEqualsNodeWithTwoDifferentViolationsDifferentEndLine() {
55          final ViolationNode node1 = createViolationNode("Foo.java", 1, 1, 5, 15, "");
56          final ViolationNode node2 = createViolationNode("Foo.java", 1, 2, 5, 15, "");
57          assertFalse("Two violations with different endLine should result in not equal nodes", node1.equalsNode(node2));
58      }
59      
60      /**
61       * Verifies that two violations nodes with different {@code beginColumn} are not equal.
62       */
63      @Test
64      public void testEqualsNodeWithTwoDifferentViolationsDifferentBeginColumn() {
65          final ViolationNode node1 = createViolationNode("Foo.java", 1, 1, 5, 15, "");
66          final ViolationNode node2 = createViolationNode("Foo.java", 1, 1, 7, 15, "");
67          assertFalse("Two violations with different beginColumn should result in not equal nodes", node1.equalsNode(node2));
68      }
69      
70      /**
71       * Verifies that two violations nodes with different {@code endColumn} are not equal.
72       */
73      @Test
74      public void testEqualsNodeWithTwoDifferentViolationsDifferentEndColumn() {
75          final ViolationNode node1 = createViolationNode("Foo.java", 1, 1, 5, 15, "");
76          final ViolationNode node2 = createViolationNode("Foo.java", 1, 1, 5, 17, "");
77          assertFalse("Two violations with different end column should result in not equal nodes", node1.equalsNode(node2));
78      }
79      
80      /**
81       * Verifies that two violations with different {@code variableName} are not equal.
82       */
83      @Test
84      public void testEqualsNodeWithTwoDifferentViolationsDifferentVariableName() {
85          final ViolationNode node1 = createViolationNode("Foo.java", 1, 1, 5, 15, "a");
86          final ViolationNode node2 = createViolationNode("Foo.java", 1, 1, 5, 15, "b");
87          assertFalse("Two violations with different variableName should result in not equal nodes", node1.equalsNode(node2));
88      }
89      
90      private ViolationNode createViolationNode(final String filename, final int beginLine, final int endLine,
91              final int beginColumn, final int endColumn, final String variableName) {
92          final RuleViolation violation = mock(RuleViolation.class);
93          when(violation.getFilename()).thenReturn(filename);
94          when(violation.getBeginLine()).thenReturn(beginLine);
95          when(violation.getEndLine()).thenReturn(endLine);
96          when(violation.getBeginColumn()).thenReturn(beginColumn);
97          when(violation.getEndColumn()).thenReturn(endColumn);
98          when(violation.getVariableName()).thenReturn(variableName);
99          return new ViolationNode(violation);
100     }
101     
102 }