1
0
mirror of https://github.com/etesync/android synced 2024-11-25 17:38:13 +00:00

Introduce local unit tests

* split tests into Android tests and local unit tests
* LoginCredentialsFragment: check for empty host before doing IDN conversion
This commit is contained in:
Ricki Hirner 2016-04-26 13:10:42 +02:00
parent 7997606550
commit 28e567cf78
8 changed files with 67 additions and 54 deletions

View File

@ -78,5 +78,6 @@ dependencies {
compile 'org.apache.commons:commons-collections4:4.1'
// for tests
testCompile 'junit:junit:4.12'
androidTestCompile 'com.squareup.okhttp3:mockwebserver:3.2.0'
}

View File

@ -1,39 +0,0 @@
/*
* 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.davdroid;
import junit.framework.TestCase;
import java.util.Arrays;
public class ArrayUtilsTest extends TestCase {
public void testPartition() {
// n == 0
assertTrue(Arrays.deepEquals(
new Long[0][0],
ArrayUtils.partition(new Long[] { }, 5)));
// n < max
assertTrue(Arrays.deepEquals(
new Long[][] { { 1l, 2l } },
ArrayUtils.partition(new Long[] { 1l, 2l }, 5)));
// n == max
assertTrue(Arrays.deepEquals(
new Long[][] { { 1l, 2l }, { 3l, 4l } },
ArrayUtils.partition(new Long[] { 1l, 2l, 3l, 4l }, 2)));
// n > max
assertTrue(Arrays.deepEquals(
new Long[][] { { 1l, 2l, 3l, 4l, 5l }, { 6l, 7l, 8l, 9l, 10l }, { 11l } },
ArrayUtils.partition(new Long[] { 1l, 2l, 3l, 4l, 5l, 6l, 7l, 8l, 9l, 10l, 11l }, 5)));
}
}

View File

@ -1,3 +1,11 @@
/*
* 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.davdroid.ui.setup;
import android.test.InstrumentationTestCase;
@ -12,7 +20,9 @@ import at.bitfire.dav4android.property.AddressbookHomeSet;
import at.bitfire.dav4android.property.ResourceType;
import at.bitfire.davdroid.App;
import at.bitfire.davdroid.HttpClient;
import at.bitfire.davdroid.ui.setup.DavResourceFinder;
import at.bitfire.davdroid.ui.setup.DavResourceFinder.Configuration.ServiceInfo;
import at.bitfire.davdroid.ui.setup.LoginCredentials;
import okhttp3.OkHttpClient;
import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockResponse;

View File

@ -9,17 +9,13 @@
package at.bitfire.davdroid;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import org.w3c.dom.Text;
import org.apache.commons.lang3.StringUtils;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import ezvcard.util.StringUtils;
import okhttp3.HttpUrl;
public class DavUtils {
@ -36,7 +32,7 @@ public class DavUtils {
Collections.reverse(segments);
for (String segment : segments)
if (!TextUtils.isEmpty(segment))
if (!StringUtils.isEmpty(segment))
return segment;
return "/";

View File

@ -118,11 +118,12 @@ public class LoginCredentialsFragment extends Fragment implements CompoundButton
Uri baseUrl = Uri.parse(editBaseURL.getText().toString());
String scheme = baseUrl.getScheme();
if ("https".equalsIgnoreCase(scheme) || "http".equalsIgnoreCase(scheme)) {
String host = IDN.toASCII(baseUrl.getHost());
String host = baseUrl.getHost();
if (host.isEmpty()) {
editBaseURL.setError(getString(R.string.login_url_host_name_required));
valid = false;
}
} else
host = IDN.toASCII(host);
String path = baseUrl.getEncodedPath();
int port = baseUrl.getPort();

View File

@ -0,0 +1,41 @@
/*
* 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.davdroid;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
public class ArrayUtilsTest {
@Test
public void testPartition() {
// n == 0
Assert.assertTrue(Arrays.deepEquals(
new Long[0][0],
ArrayUtils.partition(new Long[] {}, 5)));
// n < max
Assert.assertTrue(Arrays.deepEquals(
new Long[][] { { 1l, 2l } },
ArrayUtils.partition(new Long[] { 1l, 2l }, 5)));
// n == max
Assert.assertTrue(Arrays.deepEquals(
new Long[][] { { 1l, 2l }, { 3l, 4l } },
ArrayUtils.partition(new Long[] { 1l, 2l, 3l, 4l }, 2)));
// n > max
Assert.assertTrue(Arrays.deepEquals(
new Long[][] { { 1l, 2l, 3l, 4l, 5l }, { 6l, 7l, 8l, 9l, 10l }, { 11l } },
ArrayUtils.partition(new Long[] { 1l, 2l, 3l, 4l, 5l, 6l, 7l, 8l, 9l, 10l, 11l }, 5)));
}
}

View File

@ -8,17 +8,19 @@
package at.bitfire.davdroid;
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Test;
public class TestDavUtils extends TestCase {
public class TestDavUtils {
private static final String exampleURL = "http://example.com/";
@Test
public void testLastSegmentOfUrl() {
assertEquals("/", DavUtils.lastSegmentOfUrl(exampleURL));
assertEquals("dir", DavUtils.lastSegmentOfUrl(exampleURL + "dir"));
assertEquals("dir", DavUtils.lastSegmentOfUrl(exampleURL + "dir/"));
assertEquals("file.html", DavUtils.lastSegmentOfUrl(exampleURL + "dir/file.html"));
Assert.assertEquals("/", DavUtils.lastSegmentOfUrl(exampleURL));
Assert.assertEquals("dir", DavUtils.lastSegmentOfUrl(exampleURL + "dir"));
Assert.assertEquals("dir", DavUtils.lastSegmentOfUrl(exampleURL + "dir/"));
Assert.assertEquals("file.html", DavUtils.lastSegmentOfUrl(exampleURL + "dir/file.html"));
}
}

View File

@ -1,2 +1,3 @@
#!/bin/bash
./gradlew -i deviceCheck mergeAndroidReports --continue
./gradlew -i testDebug && \
./gradlew -i deviceCheck mergeAndroidReports --continue