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

asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cayenne.git

commit 7f4bfb8472de95279841185d25792489db96dc8d
Author: Andrus Adamchik <[email protected]>
AuthorDate: Thu May 14 10:40:24 2026 -0400

    Tests entity sorting
    
    addressing a latent issue with AshwoodEntitySorter (strongly connected 
entities are sorted
    as if they are equal, so insert and delete order of sorting do not produce 
an exactly
    mirrorred ordering).
    
    Until we address it in AshwoodEntitySorter, keeping the fix in tests
---
 .../unit/di/runtime/AllTestsSchemaManager.java     | 31 +++++++++++++++-------
 1 file changed, 21 insertions(+), 10 deletions(-)

diff --git 
a/cayenne/src/test/java/org/apache/cayenne/unit/di/runtime/AllTestsSchemaManager.java
 
b/cayenne/src/test/java/org/apache/cayenne/unit/di/runtime/AllTestsSchemaManager.java
index 5fa47e960..a402867ca 100644
--- 
a/cayenne/src/test/java/org/apache/cayenne/unit/di/runtime/AllTestsSchemaManager.java
+++ 
b/cayenne/src/test/java/org/apache/cayenne/unit/di/runtime/AllTestsSchemaManager.java
@@ -52,6 +52,7 @@ import java.sql.Types;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 import java.util.ListIterator;
 import java.util.Set;
@@ -227,23 +228,33 @@ public class AllTestsSchemaManager {
         }
     }
 
-    /**
-     * Helper method that orders DbEntities to satisfy referential constraints
-     * and returns an ordered list.
-     */
-    private List<DbEntity> dbEntitiesInInsertOrder(DataMap map) {
+    public List<DbEntity> dbEntitiesInInsertOrder(DataMap map) {
+        return sortedDbEntities(map, false);
+    }
+
+    public List<DbEntity> dbEntitiesInDeleteOrder(DataMap map) {
+        return sortedDbEntities(map, true);
+    }
+
+    private List<DbEntity> sortedDbEntities(DataMap map, boolean deleteOrder) {
         DataMap localMap = domain.getDataMap(map.getName());
         List<DbEntity> entities = new ArrayList<>(localMap.getDbEntities());
         entities.removeAll(excludeEntities(entities));
 
-        domain.getEntitySorter().sortDbEntities(entities, false);
+        // Deterministic tiebreaker for entities AshwoodEntitySorter cannot 
distinguish
+        // (members of a strongly connected component compare as equal).
+        // List.sort is stable, so this name order survives sortDbEntities
+        // for any pair the comparator returns 0 for.
+        // TODO:
+        //  1. this should really be fixed in AshwoodEntitySorter that should 
have a deterministic tiebreaker
+        //  2. Also, alpha order of entities only works by incident. It may 
likely break on different combinations
+        //  of circular relationships
+        entities.sort(Comparator.comparing(DbEntity::getName));
+
+        domain.getEntitySorter().sortDbEntities(entities, deleteOrder);
         return entities;
     }
 
-    public List<DbEntity> dbEntitiesInDeleteOrder(DataMap map) {
-        return dbEntitiesInInsertOrder(map).reversed();
-    }
-
     private List<DbEntity> excludeEntities(Collection<DbEntity> entities) {
         // exclude various unsupported tests...
 

Reply via email to