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
25 import org.codehaus.plexus.PlexusTestCase;
26 import org.codehaus.plexus.util.xml.Xpp3Dom;
27 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
28 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
29
30
31
32
33 public class DefaultBeanConfiguratorTest extends PlexusTestCase {
34
35 private BeanConfigurator configurator;
36
37 @Override
38 protected void setUp() throws Exception {
39 super.setUp();
40
41 configurator = lookup(BeanConfigurator.class);
42 }
43
44 @Override
45 protected void tearDown() throws Exception {
46 configurator = null;
47
48 super.tearDown();
49 }
50
51 private Xpp3Dom toConfig(String xml) {
52 try {
53 return Xpp3DomBuilder.build(new StringReader("<configuration>" + xml + "</configuration>"));
54 } catch (XmlPullParserException | IOException e) {
55 throw new IllegalArgumentException(e);
56 }
57 }
58
59 public void testMinimal() throws BeanConfigurationException {
60 SomeBean bean = new SomeBean();
61
62 Xpp3Dom config = toConfig("<file>test</file>");
63
64 DefaultBeanConfigurationRequest request = new DefaultBeanConfigurationRequest();
65 request.setBean(bean).setConfiguration(config);
66
67 configurator.configureBean(request);
68
69 assertEquals(new File("test"), bean.file);
70 }
71
72 public void testPreAndPostProcessing() throws BeanConfigurationException {
73 SomeBean bean = new SomeBean();
74
75 Xpp3Dom config = toConfig("<file>${test}</file>");
76
77 BeanConfigurationValuePreprocessor preprocessor = new BeanConfigurationValuePreprocessor() {
78 public Object preprocessValue(String value, Class<?> type) throws BeanConfigurationException {
79 if (value != null && value.startsWith("${") && value.endsWith("}")) {
80 return value.substring(2, value.length() - 1);
81 }
82 return value;
83 }
84 };
85
86 BeanConfigurationPathTranslator translator = new BeanConfigurationPathTranslator() {
87 public File translatePath(File path) {
88 return new File("base", path.getPath()).getAbsoluteFile();
89 }
90 };
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(new File("base/test").getAbsoluteFile(), bean.file);
99 }
100
101 public 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(new File("test"), bean.file);
112 }
113
114 static class SomeBean {
115
116 File file;
117 }
118 }