|
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 |
|
@@ -81,7 +82,8 @@ public class WireMockContainer extends GenericContainer<WireMockContainer> { |
81 | 82 | private final StringBuilder wireMockArgs; |
82 | 83 | private final Map<String, Stub> mappingStubs = new HashMap<>(); |
83 | 84 | private final Map<String, MountableFile> mappingFiles = new HashMap<>(); |
84 | | - private final Map<String, WireMockPlugin> plugins = new HashMap<>(); |
| 85 | + private final Set<String> extensionClassNames = new LinkedHashSet<>(); |
| 86 | + private final Set<File> extensionJars = new LinkedHashSet<>(); |
85 | 87 | private boolean isBannerDisabled = true; |
86 | 88 |
|
87 | 89 | private File rootDir = new File("src/test/resources"); |
@@ -292,74 +294,74 @@ public WireMockContainer withFileFromResource(Class<?> resource, String filename |
292 | 294 | } |
293 | 295 |
|
294 | 296 | /** |
295 | | - * Add extension that will be loaded from the specified JAR files. |
296 | | - * In the internal engine, it will be handled as a single plugin. |
| 297 | + * Add an extension that will be loaded from the specified JAR file. |
297 | 298 | * @param classNames Class names of the extension to be included |
298 | | - * @param jars JARs to be included into the container |
| 299 | + * @param jar JAR to be included into the container |
299 | 300 | * @return this instance |
300 | 301 | */ |
301 | | - public WireMockContainer withExtensions(Collection<String> classNames, Collection<File> jars) { |
302 | | - return withExtensions(WireMockPlugin.guessPluginId(classNames, jars), classNames, jars); |
| 302 | + public WireMockContainer withExtension(Collection<String> classNames, File jar) { |
| 303 | + return withExtensions(classNames, Collections.singleton(jar)); |
| 304 | + } |
| 305 | + |
| 306 | + // TODO: Add actual usage for the extension name |
| 307 | + /** |
| 308 | + * Add an extension that will be loaded from the specified JAR file. |
| 309 | + * @param extensionName Name of the extension to be included |
| 310 | + * @param classNames Class names of the extension to be included |
| 311 | + * @param jars JAR to be included in the container |
| 312 | + * @return this instance |
| 313 | + */ |
| 314 | + public WireMockContainer withExtension(String extensionName, Collection<String> classNames, Collection<File> jars) { |
| 315 | + return withExtensions(classNames, jars); |
303 | 316 | } |
304 | 317 |
|
305 | 318 | /** |
306 | | - * Add extension that will be loaded from the specified JAR files. |
307 | | - * In the internal engine, it will be handled as a single plugin. |
308 | | - * @param id Identifier top use |
| 319 | + * Add extension that will be loaded from the specified JAR file. |
309 | 320 | * @param classNames Class names of the extension to be included |
310 | 321 | * @param jars JARs to be included into the container |
311 | 322 | * @return this instance |
312 | 323 | */ |
313 | | - public WireMockContainer withExtensions(String id, Collection<String> classNames, Collection<File> jars) { |
314 | | - final WireMockPlugin extension = new WireMockPlugin(id) |
315 | | - .withExtensions(classNames) |
316 | | - .withJars(jars); |
317 | | - return withPlugin(extension); |
| 324 | + public WireMockContainer withExtensions(Collection<String> classNames, Collection<File> jars) { |
| 325 | + extensionClassNames.addAll(classNames); |
| 326 | + extensionJars.addAll(jars); |
| 327 | + return this; |
318 | 328 | } |
319 | 329 |
|
320 | 330 | /** |
321 | | - * Add extension that will be loaded from the specified directory with JAR files. |
322 | | - * In the internal engine, it will be handled as a single plugin. |
| 331 | + * Add an extension or multiple extensions that will be loaded from the specified directory with JAR files. |
323 | 332 | * @param classNames Class names of the extension to be included |
324 | | - * @param jarDirectory Directory that stores all JARs |
| 333 | + * @param jarsDirectory Directory that stores all JARs |
325 | 334 | * @return this instance |
326 | 335 | */ |
327 | | - public WireMockContainer withExtensions(Collection<String> classNames, File jarDirectory) { |
328 | | - final List<File> jarsInTheDirectory; |
329 | | - try (Stream<Path> walk = Files.walk(jarDirectory.toPath())) { |
330 | | - jarsInTheDirectory = walk |
| 336 | + public WireMockContainer withExtensions(Collection<String> classNames, Path jarsDirectory) { |
| 337 | + if (!Files.isDirectory(jarsDirectory)) { |
| 338 | + throw new IllegalArgumentException("Path must refers to directory " + jarsDirectory); |
| 339 | + } |
| 340 | + try (Stream<Path> walk = Files.walk(jarsDirectory)) { |
| 341 | + |
| 342 | + final List<File> jarsInTheDirectory = walk |
331 | 343 | .filter(p -> !Files.isDirectory(p)) |
332 | 344 | .map(Path::toFile) |
333 | 345 | .filter(f -> f.toString().endsWith(".jar")) |
334 | 346 | .collect(Collectors.toList()); |
| 347 | + return withExtensions(classNames, jarsInTheDirectory); |
| 348 | + |
335 | 349 | } catch (IOException e) { |
336 | | - throw new IllegalArgumentException("Cannot list JARs in the directory " + jarDirectory, e); |
| 350 | + throw new IllegalArgumentException("Cannot list JARs in the directory " + jarsDirectory, e); |
337 | 351 | } |
338 | | - |
339 | | - return withExtensions(classNames, jarsInTheDirectory); |
340 | 352 | } |
341 | 353 |
|
342 | 354 | /** |
343 | 355 | * Add extension that will be loaded from the classpath. |
344 | 356 | * This method can be used if the extension is a part of the WireMock bundle, |
345 | | - * or a Jar is already added via {@link #withExtensions(Collection, Collection)}}. |
346 | | - * In the internal engine, it will be handled as a single plugin. |
| 357 | + * or a Jar is already added via {@link #withExtensions(Collection, Collection)}} |
347 | 358 | * @param className Class name of the extension |
348 | 359 | * @return this instance |
349 | 360 | */ |
350 | 361 | public WireMockContainer withExtension(String className) { |
351 | 362 | return withExtensions(Collections.singleton(className), Collections.emptyList()); |
352 | 363 | } |
353 | 364 |
|
354 | | - private WireMockContainer withPlugin(WireMockPlugin plugin) { |
355 | | - String pluginId = plugin.getPluginId(); |
356 | | - if (plugins.containsKey(pluginId)) { |
357 | | - throw new IllegalArgumentException("The plugin is already included: " + pluginId); |
358 | | - } |
359 | | - plugins.put(pluginId, plugin); |
360 | | - return this; |
361 | | - } |
362 | | - |
363 | 365 | public String getBaseUrl() { |
364 | 366 | return String.format("http://%s:%d", getHost(), getPort()); |
365 | 367 | } |
@@ -390,14 +392,10 @@ protected void configure() { |
390 | 392 | withCopyToContainer(mount.getValue(), CONTAINER_FILES_DIR + mount.getKey()); |
391 | 393 | } |
392 | 394 |
|
393 | | - final ArrayList<String> extensionClassNames = new ArrayList<>(); |
394 | | - for (Map.Entry<String, WireMockPlugin> entry : plugins.entrySet()) { |
395 | | - final WireMockPlugin ext = entry.getValue(); |
396 | | - extensionClassNames.addAll(ext.getExtensionClassNames()); |
397 | | - for (File jar : ext.getJars()) { |
398 | | - withCopyToContainer(MountableFile.forHostPath(jar.toPath()), EXTENSIONS_DIR + jar.getName()); |
399 | | - } |
| 395 | + for (File jar : extensionJars) { |
| 396 | + withCopyToContainer(MountableFile.forHostPath(jar.toPath()), EXTENSIONS_DIR + jar.getName()); |
400 | 397 | } |
| 398 | + |
401 | 399 | if (!extensionClassNames.isEmpty()) { |
402 | 400 | wireMockArgs.append(" --extensions "); |
403 | 401 | wireMockArgs.append(String.join(",", extensionClassNames)); |
|
0 commit comments