This is an automated email from the ASF dual-hosted git repository.

thiagohp pushed a commit to branch better-page-invalidation
in repository https://gitbox.apache.org/repos/asf/tapestry-5.git


The following commit(s) were added to refs/heads/better-page-invalidation by 
this push:
     new 1ed147386 TAP5-2742: more tests and code improvements
1ed147386 is described below

commit 1ed1473864f552958e345ca62666abb6a9849992
Author: Thiago H. de Paula Figueiredo <thi...@arsmachina.com.br>
AuthorDate: Wed Nov 23 08:26:10 2022 -0300

    TAP5-2742: more tests and code improvements
---
 .../internal/event/InvalidationEventHubImpl.java   | 22 +++++---
 .../services/ComponentDependencyRegistryImpl.java  | 12 ++--
 .../event/InvalidationEventHubImplTest.java        |  6 +-
 .../ComponentDependencyRegistryImplTest.java       | 66 +++++++++++++++++++---
 4 files changed, 86 insertions(+), 20 deletions(-)

diff --git 
a/tapestry-core/src/main/java/org/apache/tapestry5/internal/event/InvalidationEventHubImpl.java
 
b/tapestry-core/src/main/java/org/apache/tapestry5/internal/event/InvalidationEventHubImpl.java
index b635730b3..46186ee00 100644
--- 
a/tapestry-core/src/main/java/org/apache/tapestry5/internal/event/InvalidationEventHubImpl.java
+++ 
b/tapestry-core/src/main/java/org/apache/tapestry5/internal/event/InvalidationEventHubImpl.java
@@ -14,11 +14,6 @@
 
 package org.apache.tapestry5.internal.event;
 
-import org.apache.tapestry5.commons.internal.util.TapestryException;
-import org.apache.tapestry5.commons.services.InvalidationEventHub;
-import org.apache.tapestry5.commons.services.InvalidationListener;
-import org.apache.tapestry5.commons.util.CollectionFactory;
-
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashSet;
@@ -26,6 +21,12 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.function.Function;
+import java.util.stream.Collectors;
+
+import org.apache.tapestry5.commons.internal.util.TapestryException;
+import org.apache.tapestry5.commons.services.InvalidationEventHub;
+import org.apache.tapestry5.commons.services.InvalidationListener;
+import org.apache.tapestry5.commons.util.CollectionFactory;
 
 /**
  * Base implementation class for classes (especially services) that need to 
manage a list of
@@ -64,16 +65,23 @@ public class InvalidationEventHubImpl implements 
InvalidationEventHub
             return;
         }
         
+        final Set<String> alreadyProcessed = new HashSet<>();
+        
         do 
         {
-            Set<String> extraResources = new HashSet<>();
+            final Set<String> extraResources = new HashSet<>();
+            Set<String> actuallyNewResources;
             for (Function<List<String>, List<String>> callback : callbacks)
             {
                 final List<String> newResources = callback.apply(resources);
                 if (newResources == null) {
                     throw new TapestryException("InvalidationEventHub callback 
functions cannot return null", null);
                 }
-                extraResources.addAll(newResources);
+                actuallyNewResources = newResources.stream()
+                        .filter(r -> !alreadyProcessed.contains(r))
+                        .collect(Collectors.toSet());
+                extraResources.addAll(actuallyNewResources);
+                alreadyProcessed.addAll(newResources);
             }
             resources = new ArrayList<>(extraResources);
         }
diff --git 
a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentDependencyRegistryImpl.java
 
b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentDependencyRegistryImpl.java
index 8c4c7d0da..a7d7dcae8 100644
--- 
a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentDependencyRegistryImpl.java
+++ 
b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentDependencyRegistryImpl.java
@@ -15,6 +15,7 @@ package org.apache.tapestry5.internal.services;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -133,7 +134,8 @@ public class ComponentDependencyRegistryImpl implements 
ComponentDependencyRegis
     @Override
     public Set<String> getDependents(String className) 
     {
-        return map.get(className);
+        final Set<String> dependents = map.get(className);
+        return dependents != null ? dependents : Collections.emptySet();
     }
 
     @Override
@@ -150,7 +152,8 @@ public class ComponentDependencyRegistryImpl implements 
ComponentDependencyRegis
         add(getClassName(component), getClassName(dependency));
     }
     
-    private void add(String component, String dependency) 
+    // Protected just for testing
+    void add(String component, String dependency) 
     {
         synchronized (map) 
         {
@@ -170,12 +173,13 @@ public class ComponentDependencyRegistryImpl implements 
ComponentDependencyRegis
         invalidationEventHub.addInvalidationCallback(this::listen);
     }
     
-    private List<String> listen(List<String> resources)
+    // Protected just for testing
+    List<String> listen(List<String> resources)
     {
         List<String> furtherDependents = new ArrayList<>();
         for (String resource : resources) 
         {
-            final Set<String> dependents = map.get(resource);
+            final Set<String> dependents = getDependents(resource);
             for (String furtherDependent : dependents) 
             {
                 if (!resources.contains(furtherDependent) && 
!furtherDependents.contains(furtherDependent))
diff --git 
a/tapestry-core/src/test/java/org/apache/tapestry5/internal/event/InvalidationEventHubImplTest.java
 
b/tapestry-core/src/test/java/org/apache/tapestry5/internal/event/InvalidationEventHubImplTest.java
index 6cf69d58a..7112126d9 100644
--- 
a/tapestry-core/src/test/java/org/apache/tapestry5/internal/event/InvalidationEventHubImplTest.java
+++ 
b/tapestry-core/src/test/java/org/apache/tapestry5/internal/event/InvalidationEventHubImplTest.java
@@ -44,9 +44,11 @@ public class InvalidationEventHubImplTest
         Function<List<String>, List<String>> callback = (r) -> {
             callCount.incrementAndGet();
             if (r.size() == 2 && r.get(0).equals(firstInitialElement) && 
r.get(1).equals(secondInitialElement)) {
-                return Arrays.asList(firstInitialElement.toUpperCase(), 
secondInitialElement.toUpperCase());
+                return Arrays.asList(firstInitialElement.toUpperCase(), 
secondInitialElement.toUpperCase(), firstInitialElement);
             }
-            else if (r.size() == 2 && 
r.get(0).equals(firstInitialElement.toUpperCase()) && 
r.get(1).equals(secondInitialElement.toUpperCase())) {
+            else if (r.size() == 3 && 
r.contains(firstInitialElement.toUpperCase()) && 
+                    r.contains(secondInitialElement.toUpperCase()) &&
+                    r.contains(firstInitialElement)) {
                 return Arrays.asList("something", "else");
             }
             else {
diff --git 
a/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/ComponentDependencyRegistryImplTest.java
 
b/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/ComponentDependencyRegistryImplTest.java
index 794ffd7dc..e6fc2bd86 100644
--- 
a/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/ComponentDependencyRegistryImplTest.java
+++ 
b/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/ComponentDependencyRegistryImplTest.java
@@ -14,17 +14,23 @@
 
 package org.apache.tapestry5.internal.services;
 
-import org.apache.tapestry5.internal.test.InternalBaseTestCase;
+import static org.testng.Assert.assertEquals;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 /**
- * Tests for the bean editor model source itself, as well as the model classes.
+ * Tests {@link ComponentDependencyRegistryImpl}.
  */
-public abstract class ComponentDependencyRegistryImplTest extends 
InternalBaseTestCase
+public class ComponentDependencyRegistryImplTest
 {
     
-    private ComponentDependencyRegistry componentDependencyRegistry;
+    private ComponentDependencyRegistryImpl componentDependencyRegistry;
     
     @BeforeClass
     public void setup()
@@ -32,10 +38,56 @@ public abstract class ComponentDependencyRegistryImplTest 
extends InternalBaseTe
         componentDependencyRegistry = new ComponentDependencyRegistryImpl();
     }
     
-    @Test
-    public void register()
+    @Test(timeOut = 5000)
+    public void listen()
     {
-        componentDependencyRegistry.register(null);
+        
+        componentDependencyRegistry.add("foo", "bar");
+        componentDependencyRegistry.add("d", "a");
+        componentDependencyRegistry.add("dd", "aa");
+        componentDependencyRegistry.add("dd", "a");
+        componentDependencyRegistry.add("dd", "a");
+        
+        final List<String> resources = Arrays.asList("a", "aa", "none");
+        final List<String> result = 
componentDependencyRegistry.listen(resources);
+        Collections.sort(result);
+        assertEquals(result, Arrays.asList("d", "dd"));
+        
     }
     
+    @Test
+    public void dependency_methods()
+    {
+        
+        final String foo = "foo";
+        final String bar = "bar";
+        final String something = "d";
+        final String other = "dd";
+        final String fulano = "a";
+        final String beltrano = "aa";
+        
+        assertEquals(componentDependencyRegistry.getDependencies(foo), 
Collections.emptySet(), 
+                "getDependents() should never return null");
+
+        assertEquals(componentDependencyRegistry.getDependents(foo), 
Collections.emptySet(), 
+                "getDependents() should never return null");
+
+        componentDependencyRegistry.add(foo, bar);
+        componentDependencyRegistry.add(something, fulano);
+        componentDependencyRegistry.add(other, beltrano);
+        componentDependencyRegistry.add(other, fulano);
+        componentDependencyRegistry.add(other, fulano);
+        
+        assertEquals(componentDependencyRegistry.getDependencies(other), new 
HashSet<>(Arrays.asList(fulano, beltrano)));
+        assertEquals(componentDependencyRegistry.getDependencies(something), 
new HashSet<>(Arrays.asList(fulano)));
+        assertEquals(componentDependencyRegistry.getDependencies(fulano), new 
HashSet<>(Arrays.asList()));
+        assertEquals(componentDependencyRegistry.getDependencies(foo), new 
HashSet<>(Arrays.asList(bar)));
+        assertEquals(componentDependencyRegistry.getDependencies(bar), new 
HashSet<>(Arrays.asList()));
+
+        assertEquals(componentDependencyRegistry.getDependents(bar), new 
HashSet<>(Arrays.asList(foo)));
+        assertEquals(componentDependencyRegistry.getDependents(fulano), new 
HashSet<>(Arrays.asList(other, something)));
+        assertEquals(componentDependencyRegistry.getDependents(foo), new 
HashSet<>(Arrays.asList()));
+        
+    }
+
 }

Reply via email to