From 1c4bec1ed7db39be51fe695fec63fc506f460f0c Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 1 Sep 2014 14:41:08 +0200 Subject: [PATCH] Add unit test coverage --- .../sonar/xoo/lang/XooTokenizerSensor.java | 2 +- .../xoo/lang/XooTokenizerSensorTest.java | 97 +++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/XooTokenizerSensorTest.java diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/XooTokenizerSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/XooTokenizerSensor.java index 1098625783f..ae1c7e2820a 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/XooTokenizerSensor.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/XooTokenizerSensor.java @@ -36,7 +36,7 @@ import java.io.IOException; import java.util.List; /** - * Parse files *.xoo.highlighting + * Tokenize xoo files (separator is whitespace) for duplication detection */ public class XooTokenizerSensor implements Sensor { diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/XooTokenizerSensorTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/XooTokenizerSensorTest.java new file mode 100644 index 00000000000..941798f9436 --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/XooTokenizerSensorTest.java @@ -0,0 +1,97 @@ +/* + * 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.xoo.lang; + +import org.apache.commons.io.FileUtils; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.CoreProperties; +import org.sonar.api.batch.fs.InputFile; +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.batch.sensor.duplication.DuplicationTokenBuilder; +import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; +import org.sonar.api.config.Settings; + +import java.io.File; +import java.io.IOException; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class XooTokenizerSensorTest { + + private XooTokenizerSensor sensor; + private SensorContext context = mock(SensorContext.class); + private DefaultFileSystem fileSystem; + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + private File baseDir; + private Settings settings; + + @Before + public void prepare() throws IOException { + baseDir = temp.newFolder(); + sensor = new XooTokenizerSensor(); + fileSystem = new DefaultFileSystem(); + when(context.fileSystem()).thenReturn(fileSystem); + settings = new Settings(); + when(context.settings()).thenReturn(settings); + } + + @Test + public void testDescriptor() { + sensor.describe(new DefaultSensorDescriptor()); + } + + @Test + public void testNoExecutionIfExclusion() { + DefaultInputFile inputFile = new DefaultInputFile("src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); + fileSystem.add(inputFile); + settings.setProperty(CoreProperties.CPD_EXCLUSIONS, "**/foo.xoo"); + sensor.execute(context); + verify(context, never()).duplicationTokenBuilder(any(InputFile.class)); + } + + @Test + public void testExecution() throws IOException { + File source = new File(baseDir, "src/foo.xoo"); + FileUtils.write(source, "token1 token2 token3\ntoken4"); + DefaultInputFile inputFile = new DefaultInputFile("src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); + fileSystem.add(inputFile); + DuplicationTokenBuilder builder = mock(DuplicationTokenBuilder.class); + when(context.duplicationTokenBuilder(inputFile)).thenReturn(builder); + + sensor.execute(context); + + verify(builder).addToken(1, "token1"); + verify(builder).addToken(1, "token2"); + verify(builder).addToken(1, "token3"); + verify(builder).addToken(2, "token4"); + verify(builder).done(); + } +} -- 2.39.5