Skip to content

Commit e40405f

Browse files
committed
add positive test for writeHAD()
1 parent 03d13d3 commit e40405f

File tree

1 file changed

+63
-0
lines changed
  • opengrok-indexer/src/test/java/org/opengrok/indexer/web

1 file changed

+63
-0
lines changed

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.net.URISyntaxException;
3232
import java.net.URL;
3333
import java.nio.charset.StandardCharsets;
34+
import java.nio.file.Files;
35+
import java.nio.file.Path;
3436
import java.util.List;
3537
import java.util.Locale;
3638
import java.util.Map;
@@ -44,6 +46,12 @@
4446
import org.junit.jupiter.api.Test;
4547
import org.junit.jupiter.api.condition.EnabledOnOs;
4648
import org.junit.jupiter.api.condition.OS;
49+
import org.opengrok.indexer.condition.EnabledForRepository;
50+
import org.opengrok.indexer.configuration.Project;
51+
import org.opengrok.indexer.configuration.RuntimeEnvironment;
52+
import org.opengrok.indexer.history.HistoryGuru;
53+
import org.opengrok.indexer.index.Indexer;
54+
import org.opengrok.indexer.util.TestRepository;
4755

4856
import static org.hamcrest.MatcherAssert.assertThat;
4957
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
@@ -52,6 +60,7 @@
5260
import static org.junit.jupiter.api.Assertions.assertNull;
5361
import static org.junit.jupiter.api.Assertions.assertThrows;
5462
import static org.junit.jupiter.api.Assertions.assertTrue;
63+
import static org.opengrok.indexer.condition.RepositoryInstalled.Type.MERCURIAL;
5564

5665
/**
5766
* Test of the methods in <code>org.opengrok.indexer.web.Util</code>.
@@ -632,4 +641,58 @@ void testWriteHADNonexistentFile() throws Exception {
632641
assertEquals("<td class=\"q\"><a href=\"/source/download/nonexistent/file.c\" title=\"Download\">D</a></td>",
633642
output);
634643
}
644+
645+
/**
646+
* Test {@link Util#writeHAD(Writer, String, String)} for a file paths that correspond to a repository
647+
* with history enabled and disabled.
648+
* @throws Exception on error
649+
*/
650+
@EnabledForRepository(MERCURIAL)
651+
@Test
652+
void testWriteHAD() throws Exception {
653+
TestRepository repository = new TestRepository();
654+
repository.create(UtilTest.class.getResource("/repositories"));
655+
656+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
657+
658+
env.setSourceRoot(repository.getSourceRoot());
659+
env.setDataRoot(repository.getDataRoot());
660+
env.setProjectsEnabled(true);
661+
env.setHistoryEnabled(true);
662+
663+
// The projects have to be added first so that prepareIndexer() can use their configuration.
664+
Project proj = new Project("mercurial", "/mercurial");
665+
proj.setHistoryEnabled(false);
666+
env.getProjects().clear();
667+
env.getProjects().put("mercurial", proj);
668+
proj = new Project("git", "/git");
669+
env.getProjects().put("git", proj);
670+
671+
HistoryGuru.getInstance().clear();
672+
Indexer.getInstance().prepareIndexer(
673+
env,
674+
true, // search for repositories
675+
true, // scan and add projects
676+
false, // don't create dictionary
677+
null, // subFiles - not needed since we don't list files
678+
null); // repositories - not needed when not refreshing history
679+
env.generateProjectRepositoriesMap();
680+
681+
StringWriter writer = new StringWriter();
682+
String filePath = "/git/main.c";
683+
Util.writeHAD(writer, "/source", filePath);
684+
String output = writer.toString();
685+
assertEquals("<td class=\"q\"><a href=\"/source/history/git/main.c\" title=\"History\">H</a> " +
686+
"<a href=\"/source/xref/git/main.c?a=true\" title=\"Annotate\">A</a> " +
687+
"<a href=\"/source/download/git/main.c\" title=\"Download\">D</a></td>",
688+
output);
689+
690+
writer = new StringWriter();
691+
filePath = "/mercurial/main.c";
692+
Util.writeHAD(writer, "/source", filePath);
693+
output = writer.toString();
694+
assertEquals("<td class=\"q\"> <a href=\"/source/xref/mercurial/main.c?a=true\" title=\"Annotate\">A</a> " +
695+
"<a href=\"/source/download/mercurial/main.c\" title=\"Download\">D</a></td>",
696+
output);
697+
}
635698
}

0 commit comments

Comments
 (0)