diff --git a/app/src/androidTest/java/it/niedermann/owncloud/notes/util/NoteUtilTest.java b/app/src/androidTest/java/it/niedermann/owncloud/notes/util/NoteUtilTest.java index 8f892311..c34ebe29 100644 --- a/app/src/androidTest/java/it/niedermann/owncloud/notes/util/NoteUtilTest.java +++ b/app/src/androidTest/java/it/niedermann/owncloud/notes/util/NoteUtilTest.java @@ -2,6 +2,9 @@ package it.niedermann.owncloud.notes.util; import junit.framework.TestCase; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + /** * Tests the NoteUtil * Created by stefan on 06.10.15. @@ -39,19 +42,39 @@ public class NoteUtilTest extends TestCase { } public void testIsEmptyLine() { - assertTrue(NoteUtil.isEmptyLine(" ")); - assertTrue(NoteUtil.isEmptyLine("\n")); - assertTrue(NoteUtil.isEmptyLine("\n ")); - assertTrue(NoteUtil.isEmptyLine(" \n")); - assertTrue(NoteUtil.isEmptyLine(" \n ")); - assertFalse(NoteUtil.isEmptyLine("a \n ")); + try { + Method m = NoteUtil.class.getDeclaredMethod("isEmptyLine"); + m.setAccessible(true); + assertTrue((Boolean) m.invoke(null, " ")); + assertTrue((Boolean) m.invoke(null, "\n")); + assertTrue((Boolean) m.invoke(null, "\n ")); + assertTrue((Boolean) m.invoke(null, " \n")); + assertTrue((Boolean) m.invoke(null, " \n ")); + assertFalse((Boolean) m.invoke(null, "a \n ")); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } } public void testGetLineWithoutMarkDown() { - assertEquals("Test", NoteUtil.getLineWithoutMarkDown("Test", 0)); - assertEquals("Test", NoteUtil.getLineWithoutMarkDown("\nTest", 0)); - assertEquals("Foo", NoteUtil.getLineWithoutMarkDown("Foo\nBar", 0)); - assertEquals("Bar", NoteUtil.getLineWithoutMarkDown("Foo\nBar", 1)); + try { + Method m = NoteUtil.class.getDeclaredMethod("isEmptyLine"); + m.setAccessible(true); + assertEquals("Test", (String) m.invoke(null, "Test", 0)); + assertEquals("Test", (String) m.invoke(null, "\nTest", 0)); + assertEquals("Foo", (String) m.invoke(null, "Foo\nBar", 0)); + assertEquals("Bar", (String) m.invoke(null, "Foo\nBar", 1)); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } } public void testGenerateNoteTitle() { diff --git a/app/src/androidTest/java/it/niedermann/owncloud/notes/util/NotesClientUtilTest.java b/app/src/androidTest/java/it/niedermann/owncloud/notes/util/NotesClientUtilTest.java index 6e85f79b..06fa9bd1 100644 --- a/app/src/androidTest/java/it/niedermann/owncloud/notes/util/NotesClientUtilTest.java +++ b/app/src/androidTest/java/it/niedermann/owncloud/notes/util/NotesClientUtilTest.java @@ -2,11 +2,27 @@ package it.niedermann.owncloud.notes.util; import junit.framework.TestCase; +import java.lang.reflect.Method; + /** * Tests the NotesClientUtil * Created by stefan on 24.09.15. */ public class NotesClientUtilTest extends TestCase { + public void testFormatURL() { + assertEquals("https://example.com/", NotesClientUtil.formatURL("example.com/")); + assertEquals("http://example.com/", NotesClientUtil.formatURL("http://example.com/")); + assertEquals("https://example.com/", NotesClientUtil.formatURL("example.com/index.php")); + assertEquals("https://example.com/", NotesClientUtil.formatURL("example.com/index.php/")); + assertEquals("https://example.com/", NotesClientUtil.formatURL("example.com/index.php/apps")); + assertEquals("https://example.com/", NotesClientUtil.formatURL("example.com/index.php/apps/notes")); + assertEquals("https://example.com/", NotesClientUtil.formatURL("example.com/index.php/apps/notes/api")); + assertEquals("https://example.com/", NotesClientUtil.formatURL("example.com/index.php/apps/notes/api/v0.2")); + assertEquals("https://example.com/", NotesClientUtil.formatURL("example.com/index.php/apps/notes/api/v0.2/notes")); + assertEquals("https://example.com/nextcloud/", NotesClientUtil.formatURL("example.com/nextcloud")); + assertEquals("http://example.com:443/nextcloud/", NotesClientUtil.formatURL("http://example.com:443/nextcloud/index.php/apps/notes/api/v0.2/notes")); + } + public void testIsHttp() { assertTrue(NotesClientUtil.isHttp("http://example.com")); assertTrue(NotesClientUtil.isHttp("http://www.example.com/")); diff --git a/app/src/main/java/it/niedermann/owncloud/notes/android/activity/SettingsActivity.java b/app/src/main/java/it/niedermann/owncloud/notes/android/activity/SettingsActivity.java index 75299867..7401bbfc 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/android/activity/SettingsActivity.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/android/activity/SettingsActivity.java @@ -178,16 +178,13 @@ public class SettingsActivity extends AppCompatActivity { String url = field_url.getText().toString().trim(); String username = field_username.getText().toString(); String password = field_password.getText().toString(); + if(password.isEmpty()) { password = old_password; } - if (!url.endsWith("/")) { - url += "/"; - } - if (!url.startsWith("http://") && !url.startsWith("https://")) { - url = "https://" + url; - } + url = NotesClientUtil.formatURL(url); + new LoginValidatorAsyncTask().execute(url, username, password); } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/util/NotesClientUtil.java b/app/src/main/java/it/niedermann/owncloud/notes/util/NotesClientUtil.java index 125bfc12..cc5f4692 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/util/NotesClientUtil.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/util/NotesClientUtil.java @@ -50,6 +50,27 @@ public class NotesClientUtil { return url != null && url.length() > 4 && url.startsWith("http") && url.charAt(4) != 's'; } + /** + * Strips the api part from the path of a given url, handles trailing slash and missing protocol + * @param url String + * @return formatted URL + */ + public static String formatURL(String url) { + if (!url.endsWith("/")) { + url += "/"; + } + if (!url.startsWith("http://") && !url.startsWith("https://")) { + url = "https://" + url; + } + String[] replacements = new String[] {"notes/", "v0.2/", "api/", "notes/", "apps/", "index.php/"}; + for(String replacement: replacements) { + if(url.endsWith(replacement)) { + url = url.substring(0, url.length() - replacement.length()); + } + } + return url; + } + /** * @param url String * @param username String