Improve code / xml formatting, closes #54
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
package org.oscim.tiling.source;
|
||||
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import com.squareup.okhttp.HttpResponseCache;
|
||||
import com.squareup.okhttp.mockwebserver.MockResponse;
|
||||
import com.squareup.okhttp.mockwebserver.MockWebServer;
|
||||
import com.squareup.okhttp.mockwebserver.RecordedRequest;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
@@ -13,92 +11,94 @@ import org.junit.Test;
|
||||
import org.oscim.core.Tile;
|
||||
import org.oscim.tiling.source.oscimap4.OSciMap4TileSource;
|
||||
|
||||
import com.squareup.okhttp.HttpResponseCache;
|
||||
import com.squareup.okhttp.mockwebserver.MockResponse;
|
||||
import com.squareup.okhttp.mockwebserver.MockWebServer;
|
||||
import com.squareup.okhttp.mockwebserver.RecordedRequest;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
|
||||
public class OkHttpEngineTest {
|
||||
private OkHttpEngine engine;
|
||||
private MockWebServer server;
|
||||
private MockResponse mockResponse;
|
||||
private HttpResponseCache cache;
|
||||
private OkHttpEngine engine;
|
||||
private MockWebServer server;
|
||||
private MockResponse mockResponse;
|
||||
private HttpResponseCache cache;
|
||||
|
||||
@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()));
|
||||
}
|
||||
@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();
|
||||
}
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
server.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotBeNull() throws Exception {
|
||||
assertThat(engine).isNotNull();
|
||||
}
|
||||
@Test
|
||||
public void shouldNotBeNull() throws Exception {
|
||||
assertThat(engine).isNotNull();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void sendRequest_shouldRejectNullTile() throws Exception {
|
||||
engine.sendRequest(null);
|
||||
}
|
||||
@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()));
|
||||
@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");
|
||||
}
|
||||
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()));
|
||||
@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");
|
||||
}
|
||||
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(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();
|
||||
}
|
||||
@Test
|
||||
public void requestCompleted_shouldReturnValueGiven() throws Exception {
|
||||
assertThat(engine.requestCompleted(true)).isTrue();
|
||||
assertThat(engine.requestCompleted(false)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void create_shouldUseTileSourceCache() throws Exception {
|
||||
cache = new HttpResponseCache(new File("tmp"), 1024);
|
||||
OSciMap4TileSource tileSource =
|
||||
new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString());
|
||||
engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory(cache).create(tileSource);
|
||||
engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
|
||||
engine.requestCompleted(true);
|
||||
assertThat(cache.getRequestCount()).isEqualTo(1);
|
||||
}
|
||||
@Test
|
||||
public void create_shouldUseTileSourceCache() throws Exception {
|
||||
cache = new HttpResponseCache(new File("tmp"), 1024);
|
||||
OSciMap4TileSource tileSource =
|
||||
new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString());
|
||||
engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory(cache).create(tileSource);
|
||||
engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
|
||||
engine.requestCompleted(true);
|
||||
assertThat(cache.getRequestCount()).isEqualTo(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,64 +16,64 @@
|
||||
*/
|
||||
package org.oscim.tiling.source;
|
||||
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.oscim.core.Tile;
|
||||
import org.oscim.tiling.ITileDataSource;
|
||||
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
|
||||
public class UrlTileSourceTest {
|
||||
private UrlTileSource tileSource;
|
||||
private UrlTileSource tileSource;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
tileSource = new TestTileSource("http://example.org/tiles/vtm", "/{Z}/{X}/{Z}.vtm");
|
||||
}
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
tileSource = new TestTileSource("http://example.org/tiles/vtm", "/{Z}/{X}/{Z}.vtm");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setApiKey_shouldAppendApiKey() throws Exception {
|
||||
tileSource.setApiKey("testkey");
|
||||
assertThat(tileSource.getTileUrl(new Tile(0, 0, (byte) 0))).endsWith("?api_key=testkey");
|
||||
}
|
||||
@Test
|
||||
public void setApiKey_shouldAppendApiKey() throws Exception {
|
||||
tileSource.setApiKey("testkey");
|
||||
assertThat(tileSource.getTileUrl(new Tile(0, 0, (byte) 0))).endsWith("?api_key=testkey");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotBeNull() throws Exception {
|
||||
assertThat(tileSource).isNotNull();
|
||||
}
|
||||
@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 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);
|
||||
}
|
||||
@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);
|
||||
}
|
||||
class TestTileSource extends UrlTileSource {
|
||||
public TestTileSource(String urlString, String tilePath) {
|
||||
super(urlString, tilePath);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITileDataSource getDataSource() {
|
||||
return new TestTileDataSource(this, null, getHttpEngine());
|
||||
}
|
||||
}
|
||||
@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);
|
||||
}
|
||||
class TestTileDataSource extends UrlTileDataSource {
|
||||
public TestTileDataSource(UrlTileSource tileSource, ITileDecoder tileDecoder,
|
||||
HttpEngine conn) {
|
||||
super(tileSource, tileDecoder, conn);
|
||||
}
|
||||
|
||||
public HttpEngine getConnection() {
|
||||
return mConn;
|
||||
}
|
||||
}
|
||||
public HttpEngine getConnection() {
|
||||
return mConn;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package org.oscim.tiling.source.bitmap;
|
||||
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
@@ -13,66 +11,68 @@ import org.oscim.tiling.source.OkHttpEngine;
|
||||
import org.oscim.tiling.source.UrlTileDataSource;
|
||||
import org.oscim.tiling.source.UrlTileSource;
|
||||
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
|
||||
public class BitmapTileSourceTest {
|
||||
private BitmapTileSource tileSource;
|
||||
private BitmapTileSource tileSource;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
tileSource = new BitmapTileSource("http://tile.openstreetmap.org", 0, 18);
|
||||
}
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
tileSource = new BitmapTileSource("http://tile.openstreetmap.org", 0, 18);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotBeNull() throws Exception {
|
||||
assertThat(tileSource).isNotNull();
|
||||
}
|
||||
@Test
|
||||
public void shouldNotBeNull() throws Exception {
|
||||
assertThat(tileSource).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUseLwHttp() throws Exception {
|
||||
LwHttp lwHttp = Mockito.mock(LwHttp.class);
|
||||
tileSource.setHttpEngine(new TestHttpFactory(lwHttp));
|
||||
ITileDataSource dataSource = tileSource.getDataSource();
|
||||
dataSource.dispose();
|
||||
Mockito.verify(lwHttp).close();
|
||||
}
|
||||
@Test
|
||||
public void shouldUseLwHttp() throws Exception {
|
||||
LwHttp lwHttp = Mockito.mock(LwHttp.class);
|
||||
tileSource.setHttpEngine(new TestHttpFactory(lwHttp));
|
||||
ITileDataSource dataSource = tileSource.getDataSource();
|
||||
dataSource.dispose();
|
||||
Mockito.verify(lwHttp).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUseOkHttp() throws Exception {
|
||||
OkHttpEngine okHttp = Mockito.mock(OkHttpEngine.class);
|
||||
tileSource.setHttpEngine(new TestHttpFactory(okHttp));
|
||||
UrlTileDataSource dataSource = (UrlTileDataSource) tileSource.getDataSource();
|
||||
dataSource.dispose();
|
||||
Mockito.verify(okHttp).close();
|
||||
}
|
||||
@Test
|
||||
public void shouldUseOkHttp() throws Exception {
|
||||
OkHttpEngine okHttp = Mockito.mock(OkHttpEngine.class);
|
||||
tileSource.setHttpEngine(new TestHttpFactory(okHttp));
|
||||
UrlTileDataSource dataSource = (UrlTileDataSource) tileSource.getDataSource();
|
||||
dataSource.dispose();
|
||||
Mockito.verify(okHttp).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUseBuilderConfig() {
|
||||
BitmapTileSource ts = BitmapTileSource.builder()
|
||||
.url("http://example.com")
|
||||
.zoomMax(42)
|
||||
.zoomMin(23)
|
||||
.fadeSteps(new FadeStep[] { new FadeStep(0, 10, 0.5f, 1.0f) })
|
||||
.build();
|
||||
@Test
|
||||
public void shouldUseBuilderConfig() {
|
||||
BitmapTileSource ts = BitmapTileSource.builder()
|
||||
.url("http://example.com")
|
||||
.zoomMax(42)
|
||||
.zoomMin(23)
|
||||
.fadeSteps(new FadeStep[]{new FadeStep(0, 10, 0.5f, 1.0f)})
|
||||
.build();
|
||||
|
||||
assertThat(ts.getUrl().getHost()).isEqualTo("example.com");
|
||||
assertThat(ts.getZoomLevelMin()).isEqualTo(23);
|
||||
assertThat(ts.getZoomLevelMax()).isEqualTo(42);
|
||||
assertThat(ts.getFadeSteps()).isNotNull();
|
||||
}
|
||||
assertThat(ts.getUrl().getHost()).isEqualTo("example.com");
|
||||
assertThat(ts.getZoomLevelMin()).isEqualTo(23);
|
||||
assertThat(ts.getZoomLevelMax()).isEqualTo(42);
|
||||
assertThat(ts.getFadeSteps()).isNotNull();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test factory that allows the specific {@link HttpEngine} instance to be
|
||||
* set.
|
||||
*/
|
||||
class TestHttpFactory implements HttpEngine.Factory {
|
||||
final HttpEngine engine;
|
||||
/**
|
||||
* Test factory that allows the specific {@link HttpEngine} instance to be
|
||||
* set.
|
||||
*/
|
||||
class TestHttpFactory implements HttpEngine.Factory {
|
||||
final HttpEngine engine;
|
||||
|
||||
public TestHttpFactory(HttpEngine engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
public TestHttpFactory(HttpEngine engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpEngine create(UrlTileSource tileSource) {
|
||||
return engine;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public HttpEngine create(UrlTileSource tileSource) {
|
||||
return engine;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package org.oscim.tiling.source.oscimap4;
|
||||
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
@@ -11,51 +9,53 @@ import org.oscim.tiling.source.LwHttp;
|
||||
import org.oscim.tiling.source.OkHttpEngine;
|
||||
import org.oscim.tiling.source.UrlTileSource;
|
||||
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
|
||||
public class OSciMap4TileSourceTest {
|
||||
private OSciMap4TileSource tileSource;
|
||||
private OSciMap4TileSource tileSource;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
tileSource = new OSciMap4TileSource("http://www.example.org/tiles/vtm");
|
||||
}
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
tileSource = new OSciMap4TileSource("http://www.example.org/tiles/vtm");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotBeNull() throws Exception {
|
||||
assertThat(tileSource).isNotNull();
|
||||
}
|
||||
@Test
|
||||
public void shouldNotBeNull() throws Exception {
|
||||
assertThat(tileSource).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUseLwHttp() throws Exception {
|
||||
LwHttp lwHttp = Mockito.mock(LwHttp.class);
|
||||
tileSource.setHttpEngine(new TestHttpFactory(lwHttp));
|
||||
ITileDataSource dataSource = tileSource.getDataSource();
|
||||
dataSource.dispose();
|
||||
Mockito.verify(lwHttp).close();
|
||||
}
|
||||
@Test
|
||||
public void shouldUseLwHttp() throws Exception {
|
||||
LwHttp lwHttp = Mockito.mock(LwHttp.class);
|
||||
tileSource.setHttpEngine(new TestHttpFactory(lwHttp));
|
||||
ITileDataSource dataSource = tileSource.getDataSource();
|
||||
dataSource.dispose();
|
||||
Mockito.verify(lwHttp).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUseOkHttp() throws Exception {
|
||||
OkHttpEngine okHttp = Mockito.mock(OkHttpEngine.class);
|
||||
tileSource.setHttpEngine(new TestHttpFactory(okHttp));
|
||||
ITileDataSource dataSource = tileSource.getDataSource();
|
||||
dataSource.dispose();
|
||||
Mockito.verify(okHttp).close();
|
||||
}
|
||||
@Test
|
||||
public void shouldUseOkHttp() throws Exception {
|
||||
OkHttpEngine okHttp = Mockito.mock(OkHttpEngine.class);
|
||||
tileSource.setHttpEngine(new TestHttpFactory(okHttp));
|
||||
ITileDataSource dataSource = tileSource.getDataSource();
|
||||
dataSource.dispose();
|
||||
Mockito.verify(okHttp).close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test factory that allows the specific {@link HttpEngine} instance to be
|
||||
* set.
|
||||
*/
|
||||
class TestHttpFactory implements HttpEngine.Factory {
|
||||
final HttpEngine engine;
|
||||
/**
|
||||
* Test factory that allows the specific {@link HttpEngine} instance to be
|
||||
* set.
|
||||
*/
|
||||
class TestHttpFactory implements HttpEngine.Factory {
|
||||
final HttpEngine engine;
|
||||
|
||||
public TestHttpFactory(HttpEngine engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
public TestHttpFactory(HttpEngine engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpEngine create(UrlTileSource tileSource) {
|
||||
return engine;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public HttpEngine create(UrlTileSource tileSource) {
|
||||
return engine;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user