diff --git a/hawkbit-repository/hawkbit-repository-api/pom.xml b/hawkbit-repository/hawkbit-repository-api/pom.xml index 0a3f541d9..ce9b47a3d 100644 --- a/hawkbit-repository/hawkbit-repository-api/pom.xml +++ b/hawkbit-repository/hawkbit-repository-api/pom.xml @@ -1,13 +1,5 @@ - + 4.0.0 @@ -40,7 +32,7 @@ org.springframework.hateoas spring-hateoas - + org.springframework.boot @@ -64,5 +56,10 @@ fest-assert test + + com.google.guava + guava + test + \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-api/src/test/java/org/eclipse/hawkbit/repository/RepositoryManagementMethodPreAuthorizeAnnotatedTest.java b/hawkbit-repository/hawkbit-repository-api/src/test/java/org/eclipse/hawkbit/repository/RepositoryManagementMethodPreAuthorizeAnnotatedTest.java index a827b36e7..1a0d85d30 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/test/java/org/eclipse/hawkbit/repository/RepositoryManagementMethodPreAuthorizeAnnotatedTest.java +++ b/hawkbit-repository/hawkbit-repository-api/src/test/java/org/eclipse/hawkbit/repository/RepositoryManagementMethodPreAuthorizeAnnotatedTest.java @@ -10,24 +10,21 @@ package org.eclipse.hawkbit.repository; import static org.fest.assertions.Assertions.assertThat; -import java.io.File; -import java.io.FileFilter; import java.io.IOException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.net.URI; import java.net.URISyntaxException; -import java.net.URL; -import java.util.ArrayList; -import java.util.Enumeration; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.regex.Pattern; +import java.util.stream.Collectors; import org.junit.Test; import org.springframework.security.access.prepost.PreAuthorize; +import com.google.common.reflect.ClassPath; + import ru.yandex.qatools.allure.annotations.Description; import ru.yandex.qatools.allure.annotations.Features; import ru.yandex.qatools.allure.annotations.Stories; @@ -83,38 +80,11 @@ public class RepositoryManagementMethodPreAuthorizeAnnotatedTest { } } - /** - * Finds all interfaces in a given packages which matches the given filter. - * - * @param p - * the package to search for interfaces in - * @param includeFilter - * the pattern which interfaces class names should be included - * @return a list of loaded interfaces in a specific package and matches the - * given filter - * @throws URISyntaxException - * @throws IOException - * @throws ClassNotFoundException - */ private List> findInterfacesInPackage(final Package p, final Pattern includeFilter) throws URISyntaxException, IOException, ClassNotFoundException { - final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - final Enumeration resources = classLoader.getResources(p.getName().replace(".", "/")); - final RegexIncludeInterfaceFileCollector regexIncludeInterfaceFileCollector = new RegexIncludeInterfaceFileCollector( - p, includeFilter); - while (resources.hasMoreElements()) { - listFilesInPackage(resources.nextElement(), regexIncludeInterfaceFileCollector); - } - return regexIncludeInterfaceFileCollector.getInterfaceClasses(); - } - - private void listFilesInPackage(final URL resource, final RegexIncludeInterfaceFileCollector clazzCollector) - throws URISyntaxException { - final String packagePath = new URI(resource.toString()).getPath(); - if (packagePath != null) { - final File packageDirectory = new File(packagePath); - packageDirectory.listFiles(clazzCollector); - } + return ClassPath.from(Thread.currentThread().getContextClassLoader()).getTopLevelClasses(p.getName()).stream() + .filter(clazzInfo -> includeFilter.matcher(clazzInfo.getSimpleName()).matches()) + .map(clazzInfo -> clazzInfo.load()).filter(clazz -> clazz.isInterface()).collect(Collectors.toList()); } private static Method getMethod(final Class clazz, final String methodName, final Class... parameterTypes) { @@ -124,43 +94,4 @@ public class RepositoryManagementMethodPreAuthorizeAnnotatedTest { throw new RuntimeException(e.getMessage(), e); } } - - private static final class RegexIncludeInterfaceFileCollector implements FileFilter { - - private final List> interfaceClasses = new ArrayList<>(); - private final Pattern includeFilter; - private final Package basePackage; - - public RegexIncludeInterfaceFileCollector(final Package basePackage, final Pattern pattern) { - this.basePackage = basePackage; - this.includeFilter = pattern; - } - - @Override - public boolean accept(final File pathname) { - final String classNameWithExtension = pathname.getName(); - final int indexOfExtension = classNameWithExtension.indexOf(".class"); - if (indexOfExtension == -1) { - return false; - } - final String classNameWithoutExtension = classNameWithExtension.substring(0, indexOfExtension); - if (!includeFilter.matcher(classNameWithoutExtension).matches()) { - return false; - } - - try { - final Class classInPackage = Class.forName(basePackage.getName() + "." + classNameWithoutExtension); - if (classInPackage.isInterface()) { - interfaceClasses.add(classInPackage); - } - } catch (final ClassNotFoundException e) { - // don't need to handle here - } - return false; - } - - public List> getInterfaceClasses() { - return interfaceClasses; - } - } }