diff options
3 files changed, 40 insertions, 16 deletions
diff --git a/main/src/cgeo/geocaching/connector/gc/UTFGridPosition.java b/main/src/cgeo/geocaching/connector/gc/UTFGridPosition.java index 5965fff..eff193a 100644 --- a/main/src/cgeo/geocaching/connector/gc/UTFGridPosition.java +++ b/main/src/cgeo/geocaching/connector/gc/UTFGridPosition.java @@ -15,9 +15,12 @@ public final class UTFGridPosition { private final static Pattern PATTERN_JSON_KEY = Pattern.compile("[^\\d]*" + "(\\d+),\\s*(\\d+)" + "[^\\d]*"); // (12, 34) public UTFGridPosition(final int x, final int y) { - assert x >= 0 && x <= UTFGrid.GRID_MAXX : "x outside bounds"; - assert y >= 0 && y <= UTFGrid.GRID_MAXY : "y outside bounds"; - + if (x < 0 || x > UTFGrid.GRID_MAXX) { + throw new IllegalArgumentException("x outside bounds"); + } + if (y < 0 || y > UTFGrid.GRID_MAXY) { + throw new IllegalArgumentException("y outside bounds"); + } this.x = x; this.y = y; } diff --git a/tests/src/cgeo/geocaching/connector/gc/GCBaseTest.java b/tests/src/cgeo/geocaching/connector/gc/GCBaseTest.java index d70ab9b..29fd886 100644 --- a/tests/src/cgeo/geocaching/connector/gc/GCBaseTest.java +++ b/tests/src/cgeo/geocaching/connector/gc/GCBaseTest.java @@ -14,19 +14,6 @@ import junit.framework.TestCase; public class GCBaseTest extends TestCase { - public static void testSplitJSONKey() { - assertKey("(1, 2)", 1, 2); - assertKey("(12, 34)", 12, 34); - assertKey("(1234,56)", 1234, 56); - assertKey("(1234, 567)", 1234, 567); - } - - private static void assertKey(String key, int x, int y) { - final UTFGridPosition pos = UTFGridPosition.fromString(key); - assertEquals(x, pos.getX()); - assertEquals(y, pos.getY()); - } - public static void testSearchFromMap() { final MockedCache mockedCache = new GC2CJPF(); diff --git a/tests/src/cgeo/geocaching/connector/gc/UTFGridPositionTest.java b/tests/src/cgeo/geocaching/connector/gc/UTFGridPositionTest.java new file mode 100644 index 0000000..a742faf --- /dev/null +++ b/tests/src/cgeo/geocaching/connector/gc/UTFGridPositionTest.java @@ -0,0 +1,34 @@ +package cgeo.geocaching.connector.gc; + +import junit.framework.TestCase; + +public class UTFGridPositionTest extends TestCase { + + public static void testValidUTFGridPosition() { + assertNotNull(new UTFGridPosition(0, 0)); + } + + public static void testInvalidUTFGridPosition() { + boolean valid = true; + try { + assertNotNull(new UTFGridPosition(-1, 0)); + } catch (Exception e) { + valid = false; + } + assertFalse(valid); + } + + public static void testFromString() throws Exception { + assertXYFromString("(1, 2)", 1, 2); + assertXYFromString("(12, 34)", 12, 34); + assertXYFromString("(34,56)", 34, 56); + assertXYFromString("(34, 56)", 34, 56); + } + + private static void assertXYFromString(final String key, int x, int y) { + final UTFGridPosition pos = UTFGridPosition.fromString(key); + assertEquals(x, pos.getX()); + assertEquals(y, pos.getY()); + } + +} |
