1 package org.apache.maven.plugins.antrun;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
39
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
67
68
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
80
81
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
92
93
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
110
111
112
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 }