Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JVM launching -D parameters should be passed before the -jar parameter to have any effect #275

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@
"type": "Secret Keyword",
"verified_result": null
}
],
"pkg/launcher/jvmLauncher_test.go": [
{
"hashed_secret": "c042bdfc4bc5516ec716afe9e85c173b614ff9f5",
"is_secret": false,
"is_verified": false,
"line_number": 824,
"type": "Hex High Entropy String",
"verified_result": null
}
]
},
"version": "0.13.1+ibm.62.dss",
Expand Down
17 changes: 10 additions & 7 deletions pkg/launcher/jvmLauncher.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,14 +667,21 @@ func getCommandSyntax(

args = appendArgsBootstrapJvmLaunchOptions(args, bootstrapProperties)

args = append(args, "-jar")
args = append(args, bootJarPath)

// Note: Any -D properties are options for the JVM, so must appear before the -jar parameter.
// Parameters after the -jar parameter get passed into the 'main' of the launched java program.
args = append(args, "-Dfile.encoding=UTF-8")

nativeGalasaHomeFolderPath := galasaHome.GetNativeFolderPath()
args = append(args, `-DGALASA_HOME="`+nativeGalasaHomeFolderPath+`"`)

// If there is a jwt, pass it through.
if jwt != "" {
args = append(args, "-DGALASA_JWT="+jwt)
}

args = append(args, "-jar")
args = append(args, bootJarPath)

// --localmaven file://${M2_PATH}/repository/
// Note: URLs always have forward-slashes
localMaven, err = defaultLocalMavenIfNotSet(localMaven, fileSystem)
Expand Down Expand Up @@ -725,10 +732,6 @@ func getCommandSyntax(
args = append(args, "--trace")
}

// If there is a jwt, pass it through.
if jwt != "" {
args = append(args, "-DGALASA_JWT="+jwt)
}
}

return cmd, args, err
Expand Down
83 changes: 83 additions & 0 deletions pkg/launcher/jvmLauncher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package launcher
import (
"log"
"strconv"
"strings"
"testing"

"github.com/galasa-dev/cli/pkg/api"
Expand Down Expand Up @@ -767,6 +768,88 @@ func TestCommandIncludesGALASA_HOMESystemProperty(t *testing.T) {
assert.Contains(t, args, `-DGALASA_HOME="/User/Home/testuser/.galasa"`)
}

func TestCommandAllDashDSystemPropertiesPassedAppearBeforeTheDashJar(t *testing.T) {

bootstrapProps, _, galasaHome, fs,
javaHome,
testObrs,
testLocation,
remoteMaven,
localMaven,
galasaVersionToRun,
overridesFilePath,
_ := getDefaultCommandSyntaxTestParameters()

isTraceEnabled := true
isDebugEnabled := false
var debugPort uint32 = 0
debugMode := ""

cmd, args, err := getCommandSyntax(
bootstrapProps,
galasaHome,
fs, javaHome,
testObrs,
testLocation,
remoteMaven,
localMaven,
galasaVersionToRun,
overridesFilePath,
"", // No Gherkin URL supplied
isTraceEnabled,
isDebugEnabled,
debugPort,
debugMode,
BLANK_JWT,
)

assert.NotNil(t, cmd)
assert.NotNil(t, args)
assert.Nil(t, err)

// Combine all arguments into a single string.
allArgs := strings.Join(args, " ")

allDashDIndexes := getAllIndexesOfSubstring(allArgs, "-D")
allDashJarIndexes := getAllIndexesOfSubstring(allArgs, "-jar")

assert.Equal(t, 1, len(allDashJarIndexes), "-jar option is found in command launch parameters an unexpected number of times")
dashJarIndex := allDashJarIndexes[0]
for _, dashDIndex := range allDashDIndexes {
assert.Less(t, dashDIndex, dashJarIndex, "A -Dxxx parameter is found after the -jar parameter, so will do nothing. -D parameters should appear before the -jar parameter")
}
}

func TestFindingIndexesOfSubstring(t *testing.T) {
originalString := "012345678901234567890"
indexes := getAllIndexesOfSubstring(originalString, "01")
if assert.Equal(t, 2, len(indexes)) {
assert.Equal(t, 0, indexes[0])
assert.Equal(t, 10, indexes[1])
}
}

func getAllIndexesOfSubstring(originalString string, subString string) []int {
var result []int = make([]int, 0)

searchString := originalString
charactersProcessed := 0
isDone := false
for !isDone {

index := strings.Index(searchString, subString)
if index == -1 {
isDone = true
} else {
result = append(result, index+charactersProcessed)
searchString = searchString[index+1:]
charactersProcessed += index + 1
}
}

return result
}

func TestCommandIncludesFlagsFromBootstrapProperties(t *testing.T) {
bootstrapProps, _, galasaHome, fs,
javaHome,
Expand Down