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 e )
65 {
66 throw new IllegalArgumentException( e );
67 }
68 catch ( IOException e )
69 {
70 throw new IllegalArgumentException( e );
71 }
72 }
73
74 public void testMinimal()
75 throws BeanConfigurationException
76 {
77 SomeBean bean = new SomeBean();
78
79 Xpp3Dom config = toConfig( "<file>test</file>" );
80
81 DefaultBeanConfigurationRequest request = new DefaultBeanConfigurationRequest();
82 request.setBean( bean ).setConfiguration( config );
83
84 configurator.configureBean( request );
85
86 assertEquals( new File( "test" ), bean.file );
87 }
88
89 public void testPreAndPostProcessing()
90 throws BeanConfigurationException
91 {
92 SomeBean bean = new SomeBean();
93
94 Xpp3Dom config = toConfig( "<file>${test}</file>" );
95
96 BeanConfigurationValuePreprocessor preprocessor = new BeanConfigurationValuePreprocessor()
97 {
98 public Object preprocessValue( String value, Class<?> type )
99 throws BeanConfigurationException
100 {
101 if ( value != null && value.startsWith( "${" ) && value.endsWith( "}" ) )
102 {
103 return value.substring( 2, value.length() - 1 );
104 }
105 return value;
106 }
107 };
108
109 BeanConfigurationPathTranslator translator = new BeanConfigurationPathTranslator()
110 {
111 public File translatePath( File path )
112 {
113 return new File( "base", path.getPath() ).getAbsoluteFile();
114 }
115 };
116
117 DefaultBeanConfigurationRequest request = new DefaultBeanConfigurationRequest();
118 request.setBean( bean ).setConfiguration( config );
119 request.setValuePreprocessor( preprocessor ).setPathTranslator( translator );
120
121 configurator.configureBean( request );
122
123 assertEquals( new File( "base/test" ).getAbsoluteFile(), bean.file );
124 }
125
126 public void testChildConfigurationElement()
127 throws BeanConfigurationException
128 {
129 SomeBean bean = new SomeBean();
130
131 Xpp3Dom config = toConfig( "<wrapper><file>test</file></wrapper>" );
132
133 DefaultBeanConfigurationRequest request = new DefaultBeanConfigurationRequest();
134 request.setBean( bean ).setConfiguration( config, "wrapper" );
135
136 configurator.configureBean( request );
137
138 assertEquals( new File( "test" ), bean.file );
139 }
140
141 static class SomeBean
142 {
143
144 File file;
145
146 }
147
148 }