Fixes HttpEngine tests
This commit is contained in:
parent
2bf5313c2b
commit
d709d7f39a
vtm-tests/test/org/oscim/tiling/source
@ -14,7 +14,6 @@ import java.io.BufferedReader;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.net.URL;
|
|
||||||
|
|
||||||
import static org.fest.assertions.api.Assertions.assertThat;
|
import static org.fest.assertions.api.Assertions.assertThat;
|
||||||
|
|
||||||
@ -30,7 +29,7 @@ public class OkHttpEngineTest {
|
|||||||
server = new MockWebServer();
|
server = new MockWebServer();
|
||||||
server.enqueue(mockResponse);
|
server.enqueue(mockResponse);
|
||||||
server.play();
|
server.play();
|
||||||
engine = new OkHttpEngine(server.getUrl("/tiles/vtm"));
|
engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory().create();
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
@ -50,7 +49,7 @@ public class OkHttpEngineTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void sendRequest_shouldAppendXYZToPath() throws Exception {
|
public void sendRequest_shouldAppendXYZToPath() throws Exception {
|
||||||
engine.sendRequest(new OSciMap4TileSource("http://www.example.org/tiles/vtm"),
|
engine.sendRequest(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()),
|
||||||
new Tile(1, 2, new Integer(3).byteValue()));
|
new Tile(1, 2, new Integer(3).byteValue()));
|
||||||
|
|
||||||
RecordedRequest request = server.takeRequest();
|
RecordedRequest request = server.takeRequest();
|
||||||
@ -59,7 +58,7 @@ public class OkHttpEngineTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void read_shouldReturnResponseStream() throws Exception {
|
public void read_shouldReturnResponseStream() throws Exception {
|
||||||
engine.sendRequest(new OSciMap4TileSource("http://www.example.org/tiles/vtm"),
|
engine.sendRequest(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()),
|
||||||
new Tile(1, 2, new Integer(3).byteValue()));
|
new Tile(1, 2, new Integer(3).byteValue()));
|
||||||
|
|
||||||
InputStream responseStream = engine.read();
|
InputStream responseStream = engine.read();
|
||||||
@ -69,7 +68,7 @@ public class OkHttpEngineTest {
|
|||||||
|
|
||||||
@Test(expected = IOException.class)
|
@Test(expected = IOException.class)
|
||||||
public void close_shouldCloseInputStream() throws Exception {
|
public void close_shouldCloseInputStream() throws Exception {
|
||||||
engine.sendRequest(new OSciMap4TileSource("http://www.example.org/tiles/vtm"),
|
engine.sendRequest(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()),
|
||||||
new Tile(1, 2, new Integer(3).byteValue()));
|
new Tile(1, 2, new Integer(3).byteValue()));
|
||||||
engine.close();
|
engine.close();
|
||||||
|
|
||||||
@ -80,7 +79,7 @@ public class OkHttpEngineTest {
|
|||||||
|
|
||||||
@Test(expected = IOException.class)
|
@Test(expected = IOException.class)
|
||||||
public void requestCompleted_shouldCloseInputStream() throws Exception {
|
public void requestCompleted_shouldCloseInputStream() throws Exception {
|
||||||
engine.sendRequest(new OSciMap4TileSource("http://www.example.org/tiles/vtm"),
|
engine.sendRequest(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()),
|
||||||
new Tile(1, 2, new Integer(3).byteValue()));
|
new Tile(1, 2, new Integer(3).byteValue()));
|
||||||
engine.requestCompleted(true);
|
engine.requestCompleted(true);
|
||||||
|
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package org.oscim.tiling.source;
|
package org.oscim.tiling.source;
|
||||||
|
|
||||||
import com.squareup.okhttp.OkHttpClient;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.oscim.tiling.ITileDataSource;
|
import org.oscim.tiling.ITileDataSource;
|
||||||
@ -29,7 +27,7 @@ public class UrlTileSourceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldUseCustomHttpEngine() throws Exception {
|
public void shouldUseCustomHttpEngine() throws Exception {
|
||||||
tileSource.setHttpEngine(new OkHttpEngine(tileSource.getUrl()));
|
tileSource.setHttpEngine(new OkHttpEngine.OkHttpFactory());
|
||||||
TestTileDataSource dataSource = (TestTileDataSource) tileSource.getDataSource();
|
TestTileDataSource dataSource = (TestTileDataSource) tileSource.getDataSource();
|
||||||
assertThat(dataSource.getConnection()).isInstanceOf(OkHttpEngine.class);
|
assertThat(dataSource.getConnection()).isInstanceOf(OkHttpEngine.class);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,9 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.oscim.tiling.ITileDataSource;
|
import org.oscim.tiling.ITileDataSource;
|
||||||
import org.oscim.tiling.source.OkHttpEngine.OkHttpFactory;
|
import org.oscim.tiling.source.HttpEngine;
|
||||||
|
import org.oscim.tiling.source.LwHttp;
|
||||||
|
import org.oscim.tiling.source.OkHttpEngine;
|
||||||
import org.oscim.tiling.source.UrlTileDataSource;
|
import org.oscim.tiling.source.UrlTileDataSource;
|
||||||
|
|
||||||
public class BitmapTileSourceTest {
|
public class BitmapTileSourceTest {
|
||||||
@ -14,7 +16,7 @@ public class BitmapTileSourceTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
tileSource = new TestBitmapTileSource("http://tile.openstreetmap.org", 0, 18);
|
tileSource = new BitmapTileSource("http://tile.openstreetmap.org", 0, 18);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -24,8 +26,8 @@ public class BitmapTileSourceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldUseLwHttp() throws Exception {
|
public void shouldUseLwHttp() throws Exception {
|
||||||
LwHttpFactory lwHttp = Mockito.mock(LwHttpFactory.class);
|
LwHttp lwHttp = Mockito.mock(LwHttp.class);
|
||||||
tileSource.setHttpEngine(lwHttp);
|
tileSource.setHttpEngine(new TestHttpFactory(lwHttp));
|
||||||
ITileDataSource dataSource = tileSource.getDataSource();
|
ITileDataSource dataSource = tileSource.getDataSource();
|
||||||
dataSource.destroy();
|
dataSource.destroy();
|
||||||
Mockito.verify(lwHttp).close();
|
Mockito.verify(lwHttp).close();
|
||||||
@ -33,16 +35,26 @@ public class BitmapTileSourceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldUseOkHttp() throws Exception {
|
public void shouldUseOkHttp() throws Exception {
|
||||||
OkHttpFactory okHttp = Mockito.mock(OkHttpFactory.class);
|
OkHttpEngine okHttp = Mockito.mock(OkHttpEngine.class);
|
||||||
tileSource.setHttpEngine(okHttp);
|
tileSource.setHttpEngine(new TestHttpFactory(okHttp));
|
||||||
UrlTileDataSource dataSource = (UrlTileDataSource) tileSource.getDataSource();
|
UrlTileDataSource dataSource = (UrlTileDataSource) tileSource.getDataSource();
|
||||||
dataSource.destroy();
|
dataSource.destroy();
|
||||||
//Mockito.verify(dataSource.mConn).close();
|
Mockito.verify(okHttp).close();
|
||||||
}
|
}
|
||||||
|
|
||||||
class TestBitmapTileSource extends BitmapTileSource {
|
/**
|
||||||
public TestBitmapTileSource(String url, int zoomMin, int zoomMax) {
|
* Test factory that allows the specific {@link HttpEngine} instance to be set.
|
||||||
super(url, zoomMin, zoomMax);
|
*/
|
||||||
|
class TestHttpFactory implements HttpEngine.Factory {
|
||||||
|
final HttpEngine engine;
|
||||||
|
|
||||||
|
public TestHttpFactory(HttpEngine engine) {
|
||||||
|
this.engine = engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public HttpEngine create() {
|
||||||
|
return engine;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.oscim.tiling.ITileDataSource;
|
import org.oscim.tiling.ITileDataSource;
|
||||||
|
import org.oscim.tiling.source.HttpEngine;
|
||||||
import org.oscim.tiling.source.LwHttp;
|
import org.oscim.tiling.source.LwHttp;
|
||||||
import org.oscim.tiling.source.OkHttpEngine;
|
import org.oscim.tiling.source.OkHttpEngine;
|
||||||
|
|
||||||
@ -25,7 +26,7 @@ public class OSciMap4TileSourceTest {
|
|||||||
@Test
|
@Test
|
||||||
public void shouldUseLwHttp() throws Exception {
|
public void shouldUseLwHttp() throws Exception {
|
||||||
LwHttp lwHttp = Mockito.mock(LwHttp.class);
|
LwHttp lwHttp = Mockito.mock(LwHttp.class);
|
||||||
tileSource.setHttpEngine(lwHttp);
|
tileSource.setHttpEngine(new TestHttpFactory(lwHttp));
|
||||||
ITileDataSource dataSource = tileSource.getDataSource();
|
ITileDataSource dataSource = tileSource.getDataSource();
|
||||||
dataSource.destroy();
|
dataSource.destroy();
|
||||||
Mockito.verify(lwHttp).close();
|
Mockito.verify(lwHttp).close();
|
||||||
@ -34,9 +35,25 @@ public class OSciMap4TileSourceTest {
|
|||||||
@Test
|
@Test
|
||||||
public void shouldUseOkHttp() throws Exception {
|
public void shouldUseOkHttp() throws Exception {
|
||||||
OkHttpEngine okHttp = Mockito.mock(OkHttpEngine.class);
|
OkHttpEngine okHttp = Mockito.mock(OkHttpEngine.class);
|
||||||
tileSource.setHttpEngine(okHttp);
|
tileSource.setHttpEngine(new TestHttpFactory(okHttp));
|
||||||
ITileDataSource dataSource = tileSource.getDataSource();
|
ITileDataSource dataSource = tileSource.getDataSource();
|
||||||
dataSource.destroy();
|
dataSource.destroy();
|
||||||
Mockito.verify(okHttp).close();
|
Mockito.verify(okHttp).close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public HttpEngine create() {
|
||||||
|
return engine;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user