aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2017-02-19 11:34:08 +0100
committerSimon Brandhof <simon.brandhof@sonarsource.com>2017-02-19 11:34:08 +0100
commitee8c7344ebc104411d623709d08a431b87c86cb7 (patch)
treec6d47468358d52cbe5e827bc4e2f1e56dddae395
parent503a775e7ed95311b9d9bbd40a86449f72c23102 (diff)
downloadsonarqube-ee8c7344ebc104411d623709d08a431b87c86cb7.tar.gz
sonarqube-ee8c7344ebc104411d623709d08a431b87c86cb7.zip
Clean-up TestUtils
The methods getResource() and newTempDir() are rarely used and should be moved to the callers
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/es/EsServerHolder.java7
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/test/index/TestIndexerTest.java7
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java14
-rw-r--r--sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java46
-rw-r--r--sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.java33
-rw-r--r--sonar-testing-harness/src/test/resources/org/sonar/test/TestUtilsTest/getResource/foo.txt1
6 files changed, 18 insertions, 90 deletions
diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/EsServerHolder.java b/server/sonar-server/src/test/java/org/sonar/server/es/EsServerHolder.java
index c55ee231f02..a30b6c90639 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/es/EsServerHolder.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/es/EsServerHolder.java
@@ -20,6 +20,8 @@
package org.sonar.server.es;
import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
import java.util.Properties;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.client.transport.TransportClient;
@@ -31,7 +33,6 @@ import org.sonar.process.ProcessEntryPoint;
import org.sonar.process.ProcessProperties;
import org.sonar.process.Props;
import org.sonar.search.SearchServer;
-import org.sonar.test.TestUtils;
public class EsServerHolder {
@@ -90,9 +91,9 @@ public class EsServerHolder {
client.close();
}
- public static synchronized EsServerHolder get() {
+ public static synchronized EsServerHolder get() throws IOException {
if (HOLDER == null) {
- File homeDir = TestUtils.newTempDir("tmp-es-");
+ File homeDir = Files.createTempDirectory("tmp-es-").toFile();
homeDir.delete();
homeDir.mkdir();
diff --git a/server/sonar-server/src/test/java/org/sonar/server/test/index/TestIndexerTest.java b/server/sonar-server/src/test/java/org/sonar/server/test/index/TestIndexerTest.java
index 188469bab50..fa52dca0df4 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/test/index/TestIndexerTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/test/index/TestIndexerTest.java
@@ -25,7 +25,7 @@ import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
-import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilders;
@@ -40,8 +40,8 @@ import org.sonar.server.es.EsTester;
import org.sonar.server.es.ProjectIndexer;
import org.sonar.server.source.index.FileSourcesUpdaterHelper;
import org.sonar.server.test.db.TestTesting;
-import org.sonar.test.TestUtils;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.sonar.server.test.index.TestIndexDefinition.FIELD_DURATION_IN_MS;
@@ -55,7 +55,6 @@ import static org.sonar.server.test.index.TestIndexDefinition.FIELD_TEST_UUID;
import static org.sonar.server.test.index.TestIndexDefinition.INDEX;
import static org.sonar.server.test.index.TestIndexDefinition.TYPE;
-
public class TestIndexerTest {
private System2 system2 = System2.INSTANCE;
@@ -173,7 +172,7 @@ public class TestIndexerTest {
es.client().prepareIndex(INDEX, TYPE)
.setId(uuid)
.setRouting(projectUuid)
- .setSource(FileUtils.readFileToString(TestUtils.getResource(this.getClass(), projectUuid + "_" + fileUuid + "_" + testName + ".json")))
+ .setSource(IOUtils.toString(getClass().getResource(format("%s/%s_%s_%s.json", getClass().getSimpleName(), projectUuid, fileUuid, testName))))
.setRefresh(true)
.get();
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java b/server/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java
index 742f0af0da2..060c669f3f3 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java
@@ -25,6 +25,7 @@ import com.google.common.collect.Lists;
import java.io.File;
import java.io.IOException;
import java.net.URL;
+import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@@ -46,7 +47,6 @@ import org.sonar.server.platform.BackendCleanup;
import org.sonar.server.platform.ServerTesterPlatform;
import org.sonar.server.plugins.UpdateCenterClient;
import org.sonar.server.ws.WsTester;
-import org.sonar.test.TestUtils;
import static org.sonar.server.platform.Platform.Startup.ALL;
import static org.sonar.server.platform.Platform.Startup.NO_STARTUP_TASKS;
@@ -66,8 +66,8 @@ public class ServerTester extends ExternalResource {
private ServerTesterPlatform platform;
private EsServerHolder esServerHolder;
- private final File homeDir = TestUtils.newTempDir("tmp-sq-");
- private final List<Object> components = Lists.<Object>newArrayList(WsTester.class);
+ private final File homeDir = newTempDir("tmp-sq-");
+ private final List<Object> components = Lists.newArrayList(WsTester.class);
private final Properties initialProps = new Properties();
private final ServletContext servletContext = new AttributeHolderServletContext();
private URL updateCenterUrl;
@@ -287,4 +287,12 @@ public class ServerTester extends ExternalResource {
return XOO_SUFFIXES;
}
}
+
+ private static File newTempDir(String prefix) {
+ try {
+ return Files.createTempDirectory(prefix).toFile();
+ } catch (Exception e) {
+ throw new IllegalStateException("Fail to create temp dir", e);
+ }
+ }
}
diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java b/sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java
index 037ac7c3d13..5e579a15ed7 100644
--- a/sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java
+++ b/sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java
@@ -19,13 +19,8 @@
*/
package org.sonar.test;
-import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
-import java.net.URL;
-import java.nio.file.Files;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.lang.StringUtils;
/**
* Utilities for unit tests
@@ -38,39 +33,6 @@ public final class TestUtils {
}
/**
- * 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 = TestUtils.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);
- }
-
- /**
* Asserts that all constructors are private, usually for helper classes with
* only static methods. If a constructor does not have any parameters, then
* it's instantiated.
@@ -90,12 +52,4 @@ public final class TestUtils {
}
return ok;
}
-
- public static File newTempDir(String prefix) {
- try {
- return Files.createTempDirectory(prefix).toFile();
- } catch (Exception e) {
- throw new IllegalStateException("Fail to create temp dir", e);
- }
- }
}
diff --git a/sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.java b/sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.java
index d89f8858b35..88f6360a69a 100644
--- a/sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.java
+++ b/sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.java
@@ -21,33 +21,12 @@ package org.sonar.test;
import org.junit.Test;
-import java.io.File;
-
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
-import static org.sonar.test.TestUtils.getResource;
public class TestUtilsTest {
@Test
- public void testResource() {
- File file = getResource("org/sonar/test/TestUtilsTest/getResource/foo.txt");
- assertThat(file).exists();
-
- file = getResource("/org/sonar/test/TestUtilsTest/getResource/foo.txt");
- assertThat(file).exists();
-
- file = getResource(getClass(), "getResource/foo.txt");
- assertThat(file).exists();
- }
-
- @Test
- public void testResourceNotFound() {
- File file = getResource("org/sonar/test/TestUtilsTest/unknown.txt");
- assertThat(file).isNull();
- }
-
- @Test
public void hasOnlyPrivateConstructors() {
assertThat(TestUtils.hasOnlyPrivateConstructors(TestUtils.class)).isTrue();
assertThat(TestUtils.hasOnlyPrivateConstructors(OnlyPrivateConstructors.class)).isTrue();
@@ -60,18 +39,6 @@ public class TestUtilsTest {
}
}
- @Test
- public void newTempDir() throws Exception {
- File dir1 = TestUtils.newTempDir("foo");
- assertThat(dir1).exists().isDirectory();
- assertThat(dir1.listFiles()).isEmpty();
-
- File dir2 = TestUtils.newTempDir("foo");
- assertThat(dir2).exists().isDirectory();
- assertThat(dir2.listFiles()).isEmpty();
- assertThat(dir2.getCanonicalPath()).isNotEqualTo(dir1.getCanonicalPath());
- }
-
public static class OnlyPrivateConstructors {
private OnlyPrivateConstructors() {
}
diff --git a/sonar-testing-harness/src/test/resources/org/sonar/test/TestUtilsTest/getResource/foo.txt b/sonar-testing-harness/src/test/resources/org/sonar/test/TestUtilsTest/getResource/foo.txt
deleted file mode 100644
index 19102815663..00000000000
--- a/sonar-testing-harness/src/test/resources/org/sonar/test/TestUtilsTest/getResource/foo.txt
+++ /dev/null
@@ -1 +0,0 @@
-foo \ No newline at end of file