diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2016-01-19 11:14:50 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2016-01-19 14:45:02 +0100 |
commit | 3e6f3ed80c0b631d794bcb2e744f3517ad0db989 (patch) | |
tree | 9b8990e103de8c8e60fac04840f7f06fdd50898f /sonar-batch/src/test | |
parent | 7d272075f919cffb2a5806f20fe027dea1cb4b28 (diff) | |
download | sonarqube-3e6f3ed80c0b631d794bcb2e744f3517ad0db989.tar.gz sonarqube-3e6f3ed80c0b631d794bcb2e744f3517ad0db989.zip |
Cut many dependencies on sonar-testing-harness
Diffstat (limited to 'sonar-batch/src/test')
7 files changed, 86 insertions, 114 deletions
diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchExtensionDictionnaryTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchExtensionDictionnaryTest.java index fad6d8bd7f5..77a0021ef31 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchExtensionDictionnaryTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchExtensionDictionnaryTest.java @@ -20,6 +20,9 @@ package org.sonar.batch.bootstrap; import com.google.common.collect.Lists; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; import org.junit.Test; import org.sonar.api.BatchExtension; import org.sonar.api.batch.BuildBreaker; @@ -38,14 +41,8 @@ import org.sonar.batch.sensor.DefaultSensorContext; import org.sonar.batch.sensor.SensorOptimizer; import org.sonar.core.platform.ComponentContainer; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; - import static org.assertj.core.api.Assertions.assertThat; -import static org.hamcrest.Matchers.hasItem; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; public class BatchExtensionDictionnaryTest { @@ -72,7 +69,7 @@ public class BatchExtensionDictionnaryTest { } }); - assertThat(sensors, hasItem(sensor1)); + assertThat(sensors).contains(sensor1); assertEquals(1, sensors.size()); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/cache/WSLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/cache/WSLoaderTest.java index 851cc3abbdb..ad7bb763d67 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/cache/WSLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/cache/WSLoaderTest.java @@ -22,7 +22,6 @@ package org.sonar.batch.cache; import java.io.IOException; import java.io.InputStream; import org.apache.commons.io.IOUtils; -import org.hamcrest.Matchers; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -33,7 +32,6 @@ import org.sonar.batch.cache.WSLoader.LoadStrategy; import org.sonar.home.cache.PersistentCache; import org.sonarqube.ws.client.HttpException; import org.sonarqube.ws.client.MockWsResponse; -import org.sonarqube.ws.client.WsConnector; import org.sonarqube.ws.client.WsRequest; import static org.assertj.core.api.Assertions.assertThat; @@ -188,7 +186,7 @@ public class WSLoaderTest { turnServerOffline(); exception.expect(IllegalStateException.class); - exception.expectMessage(Matchers.containsString("Server is not available")); + exception.expectMessage("Server is not available"); WSLoader loader = new WSLoader(LoadStrategy.SERVER_ONLY, cache, ws); loader.loadString(ID); @@ -200,7 +198,7 @@ public class WSLoaderTest { turnCacheEmpty(); exception.expect(IllegalStateException.class); - exception.expectMessage(Matchers.is("Server is not accessible and data is not cached")); + exception.expectMessage("Server is not accessible and data is not cached"); WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, ws); loader.loadString(ID); @@ -211,7 +209,7 @@ public class WSLoaderTest { turnCacheEmpty(); exception.expect(IllegalStateException.class); - exception.expectMessage(Matchers.is("Data is not cached")); + exception.expectMessage("Data is not cached"); WSLoader loader = new WSLoader(LoadStrategy.CACHE_ONLY, cache, ws); loader.loadString(ID); diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/BucketTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/BucketTest.java index e9c9374ea5c..a6a5f900088 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/BucketTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/BucketTest.java @@ -24,12 +24,9 @@ import org.sonar.api.measures.Metric; import org.sonar.api.resources.Directory; import org.sonar.api.resources.File; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.core.IsNot.not; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; public class BucketTest { @@ -43,9 +40,8 @@ public class BucketTest { Bucket fileBucket = new Bucket(javaFile); fileBucket.setParent(packageBucket); - assertThat(fileBucket.getParent(), is(packageBucket)); - assertThat(packageBucket.getChildren().size(), is(1)); - assertThat(packageBucket.getChildren(), hasItem(fileBucket)); + assertThat(fileBucket.getParent()).isEqualTo(packageBucket); + assertThat(packageBucket.getChildren()).containsExactly(fileBucket); } @Test @@ -57,6 +53,6 @@ public class BucketTest { @Test public void shouldNotBeEquals() { assertFalse(new Bucket(directory).equals(new Bucket(javaFile))); - assertThat(new Bucket(directory).hashCode(), not(is(new Bucket(javaFile).hashCode()))); + assertThat(new Bucket(directory).hashCode()).isNotEqualTo(new Bucket(javaFile).hashCode()); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/Benchmark.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/Benchmark.java deleted file mode 100644 index 766dc2c14f3..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/Benchmark.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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.batch.mediumtest; - -import org.hamcrest.Matchers; -import org.junit.rules.ErrorCollector; -import org.slf4j.LoggerFactory; - -public class Benchmark extends ErrorCollector { - - private static final boolean ENABLED = "true".equals(System.getProperty("enableBenchmarkAssertions")); - - static { - if (ENABLED) { - LoggerFactory.getLogger(Benchmark.class).warn("Assertions are calibrated for SonarSource dedicated box. " + - "They can be disabled by setting the property -DenableBenchmarkAssertions=false."); - } - } - - public void expectLessThanOrEqualTo(String label, long val, long max) { - if (ENABLED) { - checkThat(label, val, Matchers.lessThan(max)); - } - } - -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorBuilderTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorBuilderTest.java index 21aa0fa27c2..24b6bca3fc9 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorBuilderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorBuilderTest.java @@ -23,10 +23,12 @@ import com.google.common.collect.Maps; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.net.URL; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; +import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import org.junit.Before; import org.junit.Rule; @@ -38,7 +40,6 @@ import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.utils.log.LogTester; import org.sonar.api.utils.log.LoggerLevel; import org.sonar.batch.analysis.AnalysisProperties; -import org.sonar.test.TestUtils; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -74,7 +75,7 @@ public class ProjectReactorBuilderTest { public void shouldFailIfUnexistingSourceDirectory() { thrown.expect(IllegalStateException.class); thrown.expectMessage("The folder 'unexisting-source-dir' does not exist for 'com.foo.project' (base directory = " - + TestUtils.getResource(this.getClass(), "simple-project-with-unexisting-source-dir") + ")"); + + getResource(this.getClass(), "simple-project-with-unexisting-source-dir") + ")"); loadProjectDefinition("simple-project-with-unexisting-source-dir"); } @@ -133,9 +134,9 @@ public class ProjectReactorBuilderTest { assertThat(rootProject.properties().get("module2.sonar.projectKey")).isNull(); // Check baseDir and workDir assertThat(rootProject.getBaseDir().getCanonicalFile()) - .isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root")); + .isEqualTo(getResource(this.getClass(), "multi-module-definitions-all-in-root")); assertThat(rootProject.getWorkDir().getCanonicalFile()) - .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root"), ".sonar")); + .isEqualTo(new File(getResource(this.getClass(), "multi-module-definitions-all-in-root"), ".sonar")); // CHECK MODULES List<ProjectDefinition> modules = rootProject.getSubProjects(); @@ -143,7 +144,7 @@ public class ProjectReactorBuilderTest { // Module 1 ProjectDefinition module1 = modules.get(0); - assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root/module1")); + assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(getResource(this.getClass(), "multi-module-definitions-all-in-root/module1")); assertThat(module1.getKey()).isEqualTo("com.foo.project:module1"); assertThat(module1.getName()).isEqualTo("module1"); assertThat(module1.getVersion()).isEqualTo("1.0-SNAPSHOT"); @@ -157,13 +158,13 @@ public class ProjectReactorBuilderTest { assertThat(module1.properties().get("module2.sonar.projectKey")).isNull(); // Check baseDir and workDir assertThat(module1.getBaseDir().getCanonicalFile()) - .isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root/module1")); + .isEqualTo(getResource(this.getClass(), "multi-module-definitions-all-in-root/module1")); assertThat(module1.getWorkDir().getCanonicalFile()) - .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root"), ".sonar/com.foo.project_module1")); + .isEqualTo(new File(getResource(this.getClass(), "multi-module-definitions-all-in-root"), ".sonar/com.foo.project_module1")); // Module 2 ProjectDefinition module2 = modules.get(1); - assertThat(module2.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root/module2")); + assertThat(module2.getBaseDir().getCanonicalFile()).isEqualTo(getResource(this.getClass(), "multi-module-definitions-all-in-root/module2")); assertThat(module2.getKey()).isEqualTo("com.foo.project:com.foo.project.module2"); assertThat(module2.getName()).isEqualTo("Foo Module 2"); assertThat(module2.getVersion()).isEqualTo("1.0-SNAPSHOT"); @@ -176,9 +177,9 @@ public class ProjectReactorBuilderTest { assertThat(module2.properties().get("module2.sonar.projectKey")).isNull(); // Check baseDir and workDir assertThat(module2.getBaseDir().getCanonicalFile()) - .isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root/module2")); + .isEqualTo(getResource(this.getClass(), "multi-module-definitions-all-in-root/module2")); assertThat(module2.getWorkDir().getCanonicalFile()) - .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root"), ".sonar/com.foo.project_com.foo.project.module2")); + .isEqualTo(new File(getResource(this.getClass(), "multi-module-definitions-all-in-root"), ".sonar/com.foo.project_com.foo.project.module2")); } // SONAR-4876 @@ -216,19 +217,19 @@ public class ProjectReactorBuilderTest { // Module 1 ProjectDefinition module1 = modules.get(0); - assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-language-definitions-all-in-root")); + assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(getResource(this.getClass(), "multi-language-definitions-all-in-root")); assertThat(module1.getSourceDirs()).contains("src/main/java"); // and module properties must have been cleaned assertThat(module1.getWorkDir().getCanonicalFile()) - .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-language-definitions-all-in-root"), ".sonar/example_java-module")); + .isEqualTo(new File(getResource(this.getClass(), "multi-language-definitions-all-in-root"), ".sonar/example_java-module")); // Module 2 ProjectDefinition module2 = modules.get(1); - assertThat(module2.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-language-definitions-all-in-root")); + assertThat(module2.getBaseDir().getCanonicalFile()).isEqualTo(getResource(this.getClass(), "multi-language-definitions-all-in-root")); assertThat(module2.getSourceDirs()).contains("src/main/groovy"); // and module properties must have been cleaned assertThat(module2.getWorkDir().getCanonicalFile()) - .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-language-definitions-all-in-root"), ".sonar/example_groovy-module")); + .isEqualTo(new File(getResource(this.getClass(), "multi-language-definitions-all-in-root"), ".sonar/example_groovy-module")); } @Test @@ -243,7 +244,7 @@ public class ProjectReactorBuilderTest { public void shouldFailIfUnexistingModuleBaseDir() { thrown.expect(IllegalStateException.class); thrown.expectMessage("The base directory of the module 'module1' does not exist: " - + TestUtils.getResource(this.getClass(), "multi-module-with-unexisting-basedir").getAbsolutePath() + File.separator + "module1"); + + getResource(this.getClass(), "multi-module-with-unexisting-basedir").getAbsolutePath() + File.separator + "module1"); loadProjectDefinition("multi-module-with-unexisting-basedir"); } @@ -252,7 +253,7 @@ public class ProjectReactorBuilderTest { public void shouldFailIfUnexistingSourceFolderInheritedInMultimodule() { thrown.expect(IllegalStateException.class); thrown.expectMessage("The folder 'unexisting-source-dir' does not exist for 'com.foo.project:module1' (base directory = " - + TestUtils.getResource(this.getClass(), "multi-module-with-unexisting-source-dir").getAbsolutePath() + File.separator + "module1)"); + + getResource(this.getClass(), "multi-module-with-unexisting-source-dir").getAbsolutePath() + File.separator + "module1)"); loadProjectDefinition("multi-module-with-unexisting-source-dir"); } @@ -261,7 +262,7 @@ public class ProjectReactorBuilderTest { public void shouldFailIfExplicitUnexistingTestFolder() { thrown.expect(IllegalStateException.class); thrown.expectMessage("The folder 'tests' does not exist for 'com.foo.project' (base directory = " - + TestUtils.getResource(this.getClass(), "simple-project-with-unexisting-test-dir").getAbsolutePath()); + + getResource(this.getClass(), "simple-project-with-unexisting-test-dir").getAbsolutePath()); loadProjectDefinition("simple-project-with-unexisting-test-dir"); } @@ -270,7 +271,7 @@ public class ProjectReactorBuilderTest { public void shouldFailIfExplicitUnexistingTestFolderOnModule() { thrown.expect(IllegalStateException.class); thrown.expectMessage("The folder 'tests' does not exist for 'module1' (base directory = " - + TestUtils.getResource(this.getClass(), "multi-module-with-explicit-unexisting-test-dir").getAbsolutePath() + File.separator + "module1)"); + + getResource(this.getClass(), "multi-module-with-explicit-unexisting-test-dir").getAbsolutePath() + File.separator + "module1)"); loadProjectDefinition("multi-module-with-explicit-unexisting-test-dir"); } @@ -364,15 +365,15 @@ public class ProjectReactorBuilderTest { @Test public void shouldGetRelativeFile() { - assertThat(ProjectReactorBuilder.resolvePath(TestUtils.getResource(this.getClass(), "/"), "shouldGetFile/foo.properties")) - .isEqualTo(TestUtils.getResource(this.getClass(), "shouldGetFile/foo.properties")); + assertThat(ProjectReactorBuilder.resolvePath(getResource(this.getClass(), "/"), "shouldGetFile/foo.properties")) + .isEqualTo(getResource(this.getClass(), "shouldGetFile/foo.properties")); } @Test public void shouldGetAbsoluteFile() { - File file = TestUtils.getResource(this.getClass(), "shouldGetFile/foo.properties"); + File file = getResource(this.getClass(), "shouldGetFile/foo.properties"); - assertThat(ProjectReactorBuilder.resolvePath(TestUtils.getResource(this.getClass(), "/"), file.getAbsolutePath())) + assertThat(ProjectReactorBuilder.resolvePath(getResource(this.getClass(), "/"), file.getAbsolutePath())) .isEqualTo(file); } @@ -509,11 +510,11 @@ public class ProjectReactorBuilderTest { private Map<String, String> loadProps(String projectFolder) { Map<String, String> props = Maps.<String, String>newHashMap(); - Properties runnerProps = toProperties(TestUtils.getResource(this.getClass(), projectFolder + "/sonar-project.properties")); + Properties runnerProps = toProperties(getResource(this.getClass(), projectFolder + "/sonar-project.properties")); for (final String name : runnerProps.stringPropertyNames()) { props.put(name, runnerProps.getProperty(name)); } - props.put("sonar.projectBaseDir", TestUtils.getResource(this.getClass(), projectFolder).getAbsolutePath()); + props.put("sonar.projectBaseDir", getResource(this.getClass(), projectFolder).getAbsolutePath()); return props; } @@ -575,9 +576,9 @@ public class ProjectReactorBuilderTest { assertThat(rootProject.properties().get("module2.sonar.projectKey")).isNull(); // Check baseDir and workDir assertThat(rootProject.getBaseDir().getCanonicalFile()) - .isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-same-prefix")); + .isEqualTo(getResource(this.getClass(), "multi-module-definitions-same-prefix")); assertThat(rootProject.getWorkDir().getCanonicalFile()) - .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-same-prefix"), ".sonar")); + .isEqualTo(new File(getResource(this.getClass(), "multi-module-definitions-same-prefix"), ".sonar")); // CHECK MODULES List<ProjectDefinition> modules = rootProject.getSubProjects(); @@ -585,7 +586,7 @@ public class ProjectReactorBuilderTest { // Module 1 ProjectDefinition module1 = modules.get(0); - assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-same-prefix/module1")); + assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(getResource(this.getClass(), "multi-module-definitions-same-prefix/module1")); assertThat(module1.getKey()).isEqualTo("com.foo.project:module1"); assertThat(module1.getName()).isEqualTo("module1"); assertThat(module1.getVersion()).isEqualTo("1.0-SNAPSHOT"); @@ -599,13 +600,13 @@ public class ProjectReactorBuilderTest { assertThat(module1.properties().get("module2.sonar.projectKey")).isNull(); // Check baseDir and workDir assertThat(module1.getBaseDir().getCanonicalFile()) - .isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-same-prefix/module1")); + .isEqualTo(getResource(this.getClass(), "multi-module-definitions-same-prefix/module1")); assertThat(module1.getWorkDir().getCanonicalFile()) - .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-same-prefix"), ".sonar/com.foo.project_module1")); + .isEqualTo(new File(getResource(this.getClass(), "multi-module-definitions-same-prefix"), ".sonar/com.foo.project_module1")); // Module 1 Feature ProjectDefinition module1Feature = modules.get(1); - assertThat(module1Feature.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-same-prefix/module1.feature")); + assertThat(module1Feature.getBaseDir().getCanonicalFile()).isEqualTo(getResource(this.getClass(), "multi-module-definitions-same-prefix/module1.feature")); assertThat(module1Feature.getKey()).isEqualTo("com.foo.project:com.foo.project.module1.feature"); assertThat(module1Feature.getName()).isEqualTo("Foo Module 1 Feature"); assertThat(module1Feature.getVersion()).isEqualTo("1.0-SNAPSHOT"); @@ -618,9 +619,9 @@ public class ProjectReactorBuilderTest { assertThat(module1Feature.properties().get("module2.sonar.projectKey")).isNull(); // Check baseDir and workDir assertThat(module1Feature.getBaseDir().getCanonicalFile()) - .isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-same-prefix/module1.feature")); + .isEqualTo(getResource(this.getClass(), "multi-module-definitions-same-prefix/module1.feature")); assertThat(module1Feature.getWorkDir().getCanonicalFile()) - .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-same-prefix"), ".sonar/com.foo.project_com.foo.project.module1.feature")); + .isEqualTo(new File(getResource(this.getClass(), "multi-module-definitions-same-prefix"), ".sonar/com.foo.project_com.foo.project.module1.feature")); } @Test @@ -635,7 +636,7 @@ public class ProjectReactorBuilderTest { private Map<String, String> loadPropsFromFile(String filePath) throws IOException { Properties props = new Properties(); - try (FileInputStream fileInputStream = new FileInputStream(TestUtils.getResource(this.getClass(), filePath))) { + try (FileInputStream fileInputStream = new FileInputStream(getResource(this.getClass(), filePath))) { props.load(fileInputStream); } Map<String, String> result = new HashMap<>(); @@ -645,4 +646,37 @@ public class ProjectReactorBuilderTest { return result; } + /** + * Search for a test resource in the classpath. For example getResource("org/sonar/MyClass/foo.txt"); + * + * @param path the starting slash is optional + * @return the resource. Null if resource not found + */ + public static File getResource(String path) { + String resourcePath = path; + if (!resourcePath.startsWith("/")) { + resourcePath = "/" + resourcePath; + } + URL url = ProjectReactorBuilderTest.class.getResource(resourcePath); + if (url != null) { + return FileUtils.toFile(url); + } + return null; + } + + /** + * Search for a resource in the classpath. For example calling the method getResource(getClass(), "myTestName/foo.txt") from + * the class org.sonar.Foo loads the file $basedir/src/test/resources/org/sonar/Foo/myTestName/foo.txt + * + * @return the resource. Null if resource not found + */ + public static File getResource(Class baseClass, String path) { + String resourcePath = StringUtils.replaceChars(baseClass.getCanonicalName(), '.', '/'); + if (!path.startsWith("/")) { + resourcePath += "/"; + } + resourcePath += path; + return getResource(resourcePath); + } + } diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/report/JSONReportTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/report/JSONReportTest.java index 4990e3cf2a5..653b26e09bb 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/report/JSONReportTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/report/JSONReportTest.java @@ -19,10 +19,7 @@ */ package org.sonar.batch.scan.report; -import org.sonar.batch.issue.tracking.TrackedIssue; - import com.google.common.collect.Lists; - import java.io.File; import java.io.IOException; import java.io.StringWriter; @@ -30,7 +27,7 @@ import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Collections; import java.util.TimeZone; - +import org.apache.commons.io.IOUtils; import org.junit.Before; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -48,10 +45,12 @@ import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; import org.sonar.api.rule.RuleKey; import org.sonar.batch.issue.IssueCache; +import org.sonar.batch.issue.tracking.TrackedIssue; import org.sonar.batch.protocol.input.BatchInput; import org.sonar.batch.repository.user.UserRepositoryLoader; import org.sonar.batch.scan.filesystem.InputPathCache; -import org.sonar.test.JsonAssert; + +import static net.javacrumbs.jsonunit.assertj.JsonAssert.assertThatJson; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verifyZeroInteractions; @@ -126,7 +125,7 @@ public class JSONReportTest { StringWriter writer = new StringWriter(); jsonReport.writeJson(writer); - JsonAssert.assertJson(writer.toString()).isSimilarTo(this.getClass().getResource(this.getClass().getSimpleName() + "/report.json")); + assertThatJson(writer.toString()).isEqualTo(IOUtils.toString(this.getClass().getResource(this.getClass().getSimpleName() + "/report.json"))); } @Test @@ -145,17 +144,7 @@ public class JSONReportTest { StringWriter writer = new StringWriter(); jsonReport.writeJson(writer); - JsonAssert.assertJson(writer.toString()).isSimilarTo(this.getClass().getResource(this.getClass().getSimpleName() + "/report-without-resolved-issues.json")); - } - - @Test - public void should_ignore_components_without_issue() { - when(issueCache.all()).thenReturn(Collections.<TrackedIssue>emptyList()); - - StringWriter writer = new StringWriter(); - jsonReport.writeJson(writer); - - JsonAssert.assertJson(writer.toString()).isSimilarTo("{\"version\":\"3.6\"}"); + assertThatJson(writer.toString()).isEqualTo(IOUtils.toString(this.getClass().getResource(this.getClass().getSimpleName() + "/report-without-resolved-issues.json"))); } @Test diff --git a/sonar-batch/src/test/resources/org/sonar/batch/scan/report/JSONReportTest/report.json b/sonar-batch/src/test/resources/org/sonar/batch/scan/report/JSONReportTest/report.json index d05703e5d30..a33e06342fa 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/scan/report/JSONReportTest/report.json +++ b/sonar-batch/src/test/resources/org/sonar/batch/scan/report/JSONReportTest/report.json @@ -4,6 +4,7 @@ { "key": "200", "component": "struts:src/main/java/org/apache/struts/Action.java", + "line": 1, "startLine": 1, "startOffset": 3, "endLine": 2, @@ -15,7 +16,7 @@ "isNew": false, "assignee": "simon", "effortToFix": 3.14, - "creationDate": "2013-04-24T00:00:00+0200" + "creationDate": "${json-unit.ignore}" } ], "components": [ |