Merge branch 'develop' into _tmp
This commit is contained in:
@@ -1,11 +1,21 @@
|
||||
package org.libresonic.player.dao;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import junit.framework.TestCase;
|
||||
import liquibase.exception.LiquibaseException;
|
||||
import org.libresonic.player.TestCaseUtils;
|
||||
import org.libresonic.player.service.SettingsService;
|
||||
import org.libresonic.player.spring.SpringLiquibase;
|
||||
import org.libresonic.player.util.FileUtil;
|
||||
import org.libresonic.player.util.Util;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Superclass for all DAO test cases.
|
||||
@@ -32,7 +42,10 @@ public abstract class DaoTestCaseBase extends TestCase {
|
||||
protected PodcastDao podcastDao;
|
||||
|
||||
protected DaoTestCaseBase() {
|
||||
daoHelper = new HsqlDaoHelper();
|
||||
DataSource dataSource = createDataSource();
|
||||
daoHelper = new GenericDaoHelper(dataSource);
|
||||
|
||||
runDatabaseMigration(dataSource);
|
||||
|
||||
playerDao = new PlayerDao();
|
||||
internetRadioDao = new InternetRadioDao();
|
||||
@@ -57,6 +70,34 @@ public abstract class DaoTestCaseBase extends TestCase {
|
||||
getJdbcTemplate().execute("shutdown");
|
||||
}
|
||||
|
||||
private void runDatabaseMigration(DataSource dataSource) {
|
||||
SpringLiquibase springLiquibase = new SpringLiquibase();
|
||||
springLiquibase.setDataSource(dataSource);
|
||||
springLiquibase.setChangeLog("classpath:liquibase/db-changelog.xml");
|
||||
springLiquibase.setResourceLoader(new DefaultResourceLoader());
|
||||
Map<String,String> parameters = new HashMap<>();
|
||||
parameters.put("defaultMusicFolder", Util.getDefaultMusicFolder());
|
||||
parameters.put("varcharLimit", "512");
|
||||
parameters.put("userTableQuote", "");
|
||||
springLiquibase.setChangeLogParameters(parameters);
|
||||
try {
|
||||
springLiquibase.afterPropertiesSet();
|
||||
} catch (LiquibaseException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private DataSource createDataSource() {
|
||||
File libresonicHome = SettingsService.getLibresonicHome();
|
||||
DriverManagerDataSource ds = new DriverManagerDataSource();
|
||||
ds.setDriverClassName("org.hsqldb.jdbcDriver");
|
||||
ds.setUrl("jdbc:hsqldb:file:" + libresonicHome.getPath() + "/db/libresonic");
|
||||
ds.setUsername("sa");
|
||||
ds.setPassword("");
|
||||
|
||||
return ds;
|
||||
}
|
||||
|
||||
protected JdbcTemplate getJdbcTemplate() {
|
||||
return daoHelper.getJdbcTemplate();
|
||||
}
|
||||
|
||||
+8
-2
@@ -41,7 +41,13 @@ public class SettingsServiceTestCase extends TestCase {
|
||||
String libresonicHome = TestCaseUtils.libresonicHomePathForTest();
|
||||
System.setProperty("libresonic.home", libresonicHome);
|
||||
new File(libresonicHome, "libresonic.properties").delete();
|
||||
settingsService = new SettingsService();
|
||||
settingsService = newSettingsService();
|
||||
}
|
||||
|
||||
private SettingsService newSettingsService() {
|
||||
SettingsService settingsService = new SettingsService();
|
||||
settingsService.setConfigurationService(new ApacheCommonsConfigurationService());
|
||||
return settingsService;
|
||||
}
|
||||
|
||||
public void testLibresonicHome() {
|
||||
@@ -97,7 +103,7 @@ public class SettingsServiceTestCase extends TestCase {
|
||||
settingsService.save();
|
||||
verifySettings(settingsService);
|
||||
|
||||
verifySettings(new SettingsService());
|
||||
verifySettings(newSettingsService());
|
||||
}
|
||||
|
||||
private void verifySettings(SettingsService ss) {
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.libresonic.player.service;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.libresonic.player.TestCaseUtils;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class StartupTestCase extends TestCase {
|
||||
|
||||
private static String baseResources = "/org/libresonic/player/service/mediaScannerServiceTestCase/";
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
public void testStartup() throws Exception {
|
||||
String homeParent = TestCaseUtils.libresonicHomePathForTest();
|
||||
System.setProperty("libresonic.home", TestCaseUtils.libresonicHomePathForTest());
|
||||
TestCaseUtils.cleanLibresonicHomeForTest();
|
||||
File dbDirectory = new File(homeParent, "/db");
|
||||
FileUtils.forceMkdir(dbDirectory);
|
||||
org.libresonic.player.util.FileUtils.copyResourcesRecursively(getClass().getResource("/db/pre-liquibase/db"), new File(homeParent));
|
||||
|
||||
// load spring context
|
||||
ApplicationContext context = TestCaseUtils.loadSpringApplicationContext(baseResources);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package org.libresonic.player.util;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.JarURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.Enumeration;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
|
||||
public class FileUtils {
|
||||
public static boolean copyFile(final File toCopy, final File destFile) {
|
||||
try {
|
||||
return FileUtils.copyStream(new FileInputStream(toCopy), new FileOutputStream(destFile));
|
||||
} catch (final FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean copyFilesRecusively(
|
||||
final File toCopy, final File destDir
|
||||
) {
|
||||
assert destDir.isDirectory();
|
||||
|
||||
if (!toCopy.isDirectory()) {
|
||||
return FileUtils.copyFile(toCopy, new File(destDir, toCopy.getName()));
|
||||
} else {
|
||||
final File newDestDir = new File(destDir, toCopy.getName());
|
||||
if (!newDestDir.exists() && !newDestDir.mkdir()) {
|
||||
return false;
|
||||
}
|
||||
for (final File child : toCopy.listFiles()) {
|
||||
if (!FileUtils.copyFilesRecusively(child, newDestDir)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean copyJarResourcesRecursively(
|
||||
final File destDir, final JarURLConnection jarConnection
|
||||
) throws IOException {
|
||||
|
||||
final JarFile jarFile = jarConnection.getJarFile();
|
||||
|
||||
for (final Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements(); ) {
|
||||
final JarEntry entry = e.nextElement();
|
||||
if (entry.getName().startsWith(jarConnection.getEntryName())) {
|
||||
final String filename = StringUtils.removeStart(
|
||||
entry.getName(), //
|
||||
jarConnection.getEntryName());
|
||||
|
||||
final File f = new File(destDir, filename);
|
||||
if (!entry.isDirectory()) {
|
||||
final InputStream entryInputStream = jarFile.getInputStream(entry);
|
||||
if (!FileUtils.copyStream(entryInputStream, f)) {
|
||||
return false;
|
||||
}
|
||||
entryInputStream.close();
|
||||
} else {
|
||||
if (!FileUtils.ensureDirectoryExists(f)) {
|
||||
throw new IOException("Could not create directory: " + f.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean copyResourcesRecursively(final URL originUrl, final File destination) {
|
||||
try {
|
||||
final URLConnection urlConnection = originUrl.openConnection();
|
||||
if (urlConnection instanceof JarURLConnection) {
|
||||
return FileUtils.copyJarResourcesRecursively(destination, (JarURLConnection) urlConnection);
|
||||
} else {
|
||||
return FileUtils.copyFilesRecusively(new File(originUrl.getPath()), destination);
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean copyStream(final InputStream is, final File f) {
|
||||
try {
|
||||
return FileUtils.copyStream(is, new FileOutputStream(f));
|
||||
} catch (final FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean copyStream(final InputStream is, final OutputStream os) {
|
||||
try {
|
||||
final byte[] buf = new byte[1024];
|
||||
|
||||
int len = 0;
|
||||
while ((len = is.read(buf)) > 0) {
|
||||
os.write(buf, 0, len);
|
||||
}
|
||||
is.close();
|
||||
os.close();
|
||||
return true;
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean ensureDirectoryExists(final File f) {
|
||||
return f.exists() || f.mkdir();
|
||||
}
|
||||
}
|
||||
@@ -68,6 +68,8 @@ public class StringUtilTestCase extends TestCase {
|
||||
assertEquals("Error in formatBytes().", "1024 KB", StringUtil.formatBytes(1048575, locale));
|
||||
assertEquals("Error in formatBytes().", "1.2 MB", StringUtil.formatBytes(1238476, locale));
|
||||
assertEquals("Error in formatBytes().", "3.50 GB", StringUtil.formatBytes(3758096384L, locale));
|
||||
assertEquals("Error in formatBytes().", "410.00 TB", StringUtil.formatBytes(450799767388160L, locale));
|
||||
assertEquals("Error in formatBytes().", "4413.43 TB", StringUtil.formatBytes(4852617603375432L, locale));
|
||||
|
||||
locale = new Locale("no", "", "");
|
||||
assertEquals("Error in formatBytes().", "918 B", StringUtil.formatBytes(918, locale));
|
||||
@@ -77,6 +79,8 @@ public class StringUtilTestCase extends TestCase {
|
||||
assertEquals("Error in formatBytes().", "1024 KB", StringUtil.formatBytes(1048575, locale));
|
||||
assertEquals("Error in formatBytes().", "1,2 MB", StringUtil.formatBytes(1238476, locale));
|
||||
assertEquals("Error in formatBytes().", "3,50 GB", StringUtil.formatBytes(3758096384L, locale));
|
||||
assertEquals("Error in formatBytes().", "410,00 TB", StringUtil.formatBytes(450799767388160L, locale));
|
||||
assertEquals("Error in formatBytes().", "4413,43 TB", StringUtil.formatBytes(4852617603375432L, locale));
|
||||
}
|
||||
|
||||
public void testFormatDuration() {
|
||||
|
||||
Binary file not shown.
Binary file not shown.
File diff suppressed because one or more lines are too long
@@ -0,0 +1,17 @@
|
||||
#HSQL Database Engine 1.8.0.5
|
||||
#Sun Dec 18 21:11:59 MST 2016
|
||||
hsqldb.script_format=0
|
||||
runtime.gc_interval=0
|
||||
sql.enforce_strict_size=false
|
||||
hsqldb.cache_size_scale=8
|
||||
readonly=false
|
||||
hsqldb.nio_data_file=true
|
||||
hsqldb.cache_scale=14
|
||||
version=1.8.0
|
||||
hsqldb.default_table_type=memory
|
||||
hsqldb.cache_file_scale=1
|
||||
hsqldb.log_size=200
|
||||
modified=yes
|
||||
hsqldb.cache_version=1.7.0
|
||||
hsqldb.original_version=1.8.0
|
||||
hsqldb.compatible_version=1.8.0
|
||||
+34
-3
@@ -1,8 +1,8 @@
|
||||
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
|
||||
<!-- DAO's -->
|
||||
@@ -67,8 +67,36 @@
|
||||
<property name="daoHelper" ref="daoHelper"/>
|
||||
</bean>
|
||||
|
||||
<bean id="daoHelper" class="org.libresonic.player.dao.DaoHelperFactory" factory-method="create"/>
|
||||
<bean id="daoHelper" class="org.libresonic.player.dao.GenericDaoHelper">
|
||||
<constructor-arg name="dataSource" ref="dataSource" />
|
||||
</bean>
|
||||
|
||||
<bean id="rollbackFile" class="java.io.File">
|
||||
<constructor-arg type="java.io.File" index="0" value="#{T(org.libresonic.player.service.SettingsService).libresonicHome}" />
|
||||
<constructor-arg type="java.lang.String" index="1" value="rollback.sql" />
|
||||
</bean>
|
||||
|
||||
<bean id="dataSource"
|
||||
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
|
||||
|
||||
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
|
||||
<property name="url" value="jdbc:hsqldb:file:#{systemProperties['libresonic.home']}/db/libresonic" />
|
||||
<property name="username" value="sa" />
|
||||
<property name="password" value="" />
|
||||
</bean>
|
||||
|
||||
<bean id="liquibase" class="org.libresonic.player.spring.SpringLiquibase">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
<property name="changeLog" value="classpath:liquibase/db-changelog.xml" />
|
||||
<property name="rollbackFile" ref="rollbackFile" />
|
||||
<property name="changeLogParameters">
|
||||
<map>
|
||||
<entry key="defaultMusicFolder" value="#{T(org.libresonic.player.util.Util).getDefaultMusicFolder()}" />
|
||||
<entry key="varcharLimit" value="${database.varchar.maxlength:512}" />
|
||||
<entry key="userTableQuote" value="" />
|
||||
</map>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<!-- Services -->
|
||||
|
||||
@@ -87,12 +115,15 @@
|
||||
<property name="userCache" ref="userCache"/>
|
||||
</bean>
|
||||
|
||||
<bean id="configurationService" class="org.libresonic.player.service.ApacheCommonsConfigurationService" />
|
||||
|
||||
<bean id="settingsService" class="org.libresonic.player.service.SettingsService" init-method="init">
|
||||
<property name="internetRadioDao" ref="internetRadioDao"/>
|
||||
<property name="musicFolderDao" ref="musicFolderDao"/>
|
||||
<property name="userDao" ref="userDao"/>
|
||||
<property name="avatarDao" ref="avatarDao"/>
|
||||
<property name="versionService" ref="versionService"/>
|
||||
<property name="configurationService" ref="configurationService" />
|
||||
</bean>
|
||||
|
||||
<bean id="mediaScannerService" class="org.libresonic.player.service.MediaScannerService" init-method="initNoSchedule" depends-on="metaDataParserFactory">
|
||||
|
||||
Reference in New Issue
Block a user