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