Skip to content

Commit bc0d57b

Browse files
committed
Issue #23: CHE-GR needs to throw UnknownTerritoryException, not return MX-GRO
1 parent ec5d799 commit bc0d57b

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

src/main/java/com/mapcode/Territory.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ public String toAlphaCode(@Nonnull final AlphaCodeFormat format, @Nullable final
739739
checkNonnull("format", format);
740740
String result = name().replace('_', '-');
741741
if (format != AlphaCodeFormat.INTERNATIONAL) {
742-
final int index = name().indexOf('_');
742+
final int index = name().lastIndexOf('_');
743743
if (index != -1) {
744744
assert name().length() > (index + 1);
745745
final String shortName = name().substring(index + 1);
@@ -903,11 +903,15 @@ private Territory(
903903
* @return Territory.
904904
* @throws UnknownTerritoryException Thrown if the territory is not found.
905905
*/
906+
@SuppressWarnings("TailRecursion")
906907
@Nonnull
907908
private static Territory createFromString(
908909
@Nonnull final String alphaCode,
909910
@Nullable final Territory parentTerritory) throws UnknownTerritoryException {
910-
final String trimmed = Mapcode.convertStringToPlainAscii(alphaCode.trim().replace('_', '-')).toUpperCase();
911+
912+
// Replace '_' with '-', but leave spaces alone (may be part of the name).
913+
final String trimmed = Mapcode.convertStringToPlainAscii(
914+
alphaCode.trim().replace('_', '-')).toUpperCase();
911915

912916
// Try as alpha code.
913917
final List<Territory> territories = nameMap.get(trimmed);
@@ -920,14 +924,23 @@ private static Territory createFromString(
920924
return territory;
921925
}
922926
}
923-
throw new UnknownTerritoryException(trimmed);
924-
}
927+
} else {
925928

926-
// Check for a case such as USA-NLD (=NLD)
927-
final int dividerLocation = Math.max(trimmed.indexOf('-'), trimmed.indexOf(' '));
928-
if (dividerLocation >= 0) {
929-
//noinspection TailRecursion
930-
return createFromString(trimmed.substring(dividerLocation + 1), parentTerritory);
929+
// Check for a case such as "United States of America-IN".
930+
final int lastSeparator = Math.max( // Find last separator.
931+
trimmed.lastIndexOf('-'), // Allow '-'.
932+
trimmed.lastIndexOf(' ')); // And ' '.
933+
if (lastSeparator >= 0) {
934+
final String prefix = trimmed.substring(0, lastSeparator);
935+
final Territory parent = createFromString(prefix, parentTerritory);
936+
if (PARENT_TERRITORIES.contains(parent)) {
937+
final String postfix = trimmed.substring(lastSeparator + 1);
938+
final Territory child = createFromString(postfix, parentTerritory);
939+
if (child.parentTerritory == parent) {
940+
return child;
941+
}
942+
}
943+
}
931944
}
932945
throw new UnknownTerritoryException(trimmed);
933946
}
@@ -946,7 +959,7 @@ private static void addNameWithParentVariants(
946959
addNameWithSeperatorVariants(name, territory);
947960

948961
if (name.contains("-")) {
949-
final String childTerritoryName = name.substring(name.indexOf('-') + 1);
962+
final String childTerritoryName = name.substring(name.lastIndexOf('-') + 1);
950963

951964
// Tolerate a child territory specified without the parent.
952965
// (e.g. "CA" rather than "US-CA")

src/site/apt/ReleaseNotes.apt.vm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
* 2.2.3
1313

14-
* To do.
14+
* Issue #23: Fixed <<<Territory.fromString>>> to make sure the parent territory is valid for
15+
input like "CHE-GR". This returned "MX-GRO" instead of throwing <<<UnknownTerritoryException>>>.
16+
Added unit test for this type of case.
1517

1618
[]
1719

src/test/java/com/mapcode/TerritoryTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.mapcode;
1818

1919
import com.mapcode.Territory.AlphaCodeFormat;
20+
import org.junit.Ignore;
2021
import org.junit.Test;
2122
import org.slf4j.Logger;
2223
import org.slf4j.LoggerFactory;
@@ -52,6 +53,10 @@ public void checkDash() throws Exception {
5253
assertEquals(Territory.IN_MN, Territory.fromString("IND_MN"));
5354
assertEquals(Territory.US_MN, Territory.fromString("USA-MN"));
5455
assertEquals(Territory.US_MN, Territory.fromString("USA_MN"));
56+
57+
assertEquals(Territory.COD, Territory.fromString("Congo-Kinshasa"));
58+
assertEquals(Territory.US_IN, Territory.fromString("United States of America-IN"));
59+
assertEquals(Territory.US_IN, Territory.fromString("United States of America IN"));
5560
}
5661

5762
@Test
@@ -100,10 +105,26 @@ public void testTerritoryFromString() throws Exception {
100105
}
101106
}
102107

108+
@Test(expected = UnknownTerritoryException.class)
109+
public void testTerritoryFromStringIncorrectDash1() throws Exception {
110+
LOG.info("testTerritoryFromStringIncorrectDash1");
111+
112+
// Issue: https://github.com/mapcode-foundation/mapcode-java/issues/23
113+
assertEquals(Territory.AAA, Territory.fromString("CHE-GR")); // Exception must be thrown.
114+
}
115+
116+
@Test(expected = UnknownTerritoryException.class)
117+
public void testTerritoryFromStringIncorrectDash2() throws Exception {
118+
LOG.info("testTerritoryFromStringIncorrectDash2");
119+
assertEquals(Territory.AAA, Territory.fromString("USA-NLD")); // Exception must be thrown.
120+
}
121+
103122
@Test(expected = UnknownTerritoryException.class)
104123
public void testTerritoryFromStringNumeric() throws Exception {
105124
LOG.info("testTerritoryFromStringNumeric");
106-
assertEquals(Territory.VAT, Territory.fromString("0"));
125+
126+
// No longer support: numeric codes.
127+
assertEquals(Territory.AAA, Territory.fromString("0")); // Exception must be thrown.
107128
}
108129

109130
@Test

0 commit comments

Comments
 (0)