xref: /OpenGrok/opengrok-indexer/src/test/java/org/opengrok/indexer/configuration/ProjectTest.java (revision 42f8ab68c32a3a641cf6b286071a160600647684)
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  */
23 package org.opengrok.indexer.configuration;
24 
25 import java.beans.ExceptionListener;
26 import java.beans.XMLDecoder;
27 import java.beans.XMLEncoder;
28 import java.io.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.util.HashMap;
31 import java.util.LinkedList;
32 import java.util.List;
33 import org.junit.jupiter.api.Test;
34 
35 import static org.junit.jupiter.api.Assertions.assertAll;
36 import static org.junit.jupiter.api.Assertions.assertEquals;
37 import static org.junit.jupiter.api.Assertions.assertFalse;
38 import static org.junit.jupiter.api.Assertions.assertNotEquals;
39 import static org.junit.jupiter.api.Assertions.assertNotNull;
40 import static org.junit.jupiter.api.Assertions.assertNull;
41 import static org.junit.jupiter.api.Assertions.assertTrue;
42 
43 public class ProjectTest {
44 
45     /**
46      * Test that a {@code Project} instance can be encoded and decoded without
47      * errors. Bug #3077.
48      */
49     @Test
testEncodeDecode()50     public void testEncodeDecode() {
51         // Create an exception listener to detect errors while encoding and
52         // decoding
53         final LinkedList<Exception> exceptions = new LinkedList<>();
54         ExceptionListener listener = exceptions::addLast;
55 
56         ByteArrayOutputStream out = new ByteArrayOutputStream();
57         XMLEncoder enc = new XMLEncoder(out);
58         enc.setExceptionListener(listener);
59         Project p1 = new Project("foo");
60         enc.writeObject(p1);
61         enc.close();
62 
63         // verify that the write didn't fail
64         if (!exceptions.isEmpty()) {
65             // Can only chain one of the exceptions. Take the first one.
66             throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
67         }
68 
69         ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
70         XMLDecoder dec = new XMLDecoder(in, null, listener);
71         Project p2 = (Project) dec.readObject();
72         assertNotNull(p2);
73         dec.close();
74 
75         // verify that the read didn't fail
76         if (!exceptions.isEmpty()) {
77             // Can only chain one of the exceptions. Take the first one.
78             throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
79         }
80     }
81 
82     /**
83      * Test project matching.
84      */
85     @Test
testGetProject()86     public void testGetProject() {
87         // Create 2 projects, one being prefix of the other.
88         Project foo = new Project("Project foo", "/foo");
89         Project bar = new Project("Project foo-bar", "/foo-bar");
90 
91         // Make the runtime environment aware of these two projects.
92         HashMap<String, Project> projects = new HashMap<>();
93         projects.put("foo", foo);
94         projects.put("bar", bar);
95         RuntimeEnvironment env = RuntimeEnvironment.getInstance();
96         env.setProjectsEnabled(true);
97         env.setProjects(projects);
98 
99         // The matching of project name to project should be exact.
100         assertAll(
101                 () -> assertEquals(foo, Project.getProject("/foo")),
102                 () -> assertEquals(bar, Project.getProject("/foo-bar")),
103                 () -> assertEquals(foo, Project.getProject("/foo/blah.c")),
104                 () -> assertEquals(bar, Project.getProject("/foo-bar/ha.c")),
105                 () -> assertNull(Project.getProject("/foof")),
106                 () -> assertNull(Project.getProject("/foof/ha.c"))
107         );
108     }
109 
110     /**
111      * Test getProjectDescriptions().
112      */
113     @Test
testGetProjectDescriptions()114     public void testGetProjectDescriptions() {
115         // Create 2 projects.
116         Project foo = new Project("foo", "/foo");
117         Project bar = new Project("bar", "/bar");
118 
119         // Make the runtime environment aware of these two projects.
120         HashMap<String, Project> projects = new HashMap<>();
121         projects.put("foo", foo);
122         projects.put("bar", bar);
123         RuntimeEnvironment env = RuntimeEnvironment.getInstance();
124         env.setProjects(projects);
125 
126         List<String> descs = env.getProjectNames();
127         assertAll(
128                 () -> assertTrue(descs.contains("foo")),
129                 () -> assertTrue(descs.contains("bar")),
130                 () -> assertFalse(descs.contains("foobar")),
131                 () -> assertEquals(2, descs.size())
132         );
133     }
134 
135     /**
136      * Insert the value from configuration.
137      */
138     @Test
testMergeProjects1()139     public void testMergeProjects1() {
140         RuntimeEnvironment env = RuntimeEnvironment.getInstance();
141         env.setTabSize(new Configuration().getTabSize() + 3731);
142         env.setNavigateWindowEnabled(!new Configuration().isNavigateWindowEnabled());
143         env.setBugPage("http://example.com/bugPage");
144         env.setBugPattern("([1-9][0-9]{6,7})");
145         env.setReviewPage("http://example.com/reviewPage");
146         env.setReviewPattern("([A-Z]{2}ARC[ \\\\/]\\\\d{4}/\\\\d{3})");
147 
148         Project p1 = new Project();
149         assertNotNull(p1);
150 
151         p1.completeWithDefaults();
152 
153         assertAll(
154                 () -> assertEquals(env.getTabSize(), p1.getTabSize()),
155                 () -> assertEquals(env.isNavigateWindowEnabled(), p1.isNavigateWindowEnabled()),
156                 () -> assertEquals(env.getBugPage(), p1.getBugPage()),
157                 () -> assertEquals(env.getBugPattern(), p1.getBugPattern()),
158                 () -> assertEquals(env.getReviewPage(), p1.getReviewPage()),
159                 () -> assertEquals(env.getReviewPattern(), p1.getReviewPattern())
160         );
161     }
162 
163     /**
164      * Do not overwrite customized project property.
165      */
166     @Test
testMergeProjects2()167     public void testMergeProjects2() {
168         RuntimeEnvironment env = RuntimeEnvironment.getInstance();
169         env.setTabSize(new Configuration().getTabSize() + 3731);
170 
171         Project p1 = new Project();
172         p1.setTabSize(new Project().getTabSize() + 9737);
173         p1.setNavigateWindowEnabled(true);
174         p1.setHandleRenamedFiles(true);
175         final String customBugPage = "http://example.com/bugPage";
176         p1.setBugPage(customBugPage);
177         final String customBugPattern = "([1-9][0-1]{6,7})";
178         p1.setBugPattern(customBugPattern);
179         final String customReviewPage = "http://example.com/reviewPage";
180         p1.setReviewPage(customReviewPage);
181         final String customReviewPattern = "([A-Z]{2}XYZ[ \\\\/]\\\\d{4}/\\\\d{3})";
182         p1.setReviewPattern(customReviewPattern);
183 
184         p1.completeWithDefaults();
185 
186         assertAll(
187                 () -> assertNotNull(p1),
188                 () -> assertTrue(p1.isNavigateWindowEnabled(), "Navigate window should be turned on"),
189                 () -> assertTrue(p1.isHandleRenamedFiles(), "Renamed file handling should be true"),
190                 () -> assertEquals(new Project().getTabSize() + 9737, p1.getTabSize()),
191                 () -> assertEquals(p1.getBugPage(), customBugPage),
192                 () -> assertEquals(p1.getBugPattern(), customBugPattern),
193                 () -> assertEquals(p1.getReviewPage(), customReviewPage),
194                 () -> assertEquals(p1.getReviewPattern(), customReviewPattern)
195         );
196     }
197 
198     /**
199      * Create a project fill with defaults from the configuration.
200      */
201     @Test
testCreateProjectWithConfiguration()202     public void testCreateProjectWithConfiguration() {
203         RuntimeEnvironment env = RuntimeEnvironment.getInstance();
204         env.setTabSize(4);
205 
206         Project p1 = new Project("a", "/a");
207 
208         assertEquals(env.getTabSize(), p1.getTabSize());
209     }
210 
211     @Test
testEquality()212     public void testEquality() {
213         Project g1 = new Project();
214         Project g2 = new Project();
215         assertEquals(g1, g2, "null == null");
216 
217         g1 = new Project("name");
218         g2 = new Project("other");
219         assertNotEquals(g1, g2, "\"name\" != \"other\"");
220 
221         g1 = new Project("name");
222         g2 = new Project("NAME");
223         assertEquals(g1, g2, "\"name\" == \"NAME\"");
224         assertEquals(g1, g1, "\"name\" == \"name\"");
225         assertEquals(g2, g2, "\"NAME\" == \"NAME\"");
226     }
227 }
228