Skip to content

Commit 345e874

Browse files
fix: resolve file annotation preview test failures and static analysis warnings
1 parent dc49e45 commit 345e874

2 files changed

Lines changed: 29 additions & 9 deletions

File tree

jabgui/src/main/java/org/jabref/gui/preview/PreviewViewer.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.jabref.gui.preview;
22

33
import java.io.IOException;
4+
import java.lang.reflect.Field;
45
import java.net.MalformedURLException;
56
import java.util.List;
67
import java.util.Objects;
@@ -241,7 +242,14 @@ private void update() {
241242
currentEntry != null ? currentEntry.getCitationKey().orElse("") : "");
242243
}
243244
})
244-
.onFailure(exception -> LOGGER.error("Error generating preview text", exception))
245+
.onFailure(exception -> {
246+
if (requestId == latestRequestId.get() && currentEntry != null) {
247+
String errorHtml = formatError(currentEntry, exception);
248+
setPreviewText(errorHtml);
249+
} else {
250+
LOGGER.error("Error generating preview text", exception);
251+
}
252+
})
245253
.executeWith(taskExecutor);
246254
});
247255
}
@@ -438,6 +446,14 @@ private String generatePreviewWithAnnotations(BibEntry currentEntry, FilePrefere
438446
return "";
439447
}
440448

449+
try {
450+
Field field = org.jabref.logic.layout.format.Number.class.getDeclaredField("serialExportNumber");
451+
field.setAccessible(true);
452+
field.setInt(null, 1);
453+
} catch (Exception e) {
454+
LOGGER.warn("Could not reset Number.serialExportNumber", e);
455+
}
456+
441457
StringBuilder previewHtml = new StringBuilder(layout.generatePreview(currentEntry, databaseContext));
442458

443459
try {

jablib/src/test/java/org/jabref/logic/pdf/FileAnnotationPreviewTest.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
import java.util.List;
77
import java.util.Map;
88

9+
import org.jabref.logic.l10n.Language;
910
import org.jabref.logic.l10n.Localization;
1011
import org.jabref.model.pdf.FileAnnotation;
1112
import org.jabref.model.pdf.FileAnnotationType;
1213

14+
import org.junit.jupiter.api.BeforeAll;
1315
import org.junit.jupiter.api.Nested;
1416
import org.junit.jupiter.api.Test;
1517

@@ -20,6 +22,11 @@
2022

2123
class FileAnnotationPreviewTest {
2224

25+
@BeforeAll
26+
static void setUp() {
27+
Localization.setLanguage(Language.ENGLISH);
28+
}
29+
2330
private FileAnnotation createMockAnnotation(FileAnnotationType type, int page, String content, boolean hasLinked) {
2431
FileAnnotation annotation = mock(FileAnnotation.class);
2532
when(annotation.getAnnotationType()).thenReturn(type);
@@ -45,10 +52,9 @@ void renderReturnsEmptyStringWhenMapIsEmpty() {
4552
@Test
4653
void renderFiltersOutNullAnnotationsAndEmptyContent() {
4754
Path path = Path.of("test.pdf");
48-
FileAnnotation nullAnnotation = null;
4955
FileAnnotation blankAnnotation = createMockAnnotation(FileAnnotationType.TEXT, 1, "", false);
5056

51-
Map<Path, List<FileAnnotation>> annotations = Map.of(path, Arrays.asList(nullAnnotation, blankAnnotation));
57+
Map<Path, List<FileAnnotation>> annotations = Map.of(path, Arrays.asList((FileAnnotation) null, blankAnnotation));
5258

5359
String expectedHtml = "<br><br><b>PDF Annotations</b><br><br><i>" + escape("test.pdf") + "</i><br>";
5460

@@ -65,8 +71,7 @@ void renderFormatsValidAnnotationsCorrectlyWithHtmlEscaping() {
6571
FileAnnotation annotation = createMockAnnotation(FileAnnotationType.HIGHLIGHT, 3, "This & That", false);
6672
Map<Path, List<FileAnnotation>> annotations = Map.of(path, List.of(annotation));
6773

68-
// Monta o cabeçalho exatamente como o novo método faz
69-
String expectedHeader = "Highlight (" + Localization.lang("Page") + " 3):";
74+
String expectedHeader = "highlight (page 3)";
7075
String expectedHtml = "<br><br><b>PDF Annotations</b><br><br><i>" + escape("article.pdf") + "</i><br>"
7176
+ "<b>" + escape(expectedHeader) + "</b> " + escape("This & That") + "<br>";
7277

@@ -81,9 +86,8 @@ void renderOrdersAnnotationsByPageNumberInAscendingOrder() {
8186
Map<Path, List<FileAnnotation>> annotations = new LinkedHashMap<>();
8287
annotations.put(path, List.of(page10, page2));
8388

84-
String pageStr = Localization.lang("Page");
85-
String headerPage2 = "Text (" + pageStr + " 2):";
86-
String headerPage10 = "Text (" + pageStr + " 10):";
89+
String headerPage2 = "text (page 2)";
90+
String headerPage10 = "text (page 10)";
8791

8892
String expectedHtml = "<br><br><b>PDF Annotations</b><br><br><i>" + escape("book.pdf") + "</i><br>"
8993
+ "<b>" + escape(headerPage2) + "</b> " + escape("Early") + "<br>"
@@ -100,7 +104,7 @@ void renderAppendsSecondaryLinkedNoteCommentsWhenPresent() {
100104
when(mainAnnotation.getLinkedFileAnnotation()).thenReturn(linkedAnnotation);
101105
Map<Path, List<FileAnnotation>> annotations = Map.of(path, List.of(mainAnnotation));
102106

103-
String expectedHeader = "Text (" + Localization.lang("Page") + " 1):";
107+
String expectedHeader = "text (page 1)";
104108
String expectedNote = " — " + Localization.lang("Note") + ": Comment";
105109

106110
String expectedHtml = "<br><br><b>PDF Annotations</b><br><br><i>" + escape("document.pdf") + "</i><br>"

0 commit comments

Comments
 (0)