Commit 4e550cdc authored by Ricki Hirner's avatar Ricki Hirner

Refactor some tests to local unit tests, reduce Android dependencies

parent 9a334f69
......@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
android {
compileSdkVersion 24
buildToolsVersion '24.0.2'
buildToolsVersion '24.0.3'
defaultConfig {
minSdkVersion 9
......@@ -22,8 +22,12 @@ android {
}
dependencies {
compile 'org.apache.commons:commons-lang3:3.4'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
provided 'org.projectlombok:lombok:1.16.8'
provided 'org.projectlombok:lombok:1.16.10'
androidTestCompile 'com.squareup.okhttp3:mockwebserver:3.4.1'
testCompile 'junit:junit:4.12'
testCompile 'com.squareup.okhttp3:mockwebserver:3.4.1'
}
#!/bin/sh
../gradlew -i check connectedCheck
echo
echo View lint report:
echo -n file://
realpath build/outputs/lint-results-debug.html
echo
echo View local unit test reports:
echo -n file://
realpath build/reports/tests/debug/index.html
echo -n file://
realpath build/reports/tests/release/index.html
echo
echo "View connected unit test reports (debug):"
echo -n file://
realpath build/reports/androidTests/connected/index.html
/*
* Copyright © 2013 – 2016 Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.dav4android;
import junit.framework.TestCase;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
public class OkHttpTest extends TestCase {
private OkHttpClient httpClient = new OkHttpClient();
private MockWebServer mockServer = new MockWebServer();
@Override
public void setUp() throws IOException {
mockServer.start();
}
@Override
public void tearDown() throws IOException {
mockServer.shutdown();
}
public void testMultipleHeaders() throws IOException {
mockServer.enqueue(new MockResponse()
.setResponseCode(200)
.addHeader("Test", "A, B")
.addHeader("Test", "C, D")
);
Response response = httpClient.newCall(new Request.Builder()
.url(mockServer.url("/"))
.build()).execute();
assertEquals(200, response.code());
//assertEquals("A, B, C, D", response.header("Test"));
}
}
/*
* Copyright © 2013 – 2015 Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.dav4android;
import junit.framework.TestCase;
public class StringUtilsTest extends TestCase {
public void testAsQuotedString() {
assertEquals(null, StringUtils.asQuotedString(null));
assertEquals("\"\"", StringUtils.asQuotedString(""));
assertEquals("\"\\\"\"", StringUtils.asQuotedString("\""));
assertEquals("\"\\\\\"", StringUtils.asQuotedString("\\"));
}
public void testDecodeQuotedString() {
assertEquals(null, StringUtils.decodeQuotedString(null));
assertEquals("\"", StringUtils.decodeQuotedString("\""));
assertEquals("\\", StringUtils.decodeQuotedString("\"\\\""));
assertEquals("\"test", StringUtils.decodeQuotedString("\"test"));
assertEquals("test", StringUtils.decodeQuotedString("test"));
assertEquals("", StringUtils.decodeQuotedString("\"\""));
assertEquals("test", StringUtils.decodeQuotedString("\"test\""));
assertEquals("test\\", StringUtils.decodeQuotedString("\"test\\\""));
assertEquals("test", StringUtils.decodeQuotedString("\"t\\e\\st\""));
assertEquals("12\"34", StringUtils.decodeQuotedString("\"12\\\"34\""));
assertEquals("1234\"", StringUtils.decodeQuotedString("\"1234\\\"\""));
}
}
......@@ -185,7 +185,7 @@ public class DavResource {
if (ifMatchETag != null)
// only overwrite specific version
builder.header("If-Match", StringUtils.asQuotedString(ifMatchETag));
builder.header("If-Match", QuotedStringUtils.asQuotedString(ifMatchETag));
if (ifNoneMatch)
// don't overwrite anything existing
builder.header("If-None-Match", "*");
......@@ -219,7 +219,7 @@ public class DavResource {
.delete()
.url(location);
if (ifMatchETag != null)
builder.header("If-Match", StringUtils.asQuotedString(ifMatchETag));
builder.header("If-Match", QuotedStringUtils.asQuotedString(ifMatchETag));
Response response = httpClient.newCall(builder.build()).execute();
checkStatus(response, false);
......
......@@ -8,7 +8,7 @@
package at.bitfire.dav4android;
import android.text.TextUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.HashMap;
import java.util.LinkedList;
......@@ -25,8 +25,8 @@ public class HttpUtils {
private static final Pattern authSchemeWithParam = Pattern.compile("^([^ \"]+) +(.*)$");
public static String[] listHeader(Response response, String name) {
String value = TextUtils.join(",", response.headers(name));
return TextUtils.split(value, " *, *");
String value = StringUtils.join(response.headers(name), ",");
return StringUtils.splitByWholeSeparator(value, ",");
}
public static List<AuthScheme> parseWwwAuthenticate(String[] wwwAuths) {
......
......@@ -8,7 +8,7 @@
package at.bitfire.dav4android;
public class StringUtils {
public class QuotedStringUtils {
public static String asQuotedString(String raw) {
if (raw == null)
......
......@@ -17,7 +17,7 @@ import java.util.logging.Level;
import at.bitfire.dav4android.Constants;
import at.bitfire.dav4android.Property;
import at.bitfire.dav4android.PropertyFactory;
import at.bitfire.dav4android.StringUtils;
import at.bitfire.dav4android.QuotedStringUtils;
import at.bitfire.dav4android.XmlUtils;
import lombok.ToString;
......@@ -41,7 +41,7 @@ public class GetETag implements Property {
// entity tag is weak (doesn't matter for us)
rawETag = rawETag.substring(2);
eTag = StringUtils.decodeQuotedString(rawETag);
eTag = QuotedStringUtils.decodeQuotedString(rawETag);
}
......
......@@ -8,12 +8,15 @@
package at.bitfire.dav4android;
import junit.framework.TestCase;
import org.junit.Test;
import java.util.List;
public class HttpUtilsTest extends TestCase {
import static org.junit.Assert.assertEquals;
public class HttpUtilsTest {
@Test
public void testParseWwwAuthenticate() {
// two schemes: one without param (illegal!), second with two params
List<HttpUtils.AuthScheme> schemes = HttpUtils.parseWwwAuthenticate(new String[]{ " UnknownWithoutParam, Unknown WithParam1=\"a\", Param2 " });
......@@ -30,7 +33,7 @@ public class HttpUtilsTest extends TestCase {
assertEquals("Param2", schemes.get(1).unnamedParams.get(0));
// parameters with quoted strings with commas
schemes = HttpUtils.parseWwwAuthenticate(new String[]{ "X-MyScheme param1, param2=\"a,\\\"b\\\",c\", MyOtherScheme paramA" });
schemes = HttpUtils.parseWwwAuthenticate(new String[]{ "X-MyScheme param1, param2=\"a,\\\"b\\\",c\", MyOtherScheme paramA" });
assertEquals(2, schemes.size());
assertEquals("X-MyScheme", schemes.get(0).name);
assertEquals(1, schemes.get(0).params.size());
......
/*
* Copyright © 2013 – 2015 Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.dav4android;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class QuotedStringUtilsTest {
@Test
public void testAsQuotedString() {
assertEquals(null, QuotedStringUtils.asQuotedString(null));
assertEquals("\"\"", QuotedStringUtils.asQuotedString(""));
assertEquals("\"\\\"\"", QuotedStringUtils.asQuotedString("\""));
assertEquals("\"\\\\\"", QuotedStringUtils.asQuotedString("\\"));
}
public void testDecodeQuotedString() {
assertEquals(null, QuotedStringUtils.decodeQuotedString(null));
assertEquals("\"", QuotedStringUtils.decodeQuotedString("\""));
assertEquals("\\", QuotedStringUtils.decodeQuotedString("\"\\\""));
assertEquals("\"test", QuotedStringUtils.decodeQuotedString("\"test"));
assertEquals("test", QuotedStringUtils.decodeQuotedString("test"));
assertEquals("", QuotedStringUtils.decodeQuotedString("\"\""));
assertEquals("test", QuotedStringUtils.decodeQuotedString("\"test\""));
assertEquals("test\\", QuotedStringUtils.decodeQuotedString("\"test\\\""));
assertEquals("test", QuotedStringUtils.decodeQuotedString("\"t\\e\\st\""));
assertEquals("12\"34", QuotedStringUtils.decodeQuotedString("\"12\\\"34\""));
assertEquals("1234\"", QuotedStringUtils.decodeQuotedString("\"1234\\\"\""));
}
}
package at.bitfire.dav4android;
import org.junit.Test;
import okhttp3.HttpUrl;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class UrlUtilsTest extends TestCase {
public class UrlUtilsTest {
@Test
public void testEquals() {
assertTrue(UrlUtils.equals(HttpUrl.parse("http://host/resource"), HttpUrl.parse("http://host/resource")));
assertTrue(UrlUtils.equals(HttpUrl.parse("http://host:80/resource"), HttpUrl.parse("http://host/resource")));
......@@ -16,11 +21,13 @@ public class UrlUtilsTest extends TestCase {
assertFalse(UrlUtils.equals(HttpUrl.parse("http://host/resource"), HttpUrl.parse("http://host:81/resource")));
}
@Test
public void testOmitTrailingSlash() {
assertEquals(HttpUrl.parse("http://host/resource"), UrlUtils.omitTrailingSlash(HttpUrl.parse("http://host/resource")));
assertEquals(HttpUrl.parse("http://host/resource"), UrlUtils.omitTrailingSlash(HttpUrl.parse("http://host/resource/")));
}
@Test
public void testWithTrailingSlash() {
assertEquals(HttpUrl.parse("http://host/resource/"), UrlUtils.withTrailingSlash(HttpUrl.parse("http://host/resource")));
assertEquals(HttpUrl.parse("http://host/resource/"), UrlUtils.withTrailingSlash(HttpUrl.parse("http://host/resource/")));
......
......@@ -8,18 +8,23 @@
package at.bitfire.dav4android.exception;
import org.junit.Test;
import java.util.Calendar;
import java.util.Date;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.internal.http.HttpDate;
import junit.framework.TestCase;
import java.util.Calendar;
import java.util.Date;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class ServiceUnavailableExceptionTest extends TestCase {
public class ServiceUnavailableExceptionTest {
@Test
public void testRetryAfter() {
Response response = new Response.Builder()
.request(new Request.Builder()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment