1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.configuration;
20
21 import javax.xml.stream.XMLStreamException;
22
23 import java.io.File;
24 import java.io.StringReader;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27
28 import org.apache.maven.configuration.internal.DefaultBeanConfigurator;
29 import org.apache.maven.internal.xml.XmlNodeStaxBuilder;
30 import org.codehaus.plexus.util.xml.Xpp3Dom;
31 import org.junit.jupiter.api.AfterEach;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34
35 import static org.junit.jupiter.api.Assertions.assertEquals;
36
37
38
39 class DefaultBeanConfiguratorPathTest {
40
41 private BeanConfigurator configurator;
42
43 @BeforeEach
44 void setUp() throws Exception {
45 configurator = new DefaultBeanConfigurator();
46 }
47
48 @AfterEach
49 void tearDown() throws Exception {
50 configurator = null;
51 }
52
53 private Xpp3Dom toConfig(String xml) {
54 try {
55 return new Xpp3Dom(XmlNodeStaxBuilder.build(
56 new StringReader("<configuration>" + xml + "</configuration>"),
57 (XmlNodeStaxBuilder.InputLocationBuilderStax) null));
58 } catch (XMLStreamException e) {
59 throw new IllegalArgumentException(e);
60 }
61 }
62
63 @Test
64 void testMinimal() throws BeanConfigurationException {
65 SomeBean bean = new SomeBean();
66
67 Xpp3Dom config = toConfig("<file>test</file>");
68
69 DefaultBeanConfigurationRequest request = new DefaultBeanConfigurationRequest();
70 request.setBean(bean).setConfiguration(config);
71
72 configurator.configureBean(request);
73
74 assertEquals(Paths.get("test"), bean.file);
75 }
76
77 @Test
78 void testPreAndPostProcessing() throws BeanConfigurationException {
79 SomeBean bean = new SomeBean();
80
81 Xpp3Dom config = toConfig("<file>${test}</file>");
82
83 BeanConfigurationValuePreprocessor preprocessor = (value, type) -> {
84 if (value != null && value.startsWith("${") && value.endsWith("}")) {
85 return value.substring(2, value.length() - 1);
86 }
87 return value;
88 };
89
90 BeanConfigurationPathTranslator translator = path -> new File("base", path.getPath()).getAbsoluteFile();
91
92 DefaultBeanConfigurationRequest request = new DefaultBeanConfigurationRequest();
93 request.setBean(bean).setConfiguration(config);
94 request.setValuePreprocessor(preprocessor).setPathTranslator(translator);
95
96 configurator.configureBean(request);
97
98 assertEquals(Paths.get("base/test").toAbsolutePath(), bean.file);
99 }
100
101 @Test
102 void testChildConfigurationElement() throws BeanConfigurationException {
103 SomeBean bean = new SomeBean();
104
105 Xpp3Dom config = toConfig("<wrapper><file>test</file></wrapper>");
106
107 DefaultBeanConfigurationRequest request = new DefaultBeanConfigurationRequest();
108 request.setBean(bean).setConfiguration(config, "wrapper");
109
110 configurator.configureBean(request);
111
112 assertEquals(Paths.get("test"), bean.file);
113 }
114
115 static class SomeBean {
116
117 Path file;
118 }
119 }