Index: /trunk/src/org/openstreetmap/josm/data/osm/NodeGraph.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/data/osm/NodeGraph.java	(revision 12477)
+++ /trunk/src/org/openstreetmap/josm/data/osm/NodeGraph.java	(revision 12478)
@@ -21,4 +21,12 @@
  */
 public class NodeGraph {
+
+    /**
+     * Builds a list of pair of nodes from the given way.
+     * @param way way
+     * @param directed if {@code true} each pair of nodes will occur once, in the way nodes order.
+     *                 if {@code false} each pair of nodes will occur twice (the pair and its inversed copy)
+     * @return a list of pair of nodes from the given way
+     */
     public static List<NodePair> buildNodePairs(Way way, boolean directed) {
         List<NodePair> pairs = new ArrayList<>();
@@ -32,4 +40,11 @@
     }
 
+    /**
+     * Builds a list of pair of nodes from the given ways.
+     * @param ways ways
+     * @param directed if {@code true} each pair of nodes will occur once, in the way nodes order.
+     *                 if {@code false} each pair of nodes will occur twice (the pair and its inversed copy)
+     * @return a list of pair of nodes from the given ways
+     */
     public static List<NodePair> buildNodePairs(List<Way> ways, boolean directed) {
         List<NodePair> pairs = new ArrayList<>();
@@ -40,4 +55,9 @@
     }
 
+    /**
+     * Builds a new list of pair nodes without the duplicated pairs (including inversed copies).
+     * @param pairs existing list of pairs
+     * @return a new list of pair nodes without the duplicated pairs
+     */
     public static List<NodePair> eliminateDuplicateNodePairs(List<NodePair> pairs) {
         List<NodePair> cleaned = new ArrayList<>();
@@ -242,21 +262,21 @@
      */
     protected List<Node> buildSpanningPath(Node startNode) {
-        if (startNode == null)
-            return null;
-        Stack<NodePair> path = new Stack<>();
-        Stack<NodePair> nextPairs = new Stack<>();
-        nextPairs.addAll(getOutboundPairs(startNode));
-        while (!nextPairs.isEmpty()) {
-            NodePair cur = nextPairs.pop();
-            if (!path.contains(cur) && !path.contains(cur.swap())) {
-                while (!path.isEmpty() && !path.peek().isPredecessorOf(cur)) {
-                    path.pop();
+        if (startNode != null) {
+            Stack<NodePair> path = new Stack<>();
+            Stack<NodePair> nextPairs = new Stack<>();
+            nextPairs.addAll(getOutboundPairs(startNode));
+            while (!nextPairs.isEmpty()) {
+                NodePair cur = nextPairs.pop();
+                if (!path.contains(cur) && !path.contains(cur.swap())) {
+                    while (!path.isEmpty() && !path.peek().isPredecessorOf(cur)) {
+                        path.pop();
+                    }
+                    path.push(cur);
+                    if (isSpanningWay(path)) return buildPathFromNodePairs(path);
+                    nextPairs.addAll(getOutboundPairs(path.peek()));
                 }
-                path.push(cur);
-                if (isSpanningWay(path)) return buildPathFromNodePairs(path);
-                nextPairs.addAll(getOutboundPairs(path.peek()));
-            }
-        }
-        return null;
+            }
+        }
+        return Collections.emptyList();
     }
 
@@ -280,5 +300,5 @@
         for (Node n: nodes) {
             List<Node> path = buildSpanningPath(n);
-            if (path != null)
+            if (!path.isEmpty())
                 return path;
         }
Index: /trunk/test/unit/org/openstreetmap/josm/data/osm/NodeGraphTest.java
===================================================================
--- /trunk/test/unit/org/openstreetmap/josm/data/osm/NodeGraphTest.java	(revision 12478)
+++ /trunk/test/unit/org/openstreetmap/josm/data/osm/NodeGraphTest.java	(revision 12478)
@@ -0,0 +1,79 @@
+// License: GPL. For details, see LICENSE file.
+package org.openstreetmap.josm.data.osm;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.openstreetmap.josm.testutils.JOSMTestRules;
+
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+
+/**
+ * Unit tests of the {@code NodeGraph} class.
+ */
+public class NodeGraphTest {
+
+    /**
+     * Setup test.
+     */
+    @Rule
+    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
+    public JOSMTestRules test = new JOSMTestRules();
+
+    /**
+     * Unit test of {@link NodeGraph#buildNodePairs} and {@link NodeGraph#eliminateDuplicateNodePairs}
+     */
+    @Test
+    public void testNodePairs() {
+        assertTrue(NodeGraph.buildNodePairs(Collections.emptyList(), true).isEmpty());
+        assertTrue(NodeGraph.buildNodePairs(Collections.emptyList(), false).isEmpty());
+
+        Way w1 = new Way(1);
+        Way w2 = new Way(2);
+
+        Node n1 = new Node(1);
+        Node n2 = new Node(2);
+        Node n3 = new Node(3);
+
+        Node n4 = new Node(4);
+        Node n5 = new Node(5);
+        Node n6 = new Node(6);
+
+        w1.setNodes(Arrays.asList(n1, n2, n3));
+        w2.setNodes(Arrays.asList(n4, n5, n6, n4));
+
+        w1.setIncomplete(false);
+        w2.setIncomplete(false);
+
+        List<Way> ways = Arrays.asList(w1, w2);
+
+        List<NodePair> l1 = NodeGraph.buildNodePairs(ways, true);
+        List<NodePair> l2 = NodeGraph.buildNodePairs(ways, false);
+
+        assertEquals(Arrays.asList(
+                new NodePair(n1, n2),
+                new NodePair(n2, n3),
+                new NodePair(n4, n5),
+                new NodePair(n5, n6),
+                new NodePair(n6, n4)
+                ), l1);
+
+        assertEquals(l1, NodeGraph.eliminateDuplicateNodePairs(l1));
+
+        assertEquals(Arrays.asList(
+                new NodePair(n1, n2), new NodePair(n2, n1),
+                new NodePair(n2, n3), new NodePair(n3, n2),
+                new NodePair(n4, n5), new NodePair(n5, n4),
+                new NodePair(n5, n6), new NodePair(n6, n5),
+                new NodePair(n6, n4), new NodePair(n4, n6)
+                ), l2);
+
+        assertEquals(l1, NodeGraph.eliminateDuplicateNodePairs(l2));
+    }
+}
