1 package org.apache.maven.configuration;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.StringReader;
25
26 import org.codehaus.plexus.PlexusTestCase;
27 import org.codehaus.plexus.util.xml.Xpp3Dom;
28 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
29 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
30
31
32
33
34 public class DefaultBeanConfiguratorTest
35 extends PlexusTestCase
36 {
37
38 private BeanConfigurator configurator;
39
40 @Override
41 protected void setUp()
42 throws Exception
43 {
44 super.setUp();
45
46 configurator = lookup( BeanConfigurator.class );
47 }
48
49 @Override
50 protected void tearDown()
51 throws Exception
52 {
53 configurator = null;
54
55 super.tearDown();
56 }
57
58 private Xpp3Dom toConfig( String xml )
59 {
60 try
61 {
62 return Xpp3DomBuilder.build( new StringReader( "<configuration>" + xml + "</configuration>" ) );
63 }
64 catch ( XmlPullParserException | IOException e )
65 {
66 throw new IllegalArgumentException( e );
67 }
68 }
69
70 public void testMinimal()
71 throws BeanConfigurationException
72 {
73 SomeBean bean = new SomeBean();
74
75 Xpp3Dom config = toConfig( "<file>test</file>" );
76
77 DefaultBeanConfigurationRequest request = new DefaultBeanConfigurationRequest();
78 request.setBean( bean ).setConfiguration( config );
79
80 configurator.configureBean( request );
81
82 assertEquals( new File( "test" ), bean.file );
83 }
84
85 public void testPreAndPostProcessing()
86 throws BeanConfigurationException
87 {
88 SomeBean bean = new SomeBean();
89
90 Xpp3Dom config = toConfig( "<file>${test}</file>" );
91
92 BeanConfigurationValuePreprocessor preprocessor = new BeanConfigurationValuePreprocessor()
93 {
94 public Object preprocessValue( String value, Class<?> type )
95 throws BeanConfigurationException
96 {
97 if ( value != null && value.startsWith( "${" ) && value.endsWith( "}" ) )
98 {
99 return value.substring( 2, value.length() - 1 );
100 }
101 return value;
102 }
103 };
104
105 BeanConfigurationPathTranslator translator = new BeanConfigurationPathTranslator()
106 {
107 public File translatePath( File path )
108 {
109 return new File( "base", path.getPath() ).getAbsoluteFile();
110 }
111 };
112
113 DefaultBeanConfigurationRequest request = new DefaultBeanConfigurationRequest();
114 request.setBean( bean ).setConfiguration( config );
115 request.setValuePreprocessor( preprocessor ).setPathTranslator( translator );
116
117 configurator.configureBean( request );
118
119 assertEquals( new File( "base/test" ).getAbsoluteFile(), bean.file );
120 }
121
122 public void testChildConfigurationElement()
123 throws BeanConfigurationException
124 {
125 SomeBean bean = new SomeBean();
126
127 Xpp3Dom config = toConfig( "<wrapper><file>test</file></wrapper>" );
128
129 DefaultBeanConfigurationRequest request = new DefaultBeanConfigurationRequest();
130 request.setBean( bean ).setConfiguration( config, "wrapper" );
131
132 configurator.configureBean( request );
133
134 assertEquals( new File( "test" ), bean.file );
135 }
136
137 static class SomeBean
138 {
139
140 File file;
141
142 }
143
144 }