-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Refine error message in URLUtil::create #13337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
f720746
d1d206a
eb1bd34
69201fb
bc74f41
d8d3031
a8cac36
565dfde
78f577e
23cfe5b
20bbd61
6d22919
45322fa
bc0da1a
34ef825
967b66b
69139ee
7091190
117bb4f
014d909
cee1765
70291b5
1fabaa4
ef6217a
a9b65ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,7 +97,25 @@ 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(); | ||
| try { | ||
| if (url == null || url.trim().isEmpty()) { | ||
| throw new MalformedURLException("Provided URL is null or empty."); | ||
| } | ||
| URI parsedUri = new URI(url.trim()); | ||
| if (!parsedUri.isAbsolute()) { | ||
| throw new MalformedURLException("URI is not absolute: " + url); | ||
| } | ||
| if (parsedUri.getScheme() == null || parsedUri.getHost() == null) { | ||
| throw new MalformedURLException("URI must include both scheme and host: " + url); | ||
| } | ||
| return parsedUri.toURL(); | ||
| } catch (URISyntaxException e) { | ||
| throw new MalformedURLException("Invalid URI syntax: " + url + " | Error: " + e.getMessage()); | ||
calixtus marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } catch (IllegalArgumentException e) { | ||
| throw new MalformedURLException("Illegal argument in URI construction: " + url + " | Error: " + e.getMessage()); | ||
| } catch (NullPointerException e) { | ||
| throw new MalformedURLException("Null value encountered during URI parsing: " + url); | ||
| } | ||
|
||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,15 @@ | ||
| package org.jabref.logic.net; | ||
|
|
||
| import java.net.MalformedURLException; | ||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import java.net.URL; | ||
|
|
||
| import org.jabref.logic.util.URLUtil; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
calixtus marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| class URLUtilTest { | ||
|
|
||
|
|
@@ -87,4 +88,64 @@ void createUriShouldHandlePipeCharacter() { | |
| URI uri = URLUtil.createUri(input); | ||
| assertEquals("http://example.com/test%7Cfile", uri.toString()); | ||
| } | ||
|
|
||
| // valid absolute URL | ||
| @Test | ||
calixtus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public void testValidUrl() { | ||
| String input = "http://example.com"; | ||
|
|
||
| try { | ||
| URL result = URLUtil.create(input); | ||
| assertNotNull(result, "URL should not be null"); | ||
| assertEquals(input, result.toString(), "Returned URL should match input"); | ||
| } catch (MalformedURLException e) { | ||
| fail("Exception " + e.getMessage()); | ||
| } | ||
calixtus marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
|
|
||
| // null input | ||
| @Test | ||
| public void testNullUrl() { | ||
| MalformedURLException exception = assertThrows(MalformedURLException.class, () -> { | ||
| URLUtil.create(null); | ||
| }); | ||
| assertTrue(exception.getMessage().contains("null or empty"), "Error message should indicate null or empty input"); | ||
| } | ||
|
|
||
| //empty string input | ||
| @Test | ||
| public void testEmptyUrl() { | ||
| MalformedURLException exception = assertThrows(MalformedURLException.class, () -> { | ||
| URLUtil.create(" "); | ||
| }); | ||
| assertTrue(exception.getMessage().contains("null or empty"), "Error message "); | ||
| } | ||
|
|
||
| // URI without scheme | ||
| @Test | ||
| public void testUriMissingScheme() { | ||
| MalformedURLException exception = assertThrows(MalformedURLException.class, () -> { | ||
| URLUtil.create("www.example.com"); | ||
| }); | ||
| assertTrue(exception.getMessage().contains("not absolute"), "URI is not absolute"); | ||
| } | ||
|
|
||
| // URI with scheme but missing host | ||
| @Test | ||
| public void testUriMissingHost() { | ||
| MalformedURLException exception = assertThrows(MalformedURLException.class, () -> { | ||
| URLUtil.create("mailto:[email protected]"); | ||
| }); | ||
| assertTrue(exception.getMessage().contains("must include both scheme and host"), "Error message should mention scheme and host"); | ||
| } | ||
|
|
||
| // malformed syntax | ||
| @Test | ||
| public void testMalformedSyntax() { | ||
| MalformedURLException exception = assertThrows(MalformedURLException.class, () -> { | ||
| URLUtil.create("http://[invalid-url]"); | ||
| }); | ||
| assertTrue(exception.getMessage().contains("Invalid URI syntax"), " URI syntax error"); | ||
calixtus marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.