1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * See LICENSE.txt included in this distribution for the specific 9 * language governing permissions and limitations under the License. 10 * 11 * When distributing Covered Code, include this CDDL HEADER in each 12 * file and include the License file at LICENSE.txt. 13 * If applicable, add the following below this CDDL HEADER, with the 14 * fields enclosed by brackets "[]" replaced with your own identifying 15 * information: Portions Copyright [yyyy] [name of copyright owner] 16 * 17 * CDDL HEADER END 18 */ 19 20 /* 21 * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. 22 */ 23 package org.opengrok.indexer.history; 24 25 import org.apache.commons.lang3.tuple.ImmutableTriple; 26 import org.junit.jupiter.api.AfterEach; 27 import org.junit.jupiter.api.BeforeEach; 28 import org.junit.jupiter.api.Test; 29 import org.junit.jupiter.params.ParameterizedTest; 30 import org.junit.jupiter.params.provider.MethodSource; 31 import org.junit.jupiter.params.provider.ValueSource; 32 import org.mockito.Mockito; 33 import org.opengrok.indexer.configuration.RuntimeEnvironment; 34 import org.opengrok.indexer.util.TestRepository; 35 36 import java.io.File; 37 import java.util.Arrays; 38 import java.util.List; 39 import java.util.stream.Stream; 40 41 import static org.junit.jupiter.api.Assertions.assertEquals; 42 import static org.junit.jupiter.api.Assertions.assertNotEquals; 43 import static org.junit.jupiter.api.Assertions.assertNotNull; 44 import static org.junit.jupiter.api.Assertions.assertThrows; 45 import static org.junit.jupiter.api.Assertions.assertTrue; 46 47 public class BoundaryChangesetsTest { 48 49 private TestRepository repositories; 50 51 private GitRepository gitRepository; 52 53 @BeforeEach setUp()54 public void setUp() throws Exception { 55 repositories = new TestRepository(); 56 repositories.create(getClass().getResource("/repositories")); 57 58 File reposRoot = new File(repositories.getSourceRoot(), "git"); 59 Repository repo = RepositoryFactory.getRepository(reposRoot); 60 assertNotNull(repo); 61 62 assertTrue(repo instanceof RepositoryWithPerPartesHistory); 63 gitRepository = (GitRepository) repo; 64 } 65 66 @AfterEach tearDown()67 public void tearDown() { 68 repositories.destroy(); 69 repositories = null; 70 } 71 72 @ParameterizedTest 73 @ValueSource(ints = {0, 1}) testInvalidMaxCount(int maxCount)74 void testInvalidMaxCount(int maxCount) { 75 GitRepository gitSpyRepository = Mockito.spy(gitRepository); 76 Mockito.when(gitSpyRepository.getPerPartesCount()).thenReturn(maxCount); 77 assertThrows(RuntimeException.class, () -> new BoundaryChangesets(gitSpyRepository)); 78 } 79 80 @Test testMaxCountConfiguration()81 void testMaxCountConfiguration() { 82 int maxCount = (RuntimeEnvironment.getInstance().getHistoryChunkCount() + 1) * 2; 83 assertNotEquals(0, maxCount); 84 RuntimeEnvironment.getInstance().setHistoryChunkCount(maxCount); 85 int actualCount = new BoundaryChangesets(gitRepository).getMaxCount(); 86 RuntimeEnvironment.getInstance().setHistoryChunkCount(0); 87 assertEquals(maxCount, actualCount); 88 } 89 90 /** 91 * Used to supply test data for testing {@link BoundaryChangesets#getBoundaryChangesetIDs(String)}. 92 * @return triplets of (maximum count, start revision, list of expected revisions) 93 * 94 * The test expects this sequence of changesets in the repository: 95 * 96 * <pre> 97 * {@code 84599b3cccb3eeb5aa9aec64771678d6526bcecb (HEAD -> master) renaming directories 98 * 67dfbe2648c94a8825671b0f2c132828d0d43079 renaming renamed -> renamed2 99 * 1086eaf5bca6d5a056097aa76017a8ab0eade20f adding some lines into renamed.c 100 * b6413947a59f481ddc0a05e0d181731233557f6e moved renamed.c to new location 101 * ce4c98ec1d22473d4aa799c046c2a90ae05832f1 adding simple file for renamed file testing 102 * aa35c25882b9a60a97758e0ceb276a3f8cb4ae3a Add lint make target and fix lint warnings 103 * 8482156421620efbb44a7b6f0eb19d1f191163c7 Add the result of a make on Solaris x86 104 * bb74b7e849170c31dc1b1b5801c83bf0094a3b10 Added a small test program} 105 * </pre> 106 */ provideMapsForTestPerPartesHistory()107 private static Stream<ImmutableTriple<Integer, String, List<String>>> provideMapsForTestPerPartesHistory() { 108 // Cannot use List.of() because of the null element. 109 List<String> expectedChangesets2 = Arrays.asList("8482156421620efbb44a7b6f0eb19d1f191163c7", 110 "ce4c98ec1d22473d4aa799c046c2a90ae05832f1", 111 "1086eaf5bca6d5a056097aa76017a8ab0eade20f"); 112 113 List<String> expectedChangesets4 = Arrays.asList("ce4c98ec1d22473d4aa799c046c2a90ae05832f1"); 114 115 List<String> expectedChangesets2Middle = Arrays.asList("ce4c98ec1d22473d4aa799c046c2a90ae05832f1", 116 "1086eaf5bca6d5a056097aa76017a8ab0eade20f"); 117 118 return Stream.of(ImmutableTriple.of(2, null, expectedChangesets2), 119 ImmutableTriple.of(4, null, expectedChangesets4), 120 ImmutableTriple.of(2, "aa35c25882b9a60a97758e0ceb276a3f8cb4ae3a", 121 expectedChangesets2Middle)); 122 } 123 124 /** 125 * Test of {@link BoundaryChangesets#getBoundaryChangesetIDs(String)}. 126 * @throws Exception on error 127 */ 128 @ParameterizedTest 129 @MethodSource("provideMapsForTestPerPartesHistory") testBasic(ImmutableTriple<Integer, String, List<String>> integerListImmutableTriple)130 void testBasic(ImmutableTriple<Integer, String, List<String>> integerListImmutableTriple) throws Exception { 131 GitRepository gitSpyRepository = Mockito.spy(gitRepository); 132 Mockito.when(gitSpyRepository.getPerPartesCount()).thenReturn(integerListImmutableTriple.getLeft()); 133 134 BoundaryChangesets boundaryChangesets = new BoundaryChangesets(gitSpyRepository); 135 List<String> boundaryChangesetList = boundaryChangesets. 136 getBoundaryChangesetIDs(integerListImmutableTriple.getMiddle()); 137 assertEquals(integerListImmutableTriple.getRight().size(), boundaryChangesetList.size()); 138 assertEquals(integerListImmutableTriple.getRight(), boundaryChangesetList); 139 } 140 } 141