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.plugins.antrun;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  
26  import org.codehaus.plexus.configuration.PlexusConfiguration;
27  import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  import org.junit.jupiter.api.io.TempDir;
31  import org.xmlunit.builder.Input;
32  
33  import static org.hamcrest.MatcherAssert.assertThat;
34  import static org.xmlunit.matchers.CompareMatcher.isIdenticalTo;
35  
36  /**
37   * Test class for {@link AntrunXmlPlexusConfigurationWriter}.
38   * @author gboue
39   */
40  public class AntrunXmlPlexusConfigurationWriterTest {
41  
42      private static final String TARGET_NAME = "main";
43  
44      @TempDir
45      Path folder;
46  
47      private AntrunXmlPlexusConfigurationWriter configurationWriter;
48  
49      private PlexusConfiguration configuration;
50  
51      private File file;
52  
53      @BeforeEach
54      void setUp() throws IOException {
55          configurationWriter = new AntrunXmlPlexusConfigurationWriter();
56          configuration = new XmlPlexusConfiguration("target");
57          configuration.setAttribute("name", TARGET_NAME);
58          file = Files.createTempFile(folder, "junit", "antrun").toFile();
59      }
60  
61      /**
62       * Tests that the XML file produced with the writer is pretty printed and that basic attributes are kept.
63       *
64       * @throws IOException In case of problems
65       */
66      @Test
67      public void testBasic() throws IOException {
68          configuration.getChild("echo", true).setAttribute("message", "Hello");
69          configurationWriter.write(configuration, file, "", TARGET_NAME);
70          assertXmlIsExpected("/configuration-writer/basic.xml", file);
71      }
72  
73      /**
74       * Tests that serialization is correct even if Ant target is empty (no children, no attributes except name).
75       *
76       * @throws IOException In case of problems
77       */
78      @Test
79      public void testEmptyTarget() throws IOException {
80          configurationWriter.write(configuration, file, "", TARGET_NAME);
81          assertXmlIsExpected("/configuration-writer/empty-target.xml", file);
82      }
83  
84      /**
85       * Tests that setting a custom prefix ends up in the project namespace in the target element with the correct name.
86       *
87       * @throws IOException In case of problems
88       */
89      @Test
90      public void testCustomTaskPrefix() throws IOException {
91          PlexusConfiguration child = configuration.getChild("mvn:foo", true);
92          child.setAttribute("attr1", "val1");
93          child.setValue("The first value.");
94          child = configuration.getChild("bar", true);
95          child.setAttribute("attr2", "val2");
96          child.setValue("The second value.");
97          configurationWriter.write(configuration, file, "mvn", TARGET_NAME);
98          assertXmlIsExpected("/configuration-writer/custom-task-prefix.xml", file);
99      }
100 
101     /**
102      * Tests that combine.children and combine.self attributes in the XML configuration elements are ignored during
103      * serialization.
104      *
105      * @throws IOException In case of problems
106      */
107     @Test
108     public void testCombineAttributes() throws IOException {
109         configuration.setAttribute("combine.children", "append");
110         configuration.setAttribute("description", "foo");
111         configuration.getChild("child", true).setAttribute("combine.self", "override");
112         configurationWriter.write(configuration, file, "", TARGET_NAME);
113         assertXmlIsExpected("/configuration-writer/combine-attributes.xml", file);
114     }
115 
116     private void assertXmlIsExpected(String expected, File file) {
117         assertThat(Input.from(file), isIdenticalTo(Input.from(getClass().getResourceAsStream(expected))));
118     }
119 }