xref: /OpenGrok/opengrok-indexer/src/test/java/org/opengrok/indexer/history/BazaarHistoryParserTest.java (revision 6c62ede99bd45f84e663cda017732f3bcc28db30)
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) 2008, 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.history;
25 
26 import java.util.Arrays;
27 import java.util.HashSet;
28 
29 import org.apache.commons.lang3.SystemUtils;
30 import org.junit.jupiter.api.AfterEach;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.opengrok.indexer.configuration.RuntimeEnvironment;
34 
35 import static org.junit.jupiter.api.Assertions.assertEquals;
36 import static org.junit.jupiter.api.Assertions.assertNotNull;
37 import static org.junit.jupiter.api.Assertions.assertTrue;
38 
39 /**
40  * @author austvik
41  */
42 public class BazaarHistoryParserTest {
43 
44     private BazaarHistoryParser instance;
45 
46     @BeforeEach
setUp()47     public void setUp() {
48         if (RuntimeEnvironment.getInstance().getSourceRootPath() == null) {
49             RuntimeEnvironment.getInstance().setSourceRoot("");
50         }
51         BazaarRepository bzrRepo = new BazaarRepository();
52         // BazaarHistoryParser needs to have a valid directory name.
53         bzrRepo.setDirectoryNameRelative("bzrRepo");
54         instance = new BazaarHistoryParser(bzrRepo);
55     }
56 
57     @AfterEach
tearDown()58     public void tearDown() {
59         instance = null;
60     }
61 
62     /**
63      * Test of parse method, of class BazaarHistoryParser.
64      * @throws Exception exception
65      */
66     @Test
parseEmpty()67     public void parseEmpty() throws Exception {
68         History result = instance.parse("");
69         assertNotNull(result);
70         assertNotNull(result.getHistoryEntries());
71         assertEquals(0, result.getHistoryEntries().size(), "Should not contain any history entries");
72     }
73 
74     @Test
parseLogNoFile()75     public void parseLogNoFile() throws Exception {
76         String revId1 = "1234";
77         String author1 = "First Last <username@example.com>";
78         String date1 = "Wed 2008-10-01 10:01:34 +0200";
79 
80         String revId2 = "1234";
81         String author2 = "First2 Last2 <username2@example.com>";
82         String date2 = "Wed 2008-10-15 09:21:31 +0100";
83 
84         String revId3 = "4";
85         String author3 = "First3 Last3 <username3@example.com>";
86         String date3 = "Wed 2008-10-15 09:21:31 -0100";
87 
88         String revId4 = "4.1";
89         String author4 = "First3 Last3 <username3@example.com>";
90         String date4 = "Wed 2008-10-15 09:21:31 -0100";
91 
92         String output = "------------------------------------------------------------\n" +
93                 "revno: " + revId1 + "\n" +
94                 "committer: " + author1 + "\n" +
95                 "branch nick: 1.2 branch\n" +
96                 "timestamp: " + date1 + "\n" +
97                 "message:\n" +
98                 "  Some message.\n" +
99                 "------------------------------------------------------------\n" +
100                 "revno: " + revId2 + "\n" +
101                 "committer: " + author2 + "\n" +
102                 "branch nick: branch-name\n" +
103                 "timestamp: " + date2 + "\n" +
104                 "message:\n" +
105                 "  One line comment.\n" +
106                 "------------------------------------------------------------\n" +
107                 "revno: " + revId3 + "\n" +
108                 "committer: " + author3 + "\n" +
109                 "timestamp: " + date3 + "\n" +
110                 "message:\n" +
111                 "  Comment over two lines, this is line1\n" +
112                 "  and this is line2\n" +
113                 "    ------------------------------------------------------------\n" +
114                 "    revno: " + revId4 + "\n" +
115                 "    committer: " + author4 + "\n" +
116                 "    timestamp: " + date4 + "\n" +
117                 "    message:\n" +
118                 "      Just a message\n";
119 
120         History result = instance.parse(output);
121 
122         assertNotNull(result);
123         assertNotNull(result.getHistoryEntries());
124         assertEquals(3, result.getHistoryEntries().size());
125 
126         HistoryEntry e1 = result.getHistoryEntries().get(0);
127         assertEquals(revId1, e1.getRevision());
128         assertEquals(author1, e1.getAuthor());
129         assertEquals(0, e1.getFiles().size());
130 
131         HistoryEntry e2 = result.getHistoryEntries().get(1);
132         assertEquals(revId2, e2.getRevision());
133         assertEquals(author2, e2.getAuthor());
134         assertEquals(0, e2.getFiles().size());
135 
136         HistoryEntry e3 = result.getHistoryEntries().get(2);
137         assertEquals(revId3, e3.getRevision());
138         assertEquals(author3, e3.getAuthor());
139         assertEquals(0, e3.getFiles().size());
140         assertTrue(e3.getMessage().contains("line1"));
141         assertTrue(e3.getMessage().contains("line2"));
142         assertTrue(e3.getMessage().contains("revno: " + revId4));
143     }
144 
145     @Test
146     @SuppressWarnings("unchecked")
parseLogDirectory()147     public void parseLogDirectory() throws Exception {
148         String revId1 = "1234";
149         String author1 = "username@example.com";
150         String date1 = "Wed 2008-10-01 10:01:34 +0200";
151         String[] files;
152         if (SystemUtils.IS_OS_WINDOWS) {
153             files = new String[] {
154                     "\\\\filename.ext",
155                     "\\\\directory",
156                     "\\\\directory\\filename.ext",
157                     "\\\\directory\\filename2.ext2",
158                     "\\\\otherdir\\file.extension"
159             };
160         } else {
161             files = new String[] {
162                     "/filename.ext",
163                     "/directory",
164                     "/directory/filename.ext",
165                     "/directory/filename2.ext2",
166                     "/otherdir/file.extension"
167             };
168         }
169 
170         StringBuilder output = new StringBuilder();
171         output.append("-".repeat(60));
172         output.append('\n');
173         output.append("revno: ").append(revId1).append("\n");
174         output.append("committer: ").append(author1).append("\n");
175         output.append("timestamp: ").append(date1).append("\n");
176         output.append("message:\n");
177         output.append("  Some message\n");
178         output.append("added:\n");
179         for (String file : files) {
180             output.append("  ").append(file.substring(1)).append("\n");
181         }
182 
183         History result = instance.parse(output.toString());
184 
185         assertNotNull(result);
186         assertNotNull(result.getHistoryEntries());
187         assertEquals(1, result.getHistoryEntries().size());
188 
189         HistoryEntry e1 = result.getHistoryEntries().get(0);
190         assertEquals(revId1, e1.getRevision());
191         assertEquals(author1, e1.getAuthor());
192         assertEquals(new HashSet<>(Arrays.asList(files)), e1.getFiles());
193     }
194 
195 }
196