46
46
import static org .junit .jupiter .api .Assertions .assertNotNull ;
47
47
import static org .junit .jupiter .api .Assertions .assertThrows ;
48
48
import static org .mockito .Mockito .mock ;
49
+ import static org .mockito .Mockito .spy ;
49
50
import static org .mockito .Mockito .when ;
50
51
51
52
class DefaultPluginXmlFactoryReadWriteTest {
@@ -73,9 +74,9 @@ void readFromInputStreamParsesPluginDescriptorCorrectly() {
73
74
PluginDescriptor descriptor = defaultPluginXmlFactory .read (XmlReaderRequest .builder ()
74
75
.inputStream (new ByteArrayInputStream (SAMPLE_PLUGIN_XML .getBytes ()))
75
76
.build ());
76
- assertThat (descriptor .getName ()).isEqualTo (NAME );
77
- assertThat (descriptor .getGroupId ()).isEqualTo ("org.example" );
78
77
assertThat (descriptor .getArtifactId ()).isEqualTo ("sample-plugin" );
78
+ assertThat (descriptor .getGroupId ()).isEqualTo ("org.example" );
79
+ assertThat (descriptor .getName ()).isEqualTo (NAME );
79
80
assertThat (descriptor .getVersion ()).isEqualTo ("1.0.0" );
80
81
}
81
82
@@ -160,18 +161,18 @@ void toXmlStringGeneratesValidXml() {
160
161
.artifactId ("sample-plugin" )
161
162
.version ("1.0.0" )
162
163
.build ());
163
- assertThat (xml ).contains ("<name>" + NAME + "</name>" );
164
- assertThat (xml ).contains ("<groupId>org.example</groupId>" );
165
164
assertThat (xml ).contains ("<artifactId>sample-plugin</artifactId>" );
165
+ assertThat (xml ).contains ("<groupId>org.example</groupId>" );
166
+ assertThat (xml ).contains ("<name>" + NAME + "</name>" );
166
167
assertThat (xml ).contains ("<version>1.0.0</version>" );
167
168
}
168
169
169
170
@ Test
170
171
void staticFromXmlParsesValidXml () {
171
172
PluginDescriptor descriptor = DefaultPluginXmlFactory .fromXml (SAMPLE_PLUGIN_XML );
172
- assertThat (descriptor .getName ()).isEqualTo (NAME );
173
- assertThat (descriptor .getGroupId ()).isEqualTo ("org.example" );
174
173
assertThat (descriptor .getArtifactId ()).isEqualTo ("sample-plugin" );
174
+ assertThat (descriptor .getGroupId ()).isEqualTo ("org.example" );
175
+ assertThat (descriptor .getName ()).isEqualTo (NAME );
175
176
assertThat (descriptor .getVersion ()).isEqualTo ("1.0.0" );
176
177
}
177
178
@@ -184,22 +185,21 @@ void staticToXmlGeneratesValidXml() {
184
185
.version ("1.0.0" )
185
186
.build ());
186
187
assertThat (xml ).contains ("<name>" + NAME + "</name>" );
187
- assertThat (xml ).contains ("<name>" + NAME + "</name>" );
188
188
assertThat (xml ).contains ("<groupId>org.example</groupId>" );
189
189
assertThat (xml ).contains ("<artifactId>sample-plugin</artifactId>" );
190
190
assertThat (xml ).contains ("<version>1.0.0</version>" );
191
191
}
192
192
193
193
@ Test
194
194
void writeWithFailingWriterThrowsXmlWriterException () {
195
- String unableToWritePlugin = "Unable to write plugin" + randomUUID () ;
196
- String ioEx = "ioEx" + randomUUID () ;
195
+ String unableToWritePlugin = "Unable to write plugin: " + NAME ;
196
+ String ioEx = "ioEx: " + NAME ;
197
197
XmlWriterException exception = assertThatExceptionOfType (XmlWriterException .class )
198
198
.isThrownBy (() -> defaultPluginXmlFactory .write (XmlWriterRequest .<PluginDescriptor >builder ()
199
199
.writer (new Writer () {
200
200
@ Override
201
201
public void write (char [] cbuf , int off , int len ) {
202
- throw new XmlWriterException (unableToWritePlugin , null , new IOException (ioEx ));
202
+ throw new XmlWriterException (NAME , null , new IOException (ioEx ));
203
203
}
204
204
205
205
@ Override
@@ -213,10 +213,10 @@ public void close() {}
213
213
.build ())
214
214
.build ()))
215
215
.actual ();
216
- assertThat (exception .getMessage ()).contains (unableToWritePlugin );
217
216
assertThat (exception .getCause ()).isInstanceOf (XmlWriterException .class );
218
217
assertThat (exception .getCause ().getCause ().getMessage ()).isEqualTo (ioEx );
219
- assertThat (exception .getCause ().getMessage ()).isEqualTo (unableToWritePlugin );
218
+ assertThat (exception .getCause ().getMessage ()).isEqualTo (NAME );
219
+ assertThat (exception .getMessage ()).isEqualTo (unableToWritePlugin );
220
220
}
221
221
222
222
@ Test
@@ -239,8 +239,8 @@ void readMalformedXmlThrowsXmlReaderException() {
239
239
.inputStream (new ByteArrayInputStream ("<plugin><name>Broken Plugin" .getBytes ()))
240
240
.build ()))
241
241
.actual ();
242
- assertThat (exception .getMessage ()).contains ("Unable to read plugin" );
243
242
assertThat (exception .getCause ()).isInstanceOf (WstxEOFException .class );
243
+ assertThat (exception .getMessage ()).contains ("Unable to read plugin" );
244
244
}
245
245
246
246
@ Test
@@ -258,19 +258,19 @@ void readFromUrlParsesPluginDescriptorCorrectly() throws Exception {
258
258
PluginDescriptor descriptor = defaultPluginXmlFactory .read (XmlReaderRequest .builder ()
259
259
.inputStream (xmlFile .toUri ().toURL ().openStream ())
260
260
.build ());
261
- assertThat (descriptor .getName ()).isEqualTo (NAME );
262
- assertThat (descriptor .getGroupId ()).isEqualTo ("org.example" );
263
261
assertThat (descriptor .getArtifactId ()).isEqualTo ("sample-plugin" );
262
+ assertThat (descriptor .getGroupId ()).isEqualTo ("org.example" );
263
+ assertThat (descriptor .getName ()).isEqualTo (NAME );
264
264
assertThat (descriptor .getVersion ()).isEqualTo ("1.0.0" );
265
265
}
266
266
267
267
@ Test
268
268
void testReadWithPath () throws Exception {
269
269
Path tempPath = Files .createTempFile ("plugin" , ".xml" );
270
270
Files .writeString (tempPath , "<plugin/>" );
271
- XmlReaderRequest request = mock (XmlReaderRequest .class );
272
- when (request .getPath ()).thenReturn (tempPath );
271
+ XmlReaderRequest request = spy (XmlReaderRequest .class );
273
272
when (request .getInputStream ()).thenReturn (null );
273
+ when (request .getPath ()).thenReturn (tempPath );
274
274
when (request .getReader ()).thenReturn (null );
275
275
when (request .getURL ()).thenReturn (null );
276
276
when (request .isAddDefaultEntities ()).thenReturn (false );
@@ -283,9 +283,9 @@ void testReadWithPath() throws Exception {
283
283
void testReadWithPathUrlDefault () throws Exception {
284
284
Path tempPath = Files .createTempFile ("plugin" , ".xml" );
285
285
Files .writeString (tempPath , "<plugin/>" );
286
- XmlReaderRequest request = mock (XmlReaderRequest .class );
287
- when (request .getPath ()).thenReturn (null );
286
+ XmlReaderRequest request = spy (XmlReaderRequest .class );
288
287
when (request .getInputStream ()).thenReturn (null );
288
+ when (request .getPath ()).thenReturn (null );
289
289
when (request .getReader ()).thenReturn (null );
290
290
when (request .getURL ()).thenReturn (tempPath .toUri ().toURL ());
291
291
when (request .isAddDefaultEntities ()).thenReturn (false );
0 commit comments