fix: HttpEngine tests

This commit is contained in:
Hannes Janetzek 2014-04-02 07:07:30 +02:00
parent 3f9847f617
commit 5c277f4d54
5 changed files with 33 additions and 31 deletions

View File

@ -3,6 +3,7 @@ apply plugin: 'maven'
dependencies { dependencies {
compile project(':vtm') compile project(':vtm')
compile 'com.squareup.okhttp:okhttp:1.5.2'
testCompile 'junit:junit:4.11' testCompile 'junit:junit:4.11'
testCompile 'org.mockito:mockito-all:1.9.5' testCompile 'org.mockito:mockito-all:1.9.5'
testCompile 'org.easytesting:fest-assert-core:2.0M10' testCompile 'org.easytesting:fest-assert-core:2.0M10'

View File

@ -1,8 +1,11 @@
package org.oscim.tiling.source; package org.oscim.tiling.source;
import com.squareup.okhttp.mockwebserver.MockResponse; import static org.fest.assertions.api.Assertions.assertThat;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import com.squareup.okhttp.mockwebserver.RecordedRequest; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
@ -10,12 +13,9 @@ import org.junit.Test;
import org.oscim.core.Tile; import org.oscim.core.Tile;
import org.oscim.tiling.source.oscimap4.OSciMap4TileSource; import org.oscim.tiling.source.oscimap4.OSciMap4TileSource;
import java.io.BufferedReader; import com.squareup.okhttp.mockwebserver.MockResponse;
import java.io.IOException; import com.squareup.okhttp.mockwebserver.MockWebServer;
import java.io.InputStream; import com.squareup.okhttp.mockwebserver.RecordedRequest;
import java.io.InputStreamReader;
import static org.fest.assertions.api.Assertions.assertThat;
public class OkHttpEngineTest { public class OkHttpEngineTest {
private OkHttpEngine engine; private OkHttpEngine engine;
@ -29,7 +29,8 @@ public class OkHttpEngineTest {
server = new MockWebServer(); server = new MockWebServer();
server.enqueue(mockResponse); server.enqueue(mockResponse);
server.play(); server.play();
engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory().create(); engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory()
.create(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()));
} }
@After @After
@ -44,13 +45,12 @@ public class OkHttpEngineTest {
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void sendRequest_shouldRejectNullTile() throws Exception { public void sendRequest_shouldRejectNullTile() throws Exception {
engine.sendRequest(null, null); engine.sendRequest(null);
} }
@Test @Test
public void sendRequest_shouldAppendXYZToPath() throws Exception { public void sendRequest_shouldAppendXYZToPath() throws Exception {
engine.sendRequest(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()), engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
new Tile(1, 2, new Integer(3).byteValue()));
RecordedRequest request = server.takeRequest(); RecordedRequest request = server.takeRequest();
assertThat(request.getPath()).isEqualTo("/tiles/vtm/3/1/2.vtm"); assertThat(request.getPath()).isEqualTo("/tiles/vtm/3/1/2.vtm");
@ -58,8 +58,7 @@ public class OkHttpEngineTest {
@Test @Test
public void read_shouldReturnResponseStream() throws Exception { public void read_shouldReturnResponseStream() throws Exception {
engine.sendRequest(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()), engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
new Tile(1, 2, new Integer(3).byteValue()));
InputStream responseStream = engine.read(); InputStream responseStream = engine.read();
String response = new BufferedReader(new InputStreamReader(responseStream)).readLine(); String response = new BufferedReader(new InputStreamReader(responseStream)).readLine();
@ -68,8 +67,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(server.getUrl("/tiles/vtm").toString()), engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
new Tile(1, 2, new Integer(3).byteValue()));
engine.close(); engine.close();
// Calling read after the stream is closed should throw an exception. // Calling read after the stream is closed should throw an exception.
@ -79,8 +77,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(server.getUrl("/tiles/vtm").toString()), engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
new Tile(1, 2, new Integer(3).byteValue()));
engine.requestCompleted(true); engine.requestCompleted(true);
// Calling read after the stream is closed should throw an exception. // Calling read after the stream is closed should throw an exception.

View File

@ -1,17 +1,17 @@
package org.oscim.tiling.source; package org.oscim.tiling.source;
import static org.fest.assertions.api.Assertions.assertThat;
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;
import static org.fest.assertions.api.Assertions.assertThat;
public class UrlTileSourceTest { public class UrlTileSourceTest {
private UrlTileSource tileSource; private UrlTileSource tileSource;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
tileSource = new TestTileSource("http://example.org/tiles/vtm"); tileSource = new TestTileSource("http://example.org/tiles/vtm", "/{Z}/{X}/{Z}.vtm");
} }
@Test @Test
@ -33,8 +33,8 @@ public class UrlTileSourceTest {
} }
class TestTileSource extends UrlTileSource { class TestTileSource extends UrlTileSource {
public TestTileSource(String urlString) { public TestTileSource(String urlString, String tilePath) {
super(urlString); super(urlString, tilePath);
} }
@Override @Override
@ -45,7 +45,7 @@ public class UrlTileSourceTest {
class TestTileDataSource extends UrlTileDataSource { class TestTileDataSource extends UrlTileDataSource {
public TestTileDataSource(UrlTileSource tileSource, ITileDecoder tileDecoder, public TestTileDataSource(UrlTileSource tileSource, ITileDecoder tileDecoder,
HttpEngine conn) { HttpEngine conn) {
super(tileSource, tileDecoder, conn); super(tileSource, tileDecoder, conn);
} }

View File

@ -10,6 +10,7 @@ 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;
import org.oscim.tiling.source.UrlTileDataSource; import org.oscim.tiling.source.UrlTileDataSource;
import org.oscim.tiling.source.UrlTileSource;
public class BitmapTileSourceTest { public class BitmapTileSourceTest {
private BitmapTileSource tileSource; private BitmapTileSource tileSource;
@ -43,7 +44,8 @@ public class BitmapTileSourceTest {
} }
/** /**
* Test factory that allows the specific {@link HttpEngine} instance to be set. * Test factory that allows the specific {@link HttpEngine} instance to be
* set.
*/ */
class TestHttpFactory implements HttpEngine.Factory { class TestHttpFactory implements HttpEngine.Factory {
final HttpEngine engine; final HttpEngine engine;
@ -53,7 +55,7 @@ public class BitmapTileSourceTest {
} }
@Override @Override
public HttpEngine create() { public HttpEngine create(UrlTileSource tileSource) {
return engine; return engine;
} }
} }

View File

@ -1,5 +1,7 @@
package org.oscim.tiling.source.oscimap4; package org.oscim.tiling.source.oscimap4;
import static org.fest.assertions.api.Assertions.assertThat;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
@ -7,8 +9,7 @@ import org.oscim.tiling.ITileDataSource;
import org.oscim.tiling.source.HttpEngine; 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;
import org.oscim.tiling.source.UrlTileSource;
import static org.fest.assertions.api.Assertions.assertThat;
public class OSciMap4TileSourceTest { public class OSciMap4TileSourceTest {
private OSciMap4TileSource tileSource; private OSciMap4TileSource tileSource;
@ -42,7 +43,8 @@ public class OSciMap4TileSourceTest {
} }
/** /**
* Test factory that allows the specific {@link HttpEngine} instance to be set. * Test factory that allows the specific {@link HttpEngine} instance to be
* set.
*/ */
class TestHttpFactory implements HttpEngine.Factory { class TestHttpFactory implements HttpEngine.Factory {
final HttpEngine engine; final HttpEngine engine;
@ -52,7 +54,7 @@ public class OSciMap4TileSourceTest {
} }
@Override @Override
public HttpEngine create() { public HttpEngine create(UrlTileSource tileSource) {
return engine; return engine;
} }
} }