3030import java .nio .charset .StandardCharsets ;
3131import java .nio .file .Files ;
3232import java .nio .file .Path ;
33- import java .util .ArrayList ;
3433import java .util .Collection ;
3534import java .util .Collections ;
3635import java .util .HashMap ;
36+ import java .util .LinkedHashSet ;
3737import java .util .List ;
3838import java .util .Map ;
39+ import java .util .Set ;
3940import java .util .stream .Collectors ;
4041import java .util .stream .Stream ;
4142
@@ -65,7 +66,8 @@ public class WireMockContainer extends GenericContainer<WireMockContainer> {
6566 private final StringBuilder wireMockArgs ;
6667 private final Map <String , Stub > mappingStubs = new HashMap <>();
6768 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 <>();
6971 private boolean isBannerDisabled = true ;
7072
7173 /**
@@ -111,7 +113,7 @@ public WireMockContainer withBanner() {
111113 isBannerDisabled = false ;
112114 return this ;
113115 }
114-
116+
115117 /**
116118 * Adds CLI argument to the WireMock call.
117119 * @param arg Argument
@@ -170,51 +172,59 @@ public WireMockContainer withFileFromResource(String name, Class<?> resource, St
170172
171173 /**
172174 * 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.
174185 * @param classNames Class names of the extension to be included
175186 * @param jars JARs to be included into the container
176187 * @return this instance
177188 */
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 );
183192 return this ;
184193 }
185194
186195 /**
187196 * Add extension that will be loaded from the specified directory with JAR files.
188- * @param id Unique ID of the extension, for logging purposes
189197 * @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
191199 * @return this instance
192200 */
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
197208 .filter (p -> !Files .isDirectory (p ))
198209 .map (Path ::toFile )
199210 .filter (f -> f .toString ().endsWith (".jar" ))
200211 .collect (Collectors .toList ());
212+ return withExtension (classNames , jarsInTheDirectory );
213+
201214 } 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 );
203216 }
204-
205- return withExtension (id , classNames , jarsInTheDirectory );
206217 }
207218
208219 /**
209220 * Add extension that will be loaded from the classpath.
210221 * 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)}}
213223 * @param className Class name of the extension
214224 * @return this instance
215225 */
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 ());
218228 }
219229
220230 public String getBaseUrl () {
@@ -244,13 +254,8 @@ protected void configure() {
244254 withCopyToContainer (mount .getValue (), FILES_DIR + mount .getKey ());
245255 }
246256
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 ());
254259 }
255260 if (!extensionClassNames .isEmpty ()) {
256261 wireMockArgs .append (" --extensions " );
@@ -274,14 +279,4 @@ public Stub(String name, String json) {
274279 this .json = json ;
275280 }
276281 }
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- }
287282}
0 commit comments