@@ -65,7 +65,7 @@ public class WireMockContainer extends GenericContainer<WireMockContainer> {
6565 private final StringBuilder wireMockArgs ;
6666 private final Map <String , Stub > mappingStubs = new HashMap <>();
6767 private final Map <String , MountableFile > mappingFiles = new HashMap <>();
68- private final Map <String , Extension > extensions = new HashMap <>();
68+ private final Map <String , WireMockPlugin > plugins = new HashMap <>();
6969 private boolean isBannerDisabled = true ;
7070
7171 /**
@@ -268,28 +268,39 @@ public WireMockContainer withFileFromResource(Class<?> resource, String filename
268268 }
269269
270270 /**
271- * Add extension that will be loaded from the specified JAR file .
272- * @param id Unique ID of the extension, for logging purposes
271+ * Add extension that will be loaded from the specified JAR files .
272+ * In the internal engine, it will be handled as a single plugin.
273273 * @param classNames Class names of the extension to be included
274274 * @param jars JARs to be included into the container
275275 * @return this instance
276276 */
277- public WireMockContainer withExtension (String id , Collection <String > classNames , Collection <File > jars ) {
278- final Extension extension = new Extension (id );
279- extension .extensionClassNames .addAll (classNames );
280- extension .jars .addAll (jars );
281- extensions .put (id , extension );
282- return this ;
277+ public WireMockContainer withExtensions (Collection <String > classNames , Collection <File > jars ) {
278+ return withExtensions (WireMockPlugin .guessPluginId (classNames , jars ), classNames , jars );
279+ }
280+
281+ /**
282+ * Add extension that will be loaded from the specified JAR files.
283+ * In the internal engine, it will be handled as a single plugin.
284+ * @param id Identifier top use
285+ * @param classNames Class names of the extension to be included
286+ * @param jars JARs to be included into the container
287+ * @return this instance
288+ */
289+ public WireMockContainer withExtensions (String id , Collection <String > classNames , Collection <File > jars ) {
290+ final WireMockPlugin extension = new WireMockPlugin (id )
291+ .withExtensions (classNames )
292+ .withJars (jars );
293+ return withPlugin (extension );
283294 }
284295
285296 /**
286297 * Add extension that will be loaded from the specified directory with JAR files.
287- * @param id Unique ID of the extension, for logging purposes
298+ * In the internal engine, it will be handled as a single plugin.
288299 * @param classNames Class names of the extension to be included
289300 * @param jarDirectory Directory that stores all JARs
290301 * @return this instance
291302 */
292- public WireMockContainer withExtension ( String id , Collection <String > classNames , File jarDirectory ) {
303+ public WireMockContainer withExtensions ( Collection <String > classNames , File jarDirectory ) {
293304 final List <File > jarsInTheDirectory ;
294305 try (Stream <Path > walk = Files .walk (jarDirectory .toPath ())) {
295306 jarsInTheDirectory = walk
@@ -301,19 +312,28 @@ public WireMockContainer withExtension(String id, Collection<String> classNames,
301312 throw new IllegalArgumentException ("Cannot list JARs in the directory " + jarDirectory , e );
302313 }
303314
304- return withExtension ( id , classNames , jarsInTheDirectory );
315+ return withExtensions ( classNames , jarsInTheDirectory );
305316 }
306317
307318 /**
308319 * Add extension that will be loaded from the classpath.
309320 * This method can be used if the extension is a part of the WireMock bundle,
310- * or a Jar is already added via {@link #withExtension(String, Collection, Collection)}}
311- * @param id Unique ID of the extension, for logging purposes
321+ * or a Jar is already added via {@link #withExtensions( Collection, Collection)}}.
322+ * In the internal engine, it will be handled as a single plugin.
312323 * @param className Class name of the extension
313324 * @return this instance
314325 */
315- public WireMockContainer withExtension (String id , String className ) {
316- return withExtension (id , Collections .singleton (className ), Collections .emptyList ());
326+ public WireMockContainer withExtension (String className ) {
327+ return withExtensions (Collections .singleton (className ), Collections .emptyList ());
328+ }
329+
330+ private WireMockContainer withPlugin (WireMockPlugin plugin ) {
331+ String pluginId = plugin .getPluginId ();
332+ if (plugins .containsKey (pluginId )) {
333+ throw new IllegalArgumentException ("The plugin is already included: " + pluginId );
334+ }
335+ plugins .put (pluginId , plugin );
336+ return this ;
317337 }
318338
319339 public String getBaseUrl () {
@@ -344,10 +364,10 @@ protected void configure() {
344364 }
345365
346366 final ArrayList <String > extensionClassNames = new ArrayList <>();
347- for (Map .Entry <String , Extension > entry : extensions .entrySet ()) {
348- final Extension ext = entry .getValue ();
349- extensionClassNames .addAll (ext .extensionClassNames );
350- for (File jar : ext .jars ) {
367+ for (Map .Entry <String , WireMockPlugin > entry : plugins .entrySet ()) {
368+ final WireMockPlugin ext = entry .getValue ();
369+ extensionClassNames .addAll (ext .getExtensionClassNames () );
370+ for (File jar : ext .getJars () ) {
351371 withCopyToContainer (MountableFile .forHostPath (jar .toPath ()), EXTENSIONS_DIR + jar .getName ());
352372 }
353373 }
@@ -374,13 +394,5 @@ public Stub(String name, String json) {
374394 }
375395 }
376396
377- private static final class Extension {
378- final String id ;
379- final List <File > jars = new ArrayList <>();
380- final List <String > extensionClassNames = new ArrayList <>();
381397
382- public Extension (String id ) {
383- this .id = id ;
384- }
385- }
386398}
0 commit comments