001 package org.apache.maven.configuration;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import java.io.File;
023 import java.io.IOException;
024 import java.io.StringReader;
025
026 import org.codehaus.plexus.PlexusTestCase;
027 import org.codehaus.plexus.util.xml.Xpp3Dom;
028 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
029 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
030
031 /**
032 * @author Benjamin Bentmann
033 */
034 public class DefaultBeanConfiguratorTest
035 extends PlexusTestCase
036 {
037
038 private BeanConfigurator configurator;
039
040 @Override
041 protected void setUp()
042 throws Exception
043 {
044 super.setUp();
045
046 configurator = lookup( BeanConfigurator.class );
047 }
048
049 @Override
050 protected void tearDown()
051 throws Exception
052 {
053 configurator = null;
054
055 super.tearDown();
056 }
057
058 private Xpp3Dom toConfig( String xml )
059 {
060 try
061 {
062 return Xpp3DomBuilder.build( new StringReader( "<configuration>" + xml + "</configuration>" ) );
063 }
064 catch ( XmlPullParserException e )
065 {
066 throw new IllegalArgumentException( e );
067 }
068 catch ( IOException e )
069 {
070 throw new IllegalArgumentException( e );
071 }
072 }
073
074 public void testMinimal()
075 throws BeanConfigurationException
076 {
077 SomeBean bean = new SomeBean();
078
079 Xpp3Dom config = toConfig( "<file>test</file>" );
080
081 DefaultBeanConfigurationRequest request = new DefaultBeanConfigurationRequest();
082 request.setBean( bean ).setConfiguration( config );
083
084 configurator.configureBean( request );
085
086 assertEquals( new File( "test" ), bean.file );
087 }
088
089 public void testPreAndPostProcessing()
090 throws BeanConfigurationException
091 {
092 SomeBean bean = new SomeBean();
093
094 Xpp3Dom config = toConfig( "<file>${test}</file>" );
095
096 BeanConfigurationValuePreprocessor preprocessor = new BeanConfigurationValuePreprocessor()
097 {
098 public Object preprocessValue( String value, Class<?> type )
099 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 }