From ac03682a3e6513998cf46c329b024c3809ca3a84 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Thu, 22 May 2025 18:48:59 +0200 Subject: [PATCH 1/4] add test for writeAnnotation fixes #4768 --- .../indexer/history/AnnotationData.java | 5 +- .../java/org/opengrok/indexer/web/Util.java | 61 +++++++++++-------- .../org/opengrok/indexer/web/UtilTest.java | 27 ++++++++ 3 files changed, 64 insertions(+), 29 deletions(-) diff --git a/opengrok-indexer/src/main/java/org/opengrok/indexer/history/AnnotationData.java b/opengrok-indexer/src/main/java/org/opengrok/indexer/history/AnnotationData.java index 5a206936296..c3629ffd3f9 100644 --- a/opengrok-indexer/src/main/java/org/opengrok/indexer/history/AnnotationData.java +++ b/opengrok-indexer/src/main/java/org/opengrok/indexer/history/AnnotationData.java @@ -25,6 +25,7 @@ import com.fasterxml.jackson.annotation.JsonPropertyOrder; import org.jetbrains.annotations.TestOnly; +import org.jetbrains.annotations.VisibleForTesting; import java.io.File; import java.io.Serializable; @@ -131,7 +132,6 @@ public String getRevisionForDisplay(int line) { } } - /** * Gets the enabled state for the last change to the specified line. * @@ -210,7 +210,8 @@ void addLine(final AnnotationLine annotationLine) { * @param displayRevision a specialised revision number of display purposes. Can be null in which case the revision number will be used. * @see #addLine(AnnotationLine) */ - void addLine(String revision, String author, boolean enabled, String displayRevision) { + @VisibleForTesting + public void addLine(String revision, String author, boolean enabled, String displayRevision) { final AnnotationLine annotationLine = new AnnotationLine(revision, author, enabled, displayRevision); addLine(annotationLine); } diff --git a/opengrok-indexer/src/main/java/org/opengrok/indexer/web/Util.java b/opengrok-indexer/src/main/java/org/opengrok/indexer/web/Util.java index 4cb1661c709..3f5be5a8dd7 100644 --- a/opengrok-indexer/src/main/java/org/opengrok/indexer/web/Util.java +++ b/opengrok-indexer/src/main/java/org/opengrok/indexer/web/Util.java @@ -69,6 +69,8 @@ import jakarta.servlet.http.HttpServletRequest; import org.apache.commons.lang3.SystemUtils; import org.apache.lucene.queryparser.classic.QueryParserBase; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.VisibleForTesting; import org.opengrok.indexer.configuration.RuntimeEnvironment; import org.opengrok.indexer.history.Annotation; import org.opengrok.indexer.history.HistoryGuru; @@ -715,10 +717,11 @@ public static void readableLine(int num, Writer out, Annotation annotation, Stri } } - private static void writeAnnotation(int num, Writer out, Annotation annotation, String userPageLink, - String userPageSuffix, String project) throws IOException { - String revision = annotation.getRevision(num); - boolean enabled = annotation.isEnabled(num); + @VisibleForTesting + static void writeAnnotation(int lineNum, Writer out, Annotation annotation, @Nullable String userPageLink, + @Nullable String userPageSuffix, String project) throws IOException { + String revision = annotation.getRevision(lineNum); + boolean enabled = annotation.isEnabled(lineNum); out.write(""); if (enabled) { out.write(ANCHOR_CLASS_START); @@ -750,38 +753,18 @@ private static void writeAnnotation(int num, Writer out, Annotation annotation, buf.append(""); buf.append('*'); } - htmlize(annotation.getRevisionForDisplay(num), buf); + htmlize(annotation.getRevisionForDisplay(lineNum), buf); if (isMostRecentRevision) { buf.append(SPAN_END); // recent revision span } out.write(buf.toString()); buf.setLength(0); if (enabled) { - RuntimeEnvironment env = RuntimeEnvironment.getInstance(); - out.write(ANCHOR_END); - // Write link to search the revision in current project. - out.write(ANCHOR_CLASS_START); - out.write("search\" href=\"" + env.getUrlPrefix()); - out.write(QueryParameters.DEFS_SEARCH_PARAM_EQ); - out.write(AMP); - out.write(QueryParameters.REFS_SEARCH_PARAM_EQ); - out.write(AMP); - out.write(QueryParameters.PATH_SEARCH_PARAM_EQ); - out.write(project); - out.write(AMP); - out.write(QueryParameters.HIST_SEARCH_PARAM_EQ); - out.write(QUOTE); - out.write(uriEncode(revision)); - out.write(""&"); - out.write(QueryParameters.TYPE_SEARCH_PARAM_EQ); - out.write("\" title=\"Search history for this revision"); - out.write(CLOSE_QUOTED_TAG); - out.write("S"); - out.write(ANCHOR_END); + writeAnnotationSearchLink(out, project, revision); } - String a = annotation.getAuthor(num); + String a = annotation.getAuthor(lineNum); if (userPageLink == null) { out.write(HtmlConsts.SPAN_A); htmlize(a, buf); @@ -805,6 +788,30 @@ private static void writeAnnotation(int num, Writer out, Annotation annotation, out.write(SPAN_END); } + private static void writeAnnotationSearchLink(Writer out, String projectName, String revision) throws IOException { + RuntimeEnvironment env = RuntimeEnvironment.getInstance(); + + // Write link to search the revision in current project. + out.write(ANCHOR_CLASS_START); + out.write("search\" href=\"" + env.getUrlPrefix()); + out.write(QueryParameters.DEFS_SEARCH_PARAM_EQ); + out.write(AMP); + out.write(QueryParameters.REFS_SEARCH_PARAM_EQ); + out.write(AMP); + out.write(QueryParameters.PATH_SEARCH_PARAM_EQ); + out.write(projectName); + out.write(AMP); + out.write(QueryParameters.HIST_SEARCH_PARAM_EQ); + out.write(QUOTE); + out.write(uriEncode(revision)); + out.write(""&"); + out.write(QueryParameters.TYPE_SEARCH_PARAM_EQ); + out.write("\" title=\"Search history for this revision"); + out.write(CLOSE_QUOTED_TAG); + out.write("S"); + out.write(ANCHOR_END); + } + /** * Generate a string from the given path and date in a way that allows * stable lexicographic sorting (i.e. gives always the same results) as a diff --git a/opengrok-indexer/src/test/java/org/opengrok/indexer/web/UtilTest.java b/opengrok-indexer/src/test/java/org/opengrok/indexer/web/UtilTest.java index b7b413f52c3..558f6c7e1d2 100644 --- a/opengrok-indexer/src/test/java/org/opengrok/indexer/web/UtilTest.java +++ b/opengrok-indexer/src/test/java/org/opengrok/indexer/web/UtilTest.java @@ -45,9 +45,14 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledOnOs; import org.junit.jupiter.api.condition.OS; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.opengrok.indexer.condition.EnabledForRepository; import org.opengrok.indexer.configuration.Project; import org.opengrok.indexer.configuration.RuntimeEnvironment; +import org.opengrok.indexer.history.Annotation; +import org.opengrok.indexer.history.AnnotationData; +import org.opengrok.indexer.history.AnnotationLine; import org.opengrok.indexer.history.HistoryGuru; import org.opengrok.indexer.index.Indexer; import org.opengrok.indexer.util.TestRepository; @@ -691,4 +696,26 @@ void testWriteHAD() throws Exception { "D", output); } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void testWriteAnnotation(boolean enabled) throws IOException { + StringWriter writer = new StringWriter(); + AnnotationData annotationData = new AnnotationData(); + annotationData.addLine("rev", "author", enabled, "dispRev"); + Annotation annotation = new Annotation(annotationData); + Util.writeAnnotation(1, writer, annotation, null, null, "foo"); + String output = writer.toString(); + String expectedOutput; + if (enabled) { + expectedOutput = "" + + "dispRev" + + "Sauthor"; + } else { + expectedOutput = "dispRevauthor"; + } + assertEquals(expectedOutput, output); + } } From 8501a51925c20c6a2862f85174ba92e037046fd5 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Thu, 22 May 2025 18:53:24 +0200 Subject: [PATCH 2/4] test getDesc() --- .../main/java/org/opengrok/indexer/history/Annotation.java | 4 +++- .../src/test/java/org/opengrok/indexer/web/UtilTest.java | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/opengrok-indexer/src/main/java/org/opengrok/indexer/history/Annotation.java b/opengrok-indexer/src/main/java/org/opengrok/indexer/history/Annotation.java index 226c0d9f85a..08a3e096774 100644 --- a/opengrok-indexer/src/main/java/org/opengrok/indexer/history/Annotation.java +++ b/opengrok-indexer/src/main/java/org/opengrok/indexer/history/Annotation.java @@ -26,6 +26,7 @@ package org.opengrok.indexer.history; import org.jetbrains.annotations.TestOnly; +import org.jetbrains.annotations.VisibleForTesting; import org.opengrok.indexer.logger.LoggerFactory; import org.opengrok.indexer.util.Color; import org.opengrok.indexer.util.LazilyInstantiate; @@ -166,7 +167,8 @@ public boolean isEnabled(int line) { return annotationData.isEnabled(line); } - void addDesc(String revision, String description) { + @VisibleForTesting + public void addDesc(String revision, String description) { desc.put(revision, description); } diff --git a/opengrok-indexer/src/test/java/org/opengrok/indexer/web/UtilTest.java b/opengrok-indexer/src/test/java/org/opengrok/indexer/web/UtilTest.java index 558f6c7e1d2..d40f6be4291 100644 --- a/opengrok-indexer/src/test/java/org/opengrok/indexer/web/UtilTest.java +++ b/opengrok-indexer/src/test/java/org/opengrok/indexer/web/UtilTest.java @@ -702,15 +702,17 @@ void testWriteHAD() throws Exception { void testWriteAnnotation(boolean enabled) throws IOException { StringWriter writer = new StringWriter(); AnnotationData annotationData = new AnnotationData(); - annotationData.addLine("rev", "author", enabled, "dispRev"); + final String rev = "rev"; + annotationData.addLine(rev, "author", enabled, "dispRev"); Annotation annotation = new Annotation(annotationData); + annotation.addDesc(rev, "description"); Util.writeAnnotation(1, writer, annotation, null, null, "foo"); String output = writer.toString(); String expectedOutput; if (enabled) { expectedOutput = "" + "dispRev" + + "href=\"?a=true&r=rev\" title=\"description\">dispRev" + "Sauthor"; } else { From 0fb75fe7248ab147d499374985360be83fefe414 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Thu, 22 May 2025 18:58:00 +0200 Subject: [PATCH 3/4] remove unused import --- .../src/test/java/org/opengrok/indexer/web/UtilTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/opengrok-indexer/src/test/java/org/opengrok/indexer/web/UtilTest.java b/opengrok-indexer/src/test/java/org/opengrok/indexer/web/UtilTest.java index d40f6be4291..000170a8e47 100644 --- a/opengrok-indexer/src/test/java/org/opengrok/indexer/web/UtilTest.java +++ b/opengrok-indexer/src/test/java/org/opengrok/indexer/web/UtilTest.java @@ -52,7 +52,6 @@ import org.opengrok.indexer.configuration.RuntimeEnvironment; import org.opengrok.indexer.history.Annotation; import org.opengrok.indexer.history.AnnotationData; -import org.opengrok.indexer.history.AnnotationLine; import org.opengrok.indexer.history.HistoryGuru; import org.opengrok.indexer.index.Indexer; import org.opengrok.indexer.util.TestRepository; From dcb6c779329dfa4aa06e4d6c01a1373649464dc6 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Thu, 22 May 2025 19:01:54 +0200 Subject: [PATCH 4/4] rename --- .../src/test/java/org/opengrok/indexer/web/UtilTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/opengrok-indexer/src/test/java/org/opengrok/indexer/web/UtilTest.java b/opengrok-indexer/src/test/java/org/opengrok/indexer/web/UtilTest.java index 000170a8e47..70c025fc886 100644 --- a/opengrok-indexer/src/test/java/org/opengrok/indexer/web/UtilTest.java +++ b/opengrok-indexer/src/test/java/org/opengrok/indexer/web/UtilTest.java @@ -701,7 +701,7 @@ void testWriteHAD() throws Exception { void testWriteAnnotation(boolean enabled) throws IOException { StringWriter writer = new StringWriter(); AnnotationData annotationData = new AnnotationData(); - final String rev = "rev"; + final String rev = "searchRev"; annotationData.addLine(rev, "author", enabled, "dispRev"); Annotation annotation = new Annotation(annotationData); annotation.addDesc(rev, "description"); @@ -711,8 +711,8 @@ void testWriteAnnotation(boolean enabled) throws IOException { if (enabled) { expectedOutput = "" + "dispRev" + - "dispRev" + + "Sauthor"; } else { expectedOutput = "dispRevauthor";