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) Signed-off-by: Rémi Cocula <rcocula@gmail.com>
This commit is contained in:
@@ -1,16 +1,60 @@
|
|||||||
package org.libresonic.player;
|
package org.libresonic.player;
|
||||||
|
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.libresonic.player.dao.DaoHelper;
|
import org.libresonic.player.dao.DaoHelper;
|
||||||
import org.libresonic.player.service.MediaScannerService;
|
import org.libresonic.player.service.MediaScannerService;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
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;
|
||||||
|
|
||||||
public class TestCaseUtils {
|
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.
|
* 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) {
|
public static ApplicationContext loadSpringApplicationContext(String baseResources) {
|
||||||
String applicationContextService = baseResources + "applicationContext-service.xml";
|
String applicationContextService = baseResources + "applicationContext-service.xml";
|
||||||
String applicationContextCache = baseResources + "applicationContext-cache.xml";
|
String applicationContextCache = baseResources + "applicationContext-cache.xml";
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package org.libresonic.player.dao;
|
package org.libresonic.player.dao;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
import org.libresonic.player.TestCaseUtils;
|
||||||
import org.libresonic.player.util.FileUtil;
|
import org.libresonic.player.util.FileUtil;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
|
||||||
@@ -61,7 +62,7 @@ public abstract class DaoTestCaseBase extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void deleteDatabase() {
|
private static void deleteDatabase() {
|
||||||
File libresonicHome = new File("/tmp/libresonic");
|
File libresonicHome = new File(TestCaseUtils.libresonicHomePathForTest());
|
||||||
File dbHome = new File(libresonicHome, "db");
|
File dbHome = new File(libresonicHome, "db");
|
||||||
System.setProperty("libresonic.home", libresonicHome.getPath());
|
System.setProperty("libresonic.home", libresonicHome.getPath());
|
||||||
|
|
||||||
|
|||||||
+4
-1
@@ -5,6 +5,7 @@ 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.*;
|
||||||
@@ -50,7 +51,9 @@ public class MediaScannerServiceTestCase extends TestCase {
|
|||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
|
|
||||||
TestCaseUtils.setLibresonicHome(baseResources);
|
System.setProperty("libresonic.home", TestCaseUtils.libresonicHomePathForTest());
|
||||||
|
|
||||||
|
TestCaseUtils.deleteLibresonicHome();
|
||||||
|
|
||||||
// load spring context
|
// load spring context
|
||||||
ApplicationContext context = TestCaseUtils.loadSpringApplicationContext(baseResources);
|
ApplicationContext context = TestCaseUtils.loadSpringApplicationContext(baseResources);
|
||||||
|
|||||||
+5
-5
@@ -25,6 +25,7 @@ import java.util.Date;
|
|||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
import org.libresonic.player.TestCaseUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit test of {@link SettingsService}.
|
* Unit test of {@link SettingsService}.
|
||||||
@@ -33,19 +34,18 @@ import junit.framework.TestCase;
|
|||||||
*/
|
*/
|
||||||
public class SettingsServiceTestCase extends TestCase {
|
public class SettingsServiceTestCase extends TestCase {
|
||||||
|
|
||||||
private static final File LIBRESONIC_HOME = new File("/tmp/libresonic");
|
|
||||||
|
|
||||||
private SettingsService settingsService;
|
private SettingsService settingsService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
System.setProperty("libresonic.home", LIBRESONIC_HOME.getPath());
|
String libresonicHome = TestCaseUtils.libresonicHomePathForTest();
|
||||||
new File(LIBRESONIC_HOME, "libresonic.properties").delete();
|
System.setProperty("libresonic.home", libresonicHome);
|
||||||
|
new File(libresonicHome, "libresonic.properties").delete();
|
||||||
settingsService = new SettingsService();
|
settingsService = new SettingsService();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testLibresonicHome() {
|
public void testLibresonicHome() {
|
||||||
assertEquals("Wrong Libresonic home.", LIBRESONIC_HOME, SettingsService.getLibresonicHome());
|
assertEquals("Wrong Libresonic home.", TestCaseUtils.libresonicHomePathForTest(), SettingsService.getLibresonicHome().getAbsolutePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDefaultValues() {
|
public void testDefaultValues() {
|
||||||
|
|||||||
Reference in New Issue
Block a user