    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package test.net.sourceforge.pmd.cpd;
    5
    6   import junit.framework.TestCase;
    7   import net.sourceforge.pmd.PMD;
    8   import net.sourceforge.pmd.cpd.JavaTokenizer;
    9   import net.sourceforge.pmd.cpd.Match;
    10  import net.sourceforge.pmd.cpd.MatchAlgorithm;
    11  import net.sourceforge.pmd.cpd.SourceCode;
    12  import net.sourceforge.pmd.cpd.TokenEntry;
    13  import net.sourceforge.pmd.cpd.Tokens;
    14
    15  import java.util.HashMap;
    16  import java.util.Iterator;
    17  import java.util.Map;
    18
    19  public class MatchAlgorithmTest extends TestCase {
    20
    21      public static final String LINE_1 = "public class Foo { ";
    22      public static final String LINE_2 = " public void bar() {";
    23      public static final String LINE_3 = "  System.out.println(\"hello\");";
    24      public static final String LINE_4 = "  System.out.println(\"hello\");";
    25      public static final String LINE_5 = "  int i = 5";
    26      public static final String LINE_6 = "  System.out.print(\"hello\");";
    27      public static final String LINE_7 = " }";
    28      public static final String LINE_8 = "}";
    29
    30      public static String getSampleCode() {
    31          return
    32                  LINE_1 + PMD.EOL +
    33                  LINE_2 + PMD.EOL +
    34                  LINE_3 + PMD.EOL +
    35                  LINE_4 + PMD.EOL +
    36                  LINE_5 + PMD.EOL +
    37                  LINE_6 + PMD.EOL +
    38                  LINE_7 + PMD.EOL +
    39                  LINE_8;
    40      }
    41
    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 codeMap = new HashMap();
    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      public void testIgnore() throws Throwable {
    70          JavaTokenizer tokenizer = new JavaTokenizer();
    71          tokenizer.setIgnoreLiterals(true);
    72          tokenizer.setIgnoreIdentifiers(true);
    73          SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(getSampleCode(), "Foo.java"));
    74          Tokens tokens = new Tokens();
    75          TokenEntry.clearImages();
    76          tokenizer.tokenize(sourceCode, tokens);
    77          Map codeMap = new HashMap();
    78          codeMap.put("Foo.java", sourceCode);
    79
    80          MatchAlgorithm matchAlgorithm = new MatchAlgorithm(codeMap, tokens, 5);
    81          matchAlgorithm.findMatches();
    82          Iterator matches = matchAlgorithm.matches();
    83          Match match = (Match) matches.next();
    84          assertFalse(matches.hasNext());
    85
    86          Iterator marks = match.iterator();
    87          marks.next();
    88          marks.next();
    89          marks.next();
    90          assertFalse(marks.hasNext());
    91      }
    92  }