Index: trunk/test/unit/org/openstreetmap/josm/data/imagery/WMTSTileSourceTest.java
===================================================================
--- trunk/test/unit/org/openstreetmap/josm/data/imagery/WMTSTileSourceTest.java	(revision 8585)
+++ trunk/test/unit/org/openstreetmap/josm/data/imagery/WMTSTileSourceTest.java	(revision 8586)
@@ -11,5 +11,4 @@
 import org.junit.BeforeClass;
 import org.junit.Test;
-import org.openstreetmap.gui.jmapviewer.TileXY;
 import org.openstreetmap.gui.jmapviewer.tilesources.TemplatedTMSTileSource;
 import org.openstreetmap.josm.JOSMFixture;
@@ -70,5 +69,5 @@
         assertEquals("TileXMax", 2, testSource.getTileXMax(2));
         assertEquals("TileYMax", 2, testSource.getTileYMax(2));
-        assertEquals("TileXMax", 5, testSource.getTileXMax(3));
+        assertEquals("TileXMax", 4, testSource.getTileXMax(3));
         assertEquals("TileYMax", 4, testSource.getTileYMax(3));
 
@@ -79,15 +78,26 @@
         Main.setProjection(Projections.getProjectionByCode("EPSG:31370"));
         WMTSTileSource testSource = new WMTSTileSource(testImageryWALLONIE);
+        assertEquals("http://geoservices.wallonie.be/arcgis/rest/services/DONNEES_BASE/FOND_PLAN_ANNOTATIONS_2012_RW_NB/"
+                + "MapServer/WMTS/tile/1.0.0/DONNEES_BASE_FOND_PLAN_ANNOTATIONS_2012_RW_NB/default/default028mm/5/1219/1063.png",
+                testSource.getTileUrl(6, 1063, 1219));
+
+        // +bounds=2.54,49.51,6.4,51.5
         Bounds wallonieBounds = new Bounds(
                 new LatLon(49.485372459967245, 2.840548314430268),
                 new LatLon(50.820959517561256, 6.427849693016202)
                 );
-        verifyBounds(wallonieBounds, testSource, 10, 20324, 17724);
+        verifyBounds(wallonieBounds, testSource, 6, 1063, 1219);
+        verifyBounds(wallonieBounds, testSource, 11, 17724, 20324);
+        LatLon ll = new LatLon(testSource.tileXYToLatLon(1063, 1219, 6));
 
     }
 
     private void verifyBounds(Bounds bounds, WMTSTileSource testSource, int z, int x, int y) {
-        LatLon ret = new LatLon(testSource.tileXYToLatLon(y, y, z));
+        LatLon ret = new LatLon(testSource.tileXYToLatLon(x, y, z));
         assertTrue(ret.toDisplayString() + " doesn't lie within: " + bounds.toString(), bounds.contains(ret));
+        int tileXmax = testSource.getTileXMax(z);
+        int tileYmax = testSource.getTileYMax(z);
+        assertTrue("tile x: " + x + " is greater than allowed max: " + tileXmax, tileXmax >= x);
+        assertTrue("tile y: " + y + " is greater than allowed max: " + tileYmax, tileYmax >= y);
     }
 
@@ -96,7 +106,6 @@
         Main.setProjection(Projections.getProjectionByCode("EPSG:3857"));
         WMTSTileSource testSource = new WMTSTileSource(testImageryWIEN);
-        int zoomOffset = 10;
-
-        // Linz - 11/1105/709.png
+        int zoomOffset = 9;
+
         verifyMercatorTile(testSource, 0, 0, 1, zoomOffset);
         verifyMercatorTile(testSource, 1105, 709, 2, zoomOffset);
@@ -106,10 +115,4 @@
         verifyMercatorTile(testSource, 1, 1, 2, zoomOffset);
 
-
-        LatLon ll = new LatLon(testSource.tileXYToLatLon(500,  500, 1));
-
-        TileXY xy = testSource.latLonToTileXY(new LatLon(48.21, 14.24).toCoordinate(), 1);
-        assertTrue("X index is negative: " + xy.getXIndex(), xy.getXIndex() > 0);
-        assertTrue(xy.getYIndex() > 0);
         for (int x = 0; x < 4; x++) {
             for (int y = 0; y < 4; y++) {
@@ -119,16 +122,23 @@
         for (int x = 0; x < 8; x++) {
             for (int y = 0; y < 4; y++) {
-                verifyMercatorTile(testSource, x, y, zoomOffset);
-            }
-        }
-
-        verifyMercatorTile(testSource, 2 << 9 - 1, 2 << 8 - 1, zoomOffset);
-
-        assertEquals("TileXMax", 1, testSource.getTileXMax(1));
-        assertEquals("TileYMax", 1, testSource.getTileYMax(1));
-        assertEquals("TileXMax", 2, testSource.getTileXMax(2));
-        assertEquals("TileYMax", 2, testSource.getTileYMax(2));
-        assertEquals("TileXMax", 4, testSource.getTileXMax(3));
-        assertEquals("TileYMax", 4, testSource.getTileYMax(3));
+                verifyMercatorTile(testSource, x, y, 4, zoomOffset);
+            }
+        }
+
+        verifyMercatorTile(testSource, 2 << 9 - 1, 2 << 8 - 1, 2, zoomOffset);
+
+        verifyMercatorMax(testSource, 1, zoomOffset);
+        verifyMercatorMax(testSource, 2, zoomOffset);
+        verifyMercatorMax(testSource, 3, zoomOffset);
+    }
+
+    private void verifyMercatorMax(WMTSTileSource testSource, int zoom, int zoomOffset) {
+        TemplatedTMSTileSource verifier = new TemplatedTMSTileSource(testImageryTMS);
+        int result = testSource.getTileXMax(zoom);
+        int expected = verifier.getTileXMax(zoom + zoomOffset);
+        assertTrue("TileXMax expected: " + expected + " got: " + result, Math.abs(result - expected) < 5);
+        result = testSource.getTileYMax(zoom);
+        expected = verifier.getTileYMax(zoom + zoomOffset);
+        assertTrue("TileYMax expected: " + expected + " got: " + result, Math.abs(result - expected) < 5);
     }
 
@@ -141,13 +151,13 @@
         verifyTile(new LatLon(51.1268639, 16.8731360), testSource, 1, 1, 2);
 
-        assertEquals("TileXMax", 37, testSource.getTileXMax(1));
-        assertEquals("TileYMax", 19, testSource.getTileYMax(1));
-        assertEquals("TileXMax", 74, testSource.getTileXMax(2));
-        assertEquals("TileYMax", 37, testSource.getTileYMax(2));
-        assertEquals("TileXMax", 148, testSource.getTileXMax(3));
-        assertEquals("TileYMax", 74, testSource.getTileYMax(3));
+        assertEquals("TileXMax", 2, testSource.getTileXMax(1));
+        assertEquals("TileYMax", 1, testSource.getTileYMax(1));
+        assertEquals("TileXMax", 3, testSource.getTileXMax(2));
+        assertEquals("TileYMax", 2, testSource.getTileYMax(2));
+        assertEquals("TileXMax", 6, testSource.getTileXMax(3));
+        assertEquals("TileYMax", 4, testSource.getTileYMax(3));
         assertEquals(
                 "http://mapy.geoportal.gov.pl/wss/service/WMTS/guest/wmts/TOPO?SERVICE=WMTS&REQUEST=GetTile&"
-                + "VERSION=1.0.0&LAYER=MAPA TOPOGRAFICZNA&STYLE=&FORMAT=image/jpeg&tileMatrixSet=EPSG:4326&"
+                + "VERSION=1.0.0&LAYER=MAPA TOPOGRAFICZNA&STYLE=default&FORMAT=image/jpeg&tileMatrixSet=EPSG:4326&"
                 + "tileMatrix=EPSG:4326:0&tileRow=1&tileCol=1",
                 testSource.getTileUrl(1,  1,  1));
@@ -159,5 +169,4 @@
         WMTSTileSource testSource = new WMTSTileSource(testImageryORTO_PL);
         verifyTile(new LatLon(53.5993712684958, 19.560669777688176), testSource, 12412, 3941, 14);
-
         verifyTile(new LatLon(49.783096954497786, 22.79034127751704), testSource, 17714, 10206, 14);
     }
@@ -187,12 +196,7 @@
         LatLon result = new LatLon(testSource.tileXYToLatLon(x, y, z));
         LatLon expected = new LatLon(verifier.tileXYToLatLon(x, y, z + zoomOffset));
-        System.out.println(z + "/" + x + "/" + y + " - result: " + result.toDisplayString() + " osmMercator: " +  expected.toDisplayString());
+        //System.out.println(z + "/" + x + "/" + y + " - result: " + result.toDisplayString() + " osmMercator: " +  expected.toDisplayString());
         assertEquals("Longitude" , expected.lon(), result.lon(), 1e-04);
         assertEquals("Latitude", expected.lat(), result.lat(), 1e-04);
-        //assertTrue("result: " + result.toDisplayString() + " osmMercator: " +  expected.toDisplayString(), result.equalsEpsilon(expected));
-//        LatLon tileCenter = new Bounds(result, new LatLon(testSource.tileXYToLatLon(x+1, y+1, z))).getCenter();
-//        TileXY backwardsResult = testSource.latLonToTileXY(tileCenter.toCoordinate(), z);
-        //assertEquals(x, backwardsResult.getXIndex());
-        //assertEquals(y, backwardsResult.getYIndex());
     }
 }
