Index: trunk/test/data/regress/9599/ex5.osm
===================================================================
--- trunk/test/data/regress/9599/ex5.osm	(revision 15883)
+++ trunk/test/data/regress/9599/ex5.osm	(revision 15883)
@@ -0,0 +1,32 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<osm version='0.6' upload='true' generator='JOSM'>
+  <bounds minlat='43.3280894' minlon='6.5462664' maxlat='43.3282713' maxlon='6.5464564' origin='CGImap 0.3.1 (29703 thorn-02.openstreetmap.org)' />
+  <node id='2632536012' timestamp='2014-01-22T17:11:29Z' uid='875219' user='Maxime DIVO' visible='true' version='1' changeset='20145216' lat='43.3281651' lon='6.546396' />
+  <node id='2632536021' timestamp='2014-01-22T17:11:30Z' uid='875219' user='Maxime DIVO' visible='true' version='1' changeset='20145216' lat='43.3281697' lon='6.5463519' />
+  <node id='2632536045' timestamp='2014-01-22T17:11:30Z' uid='875219' user='Maxime DIVO' visible='true' version='1' changeset='20145216' lat='43.3281937' lon='6.5463563' />
+  <node id='2632536051' timestamp='2014-01-22T17:11:30Z' uid='875219' user='Maxime DIVO' visible='true' version='1' changeset='20145216' lat='43.3281998' lon='6.5462943' />
+  <node id='2632536055' timestamp='2014-01-22T17:11:30Z' uid='875219' user='Maxime DIVO' visible='true' version='1' changeset='20145216' lat='43.3282134' lon='6.5464038' />
+  <node id='2632536063' timestamp='2014-01-22T17:11:31Z' uid='875219' user='Maxime DIVO' visible='true' version='1' changeset='20145216' lat='43.3282302' lon='6.5464066' />
+  <node id='2632536064' timestamp='2014-01-22T17:11:31Z' uid='875219' user='Maxime DIVO' visible='true' version='1' changeset='20145216' lat='43.328232' lon='6.5463556' />
+  <node id='2632536067' timestamp='2014-01-22T17:11:31Z' uid='875219' user='Maxime DIVO' visible='true' version='1' changeset='20145216' lat='43.328234' lon='6.5463002' />
+  <way id='257786939' timestamp='2014-01-23T06:39:25Z' uid='870861' user='botdidier2020' visible='true' version='2' changeset='20154690'>
+    <nd ref='2632536064' />
+    <nd ref='2632536055' />
+    <nd ref='2632536063' />
+    <nd ref='2632536064' />
+    <tag k='building' v='yes' />
+    <tag k='source' v='cadastre-dgi-fr source : Direction Générale des Impôts - Cadastre. Mise à jour : 2013' />
+  </way>
+  <way id='257789043' timestamp='2014-01-23T06:44:02Z' uid='870861' user='botdidier2020' visible='true' version='2' changeset='20154690'>
+    <nd ref='2632536055' />
+    <nd ref='2632536064' />
+    <nd ref='2632536067' />
+    <nd ref='2632536051' />
+    <nd ref='2632536045' />
+    <nd ref='2632536021' />
+    <nd ref='2632536012' />
+    <nd ref='2632536055' />
+    <tag k='building' v='yes' />
+    <tag k='source' v='cadastre-dgi-fr source : Direction Générale des Impôts - Cadastre. Mise à jour : 2013' />
+  </way>
+</osm>
Index: trunk/test/data/regress/9599/three_old.osm
===================================================================
--- trunk/test/data/regress/9599/three_old.osm	(revision 15883)
+++ trunk/test/data/regress/9599/three_old.osm	(revision 15883)
@@ -0,0 +1,45 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<osm version='0.6' upload='never' generator='JOSM'>
+  <node id='353225239' timestamp='2009-02-27T13:45:58Z' uid='64197' user='Lothar Schiborr_1' visible='true' version='1' changeset='684159' lat='52.8972935' lon='8.4027792' />
+  <node id='353225241' timestamp='2009-02-27T13:45:58Z' uid='64197' user='Lothar Schiborr_1' visible='true' version='1' changeset='684159' lat='52.8972734' lon='8.402951' />
+  <node id='353225244' timestamp='2009-02-27T13:45:58Z' uid='64197' user='Lothar Schiborr_1' visible='true' version='1' changeset='684159' lat='52.8972311' lon='8.4027592' />
+  <node id='353225247' timestamp='2009-02-27T13:45:58Z' uid='64197' user='Lothar Schiborr_1' visible='true' version='1' changeset='684159' lat='52.897211' lon='8.402931' />
+  <node id='353225249' timestamp='2009-02-27T13:45:58Z' uid='64197' user='Lothar Schiborr_1' visible='true' version='1' changeset='684159' lat='52.8972161' lon='8.4028895' />
+  <node id='353225252' timestamp='2009-02-27T13:45:58Z' uid='64197' user='Lothar Schiborr_1' visible='true' version='1' changeset='684159' lat='52.8971916' lon='8.4027464' />
+  <node id='353225257' timestamp='2009-02-27T13:45:58Z' uid='64197' user='Lothar Schiborr_1' visible='true' version='1' changeset='684159' lat='52.8971763' lon='8.4028767' />
+  <node id='353225260' timestamp='2009-02-27T13:45:59Z' uid='64197' user='Lothar Schiborr_1' visible='true' version='1' changeset='684159' lat='52.8971682' lon='8.4028638' />
+  <node id='353225263' timestamp='2009-02-27T13:45:59Z' uid='64197' user='Lothar Schiborr_1' visible='true' version='1' changeset='684159' lat='52.8971543' lon='8.4029921' />
+  <node id='353225267' timestamp='2009-02-27T13:45:59Z' uid='64197' user='Lothar Schiborr_1' visible='true' version='1' changeset='684159' lat='52.8971322' lon='8.4028531' />
+  <node id='353225269' timestamp='2009-02-27T13:45:59Z' uid='64197' user='Lothar Schiborr_1' visible='true' version='1' changeset='684159' lat='52.8971182' lon='8.4029813' />
+  <node id='353225273' timestamp='2009-02-27T13:45:59Z' uid='64197' user='Lothar Schiborr_1' visible='true' version='1' changeset='684159' lat='52.8971772' lon='8.4028662' />
+  <node id='353225277' timestamp='2009-02-27T13:45:59Z' uid='64197' user='Lothar Schiborr_1' visible='true' version='1' changeset='684159' lat='52.8971669' lon='8.4028741' />
+  <way id='31567319' timestamp='2009-02-27T13:46:00Z' uid='64197' user='Lothar Schiborr_1' visible='true' version='1' changeset='684159'>
+    <nd ref='353225239' />
+    <nd ref='353225244' />
+    <nd ref='353225252' />
+    <nd ref='353225273' />
+    <nd ref='353225257' />
+    <nd ref='353225249' />
+    <nd ref='353225247' />
+    <nd ref='353225241' />
+    <nd ref='353225239' />
+    <tag k='building' v='yes' />
+  </way>
+  <way id='31567320' timestamp='2009-02-27T13:46:00Z' uid='64197' user='Lothar Schiborr_1' visible='true' version='1' changeset='684159'>
+    <nd ref='353225260' />
+    <nd ref='353225267' />
+    <nd ref='353225269' />
+    <nd ref='353225263' />
+    <nd ref='353225277' />
+    <nd ref='353225260' />
+    <tag k='building' v='yes' />
+  </way>
+  <way id='31567321' timestamp='2009-02-27T13:46:00Z' uid='64197' user='Lothar Schiborr_1' visible='true' version='1' changeset='684159'>
+    <nd ref='353225260' />
+    <nd ref='353225273' />
+    <nd ref='353225257' />
+    <nd ref='353225277' />
+    <nd ref='353225260' />
+    <tag k='building' v='yes' />
+  </way>
+</osm>
Index: trunk/test/unit/org/openstreetmap/josm/actions/JoinAreasActionTest.java
===================================================================
--- trunk/test/unit/org/openstreetmap/josm/actions/JoinAreasActionTest.java	(revision 15882)
+++ trunk/test/unit/org/openstreetmap/josm/actions/JoinAreasActionTest.java	(revision 15883)
@@ -52,4 +52,50 @@
 
     /**
+     * Non-regression test for bug #9599.
+     * @throws IOException if any I/O error occurs
+     * @throws IllegalDataException if OSM parsing fails
+     */
+    @Test
+    public void testTicket9599() throws IOException, IllegalDataException {
+        try (InputStream is = TestUtils.getRegressionDataStream(9599, "ex5.osm")) {
+            DataSet ds = OsmReader.parseDataSet(is, null);
+            Layer layer = new OsmDataLayer(ds, null, null);
+            MainApplication.getLayerManager().addLayer(layer);
+            try {
+                new JoinAreasAction(false).join(ds.getWays());
+                Collection<IPrimitive> found = SearchAction.searchAndReturn("type:way", SearchMode.replace);
+                assertEquals(1, found.size());
+                assertEquals(257786939, found.iterator().next().getUniqueId());
+            } finally {
+                // Ensure we clean the place before leaving, even if test fails.
+                MainApplication.getLayerManager().removeLayer(layer);
+            }
+        }
+    }
+
+    /**
+     * Non-regression test for bug #9599.
+     * @throws IOException if any I/O error occurs
+     * @throws IllegalDataException if OSM parsing fails
+     */
+    @Test
+    public void testTicket9599Simple() throws IOException, IllegalDataException {
+        try (InputStream is = TestUtils.getRegressionDataStream(9599, "three_old.osm")) {
+            DataSet ds = OsmReader.parseDataSet(is, null);
+            Layer layer = new OsmDataLayer(ds, null, null);
+            MainApplication.getLayerManager().addLayer(layer);
+            try {
+                new JoinAreasAction(false).join(ds.getWays());
+                Collection<IPrimitive> found = SearchAction.searchAndReturn("type:way", SearchMode.replace);
+                assertEquals(1, found.size());
+                assertEquals(31567319, found.iterator().next().getUniqueId());
+            } finally {
+                // Ensure we clean the place before leaving, even if test fails.
+                MainApplication.getLayerManager().removeLayer(layer);
+            }
+        }
+    }
+
+    /**
      * Non-regression test for bug #10511.
      * @throws IOException if any I/O error occurs
