Skip to content

Commit 63ddb75

Browse files
committed
add tests for PageConfig#addScript()
1 parent 6bce304 commit 63ddb75

File tree

3 files changed

+78
-8
lines changed

3 files changed

+78
-8
lines changed

opengrok-web/src/main/java/org/opengrok/web/PageConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ public class PageConfig {
135135
// query parameters
136136
static final String PROJECT_PARAM_NAME = "project";
137137
static final String GROUP_PARAM_NAME = "group";
138-
private static final String DEBUG_PARAM_NAME = "debug";
138+
@VisibleForTesting
139+
static final String DEBUG_PARAM_NAME = "debug";
139140

140141
private final AuthorizationFramework authFramework;
141142
private RuntimeEnvironment env;

opengrok-web/src/main/java/org/opengrok/web/Scripts.java

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

2020
/*
21-
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017, 2020, Chris Fraire <[email protected]>.
2323
* Portions Copyright (c) 2022, Krystof Tulinger <[email protected]>.
2424
*/
@@ -34,6 +34,7 @@
3434
import java.util.TreeSet;
3535

3636
import org.jetbrains.annotations.NotNull;
37+
import org.jetbrains.annotations.VisibleForTesting;
3738
import org.webjars.WebJarAssetLocator;
3839

3940
/**
@@ -43,10 +44,14 @@
4344
*/
4445
public class Scripts implements Iterable<Scripts.Script> {
4546

46-
private static final String DEBUG_SUFFIX = "-debug";
47+
@VisibleForTesting
48+
static final String DEBUG_SUFFIX = "-debug";
4749
private static final String WEBJAR_PATH_PREFIX = "META-INF/resources/";
4850

49-
enum Type {
51+
/**
52+
* Holds type of the script.
53+
*/
54+
public enum Type {
5055
MINIFIED, DEBUG
5156
}
5257

@@ -64,12 +69,18 @@ public abstract static class Script {
6469
*/
6570
protected String scriptData;
6671
protected int priority;
72+
private final String scriptName;
6773

68-
protected Script(String scriptData, int priority) {
74+
protected Script(String scriptName, String scriptData, int priority) {
75+
this.scriptName = scriptName;
6976
this.scriptData = scriptData;
7077
this.priority = priority;
7178
}
7279

80+
public String getScriptName() {
81+
return scriptName;
82+
}
83+
7384
public abstract String toHtml();
7485

7586
public String getScriptData() {
@@ -87,7 +98,11 @@ public int getPriority() {
8798
public static class FileScript extends Script {
8899

89100
public FileScript(String script, int priority) {
90-
super(script, priority);
101+
super(null, script, priority);
102+
}
103+
104+
public FileScript(String scriptName, String script, int priority) {
105+
super(scriptName, script, priority);
91106
}
92107

93108
@Override
@@ -98,7 +113,6 @@ public String toHtml() {
98113
this.getPriority() +
99114
"\"></script>\n";
100115
}
101-
102116
}
103117

104118
protected static final Map<String, Script> SCRIPTS = new TreeMap<>();
@@ -209,6 +223,11 @@ public boolean isEmpty() {
209223
return outputScripts.iterator();
210224
}
211225

226+
@VisibleForTesting
227+
List<String> getScriptNames() {
228+
return outputScripts.stream().map(Script::getScriptName).toList();
229+
}
230+
212231
/**
213232
* Add a script which is identified by the name.
214233
*
@@ -231,7 +250,7 @@ public boolean addScript(String contextPath, String scriptName, Type type) {
231250
}
232251

233252
private void addScript(String contextPath, String scriptName) {
234-
this.addScript(new FileScript(contextPath + SCRIPTS.get(scriptName).getScriptData(),
253+
this.addScript(new FileScript(scriptName, contextPath + SCRIPTS.get(scriptName).getScriptData(),
235254
SCRIPTS.get(scriptName).getPriority()));
236255
}
237256

opengrok-web/src/test/java/org/opengrok/web/PageConfigTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,4 +874,54 @@ public Cookie[] getCookies() {
874874
PageConfig cfg = PageConfig.get(req);
875875
assertEquals(List.of(SortOrder.LASTMODIFIED), cfg.getSortOrder());
876876
}
877+
878+
@ParameterizedTest
879+
@ValueSource(booleans = {true, false})
880+
void testAddScript(boolean isDebug) {
881+
HttpServletRequest req = new DummyHttpServletRequest() {
882+
@Override
883+
public String getPathInfo() {
884+
return "path";
885+
}
886+
887+
@Override
888+
public String getContextPath() {
889+
return "/";
890+
}
891+
892+
@Override
893+
public String getParameter(String name) {
894+
if (name.equals(PageConfig.DEBUG_PARAM_NAME)) {
895+
return isDebug ? "true" : "false";
896+
}
897+
return null;
898+
}
899+
};
900+
901+
PageConfig cfg = PageConfig.get(req);
902+
final String scriptName = "utils";
903+
cfg.addScript(scriptName);
904+
assertTrue(cfg.getScripts().getScriptNames().
905+
contains(isDebug ? scriptName + Scripts.DEBUG_SUFFIX : scriptName));
906+
}
907+
908+
@Test
909+
void testAddScriptInvalid() {
910+
HttpServletRequest req = new DummyHttpServletRequest() {
911+
@Override
912+
public String getPathInfo() {
913+
return "path";
914+
}
915+
916+
@Override
917+
public String getContextPath() {
918+
return "/";
919+
}
920+
};
921+
922+
PageConfig cfg = PageConfig.get(req);
923+
final String scriptName = "invalidScriptName";
924+
cfg.addScript(scriptName);
925+
assertFalse(cfg.getScripts().getScriptNames().contains(scriptName));
926+
}
877927
}

0 commit comments

Comments
 (0)