forked from googlemaps/google-maps-services-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistanceMatrixApiIntegrationTest.java
More file actions
188 lines (163 loc) · 7.04 KB
/
DistanceMatrixApiIntegrationTest.java
File metadata and controls
188 lines (163 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Copyright 2014 Google Inc. All rights reserved.
*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.google.maps;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.google.maps.DirectionsApi.RouteRestriction;
import com.google.maps.model.DistanceMatrix;
import com.google.maps.model.DistanceMatrixElement;
import com.google.maps.model.DistanceMatrixElementStatus;
import com.google.maps.model.DistanceMatrixRow;
import com.google.maps.model.TransitMode;
import com.google.maps.model.TransitRoutingPreference;
import com.google.maps.model.TravelMode;
import com.google.maps.model.Unit;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import java.util.concurrent.TimeUnit;
@Category(LargeTests.class)
public class DistanceMatrixApiIntegrationTest extends AuthenticatedTest {
private GeoApiContext context;
public DistanceMatrixApiIntegrationTest(GeoApiContext context) {
this.context = context
.setConnectTimeout(1, TimeUnit.SECONDS)
.setReadTimeout(1, TimeUnit.SECONDS)
.setWriteTimeout(1, TimeUnit.SECONDS);
}
@Test
public void testGetDistanceMatrixWithBasicStringParams() throws Exception {
String[] origins = new String[] {
"Perth, Australia", "Sydney, Australia", "Melbourne, Australia",
"Adelaide, Australia", "Brisbane, Australia", "Darwin, Australia",
"Hobart, Australia", "Canberra, Australia"
};
String[] destinations = new String[] {
"Uluru, Australia", "Kakadu, Australia", "Blue Mountains, Australia",
"Bungle Bungles, Australia", "The Pinnacles, Australia"
};
DistanceMatrix matrix =
DistanceMatrixApi.getDistanceMatrix(context, origins, destinations).await();
// Rows length will match the number of origin elements, regardless of whether they're routable.
assertEquals(8, matrix.rows.length);
assertEquals(5, matrix.rows[0].elements.length);
assertEquals(DistanceMatrixElementStatus.OK, matrix.rows[0].elements[0].status);
}
@Test
public void testNewRequestWithAllPossibleParams() throws Exception {
String[] origins = new String[] {
"Perth, Australia", "Sydney, Australia", "Melbourne, Australia",
"Adelaide, Australia", "Brisbane, Australia", "Darwin, Australia",
"Hobart, Australia", "Canberra, Australia"
};
String[] destinations = new String[] {
"Uluru, Australia", "Kakadu, Australia", "Blue Mountains, Australia",
"Bungle Bungles, Australia", "The Pinnacles, Australia"
};
DistanceMatrix matrix = DistanceMatrixApi.newRequest(context)
.origins(origins)
.destinations(destinations)
.mode(TravelMode.DRIVING)
.language("en-AU")
.avoid(RouteRestriction.TOLLS)
.units(Unit.IMPERIAL)
.departureTime(new DateTime().plusMinutes(2)) // this is ignored when an API key is used
.await();
assertEquals(8, matrix.rows.length);
assertEquals(5, matrix.rows[0].elements.length);
assertTrue(matrix.rows[0].elements[0].distance.humanReadable.endsWith("mi"));
}
/**
* Test the language parameter.
*
* <p>Sample request:
* <a href="http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR">
* origins: Vancouver BC|Seattle, destinations: San Francisco|Victoria BC, mode: bicycling,
* language: french</a>.
*/
@Test
public void testLanguageParameter() throws Exception {
DistanceMatrix matrix = DistanceMatrixApi.newRequest(context)
.origins("Vancouver BC", "Seattle")
.destinations("San Francisco", "Victoria BC")
.mode(TravelMode.BICYCLING)
.language("fr-FR")
.await();
assertNotNull(matrix);
}
@Test
public void testTransitData() throws Exception {
DistanceMatrix matrix = DistanceMatrixApi.newRequest(context)
.origins("Fisherman's Wharf, San Francisco", "Union Square, San Francisco")
.destinations("Mikkeller Bar, San Francisco", "Moscone Center, San Francisco")
.mode(TravelMode.TRANSIT)
.departureTime(new DateTime(2015, 1, 1, 19, 0, DateTimeZone.UTC))
.await();
assertNotNull(matrix);
for (DistanceMatrixRow row : matrix.rows) {
for (DistanceMatrixElement cell : row.elements) {
if (cell.fare != null) {
assertEquals("USD", cell.fare.currency.getCurrencyCode());
assertNotNull(cell.fare.value);
return;
}
}
}
fail("No fare information found in a transit search.");
}
/**
* Test transit without arrival or departure times specified.
*/
@Test
public void testTransitWithoutSpecifyingTime() throws Exception {
DistanceMatrixApi.newRequest(context)
.origins("Fisherman's Wharf, San Francisco", "Union Square, San Francisco")
.destinations("Mikkeller Bar, San Francisco", "Moscone Center, San Francisco")
.mode(TravelMode.TRANSIT)
.await();
// Since this test may run at different times-of-day, it's entirely valid to return zero
// routes, but the main thing to catch is that no exception is thrown.
}
@Test
public void testTransitWithArrivalTime() throws Exception {
DistanceMatrix matrix = DistanceMatrixApi.newRequest(context)
.origins("Fisherman's Wharf, San Francisco", "Union Square, San Francisco")
.destinations("Mikkeller Bar, San Francisco", "Moscone Center, San Francisco")
.mode(TravelMode.TRANSIT)
.arrivalTime(new DateTime(2015, 1, 1, 19, 0, DateTimeZone.UTC))
.await();
assertNotNull(matrix);
assertEquals(DistanceMatrixElementStatus.OK, matrix.rows[0].elements[0].status);
}
/**
* Test the extended transit parameters: mode and routing preference.
*/
@Test
public void testTransitParams() throws Exception {
DistanceMatrix matrix = DistanceMatrixApi.newRequest(context)
.origins("Fisherman's Wharf, San Francisco", "Union Square, San Francisco")
.destinations("Mikkeller Bar, San Francisco", "Moscone Center, San Francisco")
.mode(TravelMode.TRANSIT)
.transitModes(TransitMode.RAIL, TransitMode.TRAM)
.transitRoutingPreference(TransitRoutingPreference.LESS_WALKING)
.arrivalTime(new DateTime(2015, 1, 1, 19, 0, DateTimeZone.UTC))
.await();
assertNotNull(matrix);
assertEquals(DistanceMatrixElementStatus.OK, matrix.rows[0].elements[0].status);
}
}