Skip to content

Commit cd6877a

Browse files
committed
Add unit tests for applyTemplatePrefix in Init.java
1 parent 37576e7 commit cd6877a

2 files changed

Lines changed: 115 additions & 7 deletions

File tree

  • impl/maven-cli/src
    • main/java/org/apache/maven/cling/invoker/mvnenc/goals
    • test/java/org/apache/maven/cling/invoker/mvnenc/goals

impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnenc/goals/Init.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,7 @@ public int doExecute(EncryptContext context) throws Exception {
144144
throw new InterruptedException();
145145
}
146146
String result = editMap.get("edit").getResult();
147-
if (template.contains("$")) {
148-
String prefix = template.substring(0, template.indexOf('$'));
149-
if (!result.startsWith(prefix)) {
150-
result = prefix + result;
151-
}
152-
}
153-
final String finalResult = result;
147+
final String finalResult = applyTemplatePrefix(template, result);
154148
dispatcherConfigResult.put(editable.getKey(), new PromptResultItemIF() {
155149
@Override
156150
public String getResult() {
@@ -289,4 +283,27 @@ private PromptBuilder configureDispatcher(
289283
}
290284
return promptBuilder;
291285
}
286+
287+
/**
288+
* Applies the template prefix to the user input if needed.
289+
* <p>
290+
* When a template contains a {@code $} placeholder (e.g., {@code "env:$VARIABLE_NAME"}),
291+
* the prefix before the {@code $} is extracted and prepended to the user's input
292+
* if it's not already present. This ensures that the generated configuration always
293+
* contains well-formed source values (e.g., {@code "env:MVN_PASSWORD"} instead of
294+
* just {@code "MVN_PASSWORD"}).
295+
*
296+
* @param template the template string potentially containing a {@code $} placeholder
297+
* @param userInput the user's raw input
298+
* @return the user input with the template prefix applied if needed
299+
*/
300+
static String applyTemplatePrefix(String template, String userInput) {
301+
if (template.contains("$")) {
302+
String prefix = template.substring(0, template.indexOf('$'));
303+
if (!prefix.isEmpty() && !userInput.startsWith(prefix)) {
304+
return prefix + userInput;
305+
}
306+
}
307+
return userInput;
308+
}
292309
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.cling.invoker.mvnenc.goals;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
25+
/**
26+
* Tests for {@link Init}, specifically the prefix-prepending logic
27+
* that ensures user input is correctly formatted with the template prefix
28+
* (e.g., {@code "env:"}) when saving encryption configuration.
29+
*/
30+
class InitTest {
31+
32+
@Test
33+
void applyTemplatePrefixPrependsEnvPrefixWhenMissing() {
34+
// User types "MVN_PASSWORD" but template is "env:$VARIABLE_NAME"
35+
assertEquals("env:MVN_PASSWORD", Init.applyTemplatePrefix("env:$VARIABLE_NAME", "MVN_PASSWORD"));
36+
}
37+
38+
@Test
39+
void applyTemplatePrefixDoesNotDuplicateExistingPrefix() {
40+
// User types "env:MVN_PASSWORD" and template is "env:$VARIABLE_NAME"
41+
assertEquals("env:MVN_PASSWORD", Init.applyTemplatePrefix("env:$VARIABLE_NAME", "env:MVN_PASSWORD"));
42+
}
43+
44+
@Test
45+
void applyTemplatePrefixHandlesSysPropertyPrefix() {
46+
// User types "MY_PROP" but template is "sys-property:$PROPERTY_NAME"
47+
assertEquals("sys-property:MY_PROP", Init.applyTemplatePrefix("sys-property:$PROPERTY_NAME", "MY_PROP"));
48+
}
49+
50+
@Test
51+
void applyTemplatePrefixDoesNotDuplicateSysPropertyPrefix() {
52+
assertEquals(
53+
"sys-property:MY_PROP",
54+
Init.applyTemplatePrefix("sys-property:$PROPERTY_NAME", "sys-property:MY_PROP"));
55+
}
56+
57+
@Test
58+
void applyTemplatePrefixReturnsInputUnchangedWhenNoPlaceholder() {
59+
// Template without $ placeholder — no prefix to apply
60+
assertEquals("someValue", Init.applyTemplatePrefix("fixedValue", "someValue"));
61+
}
62+
63+
@Test
64+
void applyTemplatePrefixReturnsInputUnchangedWhenPlaceholderAtStart() {
65+
// Template like "$VARIABLE" — prefix is empty, so nothing to prepend
66+
assertEquals("MVN_PASSWORD", Init.applyTemplatePrefix("$VARIABLE_NAME", "MVN_PASSWORD"));
67+
}
68+
69+
@Test
70+
void applyTemplatePrefixHandlesCustomPrefix() {
71+
// Arbitrary prefix before $
72+
assertEquals("file:/path", Init.applyTemplatePrefix("file:$PATH", "/path"));
73+
}
74+
75+
@Test
76+
void applyTemplatePrefixDoesNotDuplicateCustomPrefix() {
77+
assertEquals("file:/path", Init.applyTemplatePrefix("file:$PATH", "file:/path"));
78+
}
79+
80+
@Test
81+
void applyTemplatePrefixHandlesEmptyUserInput() {
82+
// Empty user input — prefix should still be prepended
83+
assertEquals("env:", Init.applyTemplatePrefix("env:$VARIABLE_NAME", ""));
84+
}
85+
86+
@Test
87+
void applyTemplatePrefixHandlesPrefixOnlyInput() {
88+
// User enters just the prefix itself
89+
assertEquals("env:", Init.applyTemplatePrefix("env:$VARIABLE_NAME", "env:"));
90+
}
91+
}

0 commit comments

Comments
 (0)