Skip to content

Commit d6f0d42

Browse files
committed
5_6_profile_resolver
1 parent ab523b6 commit d6f0d42

4 files changed

Lines changed: 37 additions & 7 deletions

File tree

‎src/main/java/ru/javawebinar/topjava/Profiles.java‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ru.javawebinar.topjava;
22

3+
import org.springframework.util.ClassUtils;
4+
35
public class Profiles {
46
public static final String
57
JDBC = "jdbc",
@@ -11,5 +13,14 @@ public class Profiles {
1113
POSTGRES_DB = "postgres",
1214
HSQL_DB = "hsqldb";
1315

14-
public static final String ACTIVE_DB = POSTGRES_DB;
16+
// Get DB profile depending of DB driver in classpath
17+
public static String getActiveDbProfile() {
18+
if (ClassUtils.isPresent("org.postgresql.Driver", null)) {
19+
return POSTGRES_DB;
20+
} else if (ClassUtils.isPresent("org.hsqldb.jdbcDriver", null)) {
21+
return HSQL_DB;
22+
} else {
23+
throw new IllegalStateException("Could not find DB driver");
24+
}
25+
}
1526
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package ru.javawebinar.topjava;
2+
3+
import org.springframework.lang.NonNull;
4+
import org.springframework.test.context.support.DefaultActiveProfilesResolver;
5+
6+
import java.util.Arrays;
7+
8+
//http://stackoverflow.com/questions/23871255/spring-profiles-simple-example-of-activeprofilesresolver
9+
public class ActiveDbProfileResolver extends DefaultActiveProfilesResolver {
10+
@Override
11+
public @NonNull
12+
String[] resolve(@NonNull Class<?> aClass) {
13+
// https://stackoverflow.com/a/52438829/548473
14+
String[] activeProfiles = super.resolve(aClass);
15+
String[] activeProfilesWithDb = Arrays.copyOf(activeProfiles, activeProfiles.length + 1);
16+
activeProfilesWithDb[activeProfiles.length] = Profiles.getActiveDbProfile();
17+
return activeProfilesWithDb;
18+
}
19+
}

‎src/test/java/ru/javawebinar/topjava/service/MealServiceTest.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
import org.junit.runner.RunWith;
1010
import org.slf4j.Logger;
1111
import org.springframework.beans.factory.annotation.Autowired;
12-
import org.springframework.test.context.ActiveProfiles;
1312
import org.springframework.dao.DataAccessException;
13+
import org.springframework.test.context.ActiveProfiles;
1414
import org.springframework.test.context.ContextConfiguration;
1515
import org.springframework.test.context.jdbc.Sql;
1616
import org.springframework.test.context.jdbc.SqlConfig;
1717
import org.springframework.test.context.junit4.SpringRunner;
18-
import ru.javawebinar.topjava.Profiles;
18+
import ru.javawebinar.topjava.ActiveDbProfileResolver;
1919
import ru.javawebinar.topjava.model.Meal;
2020
import ru.javawebinar.topjava.util.exception.NotFoundException;
2121

@@ -35,7 +35,7 @@
3535
})
3636
@RunWith(SpringRunner.class)
3737
@Sql(scripts = "classpath:db/populateDB.sql", config = @SqlConfig(encoding = "UTF-8"))
38-
@ActiveProfiles(Profiles.ACTIVE_DB)
38+
@ActiveProfiles(resolver = ActiveDbProfileResolver.class)
3939
public class MealServiceTest {
4040
private static final Logger log = getLogger("result");
4141

@@ -121,7 +121,7 @@ public void update() {
121121

122122
@Test
123123
public void updateNotOwn() {
124-
NotFoundException exception = assertThrows(NotFoundException.class, () -> service.update(meal1, ADMIN_ID));
124+
NotFoundException exception = assertThrows(NotFoundException.class, () -> service.update(getUpdated(), ADMIN_ID));
125125
Assert.assertEquals("Not found entity with id=" + MEAL1_ID, exception.getMessage());
126126
MEAL_MATCHER.assertMatch(service.get(MEAL1_ID, USER_ID), meal1);
127127
}

‎src/test/java/ru/javawebinar/topjava/service/UserServiceTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import org.springframework.test.context.jdbc.Sql;
1010
import org.springframework.test.context.jdbc.SqlConfig;
1111
import org.springframework.test.context.junit4.SpringRunner;
12-
import ru.javawebinar.topjava.Profiles;
12+
import ru.javawebinar.topjava.ActiveDbProfileResolver;
1313
import ru.javawebinar.topjava.UserTestData;
1414
import ru.javawebinar.topjava.model.Role;
1515
import ru.javawebinar.topjava.model.User;
@@ -26,7 +26,7 @@
2626
})
2727
@RunWith(SpringRunner.class)
2828
@Sql(scripts = "classpath:db/populateDB.sql", config = @SqlConfig(encoding = "UTF-8"))
29-
@ActiveProfiles(Profiles.ACTIVE_DB)
29+
@ActiveProfiles(resolver = ActiveDbProfileResolver.class)
3030
public class UserServiceTest {
3131

3232
@Autowired

0 commit comments

Comments
 (0)