From 9fd1f2610559ba5fd6b2f748bc2ce501fcaaa369 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Fri, 2 Aug 2024 23:52:46 +0200 Subject: [PATCH] add tests for MercurialRepository.annotate() (#4627) --- .../indexer/history/MercurialRepository.java | 11 +++++- .../history/MercurialRepositoryTest.java | 39 ++++++++++++++++++- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/opengrok-indexer/src/main/java/org/opengrok/indexer/history/MercurialRepository.java b/opengrok-indexer/src/main/java/org/opengrok/indexer/history/MercurialRepository.java index d09b04aeb6a..f1abb03eeae 100644 --- a/opengrok-indexer/src/main/java/org/opengrok/indexer/history/MercurialRepository.java +++ b/opengrok-indexer/src/main/java/org/opengrok/indexer/history/MercurialRepository.java @@ -18,7 +18,7 @@ */ /* - * Copyright (c) 2006, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2006, 2024, Oracle and/or its affiliates. All rights reserved. * Portions Copyright (c) 2017, 2019, Chris Fraire . */ package org.opengrok.indexer.history; @@ -32,6 +32,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; +import java.util.Objects; import java.util.TreeSet; import java.util.function.Consumer; import java.util.function.Supplier; @@ -457,6 +458,7 @@ boolean getHistoryGet(OutputStream out, String parent, String basename, String r * @throws java.io.IOException if I/O exception occurred */ @Override + @Nullable public Annotation annotate(File file, String revision) throws IOException { ArrayList argv = new ArrayList<>(); ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK); @@ -483,6 +485,11 @@ public Annotation annotate(File file, String revision) throws IOException { // needed later to get user string for particular revision. try { History hist = HistoryGuru.getInstance().getHistory(file, false); + if (Objects.isNull(hist)) { + LOGGER.log(Level.SEVERE, + "Error: cannot get history for file ''{0}''", file); + return null; + } for (HistoryEntry e : hist.getHistoryEntries()) { // Chop out the colon and all hexadecimal what follows. // This is because the whole changeset identification is @@ -492,7 +499,7 @@ public Annotation annotate(File file, String revision) throws IOException { } } catch (HistoryException he) { LOGGER.log(Level.SEVERE, - "Error: cannot get history for file {0}", file); + "Error: cannot get history for file ''{0}''", file); return null; } diff --git a/opengrok-indexer/src/test/java/org/opengrok/indexer/history/MercurialRepositoryTest.java b/opengrok-indexer/src/test/java/org/opengrok/indexer/history/MercurialRepositoryTest.java index f703f8d7ec3..4654e7d4b8f 100644 --- a/opengrok-indexer/src/test/java/org/opengrok/indexer/history/MercurialRepositoryTest.java +++ b/opengrok-indexer/src/test/java/org/opengrok/indexer/history/MercurialRepositoryTest.java @@ -18,15 +18,17 @@ */ /* - * Copyright (c) 2009, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2009, 2024, Oracle and/or its affiliates. All rights reserved. * Portions Copyright (c) 2017, Chris Fraire . */ package org.opengrok.indexer.history; +import org.apache.commons.lang3.tuple.Pair; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.ValueSource; import org.opengrok.indexer.condition.EnabledForRepository; import org.opengrok.indexer.configuration.RuntimeEnvironment; @@ -43,11 +45,14 @@ import java.util.Collections; import java.util.List; import java.util.stream.Collectors; +import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.opengrok.indexer.condition.RepositoryInstalled.Type.MERCURIAL; @@ -255,7 +260,7 @@ void testGetHistoryGet() throws Exception { * Test that it is possible to get contents of multiple revisions of a file. */ @Test - void testgetHistoryGetForAll() throws Exception { + void testGetHistoryGetForAll() throws Exception { MercurialRepository mr = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot); for (String rev : REVISIONS_novel) { @@ -406,4 +411,34 @@ void testMergeCommits(boolean isMergeCommitsEnabled) throws Exception { // Cleanup. env.setMergeCommitsEnabled(isMergeCommitsEnabledOrig); } + + @Test + void testAnnotationNegative() throws Exception { + MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot); + File file = new File(repositoryRoot, "nonexistent"); + assertFalse(file.exists()); + Annotation annotation = hgRepo.annotate(file, null); + assertNull(annotation); + } + + private static Stream>> provideParametersForPositiveAnnotationTest() { + return Stream.of(Pair.of("8:6a8c423f5624", List.of("7", "8")), + Pair.of("7:db1394c05268", List.of("7"))); + } + + @ParameterizedTest + @MethodSource("provideParametersForPositiveAnnotationTest") + void testAnnotationPositive(Pair> pair) throws Exception { + MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot); + assertNotNull(hgRepo); + File file = new File(repositoryRoot, "novel.txt"); + assertTrue(file.exists()); + // The annotate() method calls uses HistoryGuru's getHistory() method which requires the RepositoryLookup + // to be initialized. Do so via setRepositories(). + RuntimeEnvironment.getInstance().setRepositories(repository.getSourceRoot()); + Annotation annotation = hgRepo.annotate(file, pair.getKey()); + assertNotNull(annotation); + List revisions = new ArrayList<>(annotation.getRevisions()); + assertEquals(pair.getValue(), revisions); + } }