Skip to content
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

Fix for IllegalArgumentException: URI is not absolute #12186 #12246

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- On saving a non-absolute URI, now a clear error message is shown. [#12186](https://github.com/JabRef/jabref/issues/12186)

### Removed

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/jabref/logic/util/URLUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/csl-locales
23 changes: 23 additions & 0 deletions 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.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;

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

@Test
void createWithAbsoluteURL() {
String absoluteUrl = "http://www.example.com";
assertDoesNotThrow(() -> {
URL url = URLUtil.create(absoluteUrl);
assertNotNull(url);
assertEquals(absoluteUrl, url.toString());
}, "MalformedURLException should not be thrown for an absolute URL");
}

@Test
void createWithNonAbsoluteURL() {
String nonAbsoluteUrl = "www.example.com";
assertThrows(MalformedURLException.class, () -> {
URLUtil.create(nonAbsoluteUrl);
});
}
}
Loading