xref: /OpenGrok/opengrok-indexer/src/test/java/org/opengrok/indexer/web/EftarFileTest.java (revision 7483fa4f6ea5b37f9dc2e3053394dfc04d7212e6)
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) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
22  */
23 package org.opengrok.indexer.web;
24 
25 import org.junit.jupiter.api.AfterAll;
26 import org.junit.jupiter.api.BeforeAll;
27 import org.junit.jupiter.api.Test;
28 
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.HashSet;
32 import java.util.Set;
33 
34 import static org.junit.jupiter.api.Assertions.assertEquals;
35 
36 /**
37  * JUnit test to test the EftarFile-system.
38  */
39 public class EftarFileTest {
40 
41     private static File eftar;
42 
43     private static final String PATH_STRING = "/path";
44 
45     @BeforeAll
setUpClass()46     public static void setUpClass() throws Exception {
47 
48         eftar = File.createTempFile("paths", ".eftar");
49         int len = 100;
50         Set<PathDescription> descriptions = new HashSet<>();
51 
52         StringBuilder sb = new StringBuilder();
53         for (int ii = 0; ii < len; ii++) {
54             sb.append(PATH_STRING);
55             sb.append(ii);
56             descriptions.add(new PathDescription(sb.toString(), "Description " + ii));
57         }
58 
59         String outputFile = eftar.getAbsolutePath();
60 
61         EftarFile ef = new EftarFile();
62         ef.create(descriptions, outputFile);
63     }
64 
65     @AfterAll
tearDownClass()66     public static void tearDownClass() {
67         if (eftar != null) {
68             eftar.delete();
69         }
70     }
71 
72     /**
73      * Test usage of an EftarFile.
74      * @throws IOException if an error occurs while accessing the eftar file
75      */
76     @Test
searchEftarFile()77     void searchEftarFile() throws IOException {
78         searchEftarFile(new EftarFileReader(eftar));
79         searchEftarFile(new EftarFileReader(eftar.getAbsolutePath()));
80     }
81 
searchEftarFile(EftarFileReader er)82     private void searchEftarFile(EftarFileReader er) throws IOException {
83         StringBuilder sb = new StringBuilder();
84         StringBuilder match = new StringBuilder();
85         match.append("Description ");
86         int offset = match.length();
87         for (int ii = 0; ii < 100; ii++) {
88             sb.append(PATH_STRING);
89             sb.append(ii);
90             match.setLength(offset);
91             match.append(ii);
92 
93             assertEquals(match.toString(), er.get(sb.toString()), "description for path " + sb.toString());
94         }
95         er.close();
96     }
97 }
98