diff --git a/openjpa-lib/src/main/java/org/apache/openjpa/lib/graph/DepthFirstAnalysis.java b/openjpa-lib/src/main/java/org/apache/openjpa/lib/graph/DepthFirstAnalysis.java index ae7647450..0f4693b7d 100644 --- a/openjpa-lib/src/main/java/org/apache/openjpa/lib/graph/DepthFirstAnalysis.java +++ b/openjpa-lib/src/main/java/org/apache/openjpa/lib/graph/DepthFirstAnalysis.java @@ -216,13 +216,13 @@ public class DepthFirstAnalysis { List cycle; int pos = 0; - if (path != null && edge.getFrom() != edge.getTo()) { + if (path != null && !edge.getFrom().equals(edge.getTo())) { // Not a single edge loop pos = findNodeInPath(edge.getTo(), path); assert (pos >= 0): _loc.get("node-not-on-path", edge, edge.getTo()); } else { - assert (edge.getFrom() == edge.getTo()): - _loc.get("edge-no-loop", edge).getMessage(); + assert (edge.getFrom().equals(edge.getTo())): + _loc.get("edge-no-loop", edge).getMessage(); path = null; } cycle = buildCycle(edge, path, pos); @@ -249,8 +249,8 @@ public class DepthFirstAnalysis { Edge edge = (Edge) itr.next(); Object other = edge.getOther(node); // Single edge loops are ignored - if (node != other) { - if (other == cycleTo) { + if (!node.equals(other)) { + if (other.equals(cycleTo)) { // Cycle complete path.add(edge); found = true; @@ -279,7 +279,7 @@ public class DepthFirstAnalysis { int pos = -1; if (path != null) { for (int i = 0; i < path.size(); i++) { - if (((Edge)path.get(i)).getFrom() == node) { + if (((Edge)path.get(i)).getFrom().equals(node)) { pos = i; } } diff --git a/openjpa-lib/src/main/java/org/apache/openjpa/lib/graph/Edge.java b/openjpa-lib/src/main/java/org/apache/openjpa/lib/graph/Edge.java index 5d5b1a7fe..94522a8ca 100644 --- a/openjpa-lib/src/main/java/org/apache/openjpa/lib/graph/Edge.java +++ b/openjpa-lib/src/main/java/org/apache/openjpa/lib/graph/Edge.java @@ -106,9 +106,9 @@ public class Edge { * given node is not part of this edge. */ public Object getOther(Object node) { - if (_to == node) + if (_to.equals(node)) return _from; - if (_from == node) + if (_from.equals(node)) return _to; return null; } @@ -118,7 +118,7 @@ public class Edge { * this method returns true if either side is equal to the given node. */ public boolean isTo(Object node) { - return _to == node || (!_directed && _from == node); + return _to.equals(node) || (!_directed && _from.equals(node)); } /** @@ -127,7 +127,7 @@ public class Edge { * node. */ public boolean isFrom(Object node) { - return _from == node || (!_directed && _to == node); + return _from.equals(node) || (!_directed && _to.equals(node)); } /** diff --git a/openjpa-lib/src/main/java/org/apache/openjpa/lib/graph/Graph.java b/openjpa-lib/src/main/java/org/apache/openjpa/lib/graph/Graph.java index e056bfe73..6077e6d0b 100644 --- a/openjpa-lib/src/main/java/org/apache/openjpa/lib/graph/Graph.java +++ b/openjpa-lib/src/main/java/org/apache/openjpa/lib/graph/Graph.java @@ -21,9 +21,9 @@ package org.apache.openjpa.lib.graph; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.Map; /** @@ -36,8 +36,11 @@ import java.util.Map; */ public class Graph { - // map each node to list of edges from that node - private final Map _nodes = new HashMap(); + /** + * Map each node to list of edges from that node. + * Using a LinkedHashMap to ensure order of iterator processing. + */ + private final Map _nodes = new LinkedHashMap(); /** * Clear the graph. diff --git a/openjpa-lib/src/test/java/org/apache/openjpa/lib/graph/TestDepthFirstAnalysis.java b/openjpa-lib/src/test/java/org/apache/openjpa/lib/graph/TestDepthFirstAnalysis.java index d16170b5a..40c663cb1 100644 --- a/openjpa-lib/src/test/java/org/apache/openjpa/lib/graph/TestDepthFirstAnalysis.java +++ b/openjpa-lib/src/test/java/org/apache/openjpa/lib/graph/TestDepthFirstAnalysis.java @@ -58,23 +58,23 @@ public class TestDepthFirstAnalysis public void setUpGraph2() { Graph graph = new Graph(); - Integer node1 = new Integer(5); - Integer node2 = new Integer(4); + Integer node1 = new Integer(1); + Integer node2 = new Integer(2); Integer node3 = new Integer(3); - Integer node4 = new Integer(2); - Integer node5 = new Integer(1); - graph.addNode(node1); - graph.addNode(node2); - graph.addNode(node3); - graph.addNode(node4); + Integer node4 = new Integer(4); + Integer node5 = new Integer(5); + graph.addNode(node2); // has to be first node for testcase graph.addNode(node5); - graph.addEdge(new Edge(node1, node2, true)); - graph.addEdge(new Edge(node2, node3, true)); + graph.addNode(node4); + graph.addNode(node3); + graph.addNode(node1); + graph.addEdge(new Edge(node5, node4, true)); + graph.addEdge(new Edge(node4, node3, true)); graph.addEdge(new Edge(node3, node3, true)); - graph.addEdge(new Edge(node3, node4, true)); - graph.addEdge(new Edge(node4, node1, true)); - graph.addEdge(new Edge(node4, node2, true)); - graph.addEdge(new Edge(node5, node2, true)); + graph.addEdge(new Edge(node3, node2, true)); + graph.addEdge(new Edge(node2, node5, true)); + graph.addEdge(new Edge(node2, node4, true)); + graph.addEdge(new Edge(node1, node4, true)); _dfa = new DepthFirstAnalysis(graph); } @@ -97,8 +97,8 @@ public class TestDepthFirstAnalysis Iterator itr = edges.iterator(); Edge edge0 = (Edge) itr.next(); Edge edge1 = (Edge) itr.next(); - assertTrue((edge0.getTo() == edge0.getFrom()) - || edge1.getTo() == edge1.getFrom()); + assertTrue((edge0.getTo().equals(edge0.getFrom())) + || edge1.getTo().equals(edge1.getFrom())); } public void testBackEdges() { @@ -108,17 +108,17 @@ public class TestDepthFirstAnalysis Iterator itr = edges.iterator(); Edge edge0 = (Edge) itr.next(); Edge edge1 = (Edge) itr.next(); - if (edge0.getTo() == edge0.getFrom()) { + if (edge0.getTo().equals(edge0.getFrom())) { assertTrue(edge0.getCycle() != null && edge0.getCycle().size() == 1); List cycle = edge1.getCycle(); assertTrue(cycle != null && cycle.size() == 4); - assertTrue(((Edge)cycle.get(0)).getFrom() == ((Edge)cycle.get(3)).getTo()); - } else if (edge1.getTo() == edge1.getFrom()) { + assertTrue(((Edge)cycle.get(0)).getFrom().equals(((Edge)cycle.get(3)).getTo())); + } else if (edge1.getTo().equals(edge1.getFrom())) { assertTrue(edge1.getCycle() != null && edge1.getCycle().size() == 1); assertTrue(edge1 == edge1.getCycle()); List cycle = edge0.getCycle(); assertTrue(cycle != null && cycle.size() == 4); - assertTrue(((Edge)cycle.get(0)).getFrom() == ((Edge)cycle.get(3)).getTo()); + assertTrue(((Edge)cycle.get(0)).getFrom().equals(((Edge)cycle.get(3)).getTo())); } else { // should not happen assertFalse(true); @@ -135,11 +135,11 @@ public class TestDepthFirstAnalysis if (edge0.getCycle() == null) { List cycle = edge1.getCycle(); assertTrue(cycle != null && cycle.size() == 3); - assertTrue(((Edge)cycle.get(0)).getFrom() == ((Edge)cycle.get(2)).getTo()); + assertTrue(((Edge)cycle.get(0)).getFrom().equals(((Edge)cycle.get(2)).getTo())); } else if (edge1.getCycle() == null) { List cycle = edge0.getCycle(); assertTrue(cycle != null && cycle.size() == 3); - assertTrue(((Edge)cycle.get(0)).getFrom() == ((Edge)cycle.get(2)).getTo()); + assertTrue(((Edge)cycle.get(0)).getFrom().equals(((Edge)cycle.get(2)).getTo())); } else { // should not happen assertFalse(true);