|
| 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