Refactoring in test cases. The /tmp/libresonic directory used as LIBRESONIC_HOME for tests is now located in the target dir of the project.

(cherry picked from commit 32d4580)
master
remi 8 years ago
parent 06e227afb3
commit d0774f757f
  1. 66
      libresonic-main/src/test/java/org/libresonic/player/TestCaseUtils.java
  2. 3
      libresonic-main/src/test/java/org/libresonic/player/dao/DaoTestCaseBase.java
  3. 5
      libresonic-main/src/test/java/org/libresonic/player/service/MediaScannerServiceTestCase.java
  4. 10
      libresonic-main/src/test/java/org/libresonic/player/service/SettingsServiceTestCase.java

@ -1,16 +1,60 @@
package org.libresonic.player;
import org.apache.commons.io.FileUtils;
import org.libresonic.player.dao.DaoHelper;
import org.libresonic.player.service.MediaScannerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class TestCaseUtils {
private static final String LIBRESONIC_HOME_FOR_TEST = "/tmp/libresonic";
public static String libresonicHomePathForTest() {
String targetDir = TestCaseUtils.class.getResource("/").toString().replaceFirst("/target.*","/target");
if (targetDir.startsWith("file:")) {
targetDir = targetDir.replace("file:","");
}
return targetDir + LIBRESONIC_HOME_FOR_TEST;
}
/**
* Constructs the path of a resource according to the path of the current class.
*
* @param baseResources
* @return
*/
private static String basePath(String baseResources) {
String basePath = TestCaseUtils.class.getResource(baseResources).toString();
if (basePath.startsWith("file:")) {
return TestCaseUtils.class.getResource(baseResources).toString().replace("file:","");
}
return basePath;
}
public static void deleteLibresonicHome() throws IOException {
File libresonicHomeDir = new File(libresonicHomePathForTest());
if (libresonicHomeDir.exists() && libresonicHomeDir.isDirectory()) {
System.out.println("Delete libresonic home (ie. "+libresonicHomeDir.getAbsolutePath()+").");
try {
FileUtils.deleteDirectory(libresonicHomeDir);
} catch (IOException e) {
System.out.println("Error while deleting libresonic home.");
e.printStackTrace();
throw e;
}
}
}
/**
* Constructs a map of records count per table.
*
@ -42,26 +86,6 @@ public class TestCaseUtils {
}
/**
* Constructs the path of a resource according to the path of the current class.
*
* @param baseResources
* @return
*/
private static String basePath(String baseResources) {
String basePath = TestCaseUtils.class.getResource(baseResources).toString();
if (basePath.startsWith("file:")) {
return TestCaseUtils.class.getResource(baseResources).toString().replace("file:","");
}
return basePath;
}
public static void setLibresonicHome(String baseResources) {
String subsoncicHome = basePath(baseResources);
System.setProperty("libresonic.home",subsoncicHome);
}
public static ApplicationContext loadSpringApplicationContext(String baseResources) {
String applicationContextService = baseResources + "applicationContext-service.xml";
String applicationContextCache = baseResources + "applicationContext-cache.xml";
@ -76,7 +100,7 @@ public class TestCaseUtils {
/**
* Scans the music library * @param mediaScannerService
*/
*/
public static void execScan(MediaScannerService mediaScannerService) {
mediaScannerService.scanLibrary();

@ -1,6 +1,7 @@
package org.libresonic.player.dao;
import junit.framework.TestCase;
import org.libresonic.player.TestCaseUtils;
import org.libresonic.player.util.FileUtil;
import org.springframework.jdbc.core.JdbcTemplate;
@ -61,7 +62,7 @@ public abstract class DaoTestCaseBase extends TestCase {
}
private static void deleteDatabase() {
File libresonicHome = new File("/tmp/libresonic");
File libresonicHome = new File(TestCaseUtils.libresonicHomePathForTest());
File dbHome = new File(libresonicHome, "db");
System.setProperty("libresonic.home", libresonicHome.getPath());

@ -5,6 +5,7 @@ import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestCase;
import org.libresonic.player.TestCaseUtils;
import org.libresonic.player.dao.*;
@ -50,7 +51,9 @@ public class MediaScannerServiceTestCase extends TestCase {
protected void setUp() throws Exception {
super.setUp();
TestCaseUtils.setLibresonicHome(baseResources);
System.setProperty("libresonic.home", TestCaseUtils.libresonicHomePathForTest());
TestCaseUtils.deleteLibresonicHome();
// load spring context
ApplicationContext context = TestCaseUtils.loadSpringApplicationContext(baseResources);

@ -25,6 +25,7 @@ import java.util.Date;
import java.util.Locale;
import junit.framework.TestCase;
import org.libresonic.player.TestCaseUtils;
/**
* Unit test of {@link SettingsService}.
@ -33,19 +34,18 @@ import junit.framework.TestCase;
*/
public class SettingsServiceTestCase extends TestCase {
private static final File LIBRESONIC_HOME = new File("/tmp/libresonic");
private SettingsService settingsService;
@Override
protected void setUp() throws Exception {
System.setProperty("libresonic.home", LIBRESONIC_HOME.getPath());
new File(LIBRESONIC_HOME, "libresonic.properties").delete();
String libresonicHome = TestCaseUtils.libresonicHomePathForTest();
System.setProperty("libresonic.home", libresonicHome);
new File(libresonicHome, "libresonic.properties").delete();
settingsService = new SettingsService();
}
public void testLibresonicHome() {
assertEquals("Wrong Libresonic home.", LIBRESONIC_HOME, SettingsService.getLibresonicHome());
assertEquals("Wrong Libresonic home.", TestCaseUtils.libresonicHomePathForTest(), SettingsService.getLibresonicHome().getAbsolutePath());
}
public void testDefaultValues() {

Loading…
Cancel
Save