From 8a403dbb1af71a3551f15f7ffec5f395009f3d61 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Fri, 25 Apr 2025 14:42:27 +0200 Subject: [PATCH 1/7] fix Signed-off-by: alperozturk --- .../android/ResponseFormatDetectorTests.kt | 46 +++++++++++++++ ...arserIT.java => XMLExceptionParserIT.java} | 24 ++++---- .../operations/RemoteOperationResult.java | 19 ++++-- ...ionParser.java => XMLExceptionParser.java} | 17 ++---- .../utils/responseFormat/ResponseFormat.kt | 12 ++++ .../responseFormat/ResponseFormatDetector.kt | 58 +++++++++++++++++++ 6 files changed, 147 insertions(+), 29 deletions(-) create mode 100644 library/src/androidTest/java/com/owncloud/android/ResponseFormatDetectorTests.kt rename library/src/androidTest/java/com/owncloud/android/{ExceptionParserIT.java => XMLExceptionParserIT.java} (79%) rename library/src/main/java/com/owncloud/android/lib/common/operations/{ExceptionParser.java => XMLExceptionParser.java} (91%) create mode 100644 library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormat.kt create mode 100644 library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormatDetector.kt diff --git a/library/src/androidTest/java/com/owncloud/android/ResponseFormatDetectorTests.kt b/library/src/androidTest/java/com/owncloud/android/ResponseFormatDetectorTests.kt new file mode 100644 index 0000000000..7dacf844ba --- /dev/null +++ b/library/src/androidTest/java/com/owncloud/android/ResponseFormatDetectorTests.kt @@ -0,0 +1,46 @@ +/* + * Nextcloud - Android Client + * + * SPDX-FileCopyrightText: 2025 Alper Ozturk + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +package com.owncloud.android + +import com.owncloud.android.lib.common.utils.responseFormat.ResponseFormat +import com.owncloud.android.lib.common.utils.responseFormat.ResponseFormatDetector +import junit.framework.TestCase.assertEquals +import org.junit.Test + +class ResponseFormatDetectorTests { + + @Test + fun testJsonDetection() { + val json = """{ "name": "Alice", "age": 30 }""" + assertEquals(ResponseFormat.JSON, ResponseFormatDetector.detectFormat(json)) + } + + @Test + fun testJsonArrayDetection() { + val jsonArray = """[{"name": "Alice"}, {"name": "Bob"}]""" + assertEquals(ResponseFormat.JSON, ResponseFormatDetector.detectFormat(jsonArray)) + } + + @Test + fun testXmlDetection() { + val xml = """Alice30""" + assertEquals(ResponseFormat.XML, ResponseFormatDetector.detectFormat(xml)) + } + + @Test + fun testInvalidFormat() { + val invalid = "Just a plain string" + assertEquals(ResponseFormat.UNKNOWN, ResponseFormatDetector.detectFormat(invalid)) + } + + @Test + fun testEmptyString() { + val empty = "" + assertEquals(ResponseFormat.UNKNOWN, ResponseFormatDetector.detectFormat(empty)) + } +} diff --git a/library/src/androidTest/java/com/owncloud/android/ExceptionParserIT.java b/library/src/androidTest/java/com/owncloud/android/XMLExceptionParserIT.java similarity index 79% rename from library/src/androidTest/java/com/owncloud/android/ExceptionParserIT.java rename to library/src/androidTest/java/com/owncloud/android/XMLExceptionParserIT.java index 017e9e589a..418e296cff 100644 --- a/library/src/androidTest/java/com/owncloud/android/ExceptionParserIT.java +++ b/library/src/androidTest/java/com/owncloud/android/XMLExceptionParserIT.java @@ -10,7 +10,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import com.owncloud.android.lib.common.operations.ExceptionParser; +import com.owncloud.android.lib.common.operations.XMLExceptionParser; import org.junit.Test; @@ -23,10 +23,10 @@ * Created by tobi on 3/21/18. */ -public class ExceptionParserIT { +public class XMLExceptionParserIT { @Test - public void testVirusException() throws IOException { + public void testVirusException() { String virusException = "\n" + "\n" + " OCA\\DAV\\Connector\\Sabre\\Exception\\UnsupportedMediaType" + @@ -36,14 +36,14 @@ public void testVirusException() throws IOException { ""; InputStream is = new ByteArrayInputStream(virusException.getBytes()); - ExceptionParser xmlParser = new ExceptionParser(is); + XMLExceptionParser xmlParser = new XMLExceptionParser(is); assertTrue(xmlParser.isVirusException()); assertFalse(xmlParser.isInvalidCharacterException()); } @Test - public void testInvalidCharacterException() throws IOException { + public void testInvalidCharacterException() { String virusException = "\n" + "\n" + " OC\\Connector\\Sabre\\Exception\\InvalidPath\n" + @@ -51,14 +51,14 @@ public void testInvalidCharacterException() throws IOException { ""; InputStream is = new ByteArrayInputStream(virusException.getBytes()); - ExceptionParser xmlParser = new ExceptionParser(is); + XMLExceptionParser xmlParser = new XMLExceptionParser(is); assertTrue(xmlParser.isInvalidCharacterException()); assertFalse(xmlParser.isVirusException()); } @Test - public void testInvalidCharacterUploadException() throws IOException { + public void testInvalidCharacterUploadException() { String virusException = "\n" + "\n" + " OCP\\Files\\InvalidPathException\n" + @@ -66,29 +66,29 @@ public void testInvalidCharacterUploadException() throws IOException { ""; InputStream is = new ByteArrayInputStream(virusException.getBytes()); - ExceptionParser xmlParser = new ExceptionParser(is); + XMLExceptionParser xmlParser = new XMLExceptionParser(is); assertTrue(xmlParser.isInvalidCharacterException()); assertFalse(xmlParser.isVirusException()); } @Test - public void testEmptyString() throws IOException { + public void testEmptyString() { String emptyString = ""; InputStream is = new ByteArrayInputStream(emptyString.getBytes()); - ExceptionParser xmlParser = new ExceptionParser(is); + XMLExceptionParser xmlParser = new XMLExceptionParser(is); assertFalse(xmlParser.isVirusException()); assertFalse(xmlParser.isInvalidCharacterException()); } @Test - public void testString() throws IOException { + public void testString() { String emptyString = ""; InputStream is = new ByteArrayInputStream(emptyString.getBytes()); - ExceptionParser xmlParser = new ExceptionParser(is); + XMLExceptionParser xmlParser = new XMLExceptionParser(is); assertFalse(xmlParser.isVirusException()); assertFalse(xmlParser.isInvalidCharacterException()); diff --git a/library/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperationResult.java b/library/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperationResult.java index 5f281a80dc..0441e588b8 100644 --- a/library/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperationResult.java +++ b/library/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperationResult.java @@ -30,6 +30,8 @@ import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException; import com.owncloud.android.lib.common.network.CertificateCombinedException; import com.owncloud.android.lib.common.utils.Log_OC; +import com.owncloud.android.lib.common.utils.responseFormat.ResponseFormat; +import com.owncloud.android.lib.common.utils.responseFormat.ResponseFormatDetector; import com.owncloud.android.lib.resources.files.CreateLocalFileException; import org.apache.commons.httpclient.ConnectTimeoutException; @@ -235,10 +237,13 @@ public RemoteOperationResult(boolean success, String bodyResponse, int httpCode) switch (httpCode) { case HttpStatus.SC_BAD_REQUEST: try { - InputStream is = new ByteArrayInputStream(bodyResponse.getBytes()); - ExceptionParser xmlParser = new ExceptionParser(is); - if (xmlParser.isInvalidCharacterException()) { - mCode = ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER; + if (!bodyResponse.isEmpty() && + ResponseFormatDetector.INSTANCE.detectFormat(bodyResponse) == ResponseFormat.XML) { + InputStream is = new ByteArrayInputStream(bodyResponse.getBytes()); + XMLExceptionParser xmlParser = new XMLExceptionParser(is); + if (xmlParser.isInvalidCharacterException()) { + mCode = ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER; + } } } catch (Exception e) { mCode = ResultCode.UNHANDLED_HTTP_CODE; @@ -336,9 +341,11 @@ public RemoteOperationResult(boolean success, HttpMethod httpMethod) { try { String bodyResponse = httpMethod.getResponseBodyAsString(); - if (bodyResponse != null && bodyResponse.length() > 0) { + if (bodyResponse != null + && !bodyResponse.isEmpty() && + ResponseFormatDetector.INSTANCE.detectFormat(bodyResponse) == ResponseFormat.XML) { InputStream is = new ByteArrayInputStream(bodyResponse.getBytes()); - ExceptionParser xmlParser = new ExceptionParser(is); + XMLExceptionParser xmlParser = new XMLExceptionParser(is); if (xmlParser.isInvalidCharacterException()) { mCode = ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER; diff --git a/library/src/main/java/com/owncloud/android/lib/common/operations/ExceptionParser.java b/library/src/main/java/com/owncloud/android/lib/common/operations/XMLExceptionParser.java similarity index 91% rename from library/src/main/java/com/owncloud/android/lib/common/operations/ExceptionParser.java rename to library/src/main/java/com/owncloud/android/lib/common/operations/XMLExceptionParser.java index 9fcb55ce33..7f458d577e 100644 --- a/library/src/main/java/com/owncloud/android/lib/common/operations/ExceptionParser.java +++ b/library/src/main/java/com/owncloud/android/lib/common/operations/XMLExceptionParser.java @@ -24,8 +24,8 @@ * * @author masensio, tobiaskaminsky */ -public class ExceptionParser { - private static final String TAG = ExceptionParser.class.getSimpleName(); +public class XMLExceptionParser { + private static final String TAG = XMLExceptionParser.class.getSimpleName(); private static final String INVALID_PATH_EXCEPTION_STRING = "OC\\Connector\\Sabre\\Exception\\InvalidPath"; private static final String INVALID_PATH_EXCEPTION_UPLOAD_STRING = "OCP\\Files\\InvalidPathException"; @@ -46,25 +46,20 @@ public class ExceptionParser { /** * Parse is as an Invalid Path Exception * - * @param is InputStream xml - * @return if The exception is an Invalid Char Exception - * @throws IOException + * @param inputStream InputStream xml */ - public ExceptionParser(InputStream is) throws IOException { - try { - // XMLPullParser + public XMLExceptionParser(InputStream inputStream) { + try (inputStream) { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); XmlPullParser parser = Xml.newPullParser(); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); - parser.setInput(is, null); + parser.setInput(inputStream, null); parser.nextTag(); readError(parser); } catch (Exception e) { Log_OC.e(TAG, "Error parsing exception: " + e.getMessage()); - } finally { - is.close(); } } diff --git a/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormat.kt b/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormat.kt new file mode 100644 index 0000000000..156305a973 --- /dev/null +++ b/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormat.kt @@ -0,0 +1,12 @@ +/* + * Nextcloud - Android Client + * + * SPDX-FileCopyrightText: 2025 Alper Ozturk + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +package com.owncloud.android.lib.common.utils.responseFormat + +enum class ResponseFormat { + JSON, XML, UNKNOWN +} diff --git a/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormatDetector.kt b/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormatDetector.kt new file mode 100644 index 0000000000..5b989b098f --- /dev/null +++ b/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormatDetector.kt @@ -0,0 +1,58 @@ +/* + * Nextcloud - Android Client + * + * SPDX-FileCopyrightText: 2025 Alper Ozturk + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +package com.owncloud.android.lib.common.utils.responseFormat + +import com.owncloud.android.lib.common.utils.Log_OC +import org.json.JSONArray +import org.json.JSONObject +import java.io.ByteArrayInputStream +import javax.xml.parsers.DocumentBuilderFactory + +object ResponseFormatDetector { + private const val TAG = "ResponseFormatDetector" + private const val JSON_OBJECT_PREFIX = "{" + private const val JSON_ARRAY_PREFIX = "[" + + fun detectFormat(input: String): ResponseFormat { + return when { + isJson(input) -> ResponseFormat.JSON + isXml(input) -> ResponseFormat.XML + else -> ResponseFormat.UNKNOWN + } + } + + private fun isJson(input: String): Boolean { + return try { + val trimmed = input.trim() + if (trimmed.startsWith(JSON_OBJECT_PREFIX)) { + JSONObject(trimmed) + } else if (trimmed.startsWith(JSON_ARRAY_PREFIX)) { + JSONArray(trimmed) + } else { + return false + } + true + } catch (e: Exception) { + Log_OC.e(TAG, "Exception isJson: $e") + false + } + } + + private fun isXml(input: String): Boolean { + return try { + val factory = DocumentBuilderFactory.newInstance() + val builder = factory.newDocumentBuilder() + val stream = ByteArrayInputStream(input.toByteArray()) + builder.parse(stream) + true + } catch (e: Exception) { + Log_OC.e(TAG, "Exception isXml: $e") + false + } + } +} From 1ba04f4f7ae7af9ef807986466aaf0a513a68b0f Mon Sep 17 00:00:00 2001 From: alperozturk Date: Fri, 25 Apr 2025 14:47:11 +0200 Subject: [PATCH 2/7] fix code analytics Signed-off-by: alperozturk --- .../owncloud/android/ResponseFormatDetectorTests.kt | 1 - .../common/utils/responseFormat/ResponseFormat.kt | 4 +++- .../utils/responseFormat/ResponseFormatDetector.kt | 12 ++++++------ 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/library/src/androidTest/java/com/owncloud/android/ResponseFormatDetectorTests.kt b/library/src/androidTest/java/com/owncloud/android/ResponseFormatDetectorTests.kt index 7dacf844ba..a7b126a57b 100644 --- a/library/src/androidTest/java/com/owncloud/android/ResponseFormatDetectorTests.kt +++ b/library/src/androidTest/java/com/owncloud/android/ResponseFormatDetectorTests.kt @@ -13,7 +13,6 @@ import junit.framework.TestCase.assertEquals import org.junit.Test class ResponseFormatDetectorTests { - @Test fun testJsonDetection() { val json = """{ "name": "Alice", "age": 30 }""" diff --git a/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormat.kt b/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormat.kt index 156305a973..1469767ea5 100644 --- a/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormat.kt +++ b/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormat.kt @@ -8,5 +8,7 @@ package com.owncloud.android.lib.common.utils.responseFormat enum class ResponseFormat { - JSON, XML, UNKNOWN + JSON, + XML, + UNKNOWN } diff --git a/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormatDetector.kt b/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormatDetector.kt index 5b989b098f..24c3fc050f 100644 --- a/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormatDetector.kt +++ b/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormatDetector.kt @@ -18,14 +18,14 @@ object ResponseFormatDetector { private const val JSON_OBJECT_PREFIX = "{" private const val JSON_ARRAY_PREFIX = "[" - fun detectFormat(input: String): ResponseFormat { - return when { + fun detectFormat(input: String): ResponseFormat = + when { isJson(input) -> ResponseFormat.JSON isXml(input) -> ResponseFormat.XML else -> ResponseFormat.UNKNOWN } - } + @Suppress("TooGenericExceptionCaught") private fun isJson(input: String): Boolean { return try { val trimmed = input.trim() @@ -43,8 +43,9 @@ object ResponseFormatDetector { } } - private fun isXml(input: String): Boolean { - return try { + @Suppress("TooGenericExceptionCaught") + private fun isXml(input: String): Boolean = + try { val factory = DocumentBuilderFactory.newInstance() val builder = factory.newDocumentBuilder() val stream = ByteArrayInputStream(input.toByteArray()) @@ -54,5 +55,4 @@ object ResponseFormatDetector { Log_OC.e(TAG, "Exception isXml: $e") false } - } } From 512234fa33400b3539857d4d7a4e690f251743bd Mon Sep 17 00:00:00 2001 From: alperozturk Date: Fri, 25 Apr 2025 15:20:49 +0200 Subject: [PATCH 3/7] better isJSON Signed-off-by: alperozturk --- .../responseFormat/ResponseFormatDetector.kt | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormatDetector.kt b/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormatDetector.kt index 24c3fc050f..a3e1d77932 100644 --- a/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormatDetector.kt +++ b/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormatDetector.kt @@ -9,42 +9,39 @@ package com.owncloud.android.lib.common.utils.responseFormat import com.owncloud.android.lib.common.utils.Log_OC import org.json.JSONArray +import org.json.JSONException import org.json.JSONObject import java.io.ByteArrayInputStream import javax.xml.parsers.DocumentBuilderFactory object ResponseFormatDetector { private const val TAG = "ResponseFormatDetector" - private const val JSON_OBJECT_PREFIX = "{" - private const val JSON_ARRAY_PREFIX = "[" fun detectFormat(input: String): ResponseFormat = when { - isJson(input) -> ResponseFormat.JSON - isXml(input) -> ResponseFormat.XML + isJSON(input) -> ResponseFormat.JSON + isXML(input) -> ResponseFormat.XML else -> ResponseFormat.UNKNOWN } - @Suppress("TooGenericExceptionCaught") - private fun isJson(input: String): Boolean { + private fun isJSON(input: String): Boolean { return try { - val trimmed = input.trim() - if (trimmed.startsWith(JSON_OBJECT_PREFIX)) { - JSONObject(trimmed) - } else if (trimmed.startsWith(JSON_ARRAY_PREFIX)) { - JSONArray(trimmed) - } else { - return false - } + JSONObject(input) true - } catch (e: Exception) { - Log_OC.e(TAG, "Exception isJson: $e") - false + } catch (e: JSONException) { + try { + Log_OC.i(TAG, "Info it's not JSONObject: $e") + JSONArray(input) + true + } catch (e: JSONException) { + Log_OC.e(TAG, "Exception it's not JSONArray: $e") + false + } } } @Suppress("TooGenericExceptionCaught") - private fun isXml(input: String): Boolean = + private fun isXML(input: String): Boolean = try { val factory = DocumentBuilderFactory.newInstance() val builder = factory.newDocumentBuilder() @@ -52,7 +49,7 @@ object ResponseFormatDetector { builder.parse(stream) true } catch (e: Exception) { - Log_OC.e(TAG, "Exception isXml: $e") + Log_OC.e(TAG, "Exception isXML: $e") false } } From caa254dbff57a0a04b41e2ee3d93964201fb1a0a Mon Sep 17 00:00:00 2001 From: alperozturk Date: Fri, 25 Apr 2025 15:22:12 +0200 Subject: [PATCH 4/7] better fix code analytics Signed-off-by: alperozturk --- .../common/utils/responseFormat/ResponseFormatDetector.kt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormatDetector.kt b/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormatDetector.kt index a3e1d77932..fd33bf66a0 100644 --- a/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormatDetector.kt +++ b/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormatDetector.kt @@ -24,8 +24,8 @@ object ResponseFormatDetector { else -> ResponseFormat.UNKNOWN } - private fun isJSON(input: String): Boolean { - return try { + private fun isJSON(input: String): Boolean = + try { JSONObject(input) true } catch (e: JSONException) { @@ -38,7 +38,6 @@ object ResponseFormatDetector { false } } - } @Suppress("TooGenericExceptionCaught") private fun isXML(input: String): Boolean = From a5d5e3f25310fa59178c903576d29dbfd10d8518 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Fri, 25 Apr 2025 15:24:06 +0200 Subject: [PATCH 5/7] fix license Signed-off-by: alperozturk --- .../java/com/owncloud/android/ResponseFormatDetectorTests.kt | 4 ++-- .../android/lib/common/utils/responseFormat/ResponseFormat.kt | 4 ++-- .../lib/common/utils/responseFormat/ResponseFormatDetector.kt | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/library/src/androidTest/java/com/owncloud/android/ResponseFormatDetectorTests.kt b/library/src/androidTest/java/com/owncloud/android/ResponseFormatDetectorTests.kt index a7b126a57b..37996adf32 100644 --- a/library/src/androidTest/java/com/owncloud/android/ResponseFormatDetectorTests.kt +++ b/library/src/androidTest/java/com/owncloud/android/ResponseFormatDetectorTests.kt @@ -1,8 +1,8 @@ /* - * Nextcloud - Android Client + * Nextcloud Android Library * * SPDX-FileCopyrightText: 2025 Alper Ozturk - * SPDX-License-Identifier: AGPL-3.0-or-later + * SPDX-License-Identifier: MIT */ package com.owncloud.android diff --git a/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormat.kt b/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormat.kt index 1469767ea5..c7c3f3f555 100644 --- a/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormat.kt +++ b/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormat.kt @@ -1,8 +1,8 @@ /* - * Nextcloud - Android Client + * Nextcloud Android Library * * SPDX-FileCopyrightText: 2025 Alper Ozturk - * SPDX-License-Identifier: AGPL-3.0-or-later + * SPDX-License-Identifier: MIT */ package com.owncloud.android.lib.common.utils.responseFormat diff --git a/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormatDetector.kt b/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormatDetector.kt index fd33bf66a0..ed77deb9ad 100644 --- a/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormatDetector.kt +++ b/library/src/main/java/com/owncloud/android/lib/common/utils/responseFormat/ResponseFormatDetector.kt @@ -1,8 +1,8 @@ /* - * Nextcloud - Android Client + * Nextcloud Android Library * * SPDX-FileCopyrightText: 2025 Alper Ozturk - * SPDX-License-Identifier: AGPL-3.0-or-later + * SPDX-License-Identifier: MIT */ package com.owncloud.android.lib.common.utils.responseFormat From 9c87d504b359e5399b9ff890b8407e159362f1b4 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Wed, 7 May 2025 08:48:59 +0200 Subject: [PATCH 6/7] convert to kt Signed-off-by: alperozturk --- .../android/XMLExceptionParserIT.java | 96 ------------------- .../android/XMLExceptionParserTests.kt | 86 +++++++++++++++++ 2 files changed, 86 insertions(+), 96 deletions(-) delete mode 100644 library/src/androidTest/java/com/owncloud/android/XMLExceptionParserIT.java create mode 100644 library/src/androidTest/java/com/owncloud/android/XMLExceptionParserTests.kt diff --git a/library/src/androidTest/java/com/owncloud/android/XMLExceptionParserIT.java b/library/src/androidTest/java/com/owncloud/android/XMLExceptionParserIT.java deleted file mode 100644 index 418e296cff..0000000000 --- a/library/src/androidTest/java/com/owncloud/android/XMLExceptionParserIT.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Nextcloud Android Library - * - * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors - * SPDX-FileCopyrightText: 2018-2022 Tobias Kaminsky - * SPDX-License-Identifier: MIT - */ -package com.owncloud.android; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import com.owncloud.android.lib.common.operations.XMLExceptionParser; - -import org.junit.Test; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; - - -/** - * Created by tobi on 3/21/18. - */ - -public class XMLExceptionParserIT { - - @Test - public void testVirusException() { - String virusException = "\n" + - "\n" + - " OCA\\DAV\\Connector\\Sabre\\Exception\\UnsupportedMediaType" + - "\n" + - " Virus Eicar-Test-Signature is detected in the file. " + - "Upload cannot be completed.\n" + - ""; - - InputStream is = new ByteArrayInputStream(virusException.getBytes()); - XMLExceptionParser xmlParser = new XMLExceptionParser(is); - - assertTrue(xmlParser.isVirusException()); - assertFalse(xmlParser.isInvalidCharacterException()); - } - - @Test - public void testInvalidCharacterException() { - String virusException = "\n" + - "\n" + - " OC\\Connector\\Sabre\\Exception\\InvalidPath\n" + - " Wrong Path\n" + - ""; - - InputStream is = new ByteArrayInputStream(virusException.getBytes()); - XMLExceptionParser xmlParser = new XMLExceptionParser(is); - - assertTrue(xmlParser.isInvalidCharacterException()); - assertFalse(xmlParser.isVirusException()); - } - - @Test - public void testInvalidCharacterUploadException() { - String virusException = "\n" + - "\n" + - " OCP\\Files\\InvalidPathException\n" + - " Wrong Path\n" + - ""; - - InputStream is = new ByteArrayInputStream(virusException.getBytes()); - XMLExceptionParser xmlParser = new XMLExceptionParser(is); - - assertTrue(xmlParser.isInvalidCharacterException()); - assertFalse(xmlParser.isVirusException()); - } - - @Test - public void testEmptyString() { - String emptyString = ""; - - InputStream is = new ByteArrayInputStream(emptyString.getBytes()); - XMLExceptionParser xmlParser = new XMLExceptionParser(is); - - assertFalse(xmlParser.isVirusException()); - assertFalse(xmlParser.isInvalidCharacterException()); - } - - @Test - public void testString() { - String emptyString = ""; - - InputStream is = new ByteArrayInputStream(emptyString.getBytes()); - XMLExceptionParser xmlParser = new XMLExceptionParser(is); - - assertFalse(xmlParser.isVirusException()); - assertFalse(xmlParser.isInvalidCharacterException()); - } -} diff --git a/library/src/androidTest/java/com/owncloud/android/XMLExceptionParserTests.kt b/library/src/androidTest/java/com/owncloud/android/XMLExceptionParserTests.kt new file mode 100644 index 0000000000..a34671861d --- /dev/null +++ b/library/src/androidTest/java/com/owncloud/android/XMLExceptionParserTests.kt @@ -0,0 +1,86 @@ +/* + * Nextcloud Android Library + * + * SPDX-FileCopyrightText: 2025 Alper Ozturk + * SPDX-License-Identifier: MIT + */ +package com.owncloud.android + +import com.owncloud.android.lib.common.operations.XMLExceptionParser +import org.junit.Assert +import org.junit.Test +import java.io.ByteArrayInputStream + +class XMLExceptionParserTests { + @Test + fun testVirusException() { + val virusException = + "\n" + + "\n" + + " OCA\\DAV\\Connector\\Sabre\\Exception\\UnsupportedMediaType" + + "\n" + + " Virus Eicar-Test-Signature is detected in the file. " + + "Upload cannot be completed.\n" + + "" + + val inputStream = ByteArrayInputStream(virusException.toByteArray()) + val xmlParser = XMLExceptionParser(inputStream) + + Assert.assertTrue(xmlParser.isVirusException) + Assert.assertFalse(xmlParser.isInvalidCharacterException) + } + + @Test + fun testInvalidCharacterException() { + val virusException = + "\n" + + "\n" + + " OC\\Connector\\Sabre\\Exception\\InvalidPath\n" + + " Wrong Path\n" + + "" + + val inputStream = ByteArrayInputStream(virusException.toByteArray()) + val xmlParser = XMLExceptionParser(inputStream) + + Assert.assertTrue(xmlParser.isInvalidCharacterException) + Assert.assertFalse(xmlParser.isVirusException) + } + + @Test + fun testInvalidCharacterUploadException() { + val virusException = + "\n" + + "\n" + + " OCP\\Files\\InvalidPathException\n" + + " Wrong Path\n" + + "" + + val inputStream = ByteArrayInputStream(virusException.toByteArray()) + val xmlParser = XMLExceptionParser(inputStream) + + Assert.assertTrue(xmlParser.isInvalidCharacterException) + Assert.assertFalse(xmlParser.isVirusException) + } + + @Test + fun testEmptyString() { + val emptyString = "" + + val inputStream = ByteArrayInputStream(emptyString.toByteArray()) + val xmlParser = XMLExceptionParser(inputStream) + + Assert.assertFalse(xmlParser.isVirusException) + Assert.assertFalse(xmlParser.isInvalidCharacterException) + } + + @Test + fun testString() { + val emptyString = "" + + val inputStream = ByteArrayInputStream(emptyString.toByteArray()) + val xmlParser = XMLExceptionParser(inputStream) + + Assert.assertFalse(xmlParser.isVirusException) + Assert.assertFalse(xmlParser.isInvalidCharacterException) + } +} From 3c3f433f9080fe7292d24e60e4e0b7f638fa0624 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alper=20=C3=96zt=C3=BCrk?= <67455295+alperozturk96@users.noreply.github.com> Date: Wed, 7 May 2025 10:54:53 +0200 Subject: [PATCH 7/7] Update library/src/androidTest/java/com/owncloud/android/XMLExceptionParserTests.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tom <70907959+ZetaTom@users.noreply.github.com> Signed-off-by: Alper Öztürk <67455295+alperozturk96@users.noreply.github.com> --- .../com/owncloud/android/XMLExceptionParserTests.kt | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/library/src/androidTest/java/com/owncloud/android/XMLExceptionParserTests.kt b/library/src/androidTest/java/com/owncloud/android/XMLExceptionParserTests.kt index a34671861d..15983fc6d8 100644 --- a/library/src/androidTest/java/com/owncloud/android/XMLExceptionParserTests.kt +++ b/library/src/androidTest/java/com/owncloud/android/XMLExceptionParserTests.kt @@ -72,15 +72,4 @@ class XMLExceptionParserTests { Assert.assertFalse(xmlParser.isVirusException) Assert.assertFalse(xmlParser.isInvalidCharacterException) } - - @Test - fun testString() { - val emptyString = "" - - val inputStream = ByteArrayInputStream(emptyString.toByteArray()) - val xmlParser = XMLExceptionParser(inputStream) - - Assert.assertFalse(xmlParser.isVirusException) - Assert.assertFalse(xmlParser.isInvalidCharacterException) - } }