1+ package org .apache .maven .impl ;
2+
3+ import org .apache .maven .api .services .xml .XmlReaderRequest ;
4+ import org .apache .maven .api .services .xml .XmlWriterRequest ;
5+ import org .apache .maven .api .plugin .descriptor .PluginDescriptor ;
6+ import org .junit .jupiter .api .BeforeEach ;
7+ import org .junit .jupiter .api .Test ;
8+ import org .junit .jupiter .api .io .TempDir ;
9+
10+ import java .io .ByteArrayInputStream ;
11+ import java .io .ByteArrayOutputStream ;
12+ import java .io .StringReader ;
13+ import java .io .StringWriter ;
14+ import java .nio .file .Files ;
15+ import java .nio .file .Path ;
16+
17+ import static org .junit .jupiter .api .Assertions .assertEquals ;
18+ import static org .junit .jupiter .api .Assertions .assertThrows ;
19+ import static org .junit .jupiter .api .Assertions .assertTrue ;
20+
21+ class DefaultPluginXmlFactoryTest {
22+
23+ private DefaultPluginXmlFactory sut ;
24+ private static final String SAMPLE_PLUGIN_XML = """
25+ <?xml version="1.0" encoding="UTF-8"?>
26+ <plugin>
27+ <name>Sample Plugin</name>
28+ <groupId>org.example</groupId>
29+ <artifactId>sample-plugin</artifactId>
30+ <version>1.0.0</version>
31+ </plugin>
32+ """ ;
33+
34+ @ BeforeEach
35+ void setUp () {
36+ sut = new DefaultPluginXmlFactory ();
37+ }
38+
39+ @ Test
40+ void shouldReadFromInputStream () {
41+ PluginDescriptor descriptor = sut .read (XmlReaderRequest .builder ()
42+ .inputStream (new ByteArrayInputStream (SAMPLE_PLUGIN_XML .getBytes ()))
43+ .build ());
44+ assertEquals ("Sample Plugin" , descriptor .getName ());
45+ assertEquals ("org.example" , descriptor .getGroupId ());
46+ assertEquals ("sample-plugin" , descriptor .getArtifactId ());
47+ assertEquals ("1.0.0" , descriptor .getVersion ());
48+ }
49+
50+ @ Test
51+ void shouldReadFromReader () {
52+ assertEquals ("Sample Plugin" , sut .read (XmlReaderRequest .builder ()
53+ .reader (new StringReader (SAMPLE_PLUGIN_XML ))
54+ .build ()).getName ());
55+ }
56+
57+ @ Test
58+ void shouldReadFromPath (@ TempDir Path tempDir ) throws Exception {
59+ Path xmlFile = tempDir .resolve ("plugin.xml" );
60+ Files .write (xmlFile , SAMPLE_PLUGIN_XML .getBytes ());
61+ assertEquals ("Sample Plugin" , sut .read (XmlReaderRequest .builder ()
62+ .path (xmlFile )
63+ .build ()).getName ());
64+ }
65+
66+ @ Test
67+ void shouldThrowExceptionOnInvalidInput () {
68+ assertThrows (IllegalArgumentException .class , () -> sut .read (XmlReaderRequest .builder ().build ()));
69+ }
70+
71+ @ Test
72+ void shouldWriteToWriter () {
73+ StringWriter writer = new StringWriter ();
74+ sut .write (XmlWriterRequest .<PluginDescriptor >builder ()
75+ .writer (writer )
76+ .content (PluginDescriptor .newBuilder ()
77+ .name ("Sample Plugin" )
78+ .groupId ("org.example" )
79+ .artifactId ("sample-plugin" )
80+ .version ("1.0.0" )
81+ .build ())
82+ .build ());
83+ String output = writer .toString ();
84+ assertTrue (output .contains ("<name>Sample Plugin</name>" ));
85+ assertTrue (output .contains ("<groupId>org.example</groupId>" ));
86+ }
87+
88+ @ Test
89+ void shouldWriteToOutputStream () {
90+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
91+ sut .write (XmlWriterRequest .<PluginDescriptor >builder ()
92+ .outputStream (outputStream )
93+ .content (PluginDescriptor .newBuilder ()
94+ .name ("Sample Plugin" )
95+ .build ())
96+ .build ());
97+ assertTrue (outputStream .toString ().contains ("<name>Sample Plugin</name>" ));
98+ }
99+
100+ @ Test
101+ void shouldWriteToPath (@ TempDir Path tempDir ) throws Exception {
102+ Path xmlFile = tempDir .resolve ("output-plugin.xml" );
103+ sut .write (XmlWriterRequest .<PluginDescriptor >builder ()
104+ .path (xmlFile )
105+ .content (PluginDescriptor .newBuilder ()
106+ .name ("Sample Plugin" )
107+ .build ())
108+ .build ());
109+ assertTrue (Files .readString (xmlFile ).contains ("<name>Sample Plugin</name>" ));
110+ }
111+
112+ @ Test
113+ void shouldParseValidXmlString () {
114+ PluginDescriptor descriptor = sut .fromXmlString (SAMPLE_PLUGIN_XML );
115+ assertEquals ("Sample Plugin" , descriptor .getName ());
116+ assertEquals ("org.example" , descriptor .getGroupId ());
117+ assertEquals ("sample-plugin" , descriptor .getArtifactId ());
118+ assertEquals ("1.0.0" , descriptor .getVersion ());
119+ }
120+
121+ @ Test
122+ void shouldGenerateValidXmlString () {
123+ String xml = sut .toXmlString (PluginDescriptor .newBuilder ()
124+ .name ("Sample Plugin" )
125+ .groupId ("org.example" )
126+ .artifactId ("sample-plugin" )
127+ .version ("1.0.0" )
128+ .build ());
129+ assertTrue (xml .contains ("<name>Sample Plugin</name>" ));
130+ assertTrue (xml .contains ("<groupId>org.example</groupId>" ));
131+ assertTrue (xml .contains ("<artifactId>sample-plugin</artifactId>" ));
132+ assertTrue (xml .contains ("<version>1.0.0</version>" ));
133+ }
134+ }
0 commit comments