View Javadoc
1   package org.apache.maven.plugins.antrun;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.codehaus.plexus.configuration.PlexusConfiguration;
23  import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
24  import org.junit.jupiter.api.BeforeEach;
25  import org.junit.jupiter.api.Test;
26  import org.junit.jupiter.api.io.TempDir;
27  import org.xmlunit.builder.Input;
28  
29  import java.io.File;
30  import java.io.IOException;
31  import java.nio.file.Files;
32  import java.nio.file.Path;
33  
34  import static org.hamcrest.MatcherAssert.assertThat;
35  import static org.xmlunit.matchers.CompareMatcher.isIdenticalTo;
36  
37  /**
38   * Test class for {@link AntrunXmlPlexusConfigurationWriter}.
39   * @author gboue
40   */
41  public class AntrunXmlPlexusConfigurationWriterTest
42  {
43  
44      private static final String TARGET_NAME = "main";
45  
46      @TempDir
47      Path folder;
48  
49      private AntrunXmlPlexusConfigurationWriter configurationWriter;
50  
51      private PlexusConfiguration configuration;
52  
53      private File file;
54  
55      @BeforeEach
56      void setUp()
57          throws IOException
58      {
59          configurationWriter = new AntrunXmlPlexusConfigurationWriter();
60          configuration = new XmlPlexusConfiguration( "target" );
61          configuration.setAttribute( "name", TARGET_NAME );
62          file = Files.createTempFile(folder, "junit", "antrun").toFile();
63      }
64  
65      /**
66       * Tests that the XML file produced with the writer is pretty printed and that basic attributes are kept.
67       *
68       * @throws IOException In case of problems
69       */
70      @Test
71      public void testBasic() throws IOException
72      {
73          configuration.getChild( "echo", true ).setAttribute( "message", "Hello" );
74          configurationWriter.write( configuration, file, "", TARGET_NAME );
75          assertXmlIsExpected( "/configuration-writer/basic.xml", file );
76      }
77  
78      /**
79       * Tests that serialization is correct even if Ant target is empty (no children, no attributes except name).
80       *
81       * @throws IOException In case of problems
82       */
83      @Test
84      public void testEmptyTarget() throws IOException
85      {
86          configurationWriter.write( configuration, file, "", TARGET_NAME );
87          assertXmlIsExpected( "/configuration-writer/empty-target.xml", file );
88      }
89  
90      /**
91       * Tests that setting a custom prefix ends up in the project namespace in the target element with the correct name.
92       *
93       * @throws IOException In case of problems
94       */
95      @Test
96      public void testCustomTaskPrefix() throws IOException
97      {
98          PlexusConfiguration child = configuration.getChild( "mvn:foo", true );
99          child.setAttribute( "attr1", "val1" );
100         child.setValue( "The first value." );
101         child = configuration.getChild( "bar", true );
102         child.setAttribute( "attr2", "val2" );
103         child.setValue( "The second value." );
104         configurationWriter.write( configuration, file, "mvn", TARGET_NAME );
105         assertXmlIsExpected( "/configuration-writer/custom-task-prefix.xml", file );
106     }
107 
108     /**
109      * Tests that combine.children and combine.self attributes in the XML configuration elements are ignored during
110      * serialization.
111      *
112      * @throws IOException In case of problems
113      */
114     @Test
115     public void testCombineAttributes() throws IOException
116     {
117         configuration.setAttribute( "combine.children", "append" );
118         configuration.setAttribute( "description", "foo" );
119         configuration.getChild( "child", true ).setAttribute( "combine.self", "override" );
120         configurationWriter.write( configuration, file, "", TARGET_NAME );
121         assertXmlIsExpected( "/configuration-writer/combine-attributes.xml", file );
122     }
123 
124     private void assertXmlIsExpected( String expected, File file )
125     {
126         assertThat( Input.from( file ), isIdenticalTo( Input.from( getClass().getResourceAsStream( expected ) ) ) );
127     }
128 }