From 28e567cf7860985c7cf960d0e89549a0783331e7 Mon Sep 17 00:00:00 2001 From: Ricki Hirner Date: Tue, 26 Apr 2016 13:10:42 +0200 Subject: [PATCH] Introduce local unit tests * split tests into Android tests and local unit tests * LoginCredentialsFragment: check for empty host before doing IDN conversion --- app/build.gradle | 1 + .../at/bitfire/davdroid/ArrayUtilsTest.java | 39 ------------------ .../ui/setup/DavResourceFinderTest.java | 10 +++++ .../java/at/bitfire/davdroid/DavUtils.java | 8 +--- .../ui/setup/LoginCredentialsFragment.java | 5 ++- .../at/bitfire/davdroid/ArrayUtilsTest.java | 41 +++++++++++++++++++ .../at/bitfire/davdroid/TestDavUtils.java | 14 ++++--- run-tests-connected.sh | 3 +- 8 files changed, 67 insertions(+), 54 deletions(-) delete mode 100644 app/src/androidTest/java/at/bitfire/davdroid/ArrayUtilsTest.java create mode 100644 app/src/test/java/at/bitfire/davdroid/ArrayUtilsTest.java rename app/src/{androidTest => test}/java/at/bitfire/davdroid/TestDavUtils.java (52%) diff --git a/app/build.gradle b/app/build.gradle index 4944d0c0..4dd48dad 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' } diff --git a/app/src/androidTest/java/at/bitfire/davdroid/ArrayUtilsTest.java b/app/src/androidTest/java/at/bitfire/davdroid/ArrayUtilsTest.java deleted file mode 100644 index 3b927aea..00000000 --- a/app/src/androidTest/java/at/bitfire/davdroid/ArrayUtilsTest.java +++ /dev/null @@ -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))); - } - -} diff --git a/app/src/androidTest/java/at/bitfire/davdroid/ui/setup/DavResourceFinderTest.java b/app/src/androidTest/java/at/bitfire/davdroid/ui/setup/DavResourceFinderTest.java index 4070c44e..806cd803 100644 --- a/app/src/androidTest/java/at/bitfire/davdroid/ui/setup/DavResourceFinderTest.java +++ b/app/src/androidTest/java/at/bitfire/davdroid/ui/setup/DavResourceFinderTest.java @@ -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; diff --git a/app/src/main/java/at/bitfire/davdroid/DavUtils.java b/app/src/main/java/at/bitfire/davdroid/DavUtils.java index 53c98e50..8664f467 100644 --- a/app/src/main/java/at/bitfire/davdroid/DavUtils.java +++ b/app/src/main/java/at/bitfire/davdroid/DavUtils.java @@ -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 "/"; diff --git a/app/src/main/java/at/bitfire/davdroid/ui/setup/LoginCredentialsFragment.java b/app/src/main/java/at/bitfire/davdroid/ui/setup/LoginCredentialsFragment.java index 61c3c313..fb7e766a 100644 --- a/app/src/main/java/at/bitfire/davdroid/ui/setup/LoginCredentialsFragment.java +++ b/app/src/main/java/at/bitfire/davdroid/ui/setup/LoginCredentialsFragment.java @@ -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(); diff --git a/app/src/test/java/at/bitfire/davdroid/ArrayUtilsTest.java b/app/src/test/java/at/bitfire/davdroid/ArrayUtilsTest.java new file mode 100644 index 00000000..b301dc9f --- /dev/null +++ b/app/src/test/java/at/bitfire/davdroid/ArrayUtilsTest.java @@ -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))); + } + +} diff --git a/app/src/androidTest/java/at/bitfire/davdroid/TestDavUtils.java b/app/src/test/java/at/bitfire/davdroid/TestDavUtils.java similarity index 52% rename from app/src/androidTest/java/at/bitfire/davdroid/TestDavUtils.java rename to app/src/test/java/at/bitfire/davdroid/TestDavUtils.java index 92549697..41c38ea0 100644 --- a/app/src/androidTest/java/at/bitfire/davdroid/TestDavUtils.java +++ b/app/src/test/java/at/bitfire/davdroid/TestDavUtils.java @@ -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")); } } diff --git a/run-tests-connected.sh b/run-tests-connected.sh index 6fd5b0ac..81b090da 100755 --- a/run-tests-connected.sh +++ b/run-tests-connected.sh @@ -1,2 +1,3 @@ #!/bin/bash -./gradlew -i deviceCheck mergeAndroidReports --continue +./gradlew -i testDebug && \ + ./gradlew -i deviceCheck mergeAndroidReports --continue