Skip to content

Commit 7b642ab

Browse files
authored
restore Groups command functionality (#4388)
fixes #4382
1 parent 6ad62cf commit 7b642ab

File tree

4 files changed

+57
-56
lines changed

4 files changed

+57
-56
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/Configuration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ public Configuration() {
576576
setFetchHistoryWhenNotInCache(true);
577577
setFoldingEnabled(true);
578578
setGenerateHtml(true);
579-
setGroups(new HashMap<>());
579+
setGroups(new TreeMap<>());
580580
setGroupsCollapseThreshold(4);
581581
setHandleHistoryOfRenamedFiles(false);
582582
setHistoryBasedReindex(true);

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/Groups.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.indexer.configuration;
2424

@@ -32,6 +32,7 @@
3232
import java.util.HashSet;
3333
import java.util.LinkedList;
3434
import java.util.List;
35+
import java.util.Map;
3536
import java.util.Set;
3637
import org.opengrok.indexer.util.Getopt;
3738

@@ -152,7 +153,7 @@ public static void main(String[] argv) {
152153
usage(System.err);
153154
System.exit(1);
154155
}
155-
matchGroups(System.out, new HashSet<>(cfg.getGroups().values()), match);
156+
matchGroups(System.out, cfg.getGroups(), match);
156157
} else if (empty) {
157158
// just list the groups
158159
if (parent != null || groupname != null || grouppattern != null) {
@@ -174,15 +175,15 @@ public static void main(String[] argv) {
174175
usage(System.err);
175176
System.exit(1);
176177
}
177-
deleteGroup(new HashSet<>(cfg.getGroups().values()), groupname);
178+
deleteGroup(cfg.getGroups(), groupname);
178179
out = prepareOutput(outFile);
179180
printOut(list, cfg, out);
180181
} else if (groupname != null) {
181182
if (grouppattern == null) {
182183
grouppattern = "";
183184
}
184185
// perform insert/update. parent may be null
185-
if (!modifyGroup(new HashSet<>(cfg.getGroups().values()), groupname, grouppattern, parent)) {
186+
if (!modifyGroup(cfg.getGroups(), groupname, grouppattern, parent)) {
186187
System.err.println("Parent group does not exist \"" + parent + "\"");
187188
} else {
188189
out = prepareOutput(outFile);
@@ -207,9 +208,8 @@ public static void main(String[] argv) {
207208
* Prints the configuration to the stream.
208209
*
209210
* @param list if true then it lists all available groups in configuration
210-
* if @param out is different than stdout it also prints the current
211+
* if @param out is different from stdout it also prints the current
211212
* configuration to that stream otherwise it prints the configuration to the
212-
* @param out stream.
213213
* @param cfg configuration
214214
* @param out output stream
215215
*/
@@ -261,7 +261,7 @@ private static void listGroups(PrintStream out, Set<Group> groups) {
261261
* @param groups set of groups
262262
* @param match project name
263263
*/
264-
private static void matchGroups(PrintStream out, Set<Group> groups, String match) {
264+
private static void matchGroups(PrintStream out, Map<String, Group> groups, String match) {
265265
Project p = new Project(match);
266266

267267
List<Group> matched = new ArrayList<>();
@@ -280,32 +280,32 @@ private static void matchGroups(PrintStream out, Set<Group> groups, String match
280280

281281
/**
282282
* Adds a group into the xml tree.
283-
*
283+
* <p>
284284
* If group already exists, only the pattern is modified. Parent group can
285285
* be null, in that case a new group is inserted as a top level group.
286-
*
286+
* </p>
287287
* @param groups existing groups
288-
* @param groupname new group name
289-
* @param grouppattern new group pattern
288+
* @param groupName new group name
289+
* @param groupPattern new group pattern
290290
* @param parent parent
291291
* @return false if parent group was not found, true otherwise
292292
*/
293-
private static boolean modifyGroup(Set<Group> groups, String groupname, String grouppattern, String parent) {
294-
Group g = new Group(groupname, grouppattern);
293+
private static boolean modifyGroup(Map<String, Group> groups, String groupName, String groupPattern, String parent) {
294+
Group g = new Group(groupName, groupPattern);
295295

296-
if (updateGroup(groups, groupname, grouppattern)) {
296+
if (updateGroup(groups, groupName, groupPattern)) {
297297
return true;
298298
}
299299

300300
if (parent != null) {
301301
if (insertToParent(groups, parent, g)) {
302-
groups.add(g);
302+
groups.put(groupName, g);
303303
return true;
304304
}
305305
return false;
306306
}
307307

308-
groups.add(g);
308+
groups.put(groupName, g);
309309
return true;
310310
}
311311

@@ -315,12 +315,12 @@ private static boolean modifyGroup(Set<Group> groups, String groupname, String g
315315
* @param groups existing groups
316316
* @param groupname group to remove
317317
*/
318-
private static void deleteGroup(Set<Group> groups, String groupname) {
319-
for (Group g : groups) {
320-
if (g.getName().equals(groupname)) {
321-
groups.remove(g);
322-
groups.removeAll(g.getDescendants());
323-
return;
318+
private static void deleteGroup(Map<String, Group> groups, String groupname) {
319+
Group group = groups.get(groupname);
320+
if (group != null) {
321+
groups.remove(groupname);
322+
for (Group g : group.getDescendants()) {
323+
groups.remove(g.getName());
324324
}
325325
}
326326
}
@@ -370,16 +370,16 @@ private static boolean treeTraverseGroups(Set<Group> groups, Walker walker) {
370370
* traversed group
371371
* @return true if {@code walker} emits true for any of the groups; false
372372
*/
373-
private static boolean linearTraverseGroups(Set<Group> groups, Walker walker) {
374-
for (Group g : groups) {
373+
private static boolean linearTraverseGroups(Map<String, Group> groups, Walker walker) {
374+
for (Group g : groups.values()) {
375375
if (walker.call(g)) {
376376
return true;
377377
}
378378
}
379379
return false;
380380
}
381381

382-
private static boolean insertToParent(Set<Group> groups, String parent, Group g) {
382+
private static boolean insertToParent(Map<String, Group> groups, String parent, Group g) {
383383
return linearTraverseGroups(groups, x -> {
384384
if (x.getName().equals(parent)) {
385385
x.addGroup(g);
@@ -394,7 +394,7 @@ private static boolean insertToParent(Set<Group> groups, String parent, Group g)
394394
});
395395
}
396396

397-
private static boolean updateGroup(Set<Group> groups, String groupname, String grouppattern) {
397+
private static boolean updateGroup(Map<String, Group> groups, String groupname, String grouppattern) {
398398
return linearTraverseGroups(groups, g -> {
399399
if (g.getName().equals(groupname)) {
400400
g.setPattern(grouppattern);

opengrok-indexer/src/test/java/org/opengrok/indexer/configuration/GroupsTest.java

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.indexer.configuration;
2424

@@ -29,12 +29,15 @@
2929
import java.io.IOException;
3030
import java.io.PrintStream;
3131
import java.lang.reflect.Method;
32-
import java.util.HashSet;
33-
import java.util.Set;
32+
import java.util.Map;
3433

3534
import static org.junit.jupiter.api.Assertions.assertEquals;
3635
import static org.junit.jupiter.api.Assertions.assertNotNull;
3736
import static org.junit.jupiter.api.Assertions.assertNull;
37+
38+
/**
39+
* Tests for the {@link Groups} command.
40+
*/
3841
public class GroupsTest {
3942

4043
Configuration cfg;
@@ -51,37 +54,39 @@ public void testBasicConfiguration() {
5154

5255
@Test
5356
public void testDeleteGroup() {
54-
Set<Group> groups = new HashSet<>(cfg.getGroups().values());
57+
Map<String, Group> groups = cfg.getGroups();
58+
final int origSize = groups.size();
5559
invokeMethod("deleteGroup",
56-
new Class<?>[]{Set.class, String.class},
60+
new Class<?>[]{Map.class, String.class},
5761
new Object[]{groups, "random not existing group"});
5862

59-
assertEquals(6, groups.size());
63+
assertEquals(origSize, groups.size());
6064

6165
invokeMethod("deleteGroup",
62-
new Class<?>[]{Set.class, String.class},
66+
new Class<?>[]{Map.class, String.class},
6367
new Object[]{groups, "apache"});
6468

65-
assertEquals(5, groups.size());
69+
assertEquals(origSize - 1, groups.size());
6670

6771
invokeMethod("deleteGroup",
68-
new Class<?>[]{Set.class, String.class},
72+
new Class<?>[]{Map.class, String.class},
6973
new Object[]{groups, "ctags"});
7074

7175
assertEquals(1, groups.size());
7276
}
7377

7478
@Test
7579
public void testAddGroup() {
76-
Set<Group> groups = new HashSet<>(cfg.getGroups().values());
80+
Map<String, Group> groups = cfg.getGroups();
81+
final int origSize = groups.size();
7782
Group grp = findGroup(groups, "new fantastic group");
7883
assertNull(grp);
7984

8085
invokeMethod("modifyGroup",
81-
new Class<?>[]{Set.class, String.class, String.class, String.class},
86+
new Class<?>[]{Map.class, String.class, String.class, String.class},
8287
new Object[]{groups, "new fantastic group", "some pattern", null});
8388

84-
assertEquals(7, groups.size());
89+
assertEquals(origSize + 1, groups.size());
8590

8691
grp = findGroup(groups, "new fantastic group");
8792
assertNotNull(grp);
@@ -91,18 +96,19 @@ public void testAddGroup() {
9196

9297
@Test
9398
public void testAddGroupToParent() {
94-
Set<Group> groups = new HashSet<>(cfg.getGroups().values());
99+
Map<String, Group> groups = cfg.getGroups();
100+
final int origSize = groups.size();
95101
Group grp = findGroup(groups, "apache");
96102
assertNotNull(grp);
97103

98104
grp = findGroup(groups, "new fantastic group");
99105
assertNull(grp);
100106

101107
invokeMethod("modifyGroup",
102-
new Class<?>[]{Set.class, String.class, String.class, String.class},
108+
new Class<?>[]{Map.class, String.class, String.class, String.class},
103109
new Object[]{groups, "new fantastic group", "some pattern", "apache"});
104110

105-
assertEquals(7, groups.size());
111+
assertEquals(origSize + 1, groups.size());
106112

107113
grp = findGroup(groups, "apache");
108114
assertNotNull(grp);
@@ -118,14 +124,14 @@ public void testAddGroupToParent() {
118124

119125
@Test
120126
public void testModifyGroup() {
121-
Set<Group> groups = new HashSet<>(cfg.getGroups().values());
127+
Map<String, Group> groups = cfg.getGroups();
122128
Group grp = findGroup(groups, "apache");
123129
assertNotNull(grp);
124130
assertEquals(grp.getName(), "apache");
125131
assertEquals(grp.getPattern(), "apache-.*");
126132

127133
invokeMethod("modifyGroup",
128-
new Class<?>[]{Set.class, String.class, String.class, String.class},
134+
new Class<?>[]{Map.class, String.class, String.class, String.class},
129135
new Object[]{groups, "apache", "different pattern", null});
130136

131137
grp = findGroup(groups, "apache");
@@ -146,18 +152,18 @@ public void testMatchGroup() {
146152
{"opengrok-12.0-rc3", 1},
147153
{"opengrk", 0}
148154
};
149-
Set<Group> groups = new HashSet<>(cfg.getGroups().values());
155+
Map<String, Group> groups = cfg.getGroups();
150156

151157
for (Object[] test : tests) {
152158
testSingleMatch(groups, (int) test[1], (String) test[0]);
153159
}
154160
}
155161

156-
private void testSingleMatch(Set<Group> groups, int expectedlines, String match) {
162+
private void testSingleMatch(Map<String, Group> groups, int expectedlines, String match) {
157163
ByteArrayOutputStream os = new ByteArrayOutputStream();
158164
PrintStream out = new PrintStream(os);
159165
invokeMethod("matchGroups",
160-
new Class<?>[]{PrintStream.class, Set.class, String.class},
166+
new Class<?>[]{PrintStream.class, Map.class, String.class},
161167
new Object[]{out, groups, match});
162168

163169
String output = os.toString();
@@ -177,13 +183,8 @@ private void invokeMethod(String name, Class<?>[] params, Object[] values) {
177183
}
178184
}
179185

180-
private Group findGroup(Set<Group> groups, String needle) {
181-
for (Group g : groups) {
182-
if (g.getName().equals(needle)) {
183-
return g;
184-
}
185-
}
186-
return null;
186+
private Group findGroup(Map<String, Group> groups, String needle) {
187+
return groups.get(needle);
187188
}
188189

189190
static final String BASIC_CONFIGURATION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"

opengrok-web/src/test/java/org/opengrok/web/api/v1/controller/GroupsControllerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.web.api.v1.controller;
2424

@@ -70,7 +70,7 @@ private List<String> listGroups() {
7070

7171
@Test
7272
void emptyGroups() {
73-
env.setGroups(new HashMap<>());
73+
env.setGroups(new TreeMap<>());
7474
assertFalse(env.hasGroups());
7575
List<String> groups = listGroups();
7676
assertNotNull(groups);

0 commit comments

Comments
 (0)