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.apache.maven.configuration.internal.DefaultBeanConfigurator;
26  import org.apache.maven.internal.xml.XmlNodeBuilder;
27  import org.codehaus.plexus.util.xml.Xpp3Dom;
28  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
29  import org.junit.jupiter.api.AfterEach;
30  import org.junit.jupiter.api.BeforeEach;
31  import org.junit.jupiter.api.Test;
32  
33  import static org.junit.jupiter.api.Assertions.assertEquals;
34  
35  /**
36   * @author Benjamin Bentmann
37   */
38  class DefaultBeanConfiguratorTest {
39  
40      private BeanConfigurator configurator;
41  
42      @BeforeEach
43      void setUp() throws Exception {
44          configurator = new DefaultBeanConfigurator();
45      }
46  
47      @AfterEach
48      void tearDown() throws Exception {
49          configurator = null;
50      }
51  
52      private Xpp3Dom toConfig(String xml) {
53          try {
54              return new Xpp3Dom(XmlNodeBuilder.build(new StringReader("<configuration>" + xml + "</configuration>")));
55          } catch (XmlPullParserException | IOException e) {
56              throw new IllegalArgumentException(e);
57          }
58      }
59  
60      @Test
61      void testMinimal() throws BeanConfigurationException {
62          SomeBean bean = new SomeBean();
63  
64          Xpp3Dom config = toConfig("<file>test</file>");
65  
66          DefaultBeanConfigurationRequest request = new DefaultBeanConfigurationRequest();
67          request.setBean(bean).setConfiguration(config);
68  
69          configurator.configureBean(request);
70  
71          assertEquals(new File("test"), bean.file);
72      }
73  
74      @Test
75      void testPreAndPostProcessing() throws BeanConfigurationException {
76          SomeBean bean = new SomeBean();
77  
78          Xpp3Dom config = toConfig("<file>${test}</file>");
79  
80          BeanConfigurationValuePreprocessor preprocessor = (value, type) -> {
81              if (value != null && value.startsWith("${") && value.endsWith("}")) {
82                  return value.substring(2, value.length() - 1);
83              }
84              return value;
85          };
86  
87          BeanConfigurationPathTranslator translator = path -> new File("base", path.getPath()).getAbsoluteFile();
88  
89          DefaultBeanConfigurationRequest request = new DefaultBeanConfigurationRequest();
90          request.setBean(bean).setConfiguration(config);
91          request.setValuePreprocessor(preprocessor).setPathTranslator(translator);
92  
93          configurator.configureBean(request);
94  
95          assertEquals(new File("base/test").getAbsoluteFile(), bean.file);
96      }
97  
98      @Test
99      void testChildConfigurationElement() throws BeanConfigurationException {
100         SomeBean bean = new SomeBean();
101 
102         Xpp3Dom config = toConfig("<wrapper><file>test</file></wrapper>");
103 
104         DefaultBeanConfigurationRequest request = new DefaultBeanConfigurationRequest();
105         request.setBean(bean).setConfiguration(config, "wrapper");
106 
107         configurator.configureBean(request);
108 
109         assertEquals(new File("test"), bean.file);
110     }
111 
112     static class SomeBean {
113 
114         File file;
115     }
116 }