refactor test using ClassPath from guava

Signed-off-by: Michael Hirsch <michael.hirsch@bosch-si.com>
This commit is contained in:
Michael Hirsch
2016-08-05 13:04:56 +02:00
parent f5d9cf19bb
commit 06f64da6d5
2 changed files with 14 additions and 86 deletions

View File

@@ -1,13 +1,5 @@
<!--
Copyright (c) 2015 Bosch Software Innovations GmbH and others.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html
-->
<!-- Copyright (c) 2015 Bosch Software Innovations GmbH and others. All rights reserved. This program and the accompanying materials are made available
under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
@@ -40,7 +32,7 @@
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
</dependency>
<!-- Optional -->
<dependency>
<groupId>org.springframework.boot</groupId>
@@ -64,5 +56,10 @@
<artifactId>fest-assert</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -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<Class<?>> findInterfacesInPackage(final Package p, final Pattern includeFilter)
throws URISyntaxException, IOException, ClassNotFoundException {
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
final Enumeration<URL> 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<Class<?>> 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<Class<?>> getInterfaceClasses() {
return interfaceClasses;
}
}
}