View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author Benjamin Bentmann
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 }