You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2023/02/23 13:30:20 UTC

[myfaces-tobago] branch tobago-5.x updated: feat: Handle config without ordering (#3790)

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

lofwyr pushed a commit to branch tobago-5.x
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git


The following commit(s) were added to refs/heads/tobago-5.x by this push:
     new 19797c1633 feat: Handle config without ordering (#3790)
19797c1633 is described below

commit 19797c1633f39549e568ca964d76a34aedf7e9df
Author: Udo Schnurpfeil <lo...@apache.org>
AuthorDate: Thu Feb 23 14:30:14 2023 +0100

    feat: Handle config without ordering (#3790)
    
    issue: TOBAGO-2187
---
 .../tobago/internal/config/TobagoConfigSorter.java | 52 +++++++++++---
 .../config/TobagoConfigSorterUnitTest.java         | 83 ++++++++++++++++++++--
 2 files changed, 120 insertions(+), 15 deletions(-)

diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigSorter.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigSorter.java
index 9213fdfb85..ef86d9fb09 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigSorter.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigSorter.java
@@ -56,9 +56,30 @@ public class TobagoConfigSorter {
     checkCycles();
 
     List<TobagoConfigFragment> result = new ArrayList<>();
+    List<Vertex> singles = new ArrayList<>();
 
     for (Vertex vertex : vertices) {
-      topologicalSort0(vertex, result);
+
+      // single notes will be added at the end, we collect them here.
+      // The reason is more practically, but not theoretically. A single note may be anywhere in the list, but
+      // people sometimes forgot to add a order tag in there tobago-config.xml, most properly they want to
+      // override configurations. See TOBAGO-2187
+      if (vertex.adjacencyList.isEmpty()) {
+        singles.add(vertex);
+      } else {
+        topologicalSort0(vertex, result);
+        LOG.debug("after sorting vertex {}: result={}", vertex, result);
+      }
+    }
+
+    for (Vertex vertex : singles) {
+      if (!vertex.isVisited()) {
+        LOG.warn("Found tobago-config.xml without ordering. "
+            + "The order should always be specified, as configurations can override each other. "
+            + "Name: '{}', file: '{}'", vertex.getFragment().getName(), vertex.getFragment().getUrl());
+        topologicalSort0(vertex, result);
+        LOG.debug("after sorting vertex {}: result={}", vertex, result);
+      }
     }
 
     logResult(result);
@@ -86,27 +107,36 @@ public class TobagoConfigSorter {
 
   private void logResult(List<TobagoConfigFragment> result) {
     if (LOG.isInfoEnabled()) {
-      StringBuilder builder = new StringBuilder("Order of the Tobago config files: ");
+      final boolean debug = LOG.isDebugEnabled();
+      final StringBuilder builder = new StringBuilder("Order of the Tobago config files: [");
       for (TobagoConfigFragment fragment : result) {
         final String name = fragment.getName();
-        if (LOG.isDebugEnabled()) {
-          builder.append("name=");
+        if (debug) {
+          builder.append("{");
+          builder.append("'name': ");
           if (name == null) {
-            builder.append("<unnamed>");
+            builder.append("'<unnamed>'");
           } else {
             builder.append("'");
             builder.append(name);
-            builder.append("'");
+            builder.append("',");
           }
-          builder.append(" url='");
+          builder.append(" 'url': '");
           builder.append(fragment.getUrl());
           builder.append("'");
+          builder.append("},");
         } else {
+          builder.append("'");
           builder.append(name);
-          builder.append(", ");
+          builder.append("',");
         }
       }
-      LOG.info(builder.toString());
+      if (builder.charAt(builder.length() - 1) == ',') {
+        builder.deleteCharAt(builder.length() - 1);
+      }
+      builder.append("]");
+      final String prepared = builder.toString();
+      LOG.info(prepared.replace('\'', '"'));
     }
   }
 
@@ -120,19 +150,21 @@ public class TobagoConfigSorter {
         final TobagoConfigFragment before = findByName(befores);
         if (before != null) {
           findVertex(before).addAdjacent(findVertex(current));
+          LOG.debug("b: {} <- {}", before, current);
         }
       }
       for (final String afters : current.getAfter()) {
         final TobagoConfigFragment after = findByName(afters);
         if (after != null) {
           findVertex(current).addAdjacent(findVertex(after));
+          LOG.debug("a: {} <- {}", current, after);
         }
       }
     }
   }
 
   /**
-   * Cycle detection: if the base in reachable form its own, than there is a cycle.
+   * Cycle detection: if the base in reachable form its own, then there is a cycle.
    *
    * @throws IllegalStateException When detecting a cycle.
    */
diff --git a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/TobagoConfigSorterUnitTest.java b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/TobagoConfigSorterUnitTest.java
index db0ec4abc8..b9bc040d2b 100644
--- a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/TobagoConfigSorterUnitTest.java
+++ b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/TobagoConfigSorterUnitTest.java
@@ -32,6 +32,70 @@ public class TobagoConfigSorterUnitTest {
 
   private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
+  /**
+   * a before b
+   * b after a
+   * single: no1, no2, no3
+   * expected: a, b, no1, no2, no3
+   */
+  @Test
+  public void testCompare3() {
+
+    // config + names
+
+    final TobagoConfigFragment a = new TobagoConfigFragment();
+    a.setName("a");
+
+    final TobagoConfigFragment b = new TobagoConfigFragment();
+    b.setName("b");
+
+    // no ordering
+    final TobagoConfigFragment no1 = new TobagoConfigFragment();
+    no1.setName("no1");
+    final TobagoConfigFragment no2 = new TobagoConfigFragment();
+    no2.setName("no2");
+    final TobagoConfigFragment no3 = new TobagoConfigFragment();
+    no3.setName("no3");
+
+    // before
+    a.getBefore().add("b");
+
+    // after
+    b.getAfter().add("a");
+
+    final List<TobagoConfigFragment> list = new ArrayList<>();
+    list.add(no1);
+    list.add(b);
+    list.add(no2);
+    list.add(a);
+    list.add(no3);
+    int size = list.size();
+
+    TobagoConfigSorter.sort(list);
+
+    Assertions.assertEquals(a, list.get(0));
+    Assertions.assertEquals(b, list.get(1));
+    Assertions.assertEquals(no1, list.get(2));
+    Assertions.assertEquals(no2, list.get(3));
+    Assertions.assertEquals(no3, list.get(4));
+    Assertions.assertEquals(size, list.size());
+  }
+
+  /**
+   * a before b
+   * b before c
+   * u1 before d
+   * u2 before d
+   * u2 before y // not in list
+   * e after d
+   * f after e
+   * u1 after c
+   * u2 after c
+   * u2 after z // not in list
+   * n after m
+   * single: no1, no2
+   * expected: a, b, c, ...
+   */
   @Test
   public void testCompare() {
 
@@ -64,7 +128,12 @@ public class TobagoConfigSorterUnitTest {
     // unnamed
     final TobagoConfigFragment u1 = new TobagoConfigFragment();
     final TobagoConfigFragment u2 = new TobagoConfigFragment();
-    final TobagoConfigFragment u3 = new TobagoConfigFragment();
+
+    // no ordering
+    final TobagoConfigFragment no1 = new TobagoConfigFragment();
+    no1.setName("no1");
+    final TobagoConfigFragment no2 = new TobagoConfigFragment();
+    no2.setName("no2");
 
     // before
     a.getBefore().add("b");
@@ -89,15 +158,17 @@ public class TobagoConfigSorterUnitTest {
     final List<TobagoConfigFragment> list = new ArrayList<>();
     list.add(a);
     list.add(b);
+    list.add(no1);
     list.add(c);
     list.add(d);
+    list.add(no2);
     list.add(e);
     list.add(f);
     list.add(u1);
     list.add(u2);
-    list.add(u3);
     list.add(m);
     list.add(n);
+    int size = list.size();
 
     TobagoConfigSorter.sort(list);
 
@@ -109,9 +180,11 @@ public class TobagoConfigSorterUnitTest {
     Assertions.assertEquals(d, list.get(5));
     Assertions.assertEquals(e, list.get(6));
     Assertions.assertEquals(f, list.get(7));
-    Assertions.assertEquals(u3, list.get(8));
-    Assertions.assertEquals(m, list.get(9));
-    Assertions.assertEquals(n, list.get(10));
+    Assertions.assertEquals(m, list.get(8));
+    Assertions.assertEquals(n, list.get(9));
+    Assertions.assertEquals(no1, list.get(10));
+    Assertions.assertEquals(no2, list.get(11));
+    Assertions.assertEquals(size, list.size());
   }
 
   @Test