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