@Test public void testSimple() throws Throwable { JavaTokenizer tokenizer = new JavaTokenizer(); SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(getSampleCode(), "Foo.java")); Tokens tokens = new Tokens(); TokenEntry.clearImages(); tokenizer.tokenize(sourceCode, tokens); assertEquals(41, tokens.size()); Map<String, SourceCode> codeMap = new HashMap<String, SourceCode>(); codeMap.put("Foo.java", sourceCode); MatchAlgorithm matchAlgorithm = new MatchAlgorithm(codeMap, tokens, 5); matchAlgorithm.findMatches(); Iterator matches = matchAlgorithm.matches(); Match match = (Match) matches.next(); assertFalse(matches.hasNext()); Iterator marks = match.iterator(); TokenEntry mark1 = (TokenEntry) marks.next(); TokenEntry mark2 = (TokenEntry) marks.next(); assertFalse(marks.hasNext()); assertEquals(3, mark1.getBeginLine()); assertEquals(4, mark2.getBeginLine()); assertTrue("Foo.java" == mark1.getTokenSrcID() && "Foo.java" == mark2.getTokenSrcID()); assertEquals(LINE_3, match.getSourceCodeSlice()); }
@Test public void testIgnore() throws Throwable { JavaTokenizer tokenizer = new JavaTokenizer(); tokenizer.setIgnoreLiterals(true); tokenizer.setIgnoreIdentifiers(true); SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(getSampleCode(), "Foo.java")); Tokens tokens = new Tokens(); TokenEntry.clearImages(); tokenizer.tokenize(sourceCode, tokens); Map<String, SourceCode> codeMap = new HashMap<String, SourceCode>(); codeMap.put("Foo.java", sourceCode); MatchAlgorithm matchAlgorithm = new MatchAlgorithm(codeMap, tokens, 5); matchAlgorithm.findMatches(); Iterator matches = matchAlgorithm.matches(); Match match = (Match) matches.next(); assertFalse(matches.hasNext()); Iterator marks = match.iterator(); marks.next(); marks.next(); marks.next(); assertFalse(marks.hasNext()); }
@Test public void testDuplicationOnSameFile() throws IOException { TokenEntry.clearImages(); AbstractLanguage cpdLanguage = new AbstractLanguage(new WebCpdTokenizer()) {}; CPD cpd = new CPD(30, cpdLanguage); cpd.setEncoding(Charset.defaultCharset().name()); cpd.setLoadSourceCodeSlices(false); cpd.add(new File("src/test/resources/duplications/fileWithDuplications.jsp")); cpd.go(); List<Match> matches = getMatches(cpd); assertThat(matches.size(), is(1)); org.sonar.duplications.cpd.Match match = matches.get(0); assertThat(match.getLineCount(), is(16)); assertThat(match.getFirstMark().getBeginLine(), is(227)); assertThat(match.getSourceCodeSlice(), is(nullValue())); }