|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 - 2025, Ashley Scopes. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.github.ascopes.protobufmavenplugin.fs; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.nio.file.Path; |
| 22 | +import org.codehaus.plexus.component.configurator.ComponentConfigurationException; |
| 23 | +import org.codehaus.plexus.component.configurator.converters.lookup.DefaultConverterLookup; |
| 24 | +import org.codehaus.plexus.component.configurator.expression.DefaultExpressionEvaluator; |
| 25 | +import org.codehaus.plexus.configuration.DefaultPlexusConfiguration; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.DisplayName; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.api.io.TempDir; |
| 30 | +import org.junit.jupiter.params.ParameterizedTest; |
| 31 | +import org.junit.jupiter.params.provider.CsvSource; |
| 32 | + |
| 33 | +@DisplayName("PathPlexusConverter tests") |
| 34 | +class PathPlexusConverterTest { |
| 35 | + |
| 36 | + PathPlexusConverter converter; |
| 37 | + |
| 38 | + @BeforeEach |
| 39 | + void setUp() { |
| 40 | + converter = new PathPlexusConverter(); |
| 41 | + } |
| 42 | + |
| 43 | + @DisplayName("only the expected types are convertible") |
| 44 | + @CsvSource({ |
| 45 | + " java.nio.file.Path, true", |
| 46 | + " java.net.URI, false", |
| 47 | + " java.lang.Object, false", |
| 48 | + " java.lang.Integer, false", |
| 49 | + " java.lang.Class, false", |
| 50 | + " java.lang.String, false", |
| 51 | + "java.lang.StringBuilder, false", |
| 52 | + " java.io.File, false", |
| 53 | + " java.net.URL, false", |
| 54 | + }) |
| 55 | + @ParameterizedTest(name = "for {0}, expect {1}") |
| 56 | + void onlyTheExpectedTypesAreConvertible(Class<?> type, boolean expectedResult) { |
| 57 | + // Then |
| 58 | + assertThat(converter.canConvert(type)) |
| 59 | + .isEqualTo(expectedResult); |
| 60 | + } |
| 61 | + |
| 62 | + @DisplayName("Relative paths can be parsed successfully") |
| 63 | + @Test |
| 64 | + void relativePathsCanBeParsedSuccessfully( |
| 65 | + @TempDir Path baseDir |
| 66 | + ) throws ComponentConfigurationException { |
| 67 | + // Given |
| 68 | + var expectedAbsolutePath = baseDir.resolve("foo").resolve("bar").resolve("baz.txt") |
| 69 | + .toAbsolutePath(); |
| 70 | + var expectedPath = baseDir |
| 71 | + .relativize(expectedAbsolutePath); |
| 72 | + var converterLookup = new DefaultConverterLookup(); |
| 73 | + var configuration = new DefaultPlexusConfiguration("path", expectedPath.toString()); |
| 74 | + var evaluator = new SomeDirectoryRelativeExpressionEvaluator(baseDir); |
| 75 | + |
| 76 | + // When |
| 77 | + var result = converter.fromConfiguration( |
| 78 | + converterLookup, |
| 79 | + configuration, |
| 80 | + Path.class, |
| 81 | + null, |
| 82 | + getClass().getClassLoader(), |
| 83 | + evaluator, |
| 84 | + null |
| 85 | + ); |
| 86 | + |
| 87 | + // Then |
| 88 | + assertThat(result).isInstanceOf(Path.class); |
| 89 | + var resultPath = (Path) result; |
| 90 | + |
| 91 | + assertThat(resultPath.toAbsolutePath()) |
| 92 | + .hasToString("%s", expectedAbsolutePath); |
| 93 | + } |
| 94 | + |
| 95 | + @DisplayName("Absolute paths can be parsed successfully") |
| 96 | + @Test |
| 97 | + void absolutePathsCanBeParsedSuccessfully( |
| 98 | + @TempDir Path baseDir |
| 99 | + ) throws ComponentConfigurationException { |
| 100 | + // Given |
| 101 | + var expectedPath = baseDir.resolve("foo").resolve("bar").resolve("baz.txt") |
| 102 | + .toAbsolutePath(); |
| 103 | + var converterLookup = new DefaultConverterLookup(); |
| 104 | + var configuration = new DefaultPlexusConfiguration("path", expectedPath.toString()); |
| 105 | + var evaluator = new SomeDirectoryRelativeExpressionEvaluator(baseDir); |
| 106 | + |
| 107 | + // When |
| 108 | + var result = converter.fromConfiguration( |
| 109 | + converterLookup, |
| 110 | + configuration, |
| 111 | + Path.class, |
| 112 | + null, |
| 113 | + getClass().getClassLoader(), |
| 114 | + evaluator, |
| 115 | + null |
| 116 | + ); |
| 117 | + |
| 118 | + // Then |
| 119 | + assertThat(result).isInstanceOf(Path.class); |
| 120 | + var resultPath = (Path) result; |
| 121 | + |
| 122 | + assertThat(resultPath.toAbsolutePath()) |
| 123 | + .hasToString("%s", expectedPath.toAbsolutePath()); |
| 124 | + } |
| 125 | + |
| 126 | + @DisplayName("Null values are returned directly") |
| 127 | + @Test |
| 128 | + void nullValuesAreReturnedDirectly( |
| 129 | + @TempDir Path baseDir |
| 130 | + ) throws ComponentConfigurationException { |
| 131 | + // Given |
| 132 | + var converterLookup = new DefaultConverterLookup(); |
| 133 | + var configuration = new DefaultPlexusConfiguration("path", null); |
| 134 | + var evaluator = new SomeDirectoryRelativeExpressionEvaluator(baseDir); |
| 135 | + |
| 136 | + // When |
| 137 | + var result = converter.fromConfiguration( |
| 138 | + converterLookup, |
| 139 | + configuration, |
| 140 | + Path.class, |
| 141 | + null, |
| 142 | + getClass().getClassLoader(), |
| 143 | + evaluator, |
| 144 | + null |
| 145 | + ); |
| 146 | + |
| 147 | + // Then |
| 148 | + assertThat(result).isNull(); |
| 149 | + } |
| 150 | + |
| 151 | + // Roughly equivalent to what Maven does, for the sake of this test. |
| 152 | + static final class SomeDirectoryRelativeExpressionEvaluator extends DefaultExpressionEvaluator { |
| 153 | + |
| 154 | + private final Path baseDir; |
| 155 | + |
| 156 | + SomeDirectoryRelativeExpressionEvaluator(Path baseDir) { |
| 157 | + this.baseDir = baseDir.toAbsolutePath(); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public File alignToBaseDirectory(File file) { |
| 162 | + if (file.isAbsolute()) { |
| 163 | + return file; |
| 164 | + } |
| 165 | + |
| 166 | + var path = file.toPath(); |
| 167 | + var fullPath = baseDir; |
| 168 | + for (var part : path) { |
| 169 | + fullPath = fullPath.resolve(part); |
| 170 | + } |
| 171 | + return fullPath.toFile(); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments