Skip to content

test writeAnnotation() #4780

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -131,7 +132,6 @@ public String getRevisionForDisplay(int line) {
}
}


/**
* Gets the enabled state for the last change to the specified line.
*
Expand Down Expand Up @@ -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);
}
Expand Down
61 changes: 34 additions & 27 deletions opengrok-indexer/src/main/java/org/opengrok/indexer/web/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("<span class=\"blame\">");
if (enabled) {
out.write(ANCHOR_CLASS_START);
Expand Down Expand Up @@ -750,38 +753,18 @@ private static void writeAnnotation(int num, Writer out, Annotation annotation,
buf.append("<span class=\"most_recent_revision\">");
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("&quot;&amp;");
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);
Expand All @@ -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("&quot;&amp;");
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -691,4 +695,28 @@ void testWriteHAD() throws Exception {
"<a href=\"/source/download/mercurial/main.c\" title=\"Download\">D</a></td>",
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 = "<span class=\"blame\">" +
"<a class=\"r title-tooltip\" style=\"background-color: rgb(255, 191, 195)\" " +
"href=\"?a=true&amp;r=searchRev\" title=\"description\">dispRev</a>" +
"<a class=\"search\" href=\"/source/s?defs=&amp;refs=&amp;path=foo&amp;hist=&quot;searchRev&quot;&amp;type=\" " +
"title=\"Search history for this revision\">S</a><span class=\"a\">author</span></span>";
} else {
expectedOutput = "<span class=\"blame\">dispRev<span class=\"a\">author</span></span>";
}
assertEquals(expectedOutput, output);
}
}
Loading