From 00932e2b1c8007bf7bf0d910f3932af6abc2fe88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cyash-agrawal03=E2=80=9D?= Date: Sat, 30 Nov 2024 01:40:05 +0530 Subject: [PATCH 1/4] Added a check to ensure that the URI is absolute before converting it to a URL in the create method. This prevents the IllegalArgumentException: URI is not absolute error by throwing a MalformedURLException with a descriptive message if the URI is not absolute. --- buildres/abbrv.jabref.org | 2 +- .../java/org/jabref/logic/util/URLUtil.java | 6 ++++- src/main/resources/csl-locales | 2 +- src/main/resources/csl-styles | 2 +- .../org/jabref/logic/net/URLUtilTest.java | 25 +++++++++++++++++++ 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/buildres/abbrv.jabref.org b/buildres/abbrv.jabref.org index 0fdf99147a8..1ad9773b13c 160000 --- a/buildres/abbrv.jabref.org +++ b/buildres/abbrv.jabref.org @@ -1 +1 @@ -Subproject commit 0fdf99147a8a5fc8ae7ccd79ad4e0029e736e4a3 +Subproject commit 1ad9773b13c37a919207a0e412c6615f42453e99 diff --git a/src/main/java/org/jabref/logic/util/URLUtil.java b/src/main/java/org/jabref/logic/util/URLUtil.java index fd6317d9d23..49d2c8c970a 100644 --- a/src/main/java/org/jabref/logic/util/URLUtil.java +++ b/src/main/java/org/jabref/logic/util/URLUtil.java @@ -89,7 +89,11 @@ public static boolean isURL(String url) { * @throws MalformedURLException if the URL is malformed and cannot be converted to a {@link URL}. */ public static URL create(String url) throws MalformedURLException { - return createUri(url).toURL(); + URI uri = createUri(url); + if (!uri.isAbsolute()) { + throw new MalformedURLException("URI is not absolute: " + url); + } + return uri.toURL(); } /** diff --git a/src/main/resources/csl-locales b/src/main/resources/csl-locales index 96d704de2fc..4753e3a9aca 160000 --- a/src/main/resources/csl-locales +++ b/src/main/resources/csl-locales @@ -1 +1 @@ -Subproject commit 96d704de2fc7b930ae4a0ec4686a7143bb4a0d33 +Subproject commit 4753e3a9aca4b806ac0e3036ed727d47bf8f678e diff --git a/src/main/resources/csl-styles b/src/main/resources/csl-styles index b90b81a58b1..1931353cec3 160000 --- a/src/main/resources/csl-styles +++ b/src/main/resources/csl-styles @@ -1 +1 @@ -Subproject commit b90b81a58b1a260423608f3868a6613cc6efe431 +Subproject commit 1931353cec337cb62ef3f9049a78e4f91a3834b9 diff --git a/src/test/java/org/jabref/logic/net/URLUtilTest.java b/src/test/java/org/jabref/logic/net/URLUtilTest.java index 7de7b641fc5..92766e72ebb 100644 --- a/src/test/java/org/jabref/logic/net/URLUtilTest.java +++ b/src/test/java/org/jabref/logic/net/URLUtilTest.java @@ -1,6 +1,8 @@ package org.jabref.logic.net; +import java.net.MalformedURLException; import java.net.URI; +import java.net.URL; import org.jabref.logic.util.URLUtil; @@ -8,7 +10,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; class URLUtilTest { @@ -87,4 +92,24 @@ void createUriShouldHandlePipeCharacter() { URI uri = URLUtil.createUri(input); assertEquals("http://example.com/test%7Cfile", uri.toString()); } + + @Test + void testCreateWithAbsoluteURL() { + String absoluteUrl = "http://www.example.com"; + try { + URL url = URLUtil.create(absoluteUrl); + assertNotNull(url); + assertEquals(absoluteUrl, url.toString()); + } catch (MalformedURLException e) { + fail("MalformedURLException should not be thrown for an absolute URL"); + } + } + + @Test + void testCreateWithNonAbsoluteURL() { + String nonAbsoluteUrl = "www.example.com"; + assertThrows(MalformedURLException.class, () -> { + URLUtil.create(nonAbsoluteUrl); + }); + } } From d606cdc4b108adf53134c7dbfae0fde6af3a21af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cyash-agrawal03=E2=80=9D?= Date: Sat, 30 Nov 2024 01:59:23 +0530 Subject: [PATCH 2/4] Updated CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0cc4fbe4094..b838901dc85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -124,6 +124,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv - We fixed an issue when the preview was out of sync. [#9172](https://github.com/JabRef/jabref/issues/9172) - We fixed an issue where identifier paste couldn't work with Unicode REPLACEMENT CHARACTER. [#11986](https://github.com/JabRef/jabref/issues/11986) - We fixed an issue when click on entry at "Check Integrity" wasn't properly focusing the entry and field. [#11997](https://github.com/JabRef/jabref/issues/11997) +- We Fixed an error where saving a non-absolute URI threw an IllegalArgumentException. Now, it checks if the URI is absolute and throws a clear error message if it is not.[#12186](https://github.com/JabRef/jabref/issues/12186) ### Removed From 2531c73de0a25a28a5d90c5343313eb96ff01463 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cyash-agrawal03=E2=80=9D?= Date: Sat, 30 Nov 2024 11:11:43 +0530 Subject: [PATCH 3/4] fix for the failing test-case --- src/test/java/org/jabref/logic/net/URLUtilTest.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/test/java/org/jabref/logic/net/URLUtilTest.java b/src/test/java/org/jabref/logic/net/URLUtilTest.java index 92766e72ebb..b33a00e0557 100644 --- a/src/test/java/org/jabref/logic/net/URLUtilTest.java +++ b/src/test/java/org/jabref/logic/net/URLUtilTest.java @@ -8,12 +8,12 @@ import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; class URLUtilTest { @@ -94,19 +94,17 @@ void createUriShouldHandlePipeCharacter() { } @Test - void testCreateWithAbsoluteURL() { + void createWithAbsoluteURL() { String absoluteUrl = "http://www.example.com"; - try { + assertDoesNotThrow(() -> { URL url = URLUtil.create(absoluteUrl); assertNotNull(url); assertEquals(absoluteUrl, url.toString()); - } catch (MalformedURLException e) { - fail("MalformedURLException should not be thrown for an absolute URL"); - } + }, "MalformedURLException should not be thrown for an absolute URL"); } @Test - void testCreateWithNonAbsoluteURL() { + void createWithNonAbsoluteURL() { String nonAbsoluteUrl = "www.example.com"; assertThrows(MalformedURLException.class, () -> { URLUtil.create(nonAbsoluteUrl); From 72be32f81440bd9192693f2ca00e18e16a549bd1 Mon Sep 17 00:00:00 2001 From: Yash Agrawal <77984203+yash-agrawal03@users.noreply.github.com> Date: Sat, 30 Nov 2024 11:22:49 +0530 Subject: [PATCH 4/4] Update CHANGELOG.md Co-authored-by: Subhramit Basu Bhowmick --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b838901dc85..039bc0cba73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -124,7 +124,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv - We fixed an issue when the preview was out of sync. [#9172](https://github.com/JabRef/jabref/issues/9172) - We fixed an issue where identifier paste couldn't work with Unicode REPLACEMENT CHARACTER. [#11986](https://github.com/JabRef/jabref/issues/11986) - We fixed an issue when click on entry at "Check Integrity" wasn't properly focusing the entry and field. [#11997](https://github.com/JabRef/jabref/issues/11997) -- We Fixed an error where saving a non-absolute URI threw an IllegalArgumentException. Now, it checks if the URI is absolute and throws a clear error message if it is not.[#12186](https://github.com/JabRef/jabref/issues/12186) +- On saving a non-absolute URI, now a clear error message is shown. [#12186](https://github.com/JabRef/jabref/issues/12186) ### Removed