diff options
author | Jenkins CI <ci@sonarsource.com> | 2015-12-17 08:01:22 +0100 |
---|---|---|
committer | Jenkins CI <ci@sonarsource.com> | 2015-12-17 08:01:22 +0100 |
commit | 5c75034675b60ec9a4decaf5ecce49574d987c8e (patch) | |
tree | a8df3eb141754e4b949eb3684335ffa537542e6c /sonar-batch | |
parent | d9e9ce0e057ecf58ccaee9cc5d7c2231b98c0947 (diff) | |
parent | 77828c13fec773d6185808dbe354886467c591fe (diff) | |
download | sonarqube-5c75034675b60ec9a4decaf5ecce49574d987c8e.tar.gz sonarqube-5c75034675b60ec9a4decaf5ecce49574d987c8e.zip |
Automatic merge from branch-5.3
* origin/branch-5.3:
open file in the workspace when click on the bubble
SONAR-7126 Scanner on Java 7 must support TLS 1.2 when connecting to SonarQube HTTPS server
Add unit test for Java CPD Engine
Diffstat (limited to 'sonar-batch')
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/cpd/JavaCpdEngineTest.java | 91 | ||||
-rw-r--r-- | sonar-batch/src/test/resources/org/sonar/batch/cpd/ManyStatements.java | 11 |
2 files changed, 102 insertions, 0 deletions
diff --git a/sonar-batch/src/test/java/org/sonar/batch/cpd/JavaCpdEngineTest.java b/sonar-batch/src/test/java/org/sonar/batch/cpd/JavaCpdEngineTest.java new file mode 100644 index 00000000000..798f821d1ab --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/cpd/JavaCpdEngineTest.java @@ -0,0 +1,91 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.cpd; + +import java.io.File; +import org.apache.commons.io.FileUtils; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.fs.internal.DefaultFileSystem; +import org.sonar.api.batch.fs.internal.DefaultInputFile; +import org.sonar.api.batch.sensor.SensorContext; +import org.sonar.api.config.Settings; +import org.sonar.batch.index.BatchComponentCache; +import org.sonar.batch.protocol.output.BatchReport; +import org.sonar.batch.protocol.output.BatchReport.Duplication; +import org.sonar.batch.protocol.output.BatchReportReader; +import org.sonar.batch.protocol.output.BatchReportWriter; +import org.sonar.batch.report.ReportPublisher; +import org.sonar.core.util.CloseableIterator; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class JavaCpdEngineTest { + + private static final String JAVA = "java"; + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void testJavaCodeWithTwoCloneGroupAtSameLines() throws Exception { + + File baseDir = temp.newFolder(); + DefaultFileSystem fs = new DefaultFileSystem(baseDir); + DefaultInputFile file = new DefaultInputFile("foo", "src/ManyStatements.java").setLanguage(JAVA); + fs.add(file); + BatchComponentCache batchComponentCache = new BatchComponentCache(); + batchComponentCache.add(org.sonar.api.resources.File.create("src/Foo.java").setEffectiveKey("foo:src/ManyStatements.java"), null).setInputComponent(file); + File ioFile = file.file(); + FileUtils.copyURLToFile(this.getClass().getResource("ManyStatements.java"), ioFile); + + File reportOut = temp.newFolder(); + ReportPublisher reportPublisher = mock(ReportPublisher.class); + when(reportPublisher.getWriter()).thenReturn(new BatchReportWriter(reportOut)); + JavaCpdEngine engine = new JavaCpdEngine(fs, new Settings(), reportPublisher, batchComponentCache); + engine.analyse(JAVA, mock(SensorContext.class)); + + BatchReportReader reader = new BatchReportReader(reportOut); + try (CloseableIterator<BatchReport.Duplication> it = reader.readComponentDuplications(1)) { + Duplication dupGroup1 = it.next(); + Duplication dupGroup2 = it.next(); + + assertThat(dupGroup1.getOriginPosition().getStartLine()).isEqualTo(6); + assertThat(dupGroup1.getOriginPosition().getEndLine()).isEqualTo(6); + assertThat(dupGroup1.getDuplicateCount()).isEqualTo(1); + assertThat(dupGroup1.getDuplicate(0).getRange().getStartLine()).isEqualTo(8); + assertThat(dupGroup1.getDuplicate(0).getRange().getEndLine()).isEqualTo(8); + + assertThat(dupGroup2.getOriginPosition().getStartLine()).isEqualTo(6); + assertThat(dupGroup2.getOriginPosition().getEndLine()).isEqualTo(6); + assertThat(dupGroup2.getDuplicateCount()).isEqualTo(2); + assertThat(dupGroup2.getDuplicate(0).getRange().getStartLine()).isEqualTo(7); + assertThat(dupGroup2.getDuplicate(0).getRange().getEndLine()).isEqualTo(7); + assertThat(dupGroup2.getDuplicate(1).getRange().getStartLine()).isEqualTo(8); + assertThat(dupGroup2.getDuplicate(1).getRange().getEndLine()).isEqualTo(8); + } catch (Exception e) { + throw new IllegalStateException(e); + } + + } + +} diff --git a/sonar-batch/src/test/resources/org/sonar/batch/cpd/ManyStatements.java b/sonar-batch/src/test/resources/org/sonar/batch/cpd/ManyStatements.java new file mode 100644 index 00000000000..ed2297068e4 --- /dev/null +++ b/sonar-batch/src/test/resources/org/sonar/batch/cpd/ManyStatements.java @@ -0,0 +1,11 @@ +package org.foo; + +public class ManyStatements { + + void foo() { + int A1 = 0; int B = 0; int C = 0; int D = 0; int E = 0; int F = 0; int G = 0; int H = 0; int I = 0; int J = 0; int K = 0; + int A2 = 0; int B = 0; int C = 0; int D = 0; int E = 0; int F = 0; int G = 0; int H = 0; int I = 0; int J = 0; int K = 0; + int A1 = 0; int B = 0; int C = 0; int D = 0; int E = 0; int F = 0; int G = 0; int H = 0; int I = 0; int J = 0; int K = 0; + } + +}
\ No newline at end of file |