    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.SourceCode;
    10  import net.sourceforge.pmd.cpd.Tokenizer;
    11  import net.sourceforge.pmd.cpd.Tokens;
    12
    13  public class JavaTokensTokenizerTest extends TestCase {
    14
    15      public void test1() throws Throwable {
    16          Tokenizer tokenizer = new JavaTokenizer();
    17          SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader("public class Foo {}"));
    18          Tokens tokens = new Tokens();
    19          tokenizer.tokenize(sourceCode, tokens);
    20          assertEquals(6, tokens.size());
    21          assertEquals("public class Foo {}", sourceCode.getSlice(1, 1));
    22      }
    23
    24      public void test2() throws Throwable {
    25          Tokenizer t = new JavaTokenizer();
    26          String data = "public class Foo {" + PMD.EOL + "public void bar() {}" + PMD.EOL + "public void buz() {}" + PMD.EOL + "}";
    27          SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(data));
    28          Tokens tokens = new Tokens();
    29          t.tokenize(sourceCode, tokens);
    30          assertEquals("public class Foo {" + PMD.EOL + "public void bar() {}", sourceCode.getSlice(1, 2));
    31      }
    32
    33      public void testDiscardSemicolons() throws Throwable {
    34          Tokenizer t = new JavaTokenizer();
    35          SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader("public class Foo {private int x;}"));
    36          Tokens tokens = new Tokens();
    37          t.tokenize(sourceCode, tokens);
    38          assertEquals(9, tokens.size());
    39      }
    40
    41      public void testDiscardImports() throws Throwable {
    42          Tokenizer t = new JavaTokenizer();
    43          SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader("import java.io.File;" + PMD.EOL + "public class Foo {}"));
    44          Tokens tokens = new Tokens();
    45          t.tokenize(sourceCode, tokens);
    46          assertEquals(6, tokens.size());
    47      }
    48
    49      public void testDiscardPkgStmts() throws Throwable {
    50          Tokenizer t = new JavaTokenizer();
    51          SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader("package foo.bar.baz;" + PMD.EOL + "public class Foo {}"));
    52          Tokens tokens = new Tokens();
    53          t.tokenize(sourceCode, tokens);
    54          assertEquals(6, tokens.size());
    55      }
    56  }
    57
    58