Search in sources :

Example 1 with SourceCode

use of net.sourceforge.pmd.cpd.SourceCode in project sonarqube by SonarSource.

the class TokenizerBridge method chunk.

public List<TokensLine> chunk(File file) {
    SourceCode sourceCode = new SourceCode(new FileCodeLoaderWithoutCache(file, encoding));
    Tokens tokens = new Tokens();
    TokenEntry.clearImages();
    try {
        tokenizer.tokenize(sourceCode, tokens);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    TokenEntry.clearImages();
    return convert(tokens.getTokens());
}
Also used : SourceCode(net.sourceforge.pmd.cpd.SourceCode) FileCodeLoaderWithoutCache(org.sonar.duplications.cpd.FileCodeLoaderWithoutCache) IOException(java.io.IOException) Tokens(net.sourceforge.pmd.cpd.Tokens)

Example 2 with SourceCode

use of net.sourceforge.pmd.cpd.SourceCode in project sonarqube by SonarSource.

the class TokenizerBridgeTest method setUp.

@Before
public void setUp() {
    Tokenizer tokenizer = new Tokenizer() {

        public void tokenize(SourceCode tokens, Tokens tokenEntries) throws IOException {
            tokenEntries.add(new TokenEntry("t1", "src", 1));
            tokenEntries.add(new TokenEntry("t2", "src", 1));
            tokenEntries.add(new TokenEntry("t3", "src", 2));
            tokenEntries.add(new TokenEntry("t1", "src", 4));
            tokenEntries.add(new TokenEntry("t3", "src", 4));
            tokenEntries.add(new TokenEntry("t3", "src", 4));
            tokenEntries.add(TokenEntry.getEOF());
        }
    };
    bridge = new TokenizerBridge(tokenizer, "UTF-8", 10);
}
Also used : SourceCode(net.sourceforge.pmd.cpd.SourceCode) TokenEntry(net.sourceforge.pmd.cpd.TokenEntry) Tokenizer(net.sourceforge.pmd.cpd.Tokenizer) Tokens(net.sourceforge.pmd.cpd.Tokens) Before(org.junit.Before)

Example 3 with SourceCode

use of net.sourceforge.pmd.cpd.SourceCode in project maven-plugins by apache.

the class CpdReportTest method testWriteNonHtml.

public void testWriteNonHtml() throws Exception {
    File testPom = new File(getBasedir(), "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml");
    CpdReport mojo = (CpdReport) lookupMojo("cpd", testPom);
    assertNotNull(mojo);
    TokenEntry tFirstEntry = new TokenEntry("public java", "MyClass.java", 2);
    TokenEntry tSecondEntry = new TokenEntry("public java", "MyClass3.java", 2);
    String duplicatedCodeFragment = "// ----- duplicated code example -----";
    SourceCode sourceCodeFirst = new SourceCode(new SourceCode.StringCodeLoader(PMD.EOL + duplicatedCodeFragment + PMD.EOL, "MyClass.java"));
    SourceCode sourceCodeSecond = new SourceCode(new SourceCode.StringCodeLoader(PMD.EOL + duplicatedCodeFragment + PMD.EOL, "MyClass3.java"));
    List<Match> tList = new ArrayList<>();
    Mark tFirstMark = new Mark(tFirstEntry);
    tFirstMark.setSourceCode(sourceCodeFirst);
    tFirstMark.setLineCount(1);
    Mark tSecondMark = new Mark(tSecondEntry);
    tSecondMark.setSourceCode(sourceCodeSecond);
    tSecondMark.setLineCount(1);
    Match tMatch = new Match(2, tFirstMark, tSecondMark);
    tList.add(tMatch);
    CPDConfiguration cpdConfiguration = new CPDConfiguration();
    cpdConfiguration.setMinimumTileSize(100);
    cpdConfiguration.setLanguage(new JavaLanguage());
    cpdConfiguration.setEncoding("UTF-8");
    CPD tCpd = new MockCpd(cpdConfiguration, tList.iterator());
    tCpd.go();
    mojo.writeNonHtml(tCpd);
    File tReport = new File("target/test/unit/default-configuration/target/cpd.xml");
    // parseDocument( new BufferedInputStream( new FileInputStream( report ) ) );
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document pmdCpdDocument = builder.parse(tReport);
    assertNotNull(pmdCpdDocument);
    String str = readFile(new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"));
    assertTrue(lowerCaseContains(str, "MyClass.java"));
    assertTrue(lowerCaseContains(str, "MyClass3.java"));
    assertTrue(lowerCaseContains(str, duplicatedCodeFragment));
}
Also used : SourceCode(net.sourceforge.pmd.cpd.SourceCode) CPDConfiguration(net.sourceforge.pmd.cpd.CPDConfiguration) ArrayList(java.util.ArrayList) Mark(net.sourceforge.pmd.cpd.Mark) JavaLanguage(net.sourceforge.pmd.cpd.JavaLanguage) Document(org.w3c.dom.Document) Match(net.sourceforge.pmd.cpd.Match) TokenEntry(net.sourceforge.pmd.cpd.TokenEntry) DocumentBuilder(javax.xml.parsers.DocumentBuilder) CPD(net.sourceforge.pmd.cpd.CPD) File(java.io.File)

Example 4 with SourceCode

use of net.sourceforge.pmd.cpd.SourceCode in project sonarqube by SonarSource.

the class XooTokenizerTest method testExecution.

@Test
public void testExecution() throws IOException {
    File source = new File(baseDir, "src/foo.xoo");
    FileUtils.write(source, "token1 token2 token3\ntoken4");
    DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build();
    fileSystem.add(inputFile);
    XooTokenizer tokenizer = new XooTokenizer(fileSystem);
    SourceCode sourceCode = mock(SourceCode.class);
    when(sourceCode.getFileName()).thenReturn(inputFile.absolutePath());
    Tokens cpdTokens = new Tokens();
    tokenizer.tokenize(sourceCode, cpdTokens);
    // 4 tokens + EOF
    assertThat(cpdTokens.getTokens()).hasSize(5);
    assertThat(cpdTokens.getTokens().get(3)).isEqualTo(new TokenEntry("token4", "src/foo.xoo", 2));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SourceCode(net.sourceforge.pmd.cpd.SourceCode) TokenEntry(net.sourceforge.pmd.cpd.TokenEntry) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Tokens(net.sourceforge.pmd.cpd.Tokens) Test(org.junit.Test)

Aggregations

SourceCode (net.sourceforge.pmd.cpd.SourceCode)4 TokenEntry (net.sourceforge.pmd.cpd.TokenEntry)3 Tokens (net.sourceforge.pmd.cpd.Tokens)3 File (java.io.File)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 CPD (net.sourceforge.pmd.cpd.CPD)1 CPDConfiguration (net.sourceforge.pmd.cpd.CPDConfiguration)1 JavaLanguage (net.sourceforge.pmd.cpd.JavaLanguage)1 Mark (net.sourceforge.pmd.cpd.Mark)1 Match (net.sourceforge.pmd.cpd.Match)1 Tokenizer (net.sourceforge.pmd.cpd.Tokenizer)1 Before (org.junit.Before)1 Test (org.junit.Test)1 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)1 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)1 FileCodeLoaderWithoutCache (org.sonar.duplications.cpd.FileCodeLoaderWithoutCache)1 Document (org.w3c.dom.Document)1