The LIBRESONIC_HOME directory used for tests is now a temporary directory.

master
remi 8 years ago
parent 12d2bfef34
commit 6767cf0f8d
  1. 29
      libresonic-main/src/test/java/org/libresonic/player/TestCaseUtils.java
  2. 3
      libresonic-main/src/test/java/org/libresonic/player/service/MediaScannerServiceTestCase.java

@ -8,6 +8,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -16,17 +17,35 @@ public class TestCaseUtils {
private static final String LIBRESONIC_HOME_FOR_TEST = "/tmp/libresonic"; private static final String LIBRESONIC_HOME_FOR_TEST = "/tmp/libresonic";
private static File libresonicHomeDirForTest = null;
/**
* Returns the path of the LIBRESONIC_HOME directory to use for tests.
* This will create a temporary directory.
*
* @return LIBRESONIC_HOME directory path.
* @throws RuntimeException if it fails to create the temp directory.
*/
public static String libresonicHomePathForTest() { public static String libresonicHomePathForTest() {
String targetDir = TestCaseUtils.class.getResource("/").toString().replaceFirst("/target.*","/target"); if (libresonicHomeDirForTest == null) {
if (targetDir.startsWith("file:")) { try {
targetDir = targetDir.replace("file:",""); libresonicHomeDirForTest = Files.createTempDirectory("libresonic_test_").toFile();
} catch (IOException e) {
throw new RuntimeException("Error while creating temporary LIBRESONIC_HOME directory for tests");
}
System.out.println("LIBRESONIC_HOME directory will be "+libresonicHomeDirForTest.getAbsolutePath());
} }
return targetDir + LIBRESONIC_HOME_FOR_TEST; return libresonicHomeDirForTest.getAbsolutePath();
} }
public static void deleteLibresonicHome() throws IOException { /**
* Cleans the LIBRESONIC_HOME directory used for tests.
*
* @throws IOException
*/
public static void cleanLibresonicHomeForTest() throws IOException {
File libresonicHomeDir = new File(libresonicHomePathForTest()); File libresonicHomeDir = new File(libresonicHomePathForTest());
if (libresonicHomeDir.exists() && libresonicHomeDir.isDirectory()) { if (libresonicHomeDir.exists() && libresonicHomeDir.isDirectory()) {

@ -5,7 +5,6 @@ import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer; import com.codahale.metrics.Timer;
import junit.framework.Assert; import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.libresonic.player.TestCaseUtils; import org.libresonic.player.TestCaseUtils;
import org.libresonic.player.dao.*; import org.libresonic.player.dao.*;
@ -53,7 +52,7 @@ public class MediaScannerServiceTestCase extends TestCase {
System.setProperty("libresonic.home", TestCaseUtils.libresonicHomePathForTest()); System.setProperty("libresonic.home", TestCaseUtils.libresonicHomePathForTest());
TestCaseUtils.deleteLibresonicHome(); TestCaseUtils.cleanLibresonicHomeForTest();
// load spring context // load spring context
ApplicationContext context = TestCaseUtils.loadSpringApplicationContext(baseResources); ApplicationContext context = TestCaseUtils.loadSpringApplicationContext(baseResources);

Loading…
Cancel
Save