ResourcePathTypeResolver implements TypeResolver { @Override public <T> Map<String, T> resolveAll(String resourcePath, String property, String keyProperty) { Map<String, T> resources = new HashMap<>(); final String path = getFullResourcePath(resourcePath); try { Stream.of(new PathMatchingResourcePatternResolver().getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + path + "/*")) .forEach(file -> { Optional<String> resourceName = Optional.ofNullable(file.getFilename()); if (!resourceName.isPresent()) { log.warn(String.format("Skip unsupported resource '%s' for resource lookup", file)); return; } if (property.equals(TYPE_PROPERTY_WILDCARD)) { Properties properties = readAsProperties(path + "/" + resourceName.get()); for (Map.Entry<Object, Object> prop : properties.entrySet()) { T resource = resolve(path + "/" + resourceName.get(), prop.getKey().toString()); resources.put(resourceName.get() + "." + prop.getKey().toString(), resource); } } else { T resource = resolve(path + "/" + resourceName.get(), property); if (keyProperty != null) { resources.put(resolveProperty(path + "/" + resourceName.get(), keyProperty), resource); } else { resources.put(resourceName.get(), resource); } } }); } catch (IOException e) { log.warn(String.format("Failed to resolve resources in '%s'", path), e); } return resources; } ResourcePathTypeResolver();  ResourcePathTypeResolver(String resourceBasePath); @Override String resolveProperty(String resourcePath, String property); @Override T resolve(String resourcePath, String property); @Override Map<String, T> resolveAll(String resourcePath, String property, String keyProperty);  }
@Test public void testResolveAll() { Map<String, Object> resolved = new ResourcePathTypeResolver().resolveAll("mocks"); Assert.assertEquals(resolved.size(), 2L); Assert.assertNotNull(resolved.get("foo")); Assert.assertEquals(resolved.get("foo").getClass(), Foo.class); Assert.assertNotNull(resolved.get("bar")); Assert.assertEquals(resolved.get("bar").getClass(), Bar.class); resolved = new ResourcePathTypeResolver("META-INF/mocks").resolveAll(); Assert.assertEquals(resolved.size(), 2L); Assert.assertNotNull(resolved.get("foo")); Assert.assertEquals(resolved.get("foo").getClass(), Foo.class); Assert.assertNotNull(resolved.get("bar")); Assert.assertEquals(resolved.get("bar").getClass(), Bar.class); resolved = new ResourcePathTypeResolver().resolveAll("mocks", TypeResolver.DEFAULT_TYPE_PROPERTY, "name"); Assert.assertEquals(resolved.size(), 2L); Assert.assertNotNull(resolved.get("fooMock")); Assert.assertEquals(resolved.get("fooMock").getClass(), Foo.class); Assert.assertNotNull(resolved.get("barMock")); Assert.assertEquals(resolved.get("barMock").getClass(), Bar.class); resolved = new ResourcePathTypeResolver().resolveAll("all", TypeResolver.TYPE_PROPERTY_WILDCARD); Assert.assertEquals(resolved.size(), 2L); Assert.assertNotNull(resolved.get("mocks.foo")); Assert.assertEquals(resolved.get("mocks.foo").getClass(), Foo.class); Assert.assertNotNull(resolved.get("mocks.bar")); Assert.assertEquals(resolved.get("mocks.bar").getClass(), Bar.class); }