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.HashMap;
11  import java.util.Iterator;
12  import java.util.Map;
13  
14  import net.sourceforge.pmd.PMD;
15  
16  import org.junit.Test;
17  
18  public class MatchAlgorithmTest {
19  
20      public static final String LINE_1 = "public class Foo { ";
21      public static final String LINE_2 = " public void bar() {";
22      public static final String LINE_3 = "  System.out.println(\"hello\");";
23      public static final String LINE_4 = "  System.out.println(\"hello\");";
24      public static final String LINE_5 = "  int i = 5";
25      public static final String LINE_6 = "  System.out.print(\"hello\");";
26      public static final String LINE_7 = " }";
27      public static final String LINE_8 = "}";
28  
29      public static String getSampleCode() {
30          return
31                  LINE_1 + PMD.EOL +
32                  LINE_2 + PMD.EOL +
33                  LINE_3 + PMD.EOL +
34                  LINE_4 + PMD.EOL +
35                  LINE_5 + PMD.EOL +
36                  LINE_6 + PMD.EOL +
37                  LINE_7 + PMD.EOL +
38                  LINE_8;
39      }
40  
41      @Test
42      public void testSimple() throws Throwable {
43          JavaTokenizer tokenizer = new JavaTokenizer();
44          SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(getSampleCode(), "Foo.java"));
45          Tokens tokens = new Tokens();
46          TokenEntry.clearImages();
47          tokenizer.tokenize(sourceCode, tokens);
48          assertEquals(41, tokens.size());
49          Map<String, SourceCode> codeMap = new HashMap<String, SourceCode>();
50          codeMap.put("Foo.java", sourceCode);
51  
52          MatchAlgorithm matchAlgorithm = new MatchAlgorithm(codeMap, tokens, 5);
53          matchAlgorithm.findMatches();
54          Iterator matches = matchAlgorithm.matches();
55          Match match = (Match) matches.next();
56          assertFalse(matches.hasNext());
57  
58          Iterator marks = match.iterator();
59          TokenEntry mark1 = (TokenEntry) marks.next();
60          TokenEntry mark2 = (TokenEntry) marks.next();
61          assertFalse(marks.hasNext());
62  
63          assertEquals(3, mark1.getBeginLine());
64          assertEquals(4, mark2.getBeginLine());
65          assertTrue("Foo.java" == mark1.getTokenSrcID() && "Foo.java" == mark2.getTokenSrcID());
66          assertEquals(LINE_3, match.getSourceCodeSlice());
67      }
68  
69      @Test
70      public void testIgnore() throws Throwable {
71          JavaTokenizer tokenizer = new JavaTokenizer();
72          tokenizer.setIgnoreLiterals(true);
73          tokenizer.setIgnoreIdentifiers(true);
74          SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(getSampleCode(), "Foo.java"));
75          Tokens tokens = new Tokens();
76          TokenEntry.clearImages();
77          tokenizer.tokenize(sourceCode, tokens);
78          Map<String, SourceCode> codeMap = new HashMap<String, SourceCode>();
79          codeMap.put("Foo.java", sourceCode);
80  
81          MatchAlgorithm matchAlgorithm = new MatchAlgorithm(codeMap, tokens, 5);
82          matchAlgorithm.findMatches();
83          Iterator matches = matchAlgorithm.matches();
84          Match match = (Match) matches.next();
85          assertFalse(matches.hasNext());
86  
87          Iterator marks = match.iterator();
88          marks.next();
89          marks.next();
90          marks.next();
91          assertFalse(marks.hasNext());
92      }
93  
94      public static junit.framework.Test suite() {
95          return new junit.framework.JUnit4TestAdapter(MatchAlgorithmTest.class);
96      }
97  }