Use robolectric for unit tests
Этот коммит содержится в:
@@ -30,4 +30,5 @@ android {
|
||||
|
||||
dependencies {
|
||||
testCompile 'junit:junit:4.12'
|
||||
testCompile "org.robolectric:robolectric:3.0"
|
||||
}
|
||||
|
||||
@@ -15,17 +15,68 @@
|
||||
*/
|
||||
package org.traccar.client;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
public class RequestManager {
|
||||
|
||||
private static final int TIMEOUT = 15 * 1000;
|
||||
|
||||
public interface RequestHandler {
|
||||
void onSuccess();
|
||||
void onFailure();
|
||||
}
|
||||
|
||||
private static class RequestAsyncTask extends AsyncTask<String, Void, Boolean> {
|
||||
|
||||
private RequestHandler handler;
|
||||
|
||||
public RequestAsyncTask(RequestHandler handler) {
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Boolean doInBackground(String... request) {
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
URL url = new URL(request[0]);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setReadTimeout(TIMEOUT);
|
||||
connection.setConnectTimeout(TIMEOUT);
|
||||
connection.connect();
|
||||
inputStream = connection.getInputStream();
|
||||
while (inputStream.read() != -1);
|
||||
return true;
|
||||
} catch (IOException error) {
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
if (inputStream != null) {
|
||||
inputStream.close();
|
||||
}
|
||||
} catch (IOException secondError) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Boolean result) {
|
||||
if (result) {
|
||||
handler.onSuccess();
|
||||
} else {
|
||||
handler.onFailure();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendRequest(String request, RequestHandler handler) {
|
||||
|
||||
handler.onSuccess();
|
||||
|
||||
RequestAsyncTask task = new RequestAsyncTask(handler);
|
||||
task.execute(request);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,28 +1,32 @@
|
||||
|
||||
package org.traccar.client;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricGradleTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import static org.junit.Assert.assertNull;
|
||||
import java.lang.Override;
|
||||
|
||||
@RunWith(RobolectricGradleTestRunner.class)
|
||||
@Config(constants = BuildConfig.class, sdk = 21)
|
||||
public class RequestManagerTest {
|
||||
|
||||
@Test
|
||||
public void testSendRequest() throws Exception {
|
||||
|
||||
assertNull(null);
|
||||
|
||||
//ApelProtocolDecoder decoder = new ApelProtocolDecoder(new ApelProtocol());
|
||||
|
||||
/*byte[] buf1 = {0x40,0x4E,0x54,0x43,0x01,0x00,0x00,0x00,0x7B,0x00,0x00,0x00,0x13,0x00,0x44,0x34,0x2A,0x3E,0x53,0x3A,0x38,0x36,0x31,0x37,0x38,0x35,0x30,0x30,0x35,0x32,0x30,0x35,0x30,0x37,0x39};
|
||||
assertNull(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, buf1)));*/
|
||||
|
||||
//0c002900f12a00000f003235303032363533343135313036340f0033353638393530333632373938313101002000000000
|
||||
//5c00380046e6a95136b693277f11b41a00172709f2ff03160002b9bc630007000000000000000000000000000000c31071090000880500000000000000000000
|
||||
//5c00380072e7a95136b693277f11b41a00172709f2ff03160002b9bc630007000000000000000000000000000000c31071090000880500000000000000000000
|
||||
|
||||
//7900040069ea030000000000
|
||||
//8300c20003006aea03005c003800223aab5107a393276617b41a0030d506e3000414010250bf630007000000000000000000000000000000c3107209000089050000000000006bea03005c003800403aab5107a393276617b41a0030d506e3000414010250bf630007000000000000000000000000000000c3107209000089050000000000006cea03005c0038005e3aab5107a393276617b41a0030d506e3000414010250bf630007000000000000000000000000000000c31072090000890500000000000000000000
|
||||
RequestManager.sendRequest("http://www.google.com", new RequestManager.RequestHandler() {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
@Override
|
||||
public void onFailure() {
|
||||
Assert.assertTrue(false);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user