aboutsummaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2016-03-17 16:00:37 +0100
committerJulien HENRY <julien.henry@sonarsource.com>2016-03-17 16:00:37 +0100
commit1447a88043e5280e79344b07a84e9c9994893391 (patch)
treebe8f724acac5eda22e6a9013f122f386edde3145 /plugins
parent91720513ee12439d09703cc8837c3f5d40668ef1 (diff)
downloadsonarqube-1447a88043e5280e79344b07a84e9c9994893391.tar.gz
sonarqube-1447a88043e5280e79344b07a84e9c9994893391.zip
SONAR-7389 Improve testability
Diffstat (limited to 'plugins')
-rw-r--r--plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/CpdTokenizerSensorTest.java87
1 files changed, 87 insertions, 0 deletions
diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/CpdTokenizerSensorTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/CpdTokenizerSensorTest.java
new file mode 100644
index 00000000000..c58f85a9be7
--- /dev/null
+++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/CpdTokenizerSensorTest.java
@@ -0,0 +1,87 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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 java.io.File;
+import java.io.IOException;
+import java.io.StringReader;
+import org.apache.commons.io.FileUtils;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.FileMetadata;
+import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
+import org.sonar.api.batch.sensor.internal.SensorContextTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
+
+public class CpdTokenizerSensorTest {
+
+ private CpdTokenizerSensor sensor;
+ private SensorContextTester context;
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private File baseDir;
+
+ @Before
+ public void prepare() throws IOException {
+ baseDir = temp.newFolder();
+ sensor = new CpdTokenizerSensor();
+ context = SensorContextTester.create(baseDir);
+ }
+
+ @Test
+ public void testDescriptor() {
+ sensor.describe(new DefaultSensorDescriptor());
+ }
+
+ @Test
+ public void testExecution() throws IOException {
+ String content = "public class Foo {\n\n}";
+
+ createSourceFile(content);
+
+ sensor.execute(context);
+
+ assertThat(context.cpdTokens("foo:src/foo.xoo")).extracting("value", "startLine", "startUnit", "endUnit")
+ .containsExactly(
+ tuple("publicclassFoo{", 1, 1, 4),
+ tuple("}", 3, 5, 5));
+ }
+
+ private void createSourceFile(String content) throws IOException {
+ File sourceFile = new File(baseDir, "src/foo.xoo");
+ FileUtils.write(sourceFile, content);
+ DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo")
+ .setLanguage("xoo")
+ .initMetadata(new FileMetadata().readMetadata(new StringReader(content)));
+ context.fileSystem().add(inputFile);
+ }
+
+}