Skip to content

Commit

Permalink
JVM launching -D parameters should be passed before the -jar paramete…
Browse files Browse the repository at this point in the history
…r to have any effect (#275)

Signed-off-by: Mike Cobbett <[email protected]>
  • Loading branch information
techcobweb authored Aug 27, 2024
1 parent 06a819d commit 8a37d12
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 7 deletions.
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

0 comments on commit 8a37d12

Please sign in to comment.