1
2
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 private static final String LINE_1 = "public class Foo { ";
21 private static final String LINE_2 = " public void bar() {";
22 private static final String LINE_3 = " System.out.println(\"hello\");";
23 private static final String LINE_4 = " System.out.println(\"hello\");";
24 private static final String LINE_5 = " int i = 5";
25 private static final String LINE_6 = " System.out.print(\"hello\");";
26 private static final String LINE_7 = " }";
27 private static final String LINE_8 = "}";
28
29 private 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<Match> matches = matchAlgorithm.matches();
55 Match match = matches.next();
56 assertFalse(matches.hasNext());
57
58 Iterator<Mark> marks = match.iterator();
59 Mark mark1 = marks.next();
60 Mark mark2 = marks.next();
61 assertFalse(marks.hasNext());
62
63 assertEquals(3, mark1.getBeginLine());
64 assertEquals("Foo.java", mark1.getFilename());
65 assertEquals(LINE_3, mark1.getSourceCodeSlice());
66
67 assertEquals(4, mark2.getBeginLine());
68 assertEquals("Foo.java", mark2.getFilename());
69 assertEquals(LINE_4, mark2.getSourceCodeSlice());
70 }
71
72 @Test
73 public void testIgnore() throws Throwable {
74 JavaTokenizer tokenizer = new JavaTokenizer();
75 tokenizer.setIgnoreLiterals(true);
76 tokenizer.setIgnoreIdentifiers(true);
77 SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(getSampleCode(), "Foo.java"));
78 Tokens tokens = new Tokens();
79 TokenEntry.clearImages();
80 tokenizer.tokenize(sourceCode, tokens);
81 Map<String, SourceCode> codeMap = new HashMap<String, SourceCode>();
82 codeMap.put("Foo.java", sourceCode);
83
84 MatchAlgorithm matchAlgorithm = new MatchAlgorithm(codeMap, tokens, 5);
85 matchAlgorithm.findMatches();
86 Iterator<Match> matches = matchAlgorithm.matches();
87 Match match = matches.next();
88 assertFalse(matches.hasNext());
89
90 Iterator<Mark> marks = match.iterator();
91 marks.next();
92 marks.next();
93 marks.next();
94 assertFalse(marks.hasNext());
95 }
96 }