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/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..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 @@ -45,9 +45,13 @@ 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.HistoryGuru; import org.opengrok.indexer.index.Indexer; import org.opengrok.indexer.util.TestRepository; @@ -691,4 +695,28 @@ 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(); + final String rev = "searchRev"; + 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" + + "Sauthor"; + } else { + expectedOutput = "dispRevauthor"; + } + assertEquals(expectedOutput, output); + } }