001: /**
002: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003: */package test.net.sourceforge.pmd.cpd;
004:
005: import static org.junit.Assert.assertEquals;
006: import static org.junit.Assert.assertFalse;
007: import static org.junit.Assert.assertTrue;
008: import net.sourceforge.pmd.PMD;
009: import net.sourceforge.pmd.cpd.JavaTokenizer;
010: import net.sourceforge.pmd.cpd.Match;
011: import net.sourceforge.pmd.cpd.MatchAlgorithm;
012: import net.sourceforge.pmd.cpd.SourceCode;
013: import net.sourceforge.pmd.cpd.TokenEntry;
014: import net.sourceforge.pmd.cpd.Tokens;
015:
016: import org.junit.Test;
017:
018: import java.util.HashMap;
019: import java.util.Iterator;
020: import java.util.Map;
021:
022: public class MatchAlgorithmTest {
023:
024: public static final String LINE_1 = "public class Foo { ";
025: public static final String LINE_2 = " public void bar() {";
026: public static final String LINE_3 = " System.out.println(\"hello\");";
027: public static final String LINE_4 = " System.out.println(\"hello\");";
028: public static final String LINE_5 = " int i = 5";
029: public static final String LINE_6 = " System.out.print(\"hello\");";
030: public static final String LINE_7 = " }";
031: public static final String LINE_8 = "}";
032:
033: public static String getSampleCode() {
034: return LINE_1 + PMD.EOL + LINE_2 + PMD.EOL + LINE_3 + PMD.EOL
035: + LINE_4 + PMD.EOL + LINE_5 + PMD.EOL + LINE_6
036: + PMD.EOL + LINE_7 + PMD.EOL + LINE_8;
037: }
038:
039: @Test
040: public void testSimple() throws Throwable {
041: JavaTokenizer tokenizer = new JavaTokenizer();
042: SourceCode sourceCode = new SourceCode(
043: new SourceCode.StringCodeLoader(getSampleCode(),
044: "Foo.java"));
045: Tokens tokens = new Tokens();
046: TokenEntry.clearImages();
047: tokenizer.tokenize(sourceCode, tokens);
048: assertEquals(41, tokens.size());
049: Map<String, SourceCode> codeMap = new HashMap<String, SourceCode>();
050: codeMap.put("Foo.java", sourceCode);
051:
052: MatchAlgorithm matchAlgorithm = new MatchAlgorithm(codeMap,
053: tokens, 5);
054: matchAlgorithm.findMatches();
055: Iterator matches = matchAlgorithm.matches();
056: Match match = (Match) matches.next();
057: assertFalse(matches.hasNext());
058:
059: Iterator marks = match.iterator();
060: TokenEntry mark1 = (TokenEntry) marks.next();
061: TokenEntry mark2 = (TokenEntry) marks.next();
062: assertFalse(marks.hasNext());
063:
064: assertEquals(3, mark1.getBeginLine());
065: assertEquals(4, mark2.getBeginLine());
066: assertTrue("Foo.java" == mark1.getTokenSrcID()
067: && "Foo.java" == mark2.getTokenSrcID());
068: assertEquals(LINE_3, match.getSourceCodeSlice());
069: }
070:
071: @Test
072: public void testIgnore() throws Throwable {
073: JavaTokenizer tokenizer = new JavaTokenizer();
074: tokenizer.setIgnoreLiterals(true);
075: tokenizer.setIgnoreIdentifiers(true);
076: SourceCode sourceCode = new SourceCode(
077: new SourceCode.StringCodeLoader(getSampleCode(),
078: "Foo.java"));
079: Tokens tokens = new Tokens();
080: TokenEntry.clearImages();
081: tokenizer.tokenize(sourceCode, tokens);
082: Map<String, SourceCode> codeMap = new HashMap<String, SourceCode>();
083: codeMap.put("Foo.java", sourceCode);
084:
085: MatchAlgorithm matchAlgorithm = new MatchAlgorithm(codeMap,
086: tokens, 5);
087: matchAlgorithm.findMatches();
088: Iterator matches = matchAlgorithm.matches();
089: Match match = (Match) matches.next();
090: assertFalse(matches.hasNext());
091:
092: Iterator marks = match.iterator();
093: marks.next();
094: marks.next();
095: marks.next();
096: assertFalse(marks.hasNext());
097: }
098:
099: public static junit.framework.Test suite() {
100: return new junit.framework.JUnit4TestAdapter(
101: MatchAlgorithmTest.class);
102: }
103: }
|