diff --git a/airsonic-main/src/main/java/org/airsonic/player/dao/AbstractDao.java b/airsonic-main/src/main/java/org/airsonic/player/dao/AbstractDao.java deleted file mode 100644 index 4a4f7eefa..000000000 --- a/airsonic-main/src/main/java/org/airsonic/player/dao/AbstractDao.java +++ /dev/null @@ -1,308 +0,0 @@ -/* - This file is part of Airsonic. - - Airsonic is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Airsonic is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Airsonic. If not, see . - - Copyright 2016 (C) Airsonic Authors - Based upon Subsonic, Copyright 2009 (C) Sindre Mehus - */ -package org.airsonic.player.dao; - -import com.google.common.base.CaseFormat; -import org.airsonic.player.util.LambdaUtils; -import org.apache.commons.lang.StringUtils; -import org.apache.commons.lang3.tuple.Pair; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.RowMapper; -import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; -import org.springframework.jdbc.core.simple.SimpleJdbcInsert; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; -import java.lang.reflect.Field; -import java.nio.file.Path; -import java.sql.Timestamp; -import java.time.Instant; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.concurrent.TimeUnit; -import java.util.function.Function; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -/** - * Abstract superclass for all DAO's. - * - * @author Sindre Mehus - */ -public class AbstractDao { - private static final Logger LOG = LoggerFactory.getLogger(AbstractDao.class); - - @Autowired - JdbcTemplate jdbcTemplate; - - @Autowired - NamedParameterJdbcTemplate namedParameterJdbcTemplate; - - public JdbcTemplate getJdbcTemplate() { - return jdbcTemplate; - } - - public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() { - return namedParameterJdbcTemplate; - } - - protected static String questionMarks(String columns) { - int numberOfColumns = StringUtils.countMatches(columns, ",") + 1; - return StringUtils.repeat("?", ", ", numberOfColumns); - } - - protected static String prefix(String columns, String prefix) { - List l = Arrays.asList(columns.split(", ")); - l.replaceAll(s -> prefix + "." + s); - return String.join(", ", l); - } - - protected static Object[] convertToDBTypes(Object[] args) { - return args == null ? null : Stream.of(args) - .map(AbstractDao::convertToDBType) - .collect(Collectors.toList()) - .toArray(); - } - - protected static Map convertToDBTypes(Map args) { - return args == null ? null : args.entrySet() - .stream() - .map(x -> Pair.of(x.getKey(), convertToDBType(x.getValue()))) - //can't use Collectors.toMap or Collectors.toConcurrentMap due to possible null value mappings - .collect(HashMap::new, (m, v) -> m.put(v.getKey(), v.getValue()), HashMap::putAll); - } - - protected static Object convertToDBType(Object x) { - if (x instanceof Instant) { - return Timestamp.from((Instant) x); - } - - if (x instanceof Enum) { - return ((Enum) x).name(); - } - - if (x instanceof Path) { - return ((Path) x).toString(); - } - - return x; - } - - protected int update(String sql, Object... args) { - long t = System.nanoTime(); - LOG.trace("Executing query: [{}]", sql); - int result = getJdbcTemplate().update(sql, convertToDBTypes(args)); - LOG.trace("Updated {} rows", result); - log(sql, t); - return result; - } - - protected int namedUpdate(String sql, Map args) { - long t = System.nanoTime(); - LOG.trace("Executing query: [{}]", sql); - int result = getNamedParameterJdbcTemplate().update(sql, convertToDBTypes(args)); - LOG.trace("Updated {} rows", result); - log(sql, t); - return result; - } - - protected int batchedUpdate(String sql, Collection batchArgs) { - long t = System.nanoTime(); - // used to get around postgres's wire limit when sending a large number of params - int batchSize = 30000 / batchArgs.stream().findAny().map(x -> x.length).orElse(1); - LOG.trace("Executing query: [{}]", sql); - int[][] result = getJdbcTemplate().batchUpdate(sql, - batchArgs.parallelStream().map(AbstractDao::convertToDBTypes).collect(Collectors.toList()), - batchSize, - (ps, args) -> { - for (int i = 0; i < args.length; i++) { - ps.setObject(i + 1, args[i]); - } - }); - int tally = Arrays.stream(result).flatMapToInt(Arrays::stream).sum(); - LOG.trace("Updated {} rows", tally); - log(sql, t); - return tally; - } - - private void log(String sql, long startTimeNano) { - long millis = (System.nanoTime() - startTimeNano) / 1000000L; - - // Log queries that take more than 2 seconds. - if (millis > TimeUnit.SECONDS.toMillis(2L)) { - LOG.debug(millis + " ms: " + sql); - } - } - - protected List query(String sql, RowMapper rowMapper, Object... args) { - long t = System.nanoTime(); - List result = getJdbcTemplate().query(sql, rowMapper, convertToDBTypes(args)); - log(sql, t); - return result; - } - - protected List namedQuery(String sql, RowMapper rowMapper, Map args) { - long t = System.nanoTime(); - List result = getNamedParameterJdbcTemplate().query(sql, convertToDBTypes(args), rowMapper); - log(sql, t); - return result; - } - - protected List queryForTypes(String sql, Class type, Object... args) { - long t = System.nanoTime(); - List result = getJdbcTemplate().queryForList(sql, type, convertToDBTypes(args)); - log(sql, t); - return result; - } - - protected List namedQueryForTypes(String sql, Class type, Map args) { - long t = System.nanoTime(); - List result = getNamedParameterJdbcTemplate().queryForList(sql, convertToDBTypes(args), type); - log(sql, t); - return result; - } - - public List queryForStrings(String sql, Object... args) { - return queryForTypes(sql, String.class, args); - } - - protected List queryForInts(String sql, Object... args) { - return queryForTypes(sql, Integer.class, args); - } - - protected List namedQueryForStrings(String sql, Map args) { - return namedQueryForTypes(sql, String.class, args); - } - - public Integer queryForInt(String sql, Integer defaultValue, Object... args) { - return queryForTypes(sql, Integer.class, args).stream().filter(Objects::nonNull).findFirst().orElse(defaultValue); - } - - protected Integer namedQueryForInt(String sql, Integer defaultValue, Map args) { - return namedQueryForTypes(sql, Integer.class, args).stream().filter(Objects::nonNull).findFirst().orElse(defaultValue); - } - - protected Instant queryForInstant(String sql, Instant defaultValue, Object... args) { - return queryForTypes(sql, Timestamp.class, args).stream().filter(Objects::nonNull).findFirst().map(x -> x.toInstant()).orElse(defaultValue); - } - - protected Long queryForLong(String sql, Long defaultValue, Object... args) { - return queryForTypes(sql, Long.class, args).stream().filter(Objects::nonNull).findFirst().orElse(defaultValue); - } - - protected Double queryForDouble(String sql, Double defaultValue, Object... args) { - return queryForTypes(sql, Double.class, args).stream().filter(Objects::nonNull).findFirst().orElse(defaultValue); - } - - protected T queryOne(String sql, RowMapper rowMapper, Object... args) { - List list = query(sql, rowMapper, args); - return list.isEmpty() ? null : list.get(0); - } - - protected T namedQueryOne(String sql, RowMapper rowMapper, Map args) { - List list = namedQuery(sql, rowMapper, args); - return list.isEmpty() ? null : list.get(0); - } - - private static Map insertTemplates = new HashMap<>(); - private static Map> methods = new HashMap<>(); - private static MethodHandles.Lookup lookup = MethodHandles.lookup(); - private static List> colNameTransforms = Arrays.asList(Function.identity(), - c -> CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, c), - c -> CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, c).toLowerCase()); - - protected void registerInserts(String table, String generatedKey, List columns, Class klazz) throws Exception { - var insert = new SimpleJdbcInsert(jdbcTemplate).withTableName(table); - if (generatedKey != null) { - insert.usingGeneratedKeyColumns(generatedKey); - } - insertTemplates.put(table, insert); - - // preprocess annotated fields - var fields = new HashMap(); - for (Field cf : klazz.getDeclaredFields()) { - fields.putIfAbsent(cf.getName(), cf); - Column annotation = cf.getAnnotation(Column.class); - if (annotation != null) { - fields.putIfAbsent(annotation.value(), cf); - } - } - - MethodHandles.Lookup privateLookup = MethodHandles.privateLookupIn(klazz, lookup); - methods.put(table, columns.parallelStream().map(LambdaUtils.uncheckFunction(c -> { - Field f = null; - var alreadyLooked = new HashSet(); - - for (Function colNameTransform : colNameTransforms) { - String lookup = colNameTransform.apply(c); - if (alreadyLooked.add(lookup)) { - f = fields.get(lookup); - - if (f != null) { - LOG.debug("Found suitable field {} (as {}) in class {} for table {} column {}", f.getName(), lookup, klazz.getName(), table, c); - break; - } - } - } - if (f == null) { - LOG.error("Could not locate a suitable field in class {} for table {} column {}", klazz.getName(), table, c); - } - return Pair.of(c, privateLookup.unreflectGetter(f)); - })).collect(Collectors.toConcurrentMap(Pair::getLeft, Pair::getRight))); - } - - protected static Integer insert(String table, Object obj) { - Map args = methods.get(table).entrySet() - .stream() - .map(e -> { - try { - return Pair.of(e.getKey(), convertToDBType(e.getValue().invoke(obj))); - } catch (Throwable x) { - throw new RuntimeException(x); - } - }) - //can't use Collectors.toMap or Collectors.toConcurrentMap due to possible null value mappings - .collect(HashMap::new, (m, v) -> m.put(v.getKey(), v.getValue()), HashMap::putAll); - var template = insertTemplates.get(table); - if (template.getGeneratedKeyNames() == null || template.getGeneratedKeyNames().length == 0) { - template.execute(args); - return null; - } else { - var keyHolder = insertTemplates.get(table).executeAndReturnKeyHolder(args); - return Optional.ofNullable(keyHolder).map(k -> k.getKey()).map(Number::intValue).orElse(null); - } - } - - @Retention(RetentionPolicy.RUNTIME) - public @interface Column { - public String value(); - } -} diff --git a/airsonic-main/src/main/java/org/airsonic/player/dao/DatabaseDao.java b/airsonic-main/src/main/java/org/airsonic/player/dao/DatabaseDao.java index 932369882..283bb97c7 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/dao/DatabaseDao.java +++ b/airsonic-main/src/main/java/org/airsonic/player/dao/DatabaseDao.java @@ -3,6 +3,8 @@ import org.airsonic.player.util.LambdaUtils.ThrowingBiFunction; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import java.nio.file.Path; @@ -10,9 +12,12 @@ import java.util.function.Consumer; @Repository -public class DatabaseDao extends AbstractDao { +public class DatabaseDao { private static final Logger LOG = LoggerFactory.getLogger(DatabaseDao.class); + @Autowired + private JdbcTemplate jdbcTemplate; + public boolean exportDB(Path tempWorkingDir, ThrowingBiFunction exportFunction) throws Exception { try (Connection con = jdbcTemplate.getDataSource().getConnection()) { return exportFunction.apply(tempWorkingDir, con); diff --git a/airsonic-main/src/test/java/org/airsonic/player/TestCaseUtils.java b/airsonic-main/src/test/java/org/airsonic/player/TestCaseUtils.java index f2249547b..3dac5dc28 100644 --- a/airsonic-main/src/test/java/org/airsonic/player/TestCaseUtils.java +++ b/airsonic-main/src/test/java/org/airsonic/player/TestCaseUtils.java @@ -3,11 +3,9 @@ import com.google.common.io.MoreFiles; import com.google.common.io.RecursiveDeleteOption; import org.airsonic.player.controller.JAXBWriter; -import org.airsonic.player.dao.AbstractDao; import org.airsonic.player.service.MediaScannerService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.boot.test.context.TestConfiguration; import java.io.IOException; import java.nio.file.Files; @@ -76,9 +74,4 @@ public static void execScan(MediaScannerService mediaScannerService) { waitForScanFinish(mediaScannerService); } - @TestConfiguration - public static class TestDao extends AbstractDao { - - } - } diff --git a/airsonic-main/src/test/java/org/airsonic/player/api/AirsonicRestApiIntTest.java b/airsonic-main/src/test/java/org/airsonic/player/api/AirsonicRestApiIntTest.java index 06460e815..cb27d1f6d 100644 --- a/airsonic-main/src/test/java/org/airsonic/player/api/AirsonicRestApiIntTest.java +++ b/airsonic-main/src/test/java/org/airsonic/player/api/AirsonicRestApiIntTest.java @@ -1,23 +1,25 @@ package org.airsonic.player.api; import org.airsonic.player.TestCaseUtils; -import org.airsonic.player.util.HomeRule; -import org.junit.ClassRule; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.servlet.MockMvc; +import java.nio.file.Path; + import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) @SpringBootTest @AutoConfigureMockMvc public class AirsonicRestApiIntTest { @@ -32,8 +34,13 @@ public class AirsonicRestApiIntTest { @Autowired private MockMvc mvc; - @ClassRule - public static final HomeRule classRule = new HomeRule(); // sets airsonic.home to a temporary dir + @TempDir + private static Path tempAirsonicHome; + + @BeforeAll + public static void beforeAll() { + System.setProperty("airsonic.home", tempAirsonicHome.toString()); + } @Test public void pingTest() throws Exception { diff --git a/airsonic-main/src/test/java/org/airsonic/player/api/CORSTest.java b/airsonic-main/src/test/java/org/airsonic/player/api/CORSTest.java index 27d29de15..99fe1dcbb 100644 --- a/airsonic-main/src/test/java/org/airsonic/player/api/CORSTest.java +++ b/airsonic-main/src/test/java/org/airsonic/player/api/CORSTest.java @@ -1,20 +1,22 @@ package org.airsonic.player.api; import org.airsonic.player.TestCaseUtils; -import org.airsonic.player.util.HomeRule; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; +import java.nio.file.Path; + import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.options; @@ -22,7 +24,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class CORSTest { @@ -37,7 +39,15 @@ public class CORSTest { private MockMvc mvc; - @Before + @TempDir + private static Path tempAirsonicHome; + + @BeforeAll + public static void beforeAll() { + System.setProperty("airsonic.home", tempAirsonicHome.toString()); + } + + @BeforeEach public void setup() { AIRSONIC_API_VERSION = TestCaseUtils.restApiVersion(); mvc = MockMvcBuilders @@ -48,9 +58,6 @@ public void setup() { .build(); } - @ClassRule - public static final HomeRule classRule = new HomeRule(); - @Test public void corsHeadersShouldBeAddedToSuccessResponses() throws Exception { mvc.perform(get("/rest/ping") diff --git a/airsonic-main/src/test/java/org/airsonic/player/io/PipeStreamsTest.java b/airsonic-main/src/test/java/org/airsonic/player/io/PipeStreamsTest.java index 3ef09a81d..5cfe1d949 100644 --- a/airsonic-main/src/test/java/org/airsonic/player/io/PipeStreamsTest.java +++ b/airsonic-main/src/test/java/org/airsonic/player/io/PipeStreamsTest.java @@ -8,7 +8,7 @@ import org.airsonic.player.io.PipeStreams.PipedInputStream; import org.airsonic.player.io.PipeStreams.PipedOutputStream; import org.airsonic.player.util.FileUtil; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; @@ -20,7 +20,7 @@ import java.util.Set; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.fail; public class PipeStreamsTest { diff --git a/airsonic-main/src/test/java/org/airsonic/player/service/BookmarkServiceTest.java b/airsonic-main/src/test/java/org/airsonic/player/service/BookmarkServiceTest.java index 1c8b558c5..74ea89bc1 100644 --- a/airsonic-main/src/test/java/org/airsonic/player/service/BookmarkServiceTest.java +++ b/airsonic-main/src/test/java/org/airsonic/player/service/BookmarkServiceTest.java @@ -22,26 +22,28 @@ import org.airsonic.player.domain.Bookmark; import org.airsonic.player.domain.MediaFile; import org.airsonic.player.repository.BookmarkRepository; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.messaging.simp.SimpMessagingTemplate; -import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; import java.util.List; import java.util.Optional; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.*; -@RunWith(SpringRunner.class) +@ExtendWith(MockitoExtension.class) public class BookmarkServiceTest { + @InjectMocks private BookmarkService bookmarkService; @Mock @@ -55,9 +57,8 @@ public class BookmarkServiceTest { private Bookmark bookmark; - @Before + @BeforeEach public void setUp() throws Exception { - bookmarkService = new BookmarkService(bookmarkRepository, mediaFileService, simpMessagingTemplate); bookmark = new Bookmark(1, 2, 5000L, "testUser", "test comment", null, null); } @@ -75,7 +76,7 @@ public void testSetBookmark() { mediaFile.setDuration((double) 10000); when(mediaFileService.getMediaFile(2)).thenReturn(mediaFile); when(bookmarkRepository.findOptByUsernameAndMediaFileId("testUser", 2)).thenReturn(Optional.empty()); - when(bookmarkRepository.saveAndFlush(any(Bookmark.class))).thenReturn(bookmark); + when(bookmarkRepository.save(any(Bookmark.class))).thenReturn(bookmark); boolean result = bookmarkService.setBookmark("testUser", 2, 5000L, "test comment"); assertTrue(result); @@ -97,7 +98,7 @@ public void testSetBookmarkReturnsFalseIfMediaFileIsNull() { // Assert assertFalse(result); verify(bookmarkRepository, never()).findOptByUsernameAndMediaFileId(anyString(), anyInt()); - verify(bookmarkRepository, never()).saveAndFlush(any()); + verify(bookmarkRepository, never()).save(any()); verify(simpMessagingTemplate, never()).convertAndSendToUser(anyString(), anyString(), any()); } @@ -113,7 +114,6 @@ public void testSetBookmarkReturnsFalseIfNotStarted() { mediaFile.setDuration((double)durationSeconds); when(mediaFileService.getMediaFile(anyInt())).thenReturn(mediaFile); - when(bookmarkRepository.findOptByUsernameAndMediaFileId(anyString(), anyInt())).thenReturn(Optional.empty()); // Act boolean result = bookmarkService.setBookmark(username, mediaFileId, 4999L, comment); @@ -121,7 +121,7 @@ public void testSetBookmarkReturnsFalseIfNotStarted() { // Assert assertFalse(result); verify(bookmarkRepository, never()).findOptByUsernameAndMediaFileId(anyString(), anyInt()); - verify(bookmarkRepository, never()).saveAndFlush(any()); + verify(bookmarkRepository, never()).save(any()); verify(simpMessagingTemplate, never()).convertAndSendToUser(anyString(), anyString(), anyInt()); verify(bookmarkRepository, never()).deleteByUsernameAndMediaFileId(anyString(), anyInt()); } @@ -140,7 +140,6 @@ public void testSetBookmarkReturnsFalseIfCloseToEnd() { mediaFile.setDuration((double)durationSeconds); when(mediaFileService.getMediaFile(anyInt())).thenReturn(mediaFile); - when(bookmarkRepository.findOptByUsernameAndMediaFileId(anyString(), anyInt())).thenReturn(Optional.empty()); // Act boolean result = bookmarkService.setBookmark(username, mediaFileId, durationSeconds * 1000L - 50000L, comment); @@ -148,7 +147,7 @@ public void testSetBookmarkReturnsFalseIfCloseToEnd() { // Assert assertFalse(result); verify(bookmarkRepository, never()).findOptByUsernameAndMediaFileId(eq(username), eq(mediaFileId)); - verify(bookmarkRepository, never()).saveAndFlush(any()); + verify(bookmarkRepository, never()).save(any()); verify(simpMessagingTemplate, times(1)).convertAndSendToUser(anyString(), anyString(), anyInt()); verify(bookmarkRepository, times(1)).deleteByUsernameAndMediaFileId(eq(username), eq(mediaFileId)); } diff --git a/airsonic-main/src/test/java/org/airsonic/player/service/LegacyDatabaseStartupTestCase.java b/airsonic-main/src/test/java/org/airsonic/player/service/LegacyDatabaseStartupTestCase.java index fab20361e..c1b839a6c 100644 --- a/airsonic-main/src/test/java/org/airsonic/player/service/LegacyDatabaseStartupTestCase.java +++ b/airsonic-main/src/test/java/org/airsonic/player/service/LegacyDatabaseStartupTestCase.java @@ -1,43 +1,39 @@ package org.airsonic.player.service; -import org.airsonic.player.TestCaseUtils; import org.airsonic.player.config.AirsonicHomeConfig; import org.airsonic.player.util.EmbeddedTestCategory; import org.airsonic.player.util.FileUtils; -import org.airsonic.player.util.HomeRule; -import org.junit.BeforeClass; -import org.junit.ClassRule; -import org.junit.Test; import org.junit.experimental.categories.Category; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; import javax.sql.DataSource; import java.io.IOException; import java.net.URISyntaxException; import java.nio.file.Path; -import java.nio.file.Paths; import static org.assertj.core.api.Assertions.assertThat; @Category(EmbeddedTestCategory.class) -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) @SpringBootTest public class LegacyDatabaseStartupTestCase { - @ClassRule - public static final HomeRule airsonicRule = new HomeRule(); + @TempDir + private static Path tempAirsonicHome; - @BeforeClass + @BeforeAll public static void setupOnce() throws IOException, URISyntaxException { - String homeParent = TestCaseUtils.airsonicHomePathForTest(); - Path dbDirectory = Paths.get(homeParent, "db"); + System.setProperty("airsonic.home", tempAirsonicHome.toString()); + Path dbDirectory = tempAirsonicHome.resolve("db"); FileUtils.copyRecursively(LegacyDatabaseStartupTestCase.class.getResource("/db/pre-liquibase/db"), dbDirectory); - // have to change the url here because old db files are libresonic - AirsonicHomeConfig config = new AirsonicHomeConfig(homeParent, null); + AirsonicHomeConfig config = new AirsonicHomeConfig(tempAirsonicHome.toString(), null); System.setProperty(SettingsService.KEY_DATABASE_URL, config.getDefaultJDBCUrl().replaceAll("airsonic;", "libresonic;")); System.setProperty(SettingsService.KEY_DATABASE_USERNAME, "sa"); diff --git a/airsonic-main/src/test/java/org/airsonic/player/util/UtilTest.java b/airsonic-main/src/test/java/org/airsonic/player/util/UtilTest.java index 7f3358531..e3ba3c92a 100644 --- a/airsonic-main/src/test/java/org/airsonic/player/util/UtilTest.java +++ b/airsonic-main/src/test/java/org/airsonic/player/util/UtilTest.java @@ -2,13 +2,13 @@ import org.airsonic.player.domain.MediaLibraryStatistics; import org.apache.commons.lang3.StringUtils; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.time.Instant; import java.util.HashMap; import java.util.Map; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class UtilTest { diff --git a/airsonic-main/src/test/java/org/airsonic/player/validator/CredentialsManagementValidationTest.java b/airsonic-main/src/test/java/org/airsonic/player/validator/CredentialsManagementValidationTest.java index 51118b7f3..52d26a11f 100644 --- a/airsonic-main/src/test/java/org/airsonic/player/validator/CredentialsManagementValidationTest.java +++ b/airsonic-main/src/test/java/org/airsonic/player/validator/CredentialsManagementValidationTest.java @@ -5,7 +5,7 @@ import org.airsonic.player.domain.UserCredential.App; import org.airsonic.player.validator.CredentialsManagementValidators.CredentialCreateChecks; import org.airsonic.player.validator.CredentialsManagementValidators.CredentialUpdateChecks; -import org.junit.Test; +import org.junit.jupiter.api.Test; import javax.validation.Validation; import javax.validation.Validator; diff --git a/integration-test/src/test/java/org/airsonic/test/PingIT.java b/integration-test/src/test/java/org/airsonic/test/PingIT.java index 44cd6bcf4..97574a5af 100644 --- a/integration-test/src/test/java/org/airsonic/test/PingIT.java +++ b/integration-test/src/test/java/org/airsonic/test/PingIT.java @@ -1,6 +1,6 @@ package org.airsonic.test; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.util.UriComponentsBuilder; diff --git a/integration-test/src/test/java/org/airsonic/test/StreamIT.java b/integration-test/src/test/java/org/airsonic/test/StreamIT.java index ce2c1bc53..327a8a1d4 100644 --- a/integration-test/src/test/java/org/airsonic/test/StreamIT.java +++ b/integration-test/src/test/java/org/airsonic/test/StreamIT.java @@ -2,7 +2,7 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.nio.file.Paths;