Skip to content

Commit

Permalink
fix #2796 Added JUnit tests for SystemHelper methods, ensuring enhanc…
Browse files Browse the repository at this point in the history
…ed reliability and coverage
  • Loading branch information
marevol committed Jan 9, 2024
1 parent b7041af commit 849fa39
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/main/java/org/codelibs/fess/helper/SystemHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public List<Map<String, String>> load(final String key) throws Exception {

ComponentUtil.doInitProcesses(Runnable::run);

parseProjectProperties();
parseProjectProperties(ResourceUtil.getProjectPropertiesFile());

updateConfigListenerMap.put("Label", () -> Integer.toString(ComponentUtil.getLabelTypeHelper().load()));
updateConfigListenerMap.put("PathMapping", () -> Integer.toString(ComponentUtil.getPathMappingHelper().load()));
Expand All @@ -179,8 +179,7 @@ public List<Map<String, String>> load(final String key) throws Exception {
updateConfigListenerMap.put("KeyMatch", () -> Integer.toString(ComponentUtil.getKeyMatchHelper().load()));
}

protected void parseProjectProperties() {
final Path propPath = ResourceUtil.getProjectPropertiesFile();
protected void parseProjectProperties(final Path propPath) {
try (final InputStream in = Files.newInputStream(propPath)) {
final Properties prop = new Properties();
prop.load(in);
Expand Down Expand Up @@ -358,7 +357,7 @@ public boolean isForceStop() {
}

public void setForceStop(final boolean b) {
forceStop.set(true);
forceStop.set(b);
}

public String generateDocId(final Map<String, Object> map) {
Expand Down
169 changes: 168 additions & 1 deletion src/test/java/org/codelibs/fess/helper/SystemHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,23 @@
*/
package org.codelibs.fess.helper;

import java.io.File;
import java.nio.file.Path;
import java.time.ZoneId;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.codelibs.core.io.FileUtil;
import org.codelibs.core.misc.Pair;
import org.codelibs.fess.mylasta.direction.FessConfig;
import org.codelibs.fess.unit.UnitFessTestCase;
import org.codelibs.fess.util.ComponentUtil;
import org.lastaflute.web.response.HtmlResponse;

public class SystemHelperTest extends UnitFessTestCase {

Expand All @@ -31,9 +42,13 @@ public class SystemHelperTest extends UnitFessTestCase {
@Override
public void setUp() throws Exception {
super.setUp();
final File propFile = File.createTempFile("project", ".properties");
propFile.deleteOnExit();
FileUtil.writeBytes(propFile.getAbsolutePath(), "fess.version=98.76.5".getBytes());
systemHelper = new SystemHelper() {
@Override
protected void parseProjectProperties() {
protected void parseProjectProperties(final Path propPath) {
super.parseProjectProperties(propFile.toPath());
}

@Override
Expand All @@ -47,6 +62,158 @@ protected Map<String, String> getEnvMap() {
};
envMap.clear();
systemHelper.init();
ComponentUtil.register(systemHelper, "systemHelper");
}

public void test_getUsername() {
assertEquals("guest", systemHelper.getUsername());
}

public void test_getCurrentTimeAsLocalDateTime() {
final long current =
1000 * systemHelper.getCurrentTimeAsLocalDateTime().atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
final long now = System.currentTimeMillis();
assertTrue(now + ">=" + current + " : " + (now - current), now >= current);
assertTrue((now - 1000) + "<" + current + " : " + (current - now + 1000), now - 1000 < current);
}

public void test_getLogFilePath() {
final File logFile = new File(systemHelper.getLogFilePath());
assertEquals("logs", logFile.getName());
assertEquals("target", logFile.getParentFile().getName());
try {
System.setProperty("fess.log.path", "logpath");
assertEquals("logpath", systemHelper.getLogFilePath());
} finally {
System.clearProperty("fess.log.path");
}
}

public void test_getForumLink() {
assertEquals("https://discuss.codelibs.org/c/FessEN/", systemHelper.getForumLink());
}

public void test_getHelpLink() {
assertEquals("https://fess.codelibs.org/98.76/admin/xxx-guide.html", systemHelper.getHelpLink("xxx"));
}

public void test_getDesignJspFileName() {
assertNull(systemHelper.getDesignJspFileName("xxx"));
systemHelper.addDesignJspFileName("xxx", "yyy");
assertEquals("yyy", systemHelper.getDesignJspFileName("xxx"));
final Pair<String, String>[] designJspFileNames = systemHelper.getDesignJspFileNames();
assertEquals(1, designJspFileNames.length);
assertEquals("xxx", designJspFileNames[0].getFirst());
assertEquals("yyy", designJspFileNames[0].getSecond());
}

public void test_setForceStop() {
assertFalse(systemHelper.isForceStop());
systemHelper.setForceStop(true);
assertTrue(systemHelper.isForceStop());
systemHelper.setForceStop(false);
assertFalse(systemHelper.isForceStop());
}

public void test_generateDocId() {
final String docId = systemHelper.generateDocId(Collections.emptyMap());
assertNotNull(docId);
assertEquals(32, docId.length());
}

public void test_abbreviateLongText() {
assertEquals("", systemHelper.abbreviateLongText(""));
assertEquals(4000, systemHelper.abbreviateLongText(Stream.generate(() -> "a").limit(4000).collect(Collectors.joining())).length());
assertEquals(4000, systemHelper.abbreviateLongText(Stream.generate(() -> "a").limit(4001).collect(Collectors.joining())).length());
}

public void test_getLanguageItems() {
List<Map<String, String>> enItems = systemHelper.getLanguageItems(Locale.ENGLISH);
assertEquals(55, enItems.size());
List<Map<String, String>> jaItems = systemHelper.getLanguageItems(Locale.JAPANESE);
assertEquals(55, jaItems.size());
}

public void test_getHostnamet() {
assertNotNull(systemHelper.getHostname());
try {
envMap.put("COMPUTERNAME", "xxx");
assertEquals("xxx", systemHelper.getHostname());
} finally {
envMap.remove("COMPUTERNAME");
}
try {
envMap.put("HOSTNAME", "yyy");
assertEquals("yyy", systemHelper.getHostname());
} finally {
envMap.remove("HOSTNAME");
}
}

public void test_isEoled() {
assertEquals(systemHelper.getCurrentTimeAsLong() > systemHelper.eolTime, systemHelper.isEoled());
}

public void test_updateConfiguration() {
assertNotNull(systemHelper.updateConfiguration());
systemHelper.addUpdateConfigListener("XXX", () -> "OK");
assertTrue(systemHelper.updateConfiguration().contains("XXX: OK"));
}

public void test_isChangedClusterState() {
systemHelper.isChangedClusterState(0);
assertFalse(systemHelper.isChangedClusterState(0));
assertTrue(systemHelper.isChangedClusterState(1));
assertTrue(systemHelper.isChangedClusterState(2));
assertFalse(systemHelper.isChangedClusterState(2));
}

public void test_getRedirectResponseToLogin() {
final HtmlResponse response = HtmlResponse.fromForwardPath("/");
assertEquals(response, systemHelper.getRedirectResponseToLogin(response));
}

public void test_getRedirectResponseToRoot() {
final HtmlResponse response = HtmlResponse.fromForwardPath("/");
assertEquals(response, systemHelper.getRedirectResponseToRoot(response));
}

public void test_getLogLevel() {
final String logLevel = systemHelper.getLogLevel();
try {
systemHelper.setLogLevel("DEBUG");
assertEquals("DEBUG", systemHelper.getLogLevel());
systemHelper.setLogLevel("INFO");
assertEquals("INFO", systemHelper.getLogLevel());
systemHelper.setLogLevel("WARN");
assertEquals("WARN", systemHelper.getLogLevel());
systemHelper.setLogLevel("ERROR");
assertEquals("ERROR", systemHelper.getLogLevel());
} finally {
systemHelper.setLogLevel(logLevel);
}
}

public void test_createTempFile() {
assertNotNull(systemHelper.createTempFile("test", ".txt"));
}

public void test_calibrateCpuLoad() {
systemHelper.setSystemCpuCheckInterval(0L);
systemHelper.calibrateCpuLoad();
assertEquals(0, systemHelper.waitingThreadNames.size());
systemHelper.waitForNoWaitingThreads();
}

public void test_getVersion() {
assertEquals("98.76.5", systemHelper.getVersion());
assertEquals(98, systemHelper.getMajorVersion());
assertEquals(76, systemHelper.getMinorVersion());
assertEquals("98.76", systemHelper.getProductVersion());
}

public void test_getEnvMap() {
assertNotNull(new SystemHelper().getEnvMap());
}

public void test_encodeUrlFilter() {
Expand Down

0 comments on commit 849fa39

Please sign in to comment.