From b8e84da2e2dc4bcaed40d169c40a97f3d11e0648 Mon Sep 17 00:00:00 2001 From: Denis Anisimov Date: Mon, 28 Nov 2016 10:10:21 +0300 Subject: Correct all tests that introspect classpath for Vaadin classes. Fixes vaadin/framework8-issues#399 RemoveListenersDeprecatedTest test is fixed. Corrections are made to make the test above passes. Change-Id: I209a4693d241a1488b69b4742f48549dbf4bf0ac --- .../com/vaadin/tests/server/ClasspathHelper.java | 122 --------------------- .../tests/server/ComponentDesignWriterUtility.java | 2 + .../com/vaadin/tests/server/DeprecatedTest.java | 5 +- .../java/com/vaadin/v7/tests/VaadinClasses.java | 60 +++++++--- .../abstractfield/FieldDefaultValuesTest.java | 7 +- 5 files changed, 57 insertions(+), 139 deletions(-) delete mode 100644 compatibility-server/src/test/java/com/vaadin/tests/server/ClasspathHelper.java (limited to 'compatibility-server/src/test') diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/ClasspathHelper.java b/compatibility-server/src/test/java/com/vaadin/tests/server/ClasspathHelper.java deleted file mode 100644 index 97005966f3..0000000000 --- a/compatibility-server/src/test/java/com/vaadin/tests/server/ClasspathHelper.java +++ /dev/null @@ -1,122 +0,0 @@ -package com.vaadin.tests.server; - -import java.io.File; -import java.io.IOException; -import java.lang.reflect.Modifier; -import java.net.URI; -import java.nio.file.FileSystems; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Locale; -import java.util.Objects; -import java.util.function.Predicate; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -/** - * Allows to get classes from the current classpath using classes FQN filter. - *

- * The methods in the class return all real (not anonymous and not private) - * classes from the filtered classpath. - * - * @author Vaadin Ltd - * - */ -class ClasspathHelper { - - public static final String COM_VAADIN_FILE_PREFIX = "com" + File.separatorChar + "vaadin" + File.separatorChar; - private final Predicate skipClassesFilter; - - ClasspathHelper(Predicate skipClassesFilter) { - this.skipClassesFilter = skipClassesFilter; - } - - Stream> getVaadinClassesFromClasspath( - Predicate classpathFilter, - Predicate> classFilter) { - return getRawClasspathEntries().stream().filter(classpathFilter) - .map(File::new).map(file -> getVaadinClassesFromFile(file)) - .flatMap(List::stream).filter(classFilter) - .filter(cls -> !cls.isSynthetic() && !cls.isAnonymousClass() - && !Modifier.isPrivate(cls.getModifiers())); - - } - - Stream> getVaadinClassesFromClasspath( - Predicate classpathFilter) { - return getVaadinClassesFromClasspath(classpathFilter, cls -> true); - } - - private List> getVaadinClassesFromFile(File classesRoot) { - try { - if (classesRoot.isDirectory()) { - return Files.walk(classesRoot.toPath()) - .filter(Files::isRegularFile) - .filter(path -> path.toFile().getName() - .endsWith(".class")) - .filter(path -> classesRoot.toPath().relativize(path) - .toString().contains(COM_VAADIN_FILE_PREFIX)) - .map(path -> getClassFromFile(path, - classesRoot.toPath())) - .filter(Objects::nonNull).collect(Collectors.toList()); - } else if (classesRoot.getName().toLowerCase(Locale.ENGLISH) - .endsWith(".jar")) { - URI uri = URI.create("jar:file:" + classesRoot.getPath()); - Path root = FileSystems - .newFileSystem(uri, Collections.emptyMap()) - .getPath(File.separator); - return Files.walk(root).filter(Files::isRegularFile) - .filter(path -> path.toUri().getSchemeSpecificPart() - .endsWith(".class")) - .filter(path -> root.relativize(path).toString() - .contains(COM_VAADIN_FILE_PREFIX)) - .map(path -> getClassFromFile(path, root)) - .filter(Objects::nonNull).collect(Collectors.toList()); - } - return null; - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - private Class getClassFromFile(Path path, Path root) { - Path relative = root.relativize(path); - String name = relative.toString(); - name = name.substring(0, name.length() - ".class".length()); - name = name.replace(File.separatorChar, '.'); - if (skipClassesFilter.test(name)) { - return null; - } - try { - return Class.forName(name, false, getClass().getClassLoader()); - } catch (ClassNotFoundException e) { - throw new RuntimeException(e); - } - } - - private final static List getRawClasspathEntries() { - List locations = new ArrayList<>(); - - String pathSep = System.getProperty("path.separator"); - String classpath = System.getProperty("java.class.path"); - - if (classpath.startsWith("\"")) { - classpath = classpath.substring(1); - } - if (classpath.endsWith("\"")) { - classpath = classpath.substring(0, classpath.length() - 1); - } - - String[] split = classpath.split(pathSep); - for (int i = 0; i < split.length; i++) { - String classpathEntry = split[i]; - locations.add(classpathEntry); - } - - return locations; - } - -} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/ComponentDesignWriterUtility.java b/compatibility-server/src/test/java/com/vaadin/tests/server/ComponentDesignWriterUtility.java index 6009385ccc..00022ba44c 100644 --- a/compatibility-server/src/test/java/com/vaadin/tests/server/ComponentDesignWriterUtility.java +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/ComponentDesignWriterUtility.java @@ -73,6 +73,8 @@ public class ComponentDesignWriterUtility { "com.vaadin.server.communication.PushAtmosphereHandler$AtmosphereResourceListener"); WHITE_LIST_FQNS .add("com.vaadin.server.communication.PushAtmosphereHandler"); + WHITE_LIST_FQNS + .add("com.vaadin.server.communication.PushRequestHandler$1"); WHITE_LIST_FQNS .add("com.vaadin.server.communication.PushRequestHandler$2"); WHITE_LIST_FQNS.add("com.vaadin.server.LegacyVaadinPortlet"); diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/DeprecatedTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/DeprecatedTest.java index ab04f0616a..f00592af6d 100644 --- a/compatibility-server/src/test/java/com/vaadin/tests/server/DeprecatedTest.java +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/DeprecatedTest.java @@ -34,7 +34,7 @@ public class DeprecatedTest { File testRoot = new File(DeprecatedTest.class.getResource("/").toURI()); - new ClasspathHelper(fqn -> false) + new ClasspathHelper() .getVaadinClassesFromClasspath( entry -> entry.contains("compatibility-server") && !testRoot.equals(new File(entry))) @@ -45,7 +45,8 @@ public class DeprecatedTest { + " is in compatability package and it's not deprecated", cls.getAnnotation(Deprecated.class)); }); - Assert.assertNotEquals("Total number of checked classes", 0, count.get()); + Assert.assertNotEquals("Total number of checked classes", 0, + count.get()); } } diff --git a/compatibility-server/src/test/java/com/vaadin/v7/tests/VaadinClasses.java b/compatibility-server/src/test/java/com/vaadin/v7/tests/VaadinClasses.java index 251f8b0223..612f85cd33 100644 --- a/compatibility-server/src/test/java/com/vaadin/v7/tests/VaadinClasses.java +++ b/compatibility-server/src/test/java/com/vaadin/v7/tests/VaadinClasses.java @@ -1,31 +1,65 @@ package com.vaadin.v7.tests; -import java.io.IOException; +import java.io.File; +import java.lang.reflect.Modifier; +import java.net.URISyntaxException; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import com.vaadin.tests.server.ClasspathHelper; import com.vaadin.ui.Component; import com.vaadin.v7.ui.Field; @SuppressWarnings("deprecation") public class VaadinClasses { + private static final Set WHITE_LIST_FQNS = new HashSet<>(); + public static List> getFields() { + return getServerClasses(Field.class::isAssignableFrom) + .map(VaadinClasses::castFieldClass) + .collect(Collectors.toList()); + } + + public static Stream> getServerClasses( + Predicate> predicate) { try { - return com.vaadin.tests.VaadinClasses.findClasses(Field.class, - "com.vaadin.v7.ui"); - } catch (IOException e) { - e.printStackTrace(); - return null; + File testRoot = new File(com.vaadin.tests.VaadinClasses.class + .getResource("/").toURI()); + File compatibilityTestRoot = new File( + VaadinClasses.class.getResource("/").toURI()); + ClasspathHelper helper = new ClasspathHelper( + fqn -> !fqn.startsWith("com.vaadin.v7.ui")); + return helper.getVaadinClassesFromClasspath( + entry -> !compatibilityTestRoot.equals(new File(entry)) + && !testRoot.equals(new File(entry)), + cls -> predicate.test(cls) && !cls.isInterface() + && !Modifier.isAbstract(cls.getModifiers())); + } catch (URISyntaxException e) { + throw new RuntimeException(e); } } public static List> getComponents() { - try { - return com.vaadin.tests.VaadinClasses.findClasses(Component.class, - "com.vaadin.v7.ui"); - } catch (IOException e) { - throw new RuntimeException( - "Could not find all Vaadin component classes", e); - } + return getServerClasses(Component.class::isAssignableFrom) + .map(VaadinClasses::castComponentClass) + .collect(Collectors.toList()); + } + + private static Class castFieldClass(Class clazz) { + return (Class) clazz; + } + + private static Class castComponentClass( + Class clazz) { + return (Class) clazz; + } + + protected static Set getWhiteListFqns() { + return WHITE_LIST_FQNS; } } diff --git a/compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/abstractfield/FieldDefaultValuesTest.java b/compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/abstractfield/FieldDefaultValuesTest.java index a53e3bbbd1..3ee4ce607b 100644 --- a/compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/abstractfield/FieldDefaultValuesTest.java +++ b/compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/abstractfield/FieldDefaultValuesTest.java @@ -21,9 +21,9 @@ import java.util.List; import org.junit.Assert; import org.junit.Test; -import com.vaadin.ui.Slider; import com.vaadin.v7.tests.VaadinClasses; import com.vaadin.v7.ui.Field; +import com.vaadin.v7.ui.Slider; public class FieldDefaultValuesTest { @@ -45,7 +45,9 @@ public class FieldDefaultValuesTest { @Test public void testFieldsAreEmptyAfterClear() throws Exception { + int count = 0; for (Field field : createFields()) { + count++; field.clear(); if (field instanceof Slider) { @@ -60,12 +62,13 @@ public class FieldDefaultValuesTest { field.isEmpty()); } } + Assert.assertTrue(count > 0); } @SuppressWarnings("rawtypes") private static List> createFields() throws InstantiationException, IllegalAccessException { - List> fieldInstances = new ArrayList>(); + List> fieldInstances = new ArrayList<>(); for (Class fieldType : VaadinClasses.getFields()) { fieldInstances.add(fieldType.newInstance()); -- cgit v1.2.3