Skip to content

Commit

Permalink
Add functional tests xwikisas#16
Browse files Browse the repository at this point in the history
* Solved merge conflicts
  • Loading branch information
ChiuchiuSorin committed Jan 30, 2024
1 parent d94abf8 commit 0dd9234
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface ServerInfo
*
* @return {@code true} if the server is used, {@code false} otherwise.
*/
boolean foundServerPath();
boolean isUsed();

/**
* Extract the hint of a component.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
import javax.inject.Provider;
import javax.inject.Singleton;

import org.xwiki.activeinstalls2.internal.data.ServletContainerPing;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.phase.Initializable;
import org.xwiki.component.phase.InitializationException;

import com.xwiki.admintools.ServerInfo;
import com.xwiki.admintools.internal.wikiUsage.UsageDataProvider;

/**
* Manages the server identifiers and offers endpoints to retrieve info about their paths.
Expand All @@ -44,11 +44,12 @@ public class CurrentServer implements Initializable
{
private static final String SERVER_NAME_KEY = "name";

private static final String SERVER_VERSION_KEY = "version";

@Inject
private Provider<List<ServerInfo>> supportedServers;

@Inject
private UsageDataProvider usageDataProvider;

private ServerInfo currentServerInfo;

@Override
Expand Down Expand Up @@ -91,24 +92,16 @@ public List<String> getSupportedServers()
return supportedServerList;
}

/**
* Access a {@link ServletContainerPing} containing the server metadata.
*
* @return the server metadata.
*/
public ServletContainerPing getServerMetadata()
{
return pingProvider.getServletPing();
}

/**
* Go through all supported servers and return the one that is used.
*/
public void updateCurrentServer()
{
this.currentServerInfo = null;
for (ServerInfo serverInfo : this.supportedServers.get()) {
if (serverInfo.isUsed()) {
boolean matchingHint = usageDataProvider.getServerMetadata().get(SERVER_NAME_KEY).toLowerCase()
.contains(serverInfo.getComponentHint());
if (matchingHint && serverInfo.isUsed()) {
this.currentServerInfo = serverInfo;
this.currentServerInfo.updatePossiblePaths();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class TomcatInfo extends AbstractServerInfo
/**
* Component identifier.
*/
public static final String HINT = "Tomcat";
public static final String HINT = "tomcat";

@Override
public boolean isUsed()
Expand Down Expand Up @@ -100,25 +100,6 @@ public Pattern getLogsPattern()
return Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
}

@Override
public boolean foundServerPath()
{
this.serverPath = null;
String providedConfigServerPath = this.adminToolsConfig.getServerPath();
if (!StringUtils.isEmpty(providedConfigServerPath)) {
return checkAndSetServerPath(providedConfigServerPath);
} else {
String catalinaBase = System.getProperty("catalina.base");
String catalinaHome = System.getenv("CATALINA_HOME");
if (!StringUtils.isEmpty(catalinaBase)) {
return checkAndSetServerPath(catalinaBase);
} else if (!StringUtils.isEmpty(catalinaHome)) {
return checkAndSetServerPath(catalinaHome);
}
}
return false;
}

private boolean checkAndSetServerPath(String path)
{
File file = new File(path + "/conf/catalina.properties");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mockito.Mock;
import org.xwiki.activeinstalls2.internal.data.DatabasePing;
import org.xwiki.activeinstalls2.internal.data.ServletContainerPing;
import org.xwiki.component.util.ReflectionUtils;
import org.xwiki.script.ScriptContextManager;
import org.xwiki.template.TemplateManager;
import org.xwiki.test.LogLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Named;
import javax.inject.Provider;

import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.xwiki.activeinstalls2.internal.data.ServletContainerPing;
import org.xwiki.component.phase.InitializationException;
import org.xwiki.test.annotation.BeforeComponent;
import org.xwiki.test.junit5.mockito.ComponentTest;
Expand All @@ -36,7 +35,7 @@

import com.xwiki.admintools.ServerInfo;
import com.xwiki.admintools.configuration.AdminToolsConfiguration;
import com.xwiki.admintools.internal.PingProvider;
import com.xwiki.admintools.internal.wikiUsage.UsageDataProvider;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand All @@ -63,15 +62,19 @@ class CurrentServerTest
@Named("default")
private AdminToolsConfiguration adminToolsConfig;

@MockComponent
private UsageDataProvider usageDataProvider;

@BeforeComponent
void setUp()
{
// Mock the list of supported servers.
List<ServerInfo> mockServerInfos = new ArrayList<>();
mockServerInfos.add(serverInfo);
when(supportedServers.get()).thenReturn(mockServerInfos);
List<ServerInfo> mockServerInfo = new ArrayList<>();
mockServerInfo.add(serverInfo);
when(supportedServers.get()).thenReturn(mockServerInfo);
when(serverInfo.isUsed()).thenReturn(true);

when(usageDataProvider.getServerMetadata()).thenReturn(Map.of("name", "Tomcat"));
when(serverInfo.getComponentHint()).thenReturn("tomcat");
// Mock the behavior of adminToolsConfig.
when(adminToolsConfig.getServerPath()).thenReturn("exampleServerPath");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* @version $Id$
*/
@ComponentTest
class TomcatIdentifierTest
class TomcatInfoTest
{
@InjectMockComponents
private TomcatInfo tomcatIdentifier;
Expand All @@ -56,7 +56,7 @@ class TomcatIdentifierTest
private File tmpDir;

@Test
void foundServerPathFound() throws IOException
void isUsedPathFound() throws IOException
{
when(adminToolsConfig.getServerPath()).thenReturn(tmpDir.getAbsolutePath());

Expand All @@ -69,11 +69,11 @@ void foundServerPathFound() throws IOException
assertTrue(testFile.exists());

// Test with a valid providedConfigServerPath
assertTrue(tomcatIdentifier.foundServerPath());
assertTrue(tomcatIdentifier.isUsed());
}

@Test
void foundServerPathWithValidCatalinaBase() throws IOException
void isUsedWithValidCatalinaBase() throws IOException
{
File configDirectory = new File(tmpDir, "conf");
configDirectory.mkdir();
Expand All @@ -85,18 +85,18 @@ void foundServerPathWithValidCatalinaBase() throws IOException

when(adminToolsConfig.getServerPath()).thenReturn(null);
System.setProperty("catalina.base", tmpDir.getAbsolutePath());
assertTrue(tomcatIdentifier.foundServerPath());
assertTrue(tomcatIdentifier.isUsed());
System.clearProperty("catalina.base");
}

@Test
void foundServerPathValidSystemPathMissingCatalinaProperties()
void isUsedValidSystemPathMissingCatalinaProperties()
{
File configDirectory = new File(tmpDir, "conf");
configDirectory.mkdir();
when(adminToolsConfig.getServerPath()).thenReturn(null);
System.setProperty("catalina.base", tmpDir.getAbsolutePath());
assertFalse(tomcatIdentifier.foundServerPath());
assertFalse(tomcatIdentifier.isUsed());
System.clearProperty("catalina.base");
configDirectory.delete();
}
Expand Down

0 comments on commit 0dd9234

Please sign in to comment.