Skip to content
This repository has been archived by the owner on Apr 27, 2024. It is now read-only.

Extracts configurable networking layer. #56

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include ':vtm'
include ':vtm-tests'
include ':vtm-extras'
include ':vtm-android'
include ':vtm-android-example'
Expand Down
1 change: 1 addition & 0 deletions vtm-android-example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {
compile project(':vtm-jeo')
compile project(':vtm-extras')
compile project(':vtm-themes')
compile 'com.squareup.okhttp:okhttp:1.5.2'
}

android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import org.oscim.core.MapPosition;
import org.oscim.layers.tile.vector.VectorTileLayer;
import org.oscim.theme.VtmThemes;
import org.oscim.tiling.TileSource;
import org.oscim.tiling.source.OkHttpEngine.OkHttpFactory;
import org.oscim.tiling.source.UrlTileSource;
import org.oscim.tiling.source.oscimap4.OSciMap4TileSource;

import android.os.Bundle;
Expand All @@ -31,11 +32,11 @@

public class BaseMapActivity extends MapActivity {

final static boolean USE_CACHE = true;
final static boolean USE_CACHE = false;

MapView mMapView;
VectorTileLayer mBaseLayer;
TileSource mTileSource;
UrlTileSource mTileSource;

private TileCache mCache;

Expand All @@ -58,6 +59,7 @@ public void onCreate(Bundle savedInstanceState) {
registerMapView(mMapView);

mTileSource = new OSciMap4TileSource();
mTileSource.setHttpEngine(new OkHttpFactory());

if (USE_CACHE) {
mCache = new TileCache(this, null, "tile.db");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,23 @@
import org.oscim.layers.TileGridLayer;
import org.oscim.layers.tile.bitmap.BitmapTileLayer;
import org.oscim.renderer.MapRenderer;
import org.oscim.tiling.TileSource;
import org.oscim.tiling.source.OkHttpEngine.OkHttpFactory;
import org.oscim.tiling.source.bitmap.BitmapTileSource;
import org.oscim.tiling.source.bitmap.DefaultSources;

import android.os.Bundle;

public class BitmapTileMapActivity extends MapActivity {

private final static boolean USE_CACHE = true;
private final TileSource mTileSource;
private final static boolean USE_CACHE = false;
private final BitmapTileSource mTileSource;
protected BitmapTileLayer mBitmapLayer;

public BitmapTileMapActivity() {
mTileSource = new DefaultSources.OpenStreetMap();
}

public BitmapTileMapActivity(TileSource tileSource) {
public BitmapTileMapActivity(BitmapTileSource tileSource) {
mTileSource = tileSource;
}

Expand All @@ -61,6 +62,7 @@ public void onCreate(Bundle savedInstanceState) {
mTileSource.setCache(mCache);
}

mTileSource.setHttpEngine(new OkHttpFactory());
mBitmapLayer = new BitmapTileLayer(mMap, mTileSource);
mMap.layers().add(mBitmapLayer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,24 @@
import org.oscim.core.MapElement;
import org.oscim.core.Tag;
import org.oscim.tiling.ITileDataSource;
import org.oscim.tiling.source.LwHttp;
import org.oscim.tiling.source.UrlTileDataSource;
import org.oscim.tiling.source.UrlTileSource;

public abstract class GeoJsonTileSource extends UrlTileSource {

public GeoJsonTileSource(String url) {
super(url);
setExtension(".json");
super(url, "/{Z}/{X}/{Y}.json");
}

public GeoJsonTileSource(String url, int zoomMin, int zoomMax) {
super(url, zoomMin, zoomMax);
setExtension(".json");
super(url, "/{Z}/{X}/{Y}.json", zoomMin, zoomMax);
}

@Override
public ITileDataSource getDataSource() {
Map<String, String> opt = new HashMap<String, String>();
opt.put("Accept-Encoding", "gzip");
return new UrlTileDataSource(this, new GeoJsonTileDecoder(this), new LwHttp(getUrl(), opt));
return new UrlTileDataSource(this, new GeoJsonTileDecoder(this), getHttpEngine());
}

public Tag getFeatureTag() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
public class MapnikVectorTileSource extends UrlTileSource {

public MapnikVectorTileSource() {
super("http://d1s11ojcu7opje.cloudfront.net/dev/764e0b8d");
super("http://d1s11ojcu7opje.cloudfront.net/dev/764e0b8d", "");
}

@Override
public ITileDataSource getDataSource() {
return new UrlTileDataSource(this, new TileDecoder(), new LwHttp(getUrl()));
return new UrlTileDataSource(this, new TileDecoder(), getHttpEngine());
}

public int formatTilePath(Tile tile, byte[] path, int pos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.oscim.tiling.source.oscimap;

import org.oscim.tiling.ITileDataSource;
import org.oscim.tiling.source.LwHttp;
import org.oscim.tiling.source.UrlTileDataSource;
import org.oscim.tiling.source.UrlTileSource;

Expand All @@ -28,13 +27,11 @@
public class OSciMap1TileSource extends UrlTileSource {

public OSciMap1TileSource(String url) {
super(url);
setExtension(".osmtile");
setMimeType("application/osmtile");
super(url, "/{Z}/{X}/{Y}.osmtile");
}

@Override
public ITileDataSource getDataSource() {
return new UrlTileDataSource(this, new TileDecoder(), new LwHttp(getUrl()));
return new UrlTileDataSource(this, new TileDecoder(), getHttpEngine());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.oscim.core.Tile;
import org.oscim.tiling.ITileDataSink;
import org.oscim.tiling.ITileDataSource;
import org.oscim.tiling.source.LwHttp;
import org.oscim.tiling.source.PbfDecoder;
import org.oscim.tiling.source.UrlTileDataSource;
import org.oscim.tiling.source.UrlTileSource;
Expand All @@ -37,14 +36,12 @@
public class OSciMap2TileSource extends UrlTileSource {

public OSciMap2TileSource(String url) {
super(url);
setExtension(".osmtile");
setMimeType("application/osmtile");
super(url, "/{Z}/{X}/{Y}.osmtile");
}

@Override
public ITileDataSource getDataSource() {
return new UrlTileDataSource(this, new TileDecoder(), new LwHttp(getUrl()));
return new UrlTileDataSource(this, new TileDecoder(), getHttpEngine());
}

static class TileDecoder extends PbfDecoder {
Expand Down
2 changes: 1 addition & 1 deletion vtm-jeo/src/org/oscim/theme/carto/RenderTheme.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public RenderTheme() {
}

// get map background
RuleList rules = mStyle.getRules().selectByName("Map", false);
RuleList rules = mStyle.getRules().selectByName("Map", false, false);
if (!rules.isEmpty()) {
Rule rule = rules.collapse();
RGB bgColor = rule.color(null, BACKGROUND_COLOR, null);
Expand Down
16 changes: 16 additions & 0 deletions vtm-tests/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apply plugin: 'java'
apply plugin: 'maven'

dependencies {
compile project(':vtm')
compile 'com.squareup.okhttp:okhttp:1.5.2'
testCompile 'junit:junit:4.11'
testCompile 'org.mockito:mockito-all:1.9.5'
testCompile 'org.easytesting:fest-assert-core:2.0M10'
testCompile 'com.squareup.okhttp:mockwebserver:1.5.2'
}

sourceSets {
main.java.srcDirs = ['src']
test.java.srcDirs = ['test']
}
93 changes: 93 additions & 0 deletions vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.oscim.tiling.source;

import static org.fest.assertions.api.Assertions.assertThat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.oscim.core.Tile;
import org.oscim.tiling.source.oscimap4.OSciMap4TileSource;

import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import com.squareup.okhttp.mockwebserver.RecordedRequest;

public class OkHttpEngineTest {
private OkHttpEngine engine;
private MockWebServer server;
private MockResponse mockResponse;

@Before
public void setUp() throws Exception {
mockResponse = new MockResponse();
mockResponse.setBody("TEST RESPONSE".getBytes());
server = new MockWebServer();
server.enqueue(mockResponse);
server.play();
engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory()
.create(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()));
}

@After
public void tearDown() throws Exception {
server.shutdown();
}

@Test
public void shouldNotBeNull() throws Exception {
assertThat(engine).isNotNull();
}

@Test(expected = IllegalArgumentException.class)
public void sendRequest_shouldRejectNullTile() throws Exception {
engine.sendRequest(null);
}

@Test
public void sendRequest_shouldAppendXYZToPath() throws Exception {
engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));

RecordedRequest request = server.takeRequest();
assertThat(request.getPath()).isEqualTo("/tiles/vtm/3/1/2.vtm");
}

@Test
public void read_shouldReturnResponseStream() throws Exception {
engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));

InputStream responseStream = engine.read();
String response = new BufferedReader(new InputStreamReader(responseStream)).readLine();
assertThat(response).isEqualTo("TEST RESPONSE");
}

@Test(expected = IOException.class)
public void close_shouldCloseInputStream() throws Exception {
engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
engine.close();

// Calling read after the stream is closed should throw an exception.
InputStream responseStream = engine.read();
responseStream.read();
}

@Test(expected = IOException.class)
public void requestCompleted_shouldCloseInputStream() throws Exception {
engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
engine.requestCompleted(true);

// Calling read after the stream is closed should throw an exception.
InputStream responseStream = engine.read();
responseStream.read();
}

@Test
public void requestCompleted_shouldReturnValueGiven() throws Exception {
assertThat(engine.requestCompleted(true)).isTrue();
assertThat(engine.requestCompleted(false)).isFalse();
}
}
56 changes: 56 additions & 0 deletions vtm-tests/test/org/oscim/tiling/source/UrlTileSourceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.oscim.tiling.source;

import static org.fest.assertions.api.Assertions.assertThat;

import org.junit.Before;
import org.junit.Test;
import org.oscim.tiling.ITileDataSource;

public class UrlTileSourceTest {
private UrlTileSource tileSource;

@Before
public void setUp() throws Exception {
tileSource = new TestTileSource("http://example.org/tiles/vtm", "/{Z}/{X}/{Z}.vtm");
}

@Test
public void shouldNotBeNull() throws Exception {
assertThat(tileSource).isNotNull();
}

@Test
public void shouldUseDefaultHttpEngine() throws Exception {
TestTileDataSource dataSource = (TestTileDataSource) tileSource.getDataSource();
assertThat(dataSource.getConnection()).isInstanceOf(LwHttp.class);
}

@Test
public void shouldUseCustomHttpEngine() throws Exception {
tileSource.setHttpEngine(new OkHttpEngine.OkHttpFactory());
TestTileDataSource dataSource = (TestTileDataSource) tileSource.getDataSource();
assertThat(dataSource.getConnection()).isInstanceOf(OkHttpEngine.class);
}

class TestTileSource extends UrlTileSource {
public TestTileSource(String urlString, String tilePath) {
super(urlString, tilePath);
}

@Override
public ITileDataSource getDataSource() {
return new TestTileDataSource(this, null, getHttpEngine());
}
}

class TestTileDataSource extends UrlTileDataSource {
public TestTileDataSource(UrlTileSource tileSource, ITileDecoder tileDecoder,
HttpEngine conn) {
super(tileSource, tileDecoder, conn);
}

public HttpEngine getConnection() {
return mConn;
}
}
}
Loading