-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPropertyMutationContextCustomizer.java
More file actions
69 lines (56 loc) · 2.35 KB
/
Copy pathPropertyMutationContextCustomizer.java
File metadata and controls
69 lines (56 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.jupitertools.springtestredis.customizer;
import com.jupitertools.springtestredis.RedisTestContainer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.MergedContextConfiguration;
import org.testcontainers.containers.GenericContainer;
import java.util.Objects;
import java.util.Set;
/**
* Created on 2019-05-23
* <p>
* The ContextCustomizer to make a different between context configurations
* of tests where used different container ports in {@link RedisTestContainer} annotation.
* In order to reload the spring context cache if it's necessary.
*
* @author Korovin Anatoliy
*/
public class PropertyMutationContextCustomizer implements ContextCustomizer {
private Set<RedisContainerDescription> descriptions;
private static final Integer REDIS_PORT = 6379;
private static final Log logger = LogFactory.getLog("RedisTestContainer");
public PropertyMutationContextCustomizer(Set<RedisContainerDescription> descriptions) {
this.descriptions = descriptions;
}
@Override
public void customizeContext(ConfigurableApplicationContext context,
MergedContextConfiguration mergedConfig) {
for (RedisContainerDescription description : descriptions) {
logger.info("Start REDIS TestContainer");
GenericContainer redis = new GenericContainer("redis:7.0.5-alpine").withExposedPorts(REDIS_PORT);
redis.withReuse(true);
redis.start();
System.setProperty(description.getHostPropertyName(),
redis.getHost());
System.setProperty(description.getPortPropertyName(),
redis.getMappedPort(REDIS_PORT).toString());
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
PropertyMutationContextCustomizer that = (PropertyMutationContextCustomizer) o;
return Objects.equals(descriptions, that.descriptions);
}
@Override
public int hashCode() {
return Objects.hash(descriptions);
}
}