Skip to content

Commit 055640f

Browse files
fix static marker annotation url generation (#479)
1 parent 130486d commit 055640f

2 files changed

Lines changed: 143 additions & 10 deletions

File tree

mapbox/libjava-services/src/main/java/com/mapbox/services/api/staticimage/v1/models/StaticMarkerAnnotation.java

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public StaticMarkerAnnotation(Builder builder) {
4444
TextUtils.formatCoordinate(builder.getLat(), builder.getPrecision())
4545
);
4646
} else {
47-
marker = String.format(Constants.DEFAULT_LOCALE, "%s-%s+%s(%s,%s)",
47+
marker = String.format(Constants.DEFAULT_LOCALE, "%s%s%s(%s,%s)",
4848
builder.getName(),
4949
builder.getLabel(),
5050
builder.getColor(),
@@ -58,7 +58,7 @@ public StaticMarkerAnnotation(Builder builder) {
5858
marker = String.format(Constants.DEFAULT_LOCALE, "url-%s(%s,%s)",
5959
builder.getUrl(), builder.getLon(), builder.getLat());
6060
} else {
61-
marker = String.format(Constants.DEFAULT_LOCALE, "%s-%s+%s(%f,%f)", builder.getName(),
61+
marker = String.format(Constants.DEFAULT_LOCALE, "%s%s%s(%f,%f)", builder.getName(),
6262
builder.getLabel(), builder.getColor(), builder.getLon(), builder.getLat());
6363
}
6464
}
@@ -81,6 +81,11 @@ public String getMarker() {
8181
*/
8282
public static class Builder {
8383

84+
private static final String EMPTY = "";
85+
private static final String HYPHEN_CHAR = "-";
86+
private static final String PLUS_CHAR = "+";
87+
private static final String NUMERIC_FROM_ZERO_TO_NINETYNINE_REGEX = "^(0|[1-9][0-9]{0,1})$";
88+
private static final String ONE_ALPHABET_LETTER_REGEX = "^([a-zA-Z])$";
8489
private String name;
8590
private String label = "";
8691
private String color = "";
@@ -144,12 +149,17 @@ public Builder setName(String name) {
144149
* @since 2.1.0
145150
*/
146151
public String getLabel() {
147-
return label;
152+
if (label.isEmpty()) {
153+
return EMPTY;
154+
}
155+
156+
return HYPHEN_CHAR.concat(label).toLowerCase();
148157
}
149158

150159
/**
151-
* StaticMarkerAnnotation symbol. Options are an alphanumeric label {@code a} through {@code z}, {@code 0} through
152-
* {@code 99}, or a valid Maki icon. If a letter is requested, it will be rendered uppercase only.
160+
* StaticMarkerAnnotation symbol. Options are an alphanumeric label {@code a}-{@code A} through
161+
* {@code z}-{@code Z}, {@code 0} through {@code 99}, or a valid Maki icon. If a letter is requested, it will be
162+
* rendered uppercase only.
153163
*
154164
* @param label String containing the marker symbol.
155165
* @return This StaticMarkerAnnotation builder.
@@ -167,7 +177,11 @@ public Builder setLabel(String label) {
167177
* @since 2.1.0
168178
*/
169179
public String getColor() {
170-
return color;
180+
if (color.isEmpty()) {
181+
return EMPTY;
182+
}
183+
184+
return PLUS_CHAR.concat(color);
171185
}
172186

173187
/**
@@ -296,6 +310,33 @@ public StaticMarkerAnnotation build() throws ServicesException {
296310
}
297311
}
298312

313+
if (!label.isEmpty()) {
314+
boolean isANumber = true;
315+
try {
316+
Integer.parseInt(label);
317+
} catch (NumberFormatException notANumber) {
318+
isANumber = false;
319+
}
320+
if (isANumber) {
321+
Pattern pattern = Pattern.compile(NUMERIC_FROM_ZERO_TO_NINETYNINE_REGEX);
322+
Matcher matcher = pattern.matcher(label);
323+
if (!matcher.matches()) {
324+
throw new ServicesException("You need to pass an alphanumeric label [0-99] code.");
325+
}
326+
} else {
327+
// TODO Find a better way to know when a label is not a valid Maki icon
328+
// Right now, this is not verified
329+
// At the moment there's no Maki icon name with 2 characters or less
330+
if (label.length() < 3) {
331+
Pattern pattern = Pattern.compile(ONE_ALPHABET_LETTER_REGEX);
332+
Matcher matcher = pattern.matcher(label);
333+
if (!matcher.matches()) {
334+
throw new ServicesException("You need to pass an alphanumeric label [a-zA-Z] code.");
335+
}
336+
}
337+
}
338+
}
339+
299340
return new StaticMarkerAnnotation(this);
300341
}
301342
}

mapbox/libjava-services/src/test/java/com/mapbox/services/api/staticimage/v1/StaticMarkerAnnotationTest.java

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void testSanity() throws ServicesException {
2626
.setName(Constants.PIN_SMALL)
2727
.build();
2828

29-
assertTrue(staticMarkerAnnotation.getMarker().contains("pin-s-+(1.000000,2.000000)"));
29+
assertTrue(staticMarkerAnnotation.getMarker().contains("pin-s(1.000000,2.000000)"));
3030
}
3131

3232
@Test
@@ -65,8 +65,76 @@ public void requireMarkerLon() throws ServicesException {
6565
new StaticMarkerAnnotation.Builder().setName(Constants.PIN_SMALL).setLat(2.0).build();
6666
}
6767

68+
@Test(expected = ServicesException.class)
69+
public void requiresOneAlphabetCharLabelIfPresent() throws ServicesException {
70+
new StaticMarkerAnnotation.Builder()
71+
.setName(Constants.PIN_SMALL)
72+
.setLat(2.0)
73+
.setLon(2.0)
74+
.setLabel("aa")
75+
.build();
76+
}
77+
78+
@Test(expected = ServicesException.class)
79+
public void requiresZeroToNinetynineNumericCharLabelIfPresent() throws ServicesException {
80+
new StaticMarkerAnnotation.Builder()
81+
.setName(Constants.PIN_SMALL)
82+
.setLat(2.0)
83+
.setLon(2.0)
84+
.setLabel("100")
85+
.build();
86+
}
87+
88+
@Test
89+
public void checksMarkerAlphabetCharLabel() throws ServicesException {
90+
StaticMarkerAnnotation staticMarkerAnnotation = new StaticMarkerAnnotation.Builder()
91+
.setLat(2.0)
92+
.setLon(1.0)
93+
.setName(Constants.PIN_SMALL)
94+
.setLabel("a")
95+
.build();
96+
97+
assertTrue(staticMarkerAnnotation.getMarker().contains("pin-s-a(1.000000,2.000000)"));
98+
}
99+
100+
@Test
101+
public void checksMarkerCaseInsensitiveAlphabetCharLabel() throws ServicesException {
102+
StaticMarkerAnnotation staticMarkerAnnotation = new StaticMarkerAnnotation.Builder()
103+
.setLat(2.0)
104+
.setLon(1.0)
105+
.setName(Constants.PIN_SMALL)
106+
.setLabel("A")
107+
.build();
108+
109+
assertTrue(staticMarkerAnnotation.getMarker().contains("pin-s-a(1.000000,2.000000)"));
110+
}
111+
112+
@Test
113+
public void checksMarkerNumericCharLabel() throws ServicesException {
114+
StaticMarkerAnnotation staticMarkerAnnotation = new StaticMarkerAnnotation.Builder()
115+
.setLat(2.0)
116+
.setLon(1.0)
117+
.setName(Constants.PIN_SMALL)
118+
.setLabel("33")
119+
.build();
120+
121+
assertTrue(staticMarkerAnnotation.getMarker().contains("pin-s-33(1.000000,2.000000)"));
122+
}
123+
68124
@Test
69-
public void requireValidMarkerColor() throws ServicesException {
125+
public void checksMarkerMapboxMakiIconCharLabel() throws ServicesException {
126+
StaticMarkerAnnotation staticMarkerAnnotation = new StaticMarkerAnnotation.Builder()
127+
.setLat(2.0)
128+
.setLon(1.0)
129+
.setName(Constants.PIN_SMALL)
130+
.setLabel("bakery")
131+
.build();
132+
133+
assertTrue(staticMarkerAnnotation.getMarker().contains("pin-s-bakery(1.000000,2.000000)"));
134+
}
135+
136+
@Test
137+
public void requiresValidColorCodeIfPresent() throws ServicesException {
70138
thrown.expect(ServicesException.class);
71139
thrown.expectMessage(startsWith("You need to pass 3- or 6-digit hexadecimal color code."));
72140

@@ -78,6 +146,30 @@ public void requireValidMarkerColor() throws ServicesException {
78146
.build();
79147
}
80148

149+
@Test
150+
public void checksMarkerWithThreeDigitColorCode() throws ServicesException {
151+
StaticMarkerAnnotation staticMarkerAnnotation = new StaticMarkerAnnotation.Builder()
152+
.setLat(2.0)
153+
.setLon(1.0)
154+
.setName(Constants.PIN_SMALL)
155+
.setColor("333")
156+
.build();
157+
158+
assertTrue(staticMarkerAnnotation.getMarker().contains("pin-s+333(1.000000,2.000000)"));
159+
}
160+
161+
@Test
162+
public void checksMarkerWithSixDigitHexColorCode() throws ServicesException {
163+
StaticMarkerAnnotation staticMarkerAnnotation = new StaticMarkerAnnotation.Builder()
164+
.setLat(2.0)
165+
.setLon(1.0)
166+
.setName(Constants.PIN_SMALL)
167+
.setColor("666666")
168+
.build();
169+
170+
assertTrue(staticMarkerAnnotation.getMarker().contains("pin-s+666666(1.000000,2.000000)"));
171+
}
172+
81173
@Test
82174
public void markerPositionWorking() throws ServicesException {
83175
Position position = Position.fromCoordinates(1.0, 2.0);
@@ -102,7 +194,7 @@ public void singleMarkerInUrl() {
102194
.setWidth(100).setHeight(200)
103195
.build();
104196

105-
assertTrue(image.getUrl().toString().contains("pin-s-+(1.000000,2.000000)"));
197+
assertTrue(image.getUrl().toString().contains("pin-s(1.000000,2.000000)"));
106198
}
107199

108200
@Test
@@ -125,7 +217,7 @@ public void doubleMarkersInUrl() {
125217
.setWidth(100).setHeight(200)
126218
.build();
127219

128-
assertTrue(image.getUrl().toString().contains("pin-s-+(1.000000,2.000000),pin-m-+(5.000000,6.000000)"));
220+
assertTrue(image.getUrl().toString().contains("pin-s(1.000000,2.000000),pin-m(5.000000,6.000000)"));
129221
}
130222

131223
@Test

0 commit comments

Comments
 (0)