OsmAnd/OsmAnd-java/test/java/net/osmand/router/RouteTestingTest.java

113 lines
4 KiB
Java
Raw Normal View History

2016-05-30 10:25:02 +02:00
package net.osmand.router;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
2016-08-04 00:19:37 +02:00
import java.util.Iterator;
2016-05-30 10:25:02 +02:00
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
2016-08-04 00:19:37 +02:00
import java.util.Map.Entry;
2016-05-30 10:25:02 +02:00
import java.util.Set;
import java.util.TreeSet;
import net.osmand.binary.BinaryInspector;
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.data.LatLon;
import org.junit.Assert;
import org.junit.BeforeClass;
2016-07-13 12:08:24 +02:00
import org.junit.Ignore;
2016-05-30 10:25:02 +02:00
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@RunWith(Parameterized.class)
2016-08-04 00:24:47 +02:00
public class RouteTestingTest {
2016-08-04 00:19:37 +02:00
private TestEntry te;
2016-05-30 10:25:02 +02:00
2016-08-04 00:24:47 +02:00
public RouteTestingTest(String name, TestEntry te) {
2016-08-04 00:19:37 +02:00
this.te = te;
2016-05-30 10:25:02 +02:00
}
@BeforeClass
public static void setUp() throws Exception {
RouteResultPreparation.PRINT_TO_CONSOLE_ROUTE_INFORMATION_TO_TEST = true;
}
@Parameterized.Parameters(name = "{index}: {0}")
2016-08-04 00:19:37 +02:00
public static Iterable<Object[]> data() throws IOException {
2016-05-30 10:25:02 +02:00
String fileName = "/test_routing.json";
2016-08-04 00:24:47 +02:00
Reader reader = new InputStreamReader(RouteTestingTest.class.getResourceAsStream(fileName));
2016-05-30 10:25:02 +02:00
Gson gson = new GsonBuilder().setPrettyPrinting().create();
TestEntry[] testEntries = gson.fromJson(reader, TestEntry[].class);
2016-08-04 00:19:37 +02:00
ArrayList<Object[]> arrayList = new ArrayList<>();
for(TestEntry te : testEntries) {
2016-08-04 00:24:47 +02:00
if(te.isIgnore()) {
continue;
}
2016-08-04 00:19:37 +02:00
arrayList.add(new Object[] {te.getTestName(), te});
2016-05-30 10:25:02 +02:00
}
reader.close();
2016-08-04 00:19:37 +02:00
return arrayList;
2016-05-30 10:25:02 +02:00
}
@Test
2016-08-04 00:19:37 +02:00
public void testRouting() throws Exception {
String fl = "../../resources/test-resources/Routing_test.obf";
RandomAccessFile raf = new RandomAccessFile(fl, "r");
RoutePlannerFrontEnd fe = new RoutePlannerFrontEnd(false);
2016-05-30 10:25:02 +02:00
BinaryMapIndexReader[] binaryMapIndexReaders = { new BinaryMapIndexReader(raf, new File(fl)) };
RoutingConfiguration.Builder builder = RoutingConfiguration.getDefault();
2016-08-04 00:19:37 +02:00
Map<String, String> params = te.getParams();
RoutingConfiguration config = builder.build(params.containsKey("vehicle") ? params.get("vehicle") : "car",
RoutingConfiguration.DEFAULT_MEMORY_LIMIT * 3, params);
RoutingContext ctx = fe.buildRoutingContext(config, null, binaryMapIndexReaders,
RoutePlannerFrontEnd.RouteCalculationMode.NORMAL);
ctx.leftSideNavigation = false;
2016-08-04 00:24:47 +02:00
List<RouteSegmentResult> routeSegments = fe.searchRoute(ctx, te.getStartPoint(), te.getEndPoint(), te.getTransitPoint());
2016-08-04 00:19:37 +02:00
Set<Long> reachedSegments = new TreeSet<Long>();
Assert.assertNotNull(routeSegments);
int prevSegment = -1;
for (int i = 0; i <= routeSegments.size(); i++) {
if (i == routeSegments.size() || routeSegments.get(i).getTurnType() != null) {
if (prevSegment >= 0) {
String name = routeSegments.get(prevSegment).getDescription();
long segmentId = routeSegments.get(prevSegment).getObject().getId() >> (BinaryInspector.SHIFT_ID);
System.out.println("segmentId: " + segmentId + " description: " + name);
}
prevSegment = i;
}
if (i < routeSegments.size()) {
reachedSegments.add(routeSegments.get(i).getObject().getId() >> (BinaryInspector.SHIFT_ID));
}
}
Map<Long, String> expectedResults = te.getExpectedResults();
Iterator<Entry<Long, String>> it = expectedResults.entrySet().iterator();
while (it.hasNext()) {
Entry<Long, String> es = it.next();
if (es.getValue().equals("false")) {
Assert.assertTrue("Expected segment " + (es.getKey()) + " was wrongly reached in route segments "
+ reachedSegments.toString(), !reachedSegments.contains(es.getKey()));
} else {
Assert.assertTrue("Expected segment " + (es.getKey()) + " weren't reached in route segments "
+ reachedSegments.toString(), reachedSegments.contains(es.getKey()));
}
}
2016-05-30 10:25:02 +02:00
2016-08-04 00:19:37 +02:00
}
2016-05-30 10:25:02 +02:00
}