Skip to content

Commit

Permalink
#36: Tool Commandlet for Tomcat
Browse files Browse the repository at this point in the history
- Includes the tests for tool dependencies
  • Loading branch information
aBega2000 committed Mar 17, 2024
1 parent 8f04879 commit c9cbe8d
Show file tree
Hide file tree
Showing 18 changed files with 257 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.devonfw.tools.ide.commandlet;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.property.KeywordProperty;
Expand All @@ -27,6 +23,7 @@
import com.devonfw.tools.ide.tool.quarkus.Quarkus;
import com.devonfw.tools.ide.tool.sonar.Sonar;
import com.devonfw.tools.ide.tool.terraform.Terraform;
import com.devonfw.tools.ide.tool.tomcat.Tomcat;
import com.devonfw.tools.ide.tool.vscode.Vscode;

/**
Expand Down Expand Up @@ -79,6 +76,7 @@ public CommandletManagerImpl(IdeContext context) {
add(new Quarkus(context));
add(new Kotlinc(context));
add(new KotlincNative(context));
add(new Tomcat(context));
add(new Vscode(context));
add(new Azure(context));
add(new Aws(context));
Expand Down
114 changes: 114 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/tool/tomcat/Tomcat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.devonfw.tools.ide.tool.tomcat;

import java.nio.file.Path;
import java.util.Set;

import com.devonfw.tools.ide.common.Tag;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.environment.EnvironmentVariables;
import com.devonfw.tools.ide.environment.EnvironmentVariablesType;
import com.devonfw.tools.ide.property.EnumProperty;
import com.devonfw.tools.ide.tool.LocalToolCommandlet;

public class Tomcat extends LocalToolCommandlet {

public final EnumProperty<TomcatCommand> command;

/**
* The constructor.
*
* @param context the {@link IdeContext}.
*/
public Tomcat(IdeContext context) {

super(context, "tomcat", Set.of(Tag.JAVA));
this.command = add(new EnumProperty<>("", true, "command", TomcatCommand.class));
add(this.arguments);
}

@Override
public boolean install(boolean silent) {

return super.install(silent);
}

@Override
public void postInstall() {

super.postInstall();

EnvironmentVariables variables = this.context.getVariables();
EnvironmentVariables typeVariables = variables.getByType(EnvironmentVariablesType.CONF);

typeVariables.set("CATALINA_HOME", getToolPath().toString(), true);
typeVariables.save();
}

@Override
protected void initProperties() {

// Empty on purpose
}

@Override
public void run() {

TomcatCommand command = this.command.getValue();

switch (command) {
case START:
// printTomcatPort();
arguments.setValueAsString("start", context);
super.run();
break;
case STOP:
arguments.setValueAsString("stop", context);
this.context.warning("TEST 2");
break;
default:
}
}

@Override
protected String getBinaryName() {

TomcatCommand command = this.command.getValue();

Path toolBinPath = getToolBinPath();

String tomcatHome = null;

if (this.context.getSystemInfo().isWindows()) {
if (command.equals(TomcatCommand.START)) {
tomcatHome = "startup.bat";
} else if (command.equals(TomcatCommand.STOP)) {
tomcatHome = "shutdown.bat";
} else {
this.context.error("Unknown tomcat command");
}
} else {
if (command.equals(TomcatCommand.START)) {
tomcatHome = "startup.sh";
} else if (command.equals(TomcatCommand.STOP)) {
tomcatHome = "shutdown.sh";
} else {
this.context.error("Unknown tomcat command");
}
}

return toolBinPath.resolve(tomcatHome).toString();
}

// private void printTomcatPort() {
//
// this.context.info("Tomcat is running at localhost on the following port (default 8080):");
// Path tomcatPropertiesPath = getToolPath().resolve("conf/server.xml");
//
// Properties tomcatProperties = PropertiesFileUtil.loadProperties(tomcatPropertiesPath);
// String tomcatWebPort = tomcatProperties.getProperty("redirectPort");
// if (tomcatWebPort != null) {
// this.context.info("TEST: " + tomcatWebPort);
// }
// }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.devonfw.tools.ide.tool.tomcat;

public enum TomcatCommand {
START, STOP
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.devonfw.tools.ide.tool.tomcat;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeTestContext;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.os.SystemInfo;
import com.devonfw.tools.ide.os.SystemInfoMock;

public class TomcatTest extends AbstractIdeContextTest {

private static final String PROJECT_TOMCAT = "tomcat";

@ParameterizedTest
@ValueSource(strings = { "windows", "linux" })
public void testTomcatInstall(String os) {

// arrange
IdeTestContext context = newContext(PROJECT_TOMCAT);
SystemInfo systemInfo = SystemInfoMock.of(os);
context.setSystemInfo(systemInfo);
Tomcat tomcatCommandlet = new Tomcat(context);

// install
tomcatCommandlet.install();

// assert
checkInstallation(context);
checkDependencyInstallation(context);
}

private void checkDependencyInstallation(IdeTestContext context) {

assertLogMessage(context, IdeLogLevel.INFO,
"Necessary version of the dependency java is already installed in repository");

}

private void checkInstallation(IdeTestContext context) {

if (context.getSystemInfo().isWindows() || context.getSystemInfo().isLinux()) {
assertThat(context.getSoftwarePath().resolve("tomcat/bin/tomcat")).exists()
.hasContent("#!/bin/bash\n" + "echo \"tomcat $*\"");
assertThat(context.getSoftwarePath().resolve("tomcat/bin/tomcat.cmd")).exists()
.hasContent("@echo off\n" + "echo tomcat %*");
} else if (context.getSystemInfo().isMac()) {
assertThat(context.getSoftwarePath().resolve("tomcat/tomcat")).exists();
}
assertThat(context.getSoftwarePath().resolve("tomcat/.ide.software.version")).exists().hasContent("10.1.14");
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully installed tomcat in version 10.1.14");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"dependencies": {
"(10.0.27,10.1.18]": [
{
"tool": "java",
"versionRange": "[11,21_35]"
}
],
"(9.0.85,10.0.27]": [
{
"tool": "java",
"versionRange": "[8,11)"
}
],
"(8.5.98,9.0.85]*": [
{
"tool": "java",
"versionRange": "[8,11)"
}
],
"(8.0.53,8.5.98]": [
{
"tool": "java",
"versionRange": "[7,8)"
}
],
"(7.0.109,8.0.53]": [
{
"tool": "java",
"versionRange": "[7,8)"
}
],
"(6.0.53,7.0.109]": [
{
"tool": "java",
"versionRange": "[7,8)"
}
],
"(5.5.36,6.0.53])": [
{
"tool": "java",
"versionRange": "[5,7)"
}
],
"(4.1.40,5.5.36]": [
{
"tool": "java",
"versionRange": "[1.4,5)"
}
],
"(3.3.2,4.1.40]": [
{
"tool": "java",
"versionRange": "[1.3,1.4)"
}
],
"(3.3.0,3.3.2]": [
{
"tool": "java",
"versionRange": "[1.1,1.3)"
}
]
}
}
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the users HOME directory
1 change: 1 addition & 0 deletions cli/src/test/resources/ide-projects/tomcat/project/readme
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the IDE_HOME directory
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
JAVA_VERSION=17.0.10_7
TOMCAT_VERSION=10.1.14
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the main workspace of tomcat test case
1 change: 1 addition & 0 deletions cli/src/test/resources/ide-projects/tomcat/readme
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the IDE_ROOT directory
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a test file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
echo "tomcat $*"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo off
echo tomcat %*
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a test file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
echo "tomcat $*"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo off
echo tomcat %*

0 comments on commit c9cbe8d

Please sign in to comment.