View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.cpd;
5   
6   import static org.junit.Assert.assertEquals;
7   import static org.junit.Assert.assertFalse;
8   import static org.junit.Assert.assertTrue;
9   
10  import java.util.Iterator;
11  
12  import org.junit.Test;
13  
14  public class MatchTest {
15  
16      @Test
17      public void testSimple() {
18          int lineCount1 = 10;
19          String codeFragment1 = "code fragment";
20          Mark mark1 = createMark("public", "/var/Foo.java", 1, lineCount1, codeFragment1);
21  
22          int lineCount2 = 20;
23          String codeFragment2 = "code fragment 2";
24          Mark mark2 = createMark("class", "/var/Foo.java", 1, lineCount2, codeFragment2);
25          Match match = new Match(1, mark1, mark2);
26  
27          assertEquals(1, match.getTokenCount());
28          //Returns the line count of the first mark
29          assertEquals(lineCount1, match.getLineCount());
30          //Returns the source code of the first mark
31          assertEquals(codeFragment1, match.getSourceCodeSlice());
32          Iterator<Mark> i = match.iterator();
33          Mark occurrence1 = i.next();
34          Mark occurrence2 = i.next();
35  
36          assertFalse(i.hasNext());
37  
38          assertEquals(mark1, occurrence1);
39          assertEquals(lineCount1, occurrence1.getLineCount());
40          assertEquals(codeFragment1, occurrence1.getSourceCodeSlice());
41  
42          assertEquals(mark2, occurrence2);
43          assertEquals(lineCount2, occurrence2.getLineCount());
44          assertEquals(codeFragment2, occurrence2.getSourceCodeSlice());
45      }
46  
47      @Test
48      public void testCompareTo() {
49          Match m1 = new Match(1, new TokenEntry("public", "/var/Foo.java", 1), new TokenEntry("class", "/var/Foo.java", 1));
50          Match m2 = new Match(2, new TokenEntry("Foo", "/var/Foo.java", 1), new TokenEntry("{", "/var/Foo.java", 1));
51          assertTrue(m2.compareTo(m1) < 0);
52      }
53  
54      private Mark createMark(String image, String tokenSrcID, int beginLine, int lineCount, String code) {
55          Mark result = new Mark(new TokenEntry(image, tokenSrcID, beginLine));
56  
57          result.setLineCount(lineCount);
58          result.setSoureCodeSlice(code);
59          return result;
60      }
61  
62  
63      public static junit.framework.Test suite() {
64          return new junit.framework.JUnit4TestAdapter(MatchTest.class);
65      }
66  }