5
5
6
6
import java .io .*;
7
7
import java .net .MalformedURLException ;
8
+ import java .net .URL ;
8
9
import java .util .Objects ;
9
10
import java .util .Properties ;
10
11
import java .util .stream .Stream ;
@@ -19,12 +20,22 @@ public class TestcontainersConfiguration {
19
20
20
21
private static String PROPERTIES_FILE_NAME = "testcontainers.properties" ;
21
22
22
- private static File GLOBAL_CONFIG_FILE = new File (System .getProperty ("user.home" ), "." + PROPERTIES_FILE_NAME );
23
+ private static File ENVIRONMENT_CONFIG_FILE = new File (System .getProperty ("user.home" ), "." + PROPERTIES_FILE_NAME );
23
24
24
25
@ Getter (lazy = true )
25
26
private static final TestcontainersConfiguration instance = loadConfiguration ();
26
27
27
- private final Properties properties ;
28
+ @ Getter (AccessLevel .NONE )
29
+ private final Properties environmentProperties ;
30
+
31
+ private final Properties properties = new Properties ();
32
+
33
+ TestcontainersConfiguration (Properties environmentProperties , Properties classpathProperties ) {
34
+ this .environmentProperties = environmentProperties ;
35
+
36
+ this .properties .putAll (classpathProperties );
37
+ this .properties .putAll (environmentProperties );
38
+ }
28
39
29
40
public String getAmbassadorContainerImage () {
30
41
return (String ) properties .getOrDefault ("ambassador.container.image" , "richnorth/ambassador:latest" );
@@ -71,13 +82,18 @@ public String getPulsarImage() {
71
82
}
72
83
73
84
public boolean isDisableChecks () {
74
- return Boolean .parseBoolean ((String ) properties .getOrDefault ("checks.disable" , "false" ));
85
+ return Boolean .parseBoolean ((String ) environmentProperties .getOrDefault ("checks.disable" , "false" ));
75
86
}
76
87
77
88
public String getDockerClientStrategyClassName () {
78
- return (String ) properties .get ("docker.client.strategy" );
89
+ return (String ) environmentProperties .get ("docker.client.strategy" );
79
90
}
80
91
92
+ /**
93
+ *
94
+ * @deprecated we no longer have different transport types
95
+ */
96
+ @ Deprecated
81
97
public String getTransportType () {
82
98
return properties .getProperty ("transport.type" , "okhttp" );
83
99
}
@@ -89,64 +105,55 @@ public Integer getImagePullPauseTimeout() {
89
105
@ Synchronized
90
106
public boolean updateGlobalConfig (@ NonNull String prop , @ NonNull String value ) {
91
107
try {
92
- Properties globalProperties = new Properties ();
93
- GLOBAL_CONFIG_FILE .createNewFile ();
94
- try (InputStream inputStream = new FileInputStream (GLOBAL_CONFIG_FILE )) {
95
- globalProperties .load (inputStream );
96
- }
97
-
98
- if (value .equals (globalProperties .get (prop ))) {
108
+ if (value .equals (environmentProperties .get (prop ))) {
99
109
return false ;
100
110
}
101
111
102
- globalProperties .setProperty (prop , value );
112
+ environmentProperties .setProperty (prop , value );
103
113
104
- try (OutputStream outputStream = new FileOutputStream (GLOBAL_CONFIG_FILE )) {
105
- globalProperties .store (outputStream , "Modified by Testcontainers" );
114
+ ENVIRONMENT_CONFIG_FILE .createNewFile ();
115
+ try (OutputStream outputStream = new FileOutputStream (ENVIRONMENT_CONFIG_FILE )) {
116
+ environmentProperties .store (outputStream , "Modified by Testcontainers" );
106
117
}
107
118
108
- // Update internal state only if global config was successfully updated
119
+ // Update internal state only if environment config was successfully updated
109
120
properties .setProperty (prop , value );
110
121
return true ;
111
122
} catch (Exception e ) {
112
- log .debug ("Can't store global property {} in {}" , prop , GLOBAL_CONFIG_FILE );
123
+ log .debug ("Can't store environment property {} in {}" , prop , ENVIRONMENT_CONFIG_FILE );
113
124
return false ;
114
125
}
115
126
}
116
127
117
128
@ SneakyThrows (MalformedURLException .class )
118
129
private static TestcontainersConfiguration loadConfiguration () {
119
- final TestcontainersConfiguration config = new TestcontainersConfiguration (
120
- Stream
121
- .of (
122
- TestcontainersConfiguration .class .getClassLoader ().getResource (PROPERTIES_FILE_NAME ),
123
- Thread .currentThread ().getContextClassLoader ().getResource (PROPERTIES_FILE_NAME ),
124
- GLOBAL_CONFIG_FILE .toURI ().toURL ()
125
- )
126
- .filter (Objects ::nonNull )
127
- .map (it -> {
128
- log .debug ("Testcontainers configuration overrides will be loaded from {}" , it );
129
-
130
- final Properties subProperties = new Properties ();
131
- try (final InputStream inputStream = it .openStream ()) {
132
- subProperties .load (inputStream );
133
- } catch (FileNotFoundException e ) {
134
- log .trace ("Testcontainers config override was found on " + it + " but the file was not found" , e );
135
- } catch (IOException e ) {
136
- log .warn ("Testcontainers config override was found on " + it + " but could not be loaded" , e );
137
- }
138
- return subProperties ;
139
- })
140
- .reduce (new Properties (), (a , b ) -> {
141
- a .putAll (b );
142
- return a ;
143
- })
130
+ return new TestcontainersConfiguration (
131
+ readProperties (ENVIRONMENT_CONFIG_FILE .toURI ().toURL ()),
132
+ Stream
133
+ .of (
134
+ TestcontainersConfiguration .class .getClassLoader (),
135
+ Thread .currentThread ().getContextClassLoader ()
136
+ )
137
+ .map (it -> it .getResource (PROPERTIES_FILE_NAME ))
138
+ .filter (Objects ::nonNull )
139
+ .map (TestcontainersConfiguration ::readProperties )
140
+ .reduce (new Properties (), (a , b ) -> {
141
+ a .putAll (b );
142
+ return a ;
143
+ })
144
144
);
145
+ }
145
146
146
- if (!config .getProperties ().isEmpty ()) {
147
- log .debug ("Testcontainers configuration overrides loaded from {}" , config );
147
+ private static Properties readProperties (URL url ) {
148
+ log .debug ("Testcontainers configuration overrides will be loaded from {}" , url );
149
+ Properties properties = new Properties ();
150
+ try (InputStream inputStream = url .openStream ()) {
151
+ properties .load (inputStream );
152
+ } catch (FileNotFoundException e ) {
153
+ log .trace ("Testcontainers config override was found on {} but the file was not found" , url , e );
154
+ } catch (IOException e ) {
155
+ log .warn ("Testcontainers config override was found on {} but could not be loaded" , url , e );
148
156
}
149
-
150
- return config ;
157
+ return properties ;
151
158
}
152
159
}
0 commit comments