Skip to content

Commit ed79b80

Browse files
committed
Added improved exception handling of isValidMapcodeFormat
1 parent 44ad1f7 commit ed79b80

File tree

6 files changed

+72
-6
lines changed

6 files changed

+72
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,10 @@ Normally, one of our developers should be able to comment on them and fix.
480480
mat
481481
These are the release notes for the Java library for mapcodes.
482482

483+
### 2.4.12
484+
485+
* Added unit tests to check internal data structures.
486+
483487
### 2.4.11
484488

485489
* Fixed a bug in `Mapcode.isValidMapcodeFormat()` which caused an exception when parsing a Unicode mapcode

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<artifactId>mapcode</artifactId>
99

1010
<packaging>jar</packaging>
11-
<version>2.4.11</version>
11+
<version>2.4.12</version>
1212

1313
<name>Mapcode Java Library</name>
1414
<description>

src/main/java/com/mapcode/Decoder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ static MapcodeZone decodeToMapcodeZone(@Nonnull final String argMapcode,
205205
// Private methods.
206206
// ----------------------------------------------------------------------
207207

208-
private final static int[] DECODE_CHARS = {
208+
final static int[] DECODE_CHARS = {
209209
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
210210
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
211211
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
@@ -224,7 +224,7 @@ static MapcodeZone decodeToMapcodeZone(@Nonnull final String argMapcode,
224224
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
225225
};
226226

227-
private static class Unicode2Ascii {
227+
static class Unicode2Ascii {
228228

229229
final char min;
230230
final char max;
@@ -245,7 +245,7 @@ private static class Unicode2Ascii {
245245
private static final char MISSCODE = '?';
246246

247247
// @formatter:off
248-
@SuppressWarnings("LongLine") private final static char[][] ASCII2LANGUAGE = {
248+
@SuppressWarnings("LongLine") final static char[][] ASCII2LANGUAGE = {
249249
// Character: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9
250250
/* Roman */ {'\u0041', '\u0042', '\u0043', '\u0044', '\u0045', '\u0046', '\u0047', '\u0048', '\u0049', '\u004a', '\u004b', '\u004c', '\u004d', '\u004e', '\u004f', '\u0050', '\u0051', '\u0052', '\u0053', '\u0054', '\u0055', '\u0056', '\u0057', '\u0058', '\u0059', '\u005a', '\u0030', '\u0031', '\u0032', '\u0033', '\u0034', '\u0035', '\u0036', '\u0037', '\u0038', '\u0039'}, // Roman
251251
/* Greek */ {'\u0391', '\u0392', '\u039e', '\u0394', '\u0388', '\u0395', '\u0393', '\u0397', '\u0399', '\u03a0', '\u039a', '\u039b', '\u039c', '\u039d', '\u039f', '\u03a1', '\u0398', '\u03a8', '\u03a3', '\u03a4', '\u0389', '\u03a6', '\u03a9', '\u03a7', '\u03a5', '\u0396', '\u0030', '\u0031', '\u0032', '\u0033', '\u0034', '\u0035', '\u0036', '\u0037', '\u0038', '\u0039'}, // Greek
@@ -279,7 +279,7 @@ private static class Unicode2Ascii {
279279
// @formatter:on
280280

281281
// @formatter:off
282-
@SuppressWarnings("LongLine") private final static Unicode2Ascii[] UNICODE2ASCII = {
282+
@SuppressWarnings("LongLine") final static Unicode2Ascii[] UNICODE2ASCII = {
283283
/* Roman */ new Unicode2Ascii('\u0041', '\u005a', "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), // Roman
284284
/* Greek */ new Unicode2Ascii('\u0388', '\u03a9', "EU???????ABGDFZHQIKLMNCOJP?STYVXRW"), // Greek
285285
/* Cyrillic */ new Unicode2Ascii('\u0410', '\u042f', "AZBGDEFNI?KLMHOJPCTYQXSVW????U?R"), // Cyrillic
@@ -728,7 +728,7 @@ static String decodeUTF16(@Nonnull final String mapcode) {
728728
if (mapcode.startsWith(String.valueOf(GREEK_CAPITAL_ALPHA))) {
729729
final String unpacked = aeuUnpack(result);
730730
if (unpacked.isEmpty()) {
731-
throw new AssertionError("decodeUTF16: cannot decode " + mapcode);
731+
throw new UnknownDecodeException("decodeUTF16: cannot decode " + mapcode);
732732
}
733733
result = Encoder.aeuPack(unpacked, false);
734734
}

src/main/java/com/mapcode/Mapcode.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ public static boolean isValidMapcodeFormat(@Nullable final String mapcode) throw
322322
return true;
323323
} catch (final UnknownPrecisionFormatException ignored) {
324324
return false;
325+
} catch (final UnknownDecodeException ignored2) {
326+
return false;
325327
}
326328
}
327329

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (C) 2014-2019, Stichting Mapcode Foundation (http://www.mapcode.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mapcode;
18+
19+
import javax.annotation.Nonnull;
20+
21+
/**
22+
* This runtime exception is thrown is decoding fails for an unknown reason.
23+
* The exception is not exposed externally.
24+
*/
25+
final class UnknownDecodeException extends IllegalStateException {
26+
private static final long serialVersionUID = 1L;
27+
28+
UnknownDecodeException(@Nonnull final String message) {
29+
super(message);
30+
assert message != null;
31+
}
32+
}

src/test/java/com/mapcode/DecoderTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.mapcode;
1818

19+
import com.mapcode.Decoder.Unicode2Ascii;
1920
import org.junit.Test;
2021
import org.slf4j.Logger;
2122
import org.slf4j.LoggerFactory;
@@ -26,9 +27,36 @@
2627
public class DecoderTest {
2728
private static final Logger LOG = LoggerFactory.getLogger(DecoderTest.class);
2829

30+
@Test
31+
public void tableDecodeChars() {
32+
LOG.info("tableDecodeChars");
33+
assertEquals("Length: " + Decoder.DECODE_CHARS.length, 256, Decoder.DECODE_CHARS.length);
34+
}
35+
36+
@Test
37+
public void tableAscii2Language() {
38+
LOG.info("tableAscii2Language");
39+
assertEquals(Decoder.ASCII2LANGUAGE.length, Decoder.UNICODE2ASCII.length - 10 /* Digits */ - 3 /* Lowercase */);
40+
int i = 1;
41+
for (char[] chars : Decoder.ASCII2LANGUAGE) {
42+
assertEquals("At row " + i + ", length: " + chars.length, 36, chars.length);
43+
++i;
44+
}
45+
}
46+
47+
@Test
48+
public void tableUnicodeToAscii() {
49+
LOG.info("tableUnicodeToAscii");
50+
for (Unicode2Ascii unicode2Ascii : Decoder.UNICODE2ASCII) {
51+
assertEquals("Error at: u" + Integer.toHexString(unicode2Ascii.min),
52+
(int) unicode2Ascii.max - (int) unicode2Ascii.min, unicode2Ascii.convert.length() - 1);
53+
}
54+
}
55+
2956
@SuppressWarnings("JUnitTestMethodWithNoAssertions")
3057
@Test
3158
public void getInternationalGrid() {
59+
LOG.info("getInternationalGrid");
3260
final DataModel dataModel = DataModel.getInstance();
3361
final int world = Territory.AAA.getNumber();
3462
final int to = dataModel.getDataLastRecord(world);

0 commit comments

Comments
 (0)