Skip to content

Commit

Permalink
Correct all tests that introspect classpath for Vaadin classes.
Browse files Browse the repository at this point in the history
Fixes vaadin/framework8-issues#399
RemoveListenersDeprecatedTest test is fixed.
Corrections are made to make the test above passes.

Change-Id: I209a4693d241a1488b69b4742f48549dbf4bf0ac
  • Loading branch information
Denis Anisimov authored and Vaadin Code Review committed Nov 29, 2016
1 parent 0d57c15 commit b8e84da
Show file tree
Hide file tree
Showing 27 changed files with 431 additions and 278 deletions.
7 changes: 5 additions & 2 deletions compatibility-server/src/main/java/com/vaadin/v7/ui/Grid.java
Expand Up @@ -57,6 +57,7 @@
import com.vaadin.server.KeyMapper; import com.vaadin.server.KeyMapper;
import com.vaadin.server.VaadinSession; import com.vaadin.server.VaadinSession;
import com.vaadin.shared.MouseEventDetails; import com.vaadin.shared.MouseEventDetails;
import com.vaadin.shared.Registration;
import com.vaadin.shared.data.sort.SortDirection; import com.vaadin.shared.data.sort.SortDirection;
import com.vaadin.shared.util.SharedUtil; import com.vaadin.shared.util.SharedUtil;
import com.vaadin.ui.AbstractFocusable; import com.vaadin.ui.AbstractFocusable;
Expand Down Expand Up @@ -89,9 +90,9 @@
import com.vaadin.v7.data.util.converter.Converter; import com.vaadin.v7.data.util.converter.Converter;
import com.vaadin.v7.data.util.converter.ConverterUtil; import com.vaadin.v7.data.util.converter.ConverterUtil;
import com.vaadin.v7.event.ItemClickEvent; import com.vaadin.v7.event.ItemClickEvent;
import com.vaadin.v7.event.SelectionEvent;
import com.vaadin.v7.event.ItemClickEvent.ItemClickListener; import com.vaadin.v7.event.ItemClickEvent.ItemClickListener;
import com.vaadin.v7.event.ItemClickEvent.ItemClickNotifier; import com.vaadin.v7.event.ItemClickEvent.ItemClickNotifier;
import com.vaadin.v7.event.SelectionEvent;
import com.vaadin.v7.event.SelectionEvent.SelectionListener; import com.vaadin.v7.event.SelectionEvent.SelectionListener;
import com.vaadin.v7.event.SelectionEvent.SelectionNotifier; import com.vaadin.v7.event.SelectionEvent.SelectionNotifier;
import com.vaadin.v7.server.communication.data.DataGenerator; import com.vaadin.v7.server.communication.data.DataGenerator;
Expand Down Expand Up @@ -6155,8 +6156,10 @@ private void sort(boolean userOriginated) {
* the sort order change listener to add * the sort order change listener to add
*/ */
@Override @Override
public void addSortListener(SortListener listener) { public Registration addSortListener(SortListener listener) {
addListener(SortEvent.class, listener, SORT_ORDER_CHANGE_METHOD); addListener(SortEvent.class, listener, SORT_ORDER_CHANGE_METHOD);
return () -> removeListener(SortEvent.class, listener,
SORT_ORDER_CHANGE_METHOD);
} }


/** /**
Expand Down
Expand Up @@ -73,6 +73,8 @@ public class ComponentDesignWriterUtility {
"com.vaadin.server.communication.PushAtmosphereHandler$AtmosphereResourceListener"); "com.vaadin.server.communication.PushAtmosphereHandler$AtmosphereResourceListener");
WHITE_LIST_FQNS WHITE_LIST_FQNS
.add("com.vaadin.server.communication.PushAtmosphereHandler"); .add("com.vaadin.server.communication.PushAtmosphereHandler");
WHITE_LIST_FQNS
.add("com.vaadin.server.communication.PushRequestHandler$1");
WHITE_LIST_FQNS WHITE_LIST_FQNS
.add("com.vaadin.server.communication.PushRequestHandler$2"); .add("com.vaadin.server.communication.PushRequestHandler$2");
WHITE_LIST_FQNS.add("com.vaadin.server.LegacyVaadinPortlet"); WHITE_LIST_FQNS.add("com.vaadin.server.LegacyVaadinPortlet");
Expand Down
Expand Up @@ -34,7 +34,7 @@ public void allTypesAreDeprecated() throws URISyntaxException {


File testRoot = new File(DeprecatedTest.class.getResource("/").toURI()); File testRoot = new File(DeprecatedTest.class.getResource("/").toURI());


new ClasspathHelper(fqn -> false) new ClasspathHelper()
.getVaadinClassesFromClasspath( .getVaadinClassesFromClasspath(
entry -> entry.contains("compatibility-server") entry -> entry.contains("compatibility-server")
&& !testRoot.equals(new File(entry))) && !testRoot.equals(new File(entry)))
Expand All @@ -45,7 +45,8 @@ public void allTypesAreDeprecated() throws URISyntaxException {
+ " is in compatability package and it's not deprecated", + " is in compatability package and it's not deprecated",
cls.getAnnotation(Deprecated.class)); cls.getAnnotation(Deprecated.class));
}); });
Assert.assertNotEquals("Total number of checked classes", 0, count.get()); Assert.assertNotEquals("Total number of checked classes", 0,
count.get());
} }


} }
@@ -1,31 +1,65 @@
package com.vaadin.v7.tests; 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.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.ui.Component;
import com.vaadin.v7.ui.Field; import com.vaadin.v7.ui.Field;


@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class VaadinClasses { public class VaadinClasses {


private static final Set<String> WHITE_LIST_FQNS = new HashSet<>();

public static List<Class<? extends Field>> getFields() { 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 { try {
return com.vaadin.tests.VaadinClasses.findClasses(Field.class, File testRoot = new File(com.vaadin.tests.VaadinClasses.class
"com.vaadin.v7.ui"); .getResource("/").toURI());
} catch (IOException e) { File compatibilityTestRoot = new File(
e.printStackTrace(); VaadinClasses.class.getResource("/").toURI());
return null; 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() { public static List<Class<? extends Component>> getComponents() {
try { return getServerClasses(Component.class::isAssignableFrom)
return com.vaadin.tests.VaadinClasses.findClasses(Component.class, .map(VaadinClasses::castComponentClass)
"com.vaadin.v7.ui"); .collect(Collectors.toList());
} catch (IOException e) { }
throw new RuntimeException(
"Could not find all Vaadin component classes", e); 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;
} }
} }
Expand Up @@ -21,9 +21,9 @@
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;


import com.vaadin.ui.Slider;
import com.vaadin.v7.tests.VaadinClasses; import com.vaadin.v7.tests.VaadinClasses;
import com.vaadin.v7.ui.Field; import com.vaadin.v7.ui.Field;
import com.vaadin.v7.ui.Slider;


public class FieldDefaultValuesTest { public class FieldDefaultValuesTest {


Expand All @@ -45,7 +45,9 @@ public void testFieldsHaveDefaultValueAfterClear() throws Exception {


@Test @Test
public void testFieldsAreEmptyAfterClear() throws Exception { public void testFieldsAreEmptyAfterClear() throws Exception {
int count = 0;
for (Field<?> field : createFields()) { for (Field<?> field : createFields()) {
count++;
field.clear(); field.clear();


if (field instanceof Slider) { if (field instanceof Slider) {
Expand All @@ -60,12 +62,13 @@ public void testFieldsAreEmptyAfterClear() throws Exception {
field.isEmpty()); field.isEmpty());
} }
} }
Assert.assertTrue(count > 0);
} }


@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
private static List<Field<?>> createFields() private static List<Field<?>> createFields()
throws InstantiationException, IllegalAccessException { throws InstantiationException, IllegalAccessException {
List<Field<?>> fieldInstances = new ArrayList<Field<?>>(); List<Field<?>> fieldInstances = new ArrayList<>();


for (Class<? extends Field> fieldType : VaadinClasses.getFields()) { for (Class<? extends Field> fieldType : VaadinClasses.getFields()) {
fieldInstances.add(fieldType.newInstance()); fieldInstances.add(fieldType.newInstance());
Expand Down
6 changes: 6 additions & 0 deletions server/src/main/java/com/vaadin/event/MethodEventSource.java
Expand Up @@ -141,7 +141,10 @@ public Registration addListener(Class<?> eventType, Object object,
* @param method * @param method
* the method owned by the target that's registered to listen to * the method owned by the target that's registered to listen to
* events of type eventType. * events of type eventType.
* @deprecated use a {@link Registration} returned by
* {@link #addListener(Class, Object, Method)}
*/ */
@Deprecated
public void removeListener(Class<?> eventType, Object target, public void removeListener(Class<?> eventType, Object target,
Method method); Method method);


Expand Down Expand Up @@ -172,7 +175,10 @@ public void removeListener(Class<?> eventType, Object target,
* @param methodName * @param methodName
* the name of the method owned by <code>target</code> that's * the name of the method owned by <code>target</code> that's
* registered to listen to events of type <code>eventType</code>. * registered to listen to events of type <code>eventType</code>.
* @deprecated use a {@link Registration} returned by
* {@link #addListener(Class, Object, String)}
*/ */
@Deprecated
public void removeListener(Class<?> eventType, Object target, public void removeListener(Class<?> eventType, Object target,
String methodName); String methodName);
} }
7 changes: 6 additions & 1 deletion server/src/main/java/com/vaadin/event/SortEvent.java
Expand Up @@ -19,6 +19,7 @@
import java.util.List; import java.util.List;


import com.vaadin.data.sort.SortOrder; import com.vaadin.data.sort.SortOrder;
import com.vaadin.shared.Registration;
import com.vaadin.ui.Component; import com.vaadin.ui.Component;


/** /**
Expand Down Expand Up @@ -95,16 +96,20 @@ public interface SortNotifier extends Serializable {
* *
* @param listener * @param listener
* the sort order change listener to add * the sort order change listener to add
* @return a registration object for removing the listener
*/ */
public void addSortListener(SortListener listener); public Registration addSortListener(SortListener listener);


/** /**
* Removes a sort order change listener previously added using * Removes a sort order change listener previously added using
* {@link #addSortListener(SortListener)}. * {@link #addSortListener(SortListener)}.
* *
* @param listener * @param listener
* the sort order change listener to remove * the sort order change listener to remove
* @deprecated use a {@link Registration} returned by
* {@link #addSortListener(SortListener)}
*/ */
@Deprecated
public void removeSortListener(SortListener listener); public void removeSortListener(SortListener listener);
} }
} }
10 changes: 8 additions & 2 deletions server/src/main/java/com/vaadin/navigator/Navigator.java
Expand Up @@ -41,6 +41,7 @@
import com.vaadin.server.Page; import com.vaadin.server.Page;
import com.vaadin.server.Page.UriFragmentChangedEvent; import com.vaadin.server.Page.UriFragmentChangedEvent;
import com.vaadin.server.Page.UriFragmentChangedListener; import com.vaadin.server.Page.UriFragmentChangedListener;
import com.vaadin.shared.Registration;
import com.vaadin.shared.util.SharedUtil; import com.vaadin.shared.util.SharedUtil;
import com.vaadin.ui.Component; import com.vaadin.ui.Component;
import com.vaadin.ui.ComponentContainer; import com.vaadin.ui.ComponentContainer;
Expand Down Expand Up @@ -345,7 +346,7 @@ public View getView(String viewName) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
// TODO error handling // TODO error handling

} }
return null; return null;
} }
Expand Down Expand Up @@ -975,16 +976,21 @@ public void setErrorProvider(ViewProvider provider) {
* @param listener * @param listener
* Listener to invoke during a view change. * Listener to invoke during a view change.
*/ */
public void addViewChangeListener(ViewChangeListener listener) { public Registration addViewChangeListener(ViewChangeListener listener) {
listeners.add(listener); listeners.add(listener);
return () -> listeners.remove(listener);
} }


/** /**
* Removes a view change listener. * Removes a view change listener.
* *
* @param listener * @param listener
* Listener to remove. * Listener to remove.
* @deprecated use a {@link Registration} object returned by
* {@link #addViewChangeListener(ViewChangeListener)} to remove
* a listener
*/ */
@Deprecated
public void removeViewChangeListener(ViewChangeListener listener) { public void removeViewChangeListener(ViewChangeListener listener) {
listeners.remove(listener); listeners.remove(listener);
} }
Expand Down
Expand Up @@ -103,6 +103,7 @@ public Registration addAttachListener(AttachListener listener) {
} }


@Override @Override
@Deprecated
public void removeAttachListener(AttachListener listener) { public void removeAttachListener(AttachListener listener) {
removeListener(AttachEvent.ATTACH_EVENT_IDENTIFIER, AttachEvent.class, removeListener(AttachEvent.ATTACH_EVENT_IDENTIFIER, AttachEvent.class,
listener); listener);
Expand All @@ -115,6 +116,7 @@ public Registration addDetachListener(DetachListener listener) {
} }


@Override @Override
@Deprecated
public void removeDetachListener(DetachListener listener) { public void removeDetachListener(DetachListener listener) {
removeListener(DetachEvent.DETACH_EVENT_IDENTIFIER, DetachEvent.class, removeListener(DetachEvent.DETACH_EVENT_IDENTIFIER, DetachEvent.class,
listener); listener);
Expand Down Expand Up @@ -940,8 +942,12 @@ public void removeListener(Class<?> eventType, Object target) {
* @param method * @param method
* the method owned by <code>target</code> that's registered to * the method owned by <code>target</code> that's registered to
* listen to events of type <code>eventType</code>. * listen to events of type <code>eventType</code>.
* @deprecated use a {@link Registration} from
* {@link #addListener(Class, Object, Method)} to remove a
* listener
*/ */
@Override @Override
@Deprecated
public void removeListener(Class<?> eventType, Object target, public void removeListener(Class<?> eventType, Object target,
Method method) { Method method) {
if (eventRouter != null) { if (eventRouter != null) {
Expand Down
20 changes: 11 additions & 9 deletions server/src/main/java/com/vaadin/server/VaadinPortletSession.java
Expand Up @@ -42,6 +42,7 @@
import javax.xml.namespace.QName; import javax.xml.namespace.QName;


import com.vaadin.server.communication.PortletListenerNotifier; import com.vaadin.server.communication.PortletListenerNotifier;
import com.vaadin.shared.Registration;
import com.vaadin.ui.UI; import com.vaadin.ui.UI;
import com.vaadin.util.CurrentInstance; import com.vaadin.util.CurrentInstance;


Expand Down Expand Up @@ -123,8 +124,9 @@ public PortletConfig getPortletConfig() {
* @param listener * @param listener
* to add * to add
*/ */
public void addPortletListener(PortletListener listener) { public Registration addPortletListener(PortletListener listener) {
portletListeners.add(listener); portletListeners.add(listener);
return () -> portletListeners.remove(listener);
} }


/** /**
Expand All @@ -133,7 +135,11 @@ public void addPortletListener(PortletListener listener) {
* *
* @param listener * @param listener
* to remove * to remove
* @deprecated Use a {@link Registration} object returned by
* {@link #addPortletListener(PortletListener)} to remove a
* listener
*/ */
@Deprecated
public void removePortletListener(PortletListener listener) { public void removePortletListener(PortletListener listener) {
portletListeners.remove(listener); portletListeners.remove(listener);
} }
Expand All @@ -143,8 +149,7 @@ public void removePortletListener(PortletListener listener) {
*/ */
public void firePortletRenderRequest(UI uI, RenderRequest request, public void firePortletRenderRequest(UI uI, RenderRequest request,
RenderResponse response) { RenderResponse response) {
for (PortletListener l : new ArrayList<>( for (PortletListener l : new ArrayList<>(portletListeners)) {
portletListeners)) {
l.handleRenderRequest(request, l.handleRenderRequest(request,
new RestrictedRenderResponse(response), uI); new RestrictedRenderResponse(response), uI);
} }
Expand Down Expand Up @@ -172,8 +177,7 @@ public void firePortletActionRequest(UI uI, ActionRequest request,
sharedParameterActionValueMap.remove(key); sharedParameterActionValueMap.remove(key);
} else { } else {
// normal action request, notify listeners // normal action request, notify listeners
for (PortletListener l : new ArrayList<>( for (PortletListener l : new ArrayList<>(portletListeners)) {
portletListeners)) {
l.handleActionRequest(request, response, uI); l.handleActionRequest(request, response, uI);
} }
} }
Expand All @@ -184,8 +188,7 @@ public void firePortletActionRequest(UI uI, ActionRequest request,
*/ */
public void firePortletEventRequest(UI uI, EventRequest request, public void firePortletEventRequest(UI uI, EventRequest request,
EventResponse response) { EventResponse response) {
for (PortletListener l : new ArrayList<>( for (PortletListener l : new ArrayList<>(portletListeners)) {
portletListeners)) {
l.handleEventRequest(request, response, uI); l.handleEventRequest(request, response, uI);
} }
} }
Expand All @@ -195,8 +198,7 @@ public void firePortletEventRequest(UI uI, EventRequest request,
*/ */
public void firePortletResourceRequest(UI uI, ResourceRequest request, public void firePortletResourceRequest(UI uI, ResourceRequest request,
ResourceResponse response) { ResourceResponse response) {
for (PortletListener l : new ArrayList<>( for (PortletListener l : new ArrayList<>(portletListeners)) {
portletListeners)) {
l.handleResourceRequest(request, response, uI); l.handleResourceRequest(request, response, uI);
} }
} }
Expand Down

0 comments on commit b8e84da

Please sign in to comment.