Skip to content
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion jablib/src/main/java/org/jabref/logic/util/URLUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,21 @@ 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();
if (url == null || url.trim().isEmpty()) {
throw new IllegalArgumentException("URL must not be null or empty.");
}
try {
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 | IllegalArgumentException e) {
throw new MalformedURLException("Invalid URI: " + url + " | " + e.getMessage());
}
}

/**
Expand Down
50 changes: 50 additions & 0 deletions jablib/src/test/java/org/jabref/logic/net/URLUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package org.jabref.logic.net;

import java.net.MalformedURLException;
import java.net.URI;
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.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class URLUtilTest {
Expand Down Expand Up @@ -87,4 +92,49 @@ void createUriShouldHandlePipeCharacter() {
URI uri = URLUtil.createUri(input);
assertEquals("http://example.com/test%7Cfile", uri.toString());
}

@Test
public void validUrl() throws MalformedURLException {
String input = "http://example.com";

URL result = URLUtil.create(input);
assertNotNull(result);
assertNotEquals("", result.toString().trim());
assertEquals(input, result.toString());
}

@Test
public void nullUrl() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () ->
URLUtil.create(null));
assertTrue(exception.getMessage().contains("null or empty"));
}

@Test
public void emptyUrl() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () ->
URLUtil.create(" "));
assertTrue(exception.getMessage().contains("null or empty"));
}

@Test
public void uriMissingScheme() {
MalformedURLException exception = assertThrows(MalformedURLException.class, () ->
URLUtil.create("www.example.com"));
assertTrue(exception.getMessage().contains("not absolute"));
}

@Test
public void uriMissingHost() {
MalformedURLException exception = assertThrows(MalformedURLException.class, () ->
URLUtil.create("mailto:[email protected]"));
assertTrue(exception.getMessage().contains("must include both scheme and host"));
}

@Test
public void malformedSyntax() {
MalformedURLException exception = assertThrows(MalformedURLException.class, () ->
URLUtil.create("http://[invalid-url]"));
assertTrue(exception.getMessage().contains("Invalid URI"));
}
}