diff options
author | Denis Anisimov <denis@vaadin.com> | 2016-11-28 10:10:21 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-11-29 10:35:59 +0000 |
commit | b8e84da2e2dc4bcaed40d169c40a97f3d11e0648 (patch) | |
tree | 80e5a8e7a7856b67048319958ef88b5466d3e4bc /compatibility-server/src/test | |
parent | 0d57c15577f5d5e15453024ce90144120948eae9 (diff) | |
download | vaadin-framework-b8e84da2e2dc4bcaed40d169c40a97f3d11e0648.tar.gz vaadin-framework-b8e84da2e2dc4bcaed40d169c40a97f3d11e0648.zip |
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
Diffstat (limited to 'compatibility-server/src/test')
5 files changed, 57 insertions, 139 deletions
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. - * <p> - * 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<String> skipClassesFilter; - - ClasspathHelper(Predicate<String> skipClassesFilter) { - this.skipClassesFilter = skipClassesFilter; - } - - Stream<Class<?>> getVaadinClassesFromClasspath( - Predicate<String> classpathFilter, - Predicate<Class<?>> 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<Class<?>> getVaadinClassesFromClasspath( - Predicate<String> classpathFilter) { - return getVaadinClassesFromClasspath(classpathFilter, cls -> true); - } - - private List<Class<?>> 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<String> getRawClasspathEntries() { - List<String> 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 @@ -74,6 +74,8 @@ public class ComponentDesignWriterUtility { 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"); WHITE_LIST_FQNS.add("com.vaadin.server.RestrictedRenderResponse"); 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<String> WHITE_LIST_FQNS = new HashSet<>(); + public static List<Class<? extends Field>> getFields() { + return getServerClasses(Field.class::isAssignableFrom) + .map(VaadinClasses::castFieldClass) + .collect(Collectors.toList()); + } + + public static Stream<Class<?>> getServerClasses( + Predicate<? super Class<?>> 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<Class<? extends Component>> 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<? extends Field> castFieldClass(Class<?> clazz) { + return (Class<? extends Field>) clazz; + } + + private static Class<? extends Component> castComponentClass( + Class<?> clazz) { + return (Class<? extends Component>) clazz; + } + + protected static Set<String> 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<Field<?>> createFields() throws InstantiationException, IllegalAccessException { - List<Field<?>> fieldInstances = new ArrayList<Field<?>>(); + List<Field<?>> fieldInstances = new ArrayList<>(); for (Class<? extends Field> fieldType : VaadinClasses.getFields()) { fieldInstances.add(fieldType.newInstance()); |