Skip to content

Commit 6ced2cb

Browse files
authored
Merge pull request #1693 from tulinkry/project-customizations
project customizations in read-only configuration
2 parents b465b37 + bd1f0a1 commit 6ced2cb

File tree

4 files changed

+128
-16
lines changed

4 files changed

+128
-16
lines changed

src/org/opensolaris/opengrok/configuration/Configuration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import java.io.OutputStream;
4040
import java.util.ArrayList;
4141
import java.util.Collections;
42-
import java.util.Date;
4342
import java.util.HashMap;
4443
import java.util.HashSet;
4544
import java.util.LinkedList;
@@ -358,6 +357,9 @@ public void setGroupsCollapseThreshold(int groupsCollapseThreshold) throws Illeg
358357
* Creates a new instance of Configuration
359358
*/
360359
public Configuration() {
360+
/**
361+
* This list of calls is sorted alphabetically so please keep it.
362+
*/
361363
// defaults for an opengrok instance configuration
362364
cmds = new HashMap<>();
363365
setAllowedSymlinks(new HashSet<>());

src/org/opensolaris/opengrok/configuration/Project.java

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ public class Project implements Comparable<Project>, Nameable, Serializable {
6363
private boolean navigateWindowEnabled = false;
6464

6565
/**
66-
* This marks the project as (not)ready before initial index is done.
67-
* this is to avoid all/multi-project searches referencing this project
68-
* from failing.
66+
* This marks the project as (not)ready before initial index is done. this
67+
* is to avoid all/multi-project searches referencing this project from
68+
* failing.
6969
*/
7070
private boolean indexed = false;
7171

@@ -78,15 +78,40 @@ public class Project implements Comparable<Project>, Nameable, Serializable {
7878
public Project() {
7979
}
8080

81+
/**
82+
* Create a project with given name.
83+
*
84+
* @param name the name of the project
85+
*/
8186
public Project(String name) {
8287
this.name = name;
8388
}
8489

90+
/**
91+
* Create a project with given name and path
92+
*
93+
* @param name the name of the project
94+
* @param path the path of the project relative to the source root
95+
*/
8596
public Project(String name, String path) {
8697
this.name = name;
8798
this.path = path;
8899
}
89100

101+
/**
102+
* Create a project with given name and path and default configuration
103+
* values.
104+
*
105+
* @param name the name of the project
106+
* @param path the path of the project relative to the source root
107+
* @param cfg configuration containing the default values for project
108+
* properties
109+
*/
110+
public Project(String name, String path, Configuration cfg) {
111+
this(name, path);
112+
completeWithDefaults(cfg);
113+
}
114+
90115
/**
91116
* Get a textual name of this project
92117
*
@@ -223,6 +248,32 @@ public void addGroup(Group group) {
223248
}
224249
}
225250

251+
/**
252+
* Fill the project with the current configuration where the applicable
253+
* project property has a default value.
254+
*
255+
* @param cfg configuration with default values if applicable
256+
*/
257+
final public void completeWithDefaults(Configuration cfg) {
258+
Configuration defaultCfg = new Configuration();
259+
/**
260+
* Choosing strategy for properties (tabSize here):
261+
* <pre>
262+
* this cfg defaultCfg chosen value
263+
* ===============================================
264+
* |5| 4 0 5
265+
* 0 |4| 0 4
266+
* </pre>
267+
*
268+
* The strategy is:
269+
* 1) if the project has some non-default value; use that
270+
* 2) if the project has a default value; use the provided configuration
271+
*/
272+
if (getTabSize() == defaultCfg.getTabSize()) {
273+
setTabSize(cfg.getTabSize());
274+
}
275+
}
276+
226277
/**
227278
* Get the project for a specific file
228279
*

src/org/opensolaris/opengrok/index/Indexer.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.HashSet;
3636
import java.util.List;
3737
import java.util.Map;
38+
import java.util.Map.Entry;
3839
import java.util.Set;
3940
import java.util.TreeSet;
4041
import java.util.concurrent.ExecutorService;
@@ -175,6 +176,16 @@ public static void main(String argv[]) {
175176
subFilesList.add(path);
176177
}
177178

179+
// If an user used customizations for projects he perhaps just
180+
// used the key value for project without a name but the code
181+
// expects a name for the project. Therefore we fill the name
182+
// according to the project key which is the same.
183+
for (Entry<String, Project> entry : cfg.getProjects().entrySet()) {
184+
if (entry.getValue().getName() == null) {
185+
entry.getValue().setName(entry.getKey());
186+
}
187+
}
188+
178189
// Set updated configuration in RuntimeEnvironment.
179190
env.setConfiguration(cfg, subFilesList);
180191

@@ -886,13 +897,15 @@ public void prepareIndexer(RuntimeEnvironment env,
886897
// This is an existing object. Reuse the old project,
887898
// possibly with customizations, instead of creating a
888899
// new with default values.
889-
projects.put(name, oldProjects.get(name));
900+
Project p = oldProjects.get(name);
901+
p.setPath(path);
902+
p.setName(name);
903+
p.completeWithDefaults(env.getConfiguration());
904+
projects.put(name, p);
890905
} else if (!name.startsWith(".") && file.isDirectory()) {
891906
// Found a new directory with no matching project, so
892907
// create a new project with default properties.
893-
Project p = new Project(name, path);
894-
p.setTabSize(env.getConfiguration().getTabSize());
895-
projects.put(p.getName(), p);
908+
projects.put(name, new Project(name, path, env.getConfiguration()));
896909
}
897910
}
898911
}

test/org/opensolaris/opengrok/configuration/ProjectTest.java

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,16 @@
1717
* CDDL HEADER END
1818
*/
1919

20-
/*
20+
/*
2121
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
23-
2423
package org.opensolaris.opengrok.configuration;
2524

2625
import java.beans.ExceptionListener;
2726
import java.beans.XMLDecoder;
2827
import java.beans.XMLEncoder;
2928
import java.io.ByteArrayInputStream;
3029
import java.io.ByteArrayOutputStream;
31-
import java.util.ArrayList;
32-
import java.util.Collections;
3330
import java.util.HashMap;
3431
import java.util.LinkedList;
3532
import java.util.List;
@@ -39,9 +36,10 @@
3936
import static org.junit.Assert.*;
4037

4138
public class ProjectTest {
39+
4240
/**
43-
* Test that a {@code Project} instance can be encoded and decoded
44-
* without errors. Bug #3077.
41+
* Test that a {@code Project} instance can be encoded and decoded without
42+
* errors. Bug #3077.
4543
*/
4644
@Test
4745
public void testEncodeDecode() {
@@ -96,7 +94,7 @@ public void testGetProject() {
9694
Project bar = new Project("Project foo-bar", "/foo-bar");
9795

9896
// Make the runtime environment aware of these two projects.
99-
HashMap<String,Project> projects = new HashMap<>();
97+
HashMap<String, Project> projects = new HashMap<>();
10098
projects.put("foo", foo);
10199
projects.put("bar", bar);
102100
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
@@ -122,7 +120,7 @@ public void testGetProjectDescriptions() {
122120
Project bar = new Project("bar", "/bar");
123121

124122
// Make the runtime environment aware of these two projects.
125-
HashMap<String,Project> projects = new HashMap<>();
123+
HashMap<String, Project> projects = new HashMap<>();
126124
projects.put("foo", foo);
127125
projects.put("bar", bar);
128126
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
@@ -134,4 +132,52 @@ public void testGetProjectDescriptions() {
134132
assertFalse(descs.contains("foobar"));
135133
assertEquals(2, descs.size());
136134
}
135+
136+
/**
137+
* Insert the value from configuration.
138+
*/
139+
@Test
140+
public void testMergeProjects1() {
141+
Configuration cfg = new Configuration();
142+
cfg.setTabSize(new Configuration().getTabSize() + 3731);
143+
144+
Project p1 = new Project();
145+
p1.setNavigateWindowEnabled(true);
146+
147+
p1.completeWithDefaults(cfg);
148+
149+
assertNotNull(p1);
150+
assertTrue("Navigate window should be turned on", p1.isNavigateWindowEnabled());
151+
assertEquals(new Configuration().getTabSize() + 3731, p1.getTabSize());
152+
}
153+
154+
/**
155+
* Do not overwrite customized project property.
156+
*/
157+
@Test
158+
public void testMergeProjects2() {
159+
Configuration cfg = new Configuration();
160+
cfg.setTabSize(new Configuration().getTabSize() + 3731);
161+
162+
Project p1 = new Project();
163+
p1.setTabSize(new Project().getTabSize() + 9737);
164+
165+
p1.completeWithDefaults(cfg);
166+
167+
assertNotNull(p1);
168+
assertEquals(new Project().getTabSize() + 9737, p1.getTabSize());
169+
}
170+
171+
/**
172+
* Create a project fill with defaults from the configuration.
173+
*/
174+
@Test
175+
public void testCreateProjectWithConfiguration() {
176+
Configuration cfg = new Configuration();
177+
cfg.setTabSize(4);
178+
179+
Project p1 = new Project("a", "/a", cfg);
180+
181+
assertEquals(cfg.getTabSize(), p1.getTabSize());
182+
}
137183
}

0 commit comments

Comments
 (0)