30
30
import java .nio .charset .StandardCharsets ;
31
31
import java .nio .file .Files ;
32
32
import java .nio .file .Path ;
33
- import java .util .ArrayList ;
34
33
import java .util .Collection ;
35
34
import java .util .Collections ;
36
35
import java .util .HashMap ;
36
+ import java .util .LinkedHashSet ;
37
37
import java .util .List ;
38
38
import java .util .Map ;
39
+ import java .util .Set ;
39
40
import java .util .stream .Collectors ;
40
41
import java .util .stream .Stream ;
41
42
@@ -65,7 +66,8 @@ public class WireMockContainer extends GenericContainer<WireMockContainer> {
65
66
private final StringBuilder wireMockArgs ;
66
67
private final Map <String , Stub > mappingStubs = new HashMap <>();
67
68
private final Map <String , MountableFile > mappingFiles = new HashMap <>();
68
- private final Map <String , Extension > extensions = new HashMap <>();
69
+ private final Set <String > extensionClassNames = new LinkedHashSet <>();
70
+ private final Set <File > extensionJars = new LinkedHashSet <>();
69
71
private boolean isBannerDisabled = true ;
70
72
71
73
/**
@@ -111,7 +113,7 @@ public WireMockContainer withBanner() {
111
113
isBannerDisabled = false ;
112
114
return this ;
113
115
}
114
-
116
+
115
117
/**
116
118
* Adds CLI argument to the WireMock call.
117
119
* @param arg Argument
@@ -170,51 +172,59 @@ public WireMockContainer withFileFromResource(String name, Class<?> resource, St
170
172
171
173
/**
172
174
* Add extension that will be loaded from the specified JAR file.
173
- * @param id Unique ID of the extension, for logging purposes
175
+ * @param classNames Class names of the extension to be included
176
+ * @param jar JAR to be included into the container
177
+ * @return this instance
178
+ */
179
+ public WireMockContainer withExtension (Collection <String > classNames , File jar ) {
180
+ return withExtension (classNames , Collections .singleton (jar ));
181
+ }
182
+
183
+ /**
184
+ * Add extension that will be loaded from the specified JAR file.
174
185
* @param classNames Class names of the extension to be included
175
186
* @param jars JARs to be included into the container
176
187
* @return this instance
177
188
*/
178
- public WireMockContainer withExtension (String id , Collection <String > classNames , Collection <File > jars ) {
179
- final Extension extension = new Extension (id );
180
- extension .extensionClassNames .addAll (classNames );
181
- extension .jars .addAll (jars );
182
- extensions .put (id , extension );
189
+ public WireMockContainer withExtension (Collection <String > classNames , Collection <File > jars ) {
190
+ extensionClassNames .addAll (classNames );
191
+ extensionJars .addAll (jars );
183
192
return this ;
184
193
}
185
194
186
195
/**
187
196
* Add extension that will be loaded from the specified directory with JAR files.
188
- * @param id Unique ID of the extension, for logging purposes
189
197
* @param classNames Class names of the extension to be included
190
- * @param jarDirectory Directory that stores all JARs
198
+ * @param jarsDirectory Directory that stores all JARs
191
199
* @return this instance
192
200
*/
193
- public WireMockContainer withExtension (String id , Collection <String > classNames , File jarDirectory ) {
194
- final List <File > jarsInTheDirectory ;
195
- try (Stream <Path > walk = Files .walk (jarDirectory .toPath ())) {
196
- jarsInTheDirectory = walk
201
+ public WireMockContainer withExtension (Collection <String > classNames , Path jarsDirectory ) {
202
+ if (!Files .isDirectory (jarsDirectory )) {
203
+ throw new IllegalArgumentException ("Path must refers to directory " + jarsDirectory );
204
+ }
205
+ try (Stream <Path > walk = Files .walk (jarsDirectory )) {
206
+
207
+ final List <File > jarsInTheDirectory = walk
197
208
.filter (p -> !Files .isDirectory (p ))
198
209
.map (Path ::toFile )
199
210
.filter (f -> f .toString ().endsWith (".jar" ))
200
211
.collect (Collectors .toList ());
212
+ return withExtension (classNames , jarsInTheDirectory );
213
+
201
214
} catch (IOException e ) {
202
- throw new IllegalArgumentException ("Cannot list JARs in the directory " + jarDirectory , e );
215
+ throw new IllegalArgumentException ("Cannot list JARs in the directory " + jarsDirectory , e );
203
216
}
204
-
205
- return withExtension (id , classNames , jarsInTheDirectory );
206
217
}
207
218
208
219
/**
209
220
* Add extension that will be loaded from the classpath.
210
221
* This method can be used if the extension is a part of the WireMock bundle,
211
- * or a Jar is already added via {@link #withExtension(String, Collection, Collection)}}
212
- * @param id Unique ID of the extension, for logging purposes
222
+ * or a Jar is already added via {@link #withExtension(Collection, Collection)}}
213
223
* @param className Class name of the extension
214
224
* @return this instance
215
225
*/
216
- public WireMockContainer withExtension (String id , String className ) {
217
- return withExtension (id , Collections .singleton (className ), Collections .emptyList ());
226
+ public WireMockContainer withExtension (String className ) {
227
+ return withExtension (Collections .singleton (className ), Collections .emptyList ());
218
228
}
219
229
220
230
public String getBaseUrl () {
@@ -244,13 +254,8 @@ protected void configure() {
244
254
withCopyToContainer (mount .getValue (), FILES_DIR + mount .getKey ());
245
255
}
246
256
247
- final ArrayList <String > extensionClassNames = new ArrayList <>();
248
- for (Map .Entry <String , Extension > entry : extensions .entrySet ()) {
249
- final Extension ext = entry .getValue ();
250
- extensionClassNames .addAll (ext .extensionClassNames );
251
- for (File jar : ext .jars ) {
252
- withCopyToContainer (MountableFile .forHostPath (jar .toPath ()), EXTENSIONS_DIR + jar .getName ());
253
- }
257
+ for (File jar : extensionJars ) {
258
+ withCopyToContainer (MountableFile .forHostPath (jar .toPath ()), EXTENSIONS_DIR + jar .getName ());
254
259
}
255
260
if (!extensionClassNames .isEmpty ()) {
256
261
wireMockArgs .append (" --extensions " );
@@ -274,14 +279,4 @@ public Stub(String name, String json) {
274
279
this .json = json ;
275
280
}
276
281
}
277
-
278
- private static final class Extension {
279
- final String id ;
280
- final List <File > jars = new ArrayList <>();
281
- final List <String > extensionClassNames = new ArrayList <>();
282
-
283
- public Extension (String id ) {
284
- this .id = id ;
285
- }
286
- }
287
282
}
0 commit comments