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