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