xref: /OpenGrok/opengrok-indexer/src/test/java/org/opengrok/indexer/index/IndexCheckTest.java (revision 9a303f1948b99409943e5b2e00a139a726dbc8d4)
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) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
22  * Portions Copyright (c) 2019, Chris Fraire <cfraire@me.com>.
23  */
24 package org.opengrok.indexer.index;
25 
26 import java.io.File;
27 import java.io.IOException;
28 import java.net.URISyntaxException;
29 import java.net.URL;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.List;
35 
36 import static org.junit.jupiter.api.Assertions.assertFalse;
37 import static org.junit.jupiter.api.Assertions.assertNotNull;
38 import static org.junit.jupiter.api.Assertions.assertThrows;
39 import static org.junit.jupiter.api.Assertions.assertTrue;
40 
41 import org.junit.jupiter.api.AfterEach;
42 import org.junit.jupiter.api.BeforeAll;
43 import org.junit.jupiter.api.BeforeEach;
44 import org.junit.jupiter.api.Test;
45 import org.opengrok.indexer.configuration.Configuration;
46 import org.opengrok.indexer.configuration.RuntimeEnvironment;
47 import org.opengrok.indexer.history.RepositoryFactory;
48 import org.opengrok.indexer.util.FileUtilities;
49 import org.opengrok.indexer.util.IOUtils;
50 import org.opengrok.indexer.util.TestRepository;
51 
52 /**
53  * Verify index check.
54  * @author Vladimír Kotal
55  */
56 class IndexCheckTest {
57 
58     private TestRepository repository;
59     private final RuntimeEnvironment env = RuntimeEnvironment.getInstance();
60     private Path oldIndexDataDir;
61     private Configuration configuration;
62 
63     @BeforeAll
setUpClass()64     public static void setUpClass() {
65         RuntimeEnvironment env = RuntimeEnvironment.getInstance();
66         RepositoryFactory.initializeIgnoredNames(env);
67     }
68 
69     @BeforeEach
setUp()70     public void setUp() throws IOException, URISyntaxException {
71         repository = new TestRepository();
72         repository.create(IndexerTest.class.getResource("/repositories"));
73         oldIndexDataDir = null;
74         configuration = new Configuration();
75         configuration.setDataRoot(env.getDataRootPath());
76         configuration.setSourceRoot(env.getSourceRootPath());
77     }
78 
79     @AfterEach
tearDown()80     public void tearDown() throws IOException {
81         repository.destroy();
82 
83         if (oldIndexDataDir != null) {
84             IOUtils.removeRecursive(oldIndexDataDir);
85         }
86     }
87 
88     /**
89      * Generate index(es) and check version.
90      */
testIndexVersion(boolean projectsEnabled, List<String> subFiles)91     private void testIndexVersion(boolean projectsEnabled, List<String> subFiles) throws Exception {
92         env.setHistoryEnabled(false);
93         configuration.setHistoryEnabled(false);
94         env.setProjectsEnabled(projectsEnabled);
95         configuration.setProjectsEnabled(projectsEnabled);
96         Indexer.getInstance().prepareIndexer(env, true, true,
97                 false, null, null);
98         Indexer.getInstance().doIndexerExecution(true, null, null);
99 
100         IndexCheck.check(configuration, subFiles);
101     }
102 
103     @Test
testIndexVersionNoIndex()104     void testIndexVersionNoIndex() {
105         IndexCheck.check(configuration, new ArrayList<>());
106     }
107 
108     @Test
testIndexVersionProjects()109     void testIndexVersionProjects() throws Exception {
110         testIndexVersion(true, new ArrayList<>());
111     }
112 
113     @Test
testIndexVersionSelectedProjects()114     void testIndexVersionSelectedProjects() throws Exception {
115         testIndexVersion(true, Arrays.asList("mercurial", "git"));
116     }
117 
118     @Test
testIndexVersionNoProjects()119     void testIndexVersionNoProjects() throws Exception {
120         testIndexVersion(false, new ArrayList<>());
121     }
122 
123     @Test
testIndexVersionOldIndex()124     void testIndexVersionOldIndex() throws Exception {
125         oldIndexDataDir = Files.createTempDirectory("data");
126         Path indexPath = oldIndexDataDir.resolve("index");
127         Files.createDirectory(indexPath);
128         File indexDir = new File(indexPath.toString());
129         assertTrue(indexDir.isDirectory(), "index directory check");
130         URL oldIndex = getClass().getResource("/index/oldindex.zip");
131         assertNotNull(oldIndex, "resource needs to be non null");
132         File archive = new File(oldIndex.getPath());
133         assertTrue(archive.isFile(), "archive exists");
134         FileUtilities.extractArchive(archive, indexDir);
135         env.setDataRoot(oldIndexDataDir.toString());
136         configuration.setDataRoot(oldIndexDataDir.toString());
137         env.setProjectsEnabled(false);
138         configuration.setProjectsEnabled(false);
139         assertFalse(IndexCheck.check(configuration, new ArrayList<>()));
140 
141         assertThrows(IndexCheck.IndexVersionException.class, () -> IndexCheck.checkDir(indexDir));
142     }
143 }
144