Skip to content

Commit 75e512a

Browse files
authored
test writeAnnotation() (#4780)
fixes #4768
1 parent a0f4146 commit 75e512a

File tree

4 files changed

+68
-30
lines changed

4 files changed

+68
-30
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/history/Annotation.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package org.opengrok.indexer.history;
2727

2828
import org.jetbrains.annotations.TestOnly;
29+
import org.jetbrains.annotations.VisibleForTesting;
2930
import org.opengrok.indexer.logger.LoggerFactory;
3031
import org.opengrok.indexer.util.Color;
3132
import org.opengrok.indexer.util.LazilyInstantiate;
@@ -166,7 +167,8 @@ public boolean isEnabled(int line) {
166167
return annotationData.isEnabled(line);
167168
}
168169

169-
void addDesc(String revision, String description) {
170+
@VisibleForTesting
171+
public void addDesc(String revision, String description) {
170172
desc.put(revision, description);
171173
}
172174

opengrok-indexer/src/main/java/org/opengrok/indexer/history/AnnotationData.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
2727
import org.jetbrains.annotations.TestOnly;
28+
import org.jetbrains.annotations.VisibleForTesting;
2829

2930
import java.io.File;
3031
import java.io.Serializable;
@@ -131,7 +132,6 @@ public String getRevisionForDisplay(int line) {
131132
}
132133
}
133134

134-
135135
/**
136136
* Gets the enabled state for the last change to the specified line.
137137
*
@@ -210,7 +210,8 @@ void addLine(final AnnotationLine annotationLine) {
210210
* @param displayRevision a specialised revision number of display purposes. Can be null in which case the revision number will be used.
211211
* @see #addLine(AnnotationLine)
212212
*/
213-
void addLine(String revision, String author, boolean enabled, String displayRevision) {
213+
@VisibleForTesting
214+
public void addLine(String revision, String author, boolean enabled, String displayRevision) {
214215
final AnnotationLine annotationLine = new AnnotationLine(revision, author, enabled, displayRevision);
215216
addLine(annotationLine);
216217
}

opengrok-indexer/src/main/java/org/opengrok/indexer/web/Util.java

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
import jakarta.servlet.http.HttpServletRequest;
7070
import org.apache.commons.lang3.SystemUtils;
7171
import org.apache.lucene.queryparser.classic.QueryParserBase;
72+
import org.jetbrains.annotations.Nullable;
73+
import org.jetbrains.annotations.VisibleForTesting;
7274
import org.opengrok.indexer.configuration.RuntimeEnvironment;
7375
import org.opengrok.indexer.history.Annotation;
7476
import org.opengrok.indexer.history.HistoryGuru;
@@ -715,10 +717,11 @@ public static void readableLine(int num, Writer out, Annotation annotation, Stri
715717
}
716718
}
717719

718-
private static void writeAnnotation(int num, Writer out, Annotation annotation, String userPageLink,
719-
String userPageSuffix, String project) throws IOException {
720-
String revision = annotation.getRevision(num);
721-
boolean enabled = annotation.isEnabled(num);
720+
@VisibleForTesting
721+
static void writeAnnotation(int lineNum, Writer out, Annotation annotation, @Nullable String userPageLink,
722+
@Nullable String userPageSuffix, String project) throws IOException {
723+
String revision = annotation.getRevision(lineNum);
724+
boolean enabled = annotation.isEnabled(lineNum);
722725
out.write("<span class=\"blame\">");
723726
if (enabled) {
724727
out.write(ANCHOR_CLASS_START);
@@ -750,38 +753,18 @@ private static void writeAnnotation(int num, Writer out, Annotation annotation,
750753
buf.append("<span class=\"most_recent_revision\">");
751754
buf.append('*');
752755
}
753-
htmlize(annotation.getRevisionForDisplay(num), buf);
756+
htmlize(annotation.getRevisionForDisplay(lineNum), buf);
754757
if (isMostRecentRevision) {
755758
buf.append(SPAN_END); // recent revision span
756759
}
757760
out.write(buf.toString());
758761
buf.setLength(0);
759762
if (enabled) {
760-
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
761-
762763
out.write(ANCHOR_END);
763764

764-
// Write link to search the revision in current project.
765-
out.write(ANCHOR_CLASS_START);
766-
out.write("search\" href=\"" + env.getUrlPrefix());
767-
out.write(QueryParameters.DEFS_SEARCH_PARAM_EQ);
768-
out.write(AMP);
769-
out.write(QueryParameters.REFS_SEARCH_PARAM_EQ);
770-
out.write(AMP);
771-
out.write(QueryParameters.PATH_SEARCH_PARAM_EQ);
772-
out.write(project);
773-
out.write(AMP);
774-
out.write(QueryParameters.HIST_SEARCH_PARAM_EQ);
775-
out.write(QUOTE);
776-
out.write(uriEncode(revision));
777-
out.write("&quot;&amp;");
778-
out.write(QueryParameters.TYPE_SEARCH_PARAM_EQ);
779-
out.write("\" title=\"Search history for this revision");
780-
out.write(CLOSE_QUOTED_TAG);
781-
out.write("S");
782-
out.write(ANCHOR_END);
765+
writeAnnotationSearchLink(out, project, revision);
783766
}
784-
String a = annotation.getAuthor(num);
767+
String a = annotation.getAuthor(lineNum);
785768
if (userPageLink == null) {
786769
out.write(HtmlConsts.SPAN_A);
787770
htmlize(a, buf);
@@ -805,6 +788,30 @@ private static void writeAnnotation(int num, Writer out, Annotation annotation,
805788
out.write(SPAN_END);
806789
}
807790

791+
private static void writeAnnotationSearchLink(Writer out, String projectName, String revision) throws IOException {
792+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
793+
794+
// Write link to search the revision in current project.
795+
out.write(ANCHOR_CLASS_START);
796+
out.write("search\" href=\"" + env.getUrlPrefix());
797+
out.write(QueryParameters.DEFS_SEARCH_PARAM_EQ);
798+
out.write(AMP);
799+
out.write(QueryParameters.REFS_SEARCH_PARAM_EQ);
800+
out.write(AMP);
801+
out.write(QueryParameters.PATH_SEARCH_PARAM_EQ);
802+
out.write(projectName);
803+
out.write(AMP);
804+
out.write(QueryParameters.HIST_SEARCH_PARAM_EQ);
805+
out.write(QUOTE);
806+
out.write(uriEncode(revision));
807+
out.write("&quot;&amp;");
808+
out.write(QueryParameters.TYPE_SEARCH_PARAM_EQ);
809+
out.write("\" title=\"Search history for this revision");
810+
out.write(CLOSE_QUOTED_TAG);
811+
out.write("S");
812+
out.write(ANCHOR_END);
813+
}
814+
808815
/**
809816
* Generate a string from the given path and date in a way that allows
810817
* stable lexicographic sorting (i.e. gives always the same results) as a

opengrok-indexer/src/test/java/org/opengrok/indexer/web/UtilTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@
4545
import org.junit.jupiter.api.Test;
4646
import org.junit.jupiter.api.condition.EnabledOnOs;
4747
import org.junit.jupiter.api.condition.OS;
48+
import org.junit.jupiter.params.ParameterizedTest;
49+
import org.junit.jupiter.params.provider.ValueSource;
4850
import org.opengrok.indexer.condition.EnabledForRepository;
4951
import org.opengrok.indexer.configuration.Project;
5052
import org.opengrok.indexer.configuration.RuntimeEnvironment;
53+
import org.opengrok.indexer.history.Annotation;
54+
import org.opengrok.indexer.history.AnnotationData;
5155
import org.opengrok.indexer.history.HistoryGuru;
5256
import org.opengrok.indexer.index.Indexer;
5357
import org.opengrok.indexer.util.TestRepository;
@@ -691,4 +695,28 @@ void testWriteHAD() throws Exception {
691695
"<a href=\"/source/download/mercurial/main.c\" title=\"Download\">D</a></td>",
692696
output);
693697
}
698+
699+
@ParameterizedTest
700+
@ValueSource(booleans = {true, false})
701+
void testWriteAnnotation(boolean enabled) throws IOException {
702+
StringWriter writer = new StringWriter();
703+
AnnotationData annotationData = new AnnotationData();
704+
final String rev = "searchRev";
705+
annotationData.addLine(rev, "author", enabled, "dispRev");
706+
Annotation annotation = new Annotation(annotationData);
707+
annotation.addDesc(rev, "description");
708+
Util.writeAnnotation(1, writer, annotation, null, null, "foo");
709+
String output = writer.toString();
710+
String expectedOutput;
711+
if (enabled) {
712+
expectedOutput = "<span class=\"blame\">" +
713+
"<a class=\"r title-tooltip\" style=\"background-color: rgb(255, 191, 195)\" " +
714+
"href=\"?a=true&amp;r=searchRev\" title=\"description\">dispRev</a>" +
715+
"<a class=\"search\" href=\"/source/s?defs=&amp;refs=&amp;path=foo&amp;hist=&quot;searchRev&quot;&amp;type=\" " +
716+
"title=\"Search history for this revision\">S</a><span class=\"a\">author</span></span>";
717+
} else {
718+
expectedOutput = "<span class=\"blame\">dispRev<span class=\"a\">author</span></span>";
719+
}
720+
assertEquals(expectedOutput, output);
721+
}
694722
}

0 commit comments

Comments
 (0)