19
19
package org .apache .maven .plugins .dependency .utils ;
20
20
21
21
import java .io .File ;
22
- import java .util .ArrayList ;
23
- import java .util .List ;
24
- import java .util .Random ;
22
+ import java .io .IOException ;
25
23
26
- import junit .framework .TestCase ;
27
24
import org .apache .maven .artifact .Artifact ;
28
25
import org .apache .maven .artifact .DefaultArtifact ;
29
26
import org .apache .maven .artifact .handler .ArtifactHandler ;
30
27
import org .apache .maven .artifact .versioning .VersionRange ;
31
- import org .apache .maven .plugin .logging .Log ;
32
- import org .apache .maven .plugin .testing .SilentLog ;
33
28
import org .apache .maven .plugin .testing .stubs .DefaultArtifactHandlerStub ;
29
+ import org .junit .jupiter .api .BeforeEach ;
30
+ import org .junit .jupiter .api .Test ;
31
+ import org .junit .jupiter .api .io .TempDir ;
32
+
33
+ import static org .assertj .core .api .Assertions .assertThat ;
34
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
35
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
34
36
35
37
/**
36
38
* @author brianf
37
39
*/
38
- public class TestDependencyUtil extends TestCase {
39
- List <Artifact > artifacts = new ArrayList <>();
40
+ class TestDependencyUtil {
40
41
41
- Log log = new SilentLog ();
42
+ private static final String TEST_CONTENT =
43
+ "Test line 1" + System .lineSeparator () + "Test line 2" + System .lineSeparator ();
42
44
43
- File outputFolder ;
45
+ @ TempDir
46
+ File temDir ;
44
47
45
48
Artifact snap ;
46
49
@@ -50,40 +53,31 @@ public class TestDependencyUtil extends TestCase {
50
53
51
54
Artifact sources ;
52
55
53
- @ Override
54
- protected void setUp () throws Exception {
55
- super .setUp ();
56
+ @ BeforeEach
57
+ protected void setUp () {
56
58
57
59
ArtifactHandler ah = new DefaultArtifactHandlerStub ("jar" , null );
58
60
VersionRange vr = VersionRange .createFromVersion ("1.1" );
59
61
release = new DefaultArtifact ("test" , "one" , vr , Artifact .SCOPE_COMPILE , "jar" , "sources" , ah , false );
60
- artifacts .add (release );
61
62
62
63
ah = new DefaultArtifactHandlerStub ("war" , null );
63
64
vr = VersionRange .createFromVersion ("1.1-SNAPSHOT" );
64
65
snap = new DefaultArtifact ("test" , "two" , vr , Artifact .SCOPE_PROVIDED , "war" , null , ah , false );
65
- artifacts .add (snap );
66
66
67
67
ah = new DefaultArtifactHandlerStub ("war" , null );
68
68
vr = VersionRange .createFromVersion ("1.1-SNAPSHOT" );
69
69
snapResolvedVersion = new DefaultArtifact ("test" , "three" , vr , Artifact .SCOPE_PROVIDED , "war" , null , ah , false );
70
70
snapResolvedVersion .setResolvedVersion ("1.1-20121003.035531-117" );
71
- artifacts .add (snapResolvedVersion );
72
71
73
72
ah = new DefaultArtifactHandlerStub ("war" , null );
74
73
vr = VersionRange .createFromVersion ("1.1-SNAPSHOT" );
75
74
sources = new DefaultArtifact ("test" , "two" , vr , Artifact .SCOPE_PROVIDED , "sources" , "sources" , ah , false );
76
-
77
- // pick random output location
78
- Random a = new Random ();
79
- outputFolder = new File ("target/copy" + a .nextLong () + "/" );
80
- outputFolder .delete ();
81
- assertFalse (outputFolder .exists ());
82
75
}
83
76
84
- public void testDirectoryName () {
77
+ @ Test
78
+ void testDirectoryName () {
85
79
File folder = new File ("target/a" );
86
- final Artifact artifact = artifacts . get ( 0 ) ;
80
+ final Artifact artifact = release ;
87
81
File name =
88
82
DependencyUtil .getFormattedOutputDirectory (false , false , false , false , false , false , folder , artifact );
89
83
// object is the same.
@@ -153,9 +147,10 @@ public void testDirectoryName() {
153
147
assertEquals (expectedResult , name .getAbsolutePath ());
154
148
}
155
149
156
- public void testDirectoryName2 () {
150
+ @ Test
151
+ void testDirectoryName2 () {
157
152
File folder = new File ("target/a" );
158
- final Artifact artifact = artifacts . get ( 1 ) ;
153
+ final Artifact artifact = snap ;
159
154
File name =
160
155
DependencyUtil .getFormattedOutputDirectory (false , false , false , false , false , false , folder , artifact );
161
156
// object is the same.
@@ -205,7 +200,8 @@ public void testDirectoryName2() {
205
200
assertEquals (expectedResult , name .getAbsolutePath ());
206
201
}
207
202
208
- public void testDirectoryNameSources () {
203
+ @ Test
204
+ void testDirectoryNameSources () {
209
205
File folder = new File ("target/a" );
210
206
File name = DependencyUtil .getFormattedOutputDirectory (false , false , true , false , true , false , folder , sources );
211
207
String expectedResult = folder .getAbsolutePath () + File .separatorChar + "two-sources" ;
@@ -224,8 +220,9 @@ public void testDirectoryNameSources() {
224
220
assertEquals (expectedResult , name .getAbsolutePath ());
225
221
}
226
222
227
- public void testFileName () {
228
- Artifact artifact = artifacts .get (0 );
223
+ @ Test
224
+ void testFileName () {
225
+ Artifact artifact = release ;
229
226
230
227
String name = DependencyUtil .getFormattedFileName (artifact , false );
231
228
String expectedResult = "one-1.1-sources.jar" ;
@@ -235,7 +232,8 @@ public void testFileName() {
235
232
assertEquals (expectedResult , name );
236
233
}
237
234
238
- public void testFileNameUseBaseVersion () {
235
+ @ Test
236
+ void testFileNameUseBaseVersion () {
239
237
Artifact artifact = snapResolvedVersion ;
240
238
241
239
String name = DependencyUtil .getFormattedFileName (artifact , false , false , true );
@@ -246,7 +244,8 @@ public void testFileNameUseBaseVersion() {
246
244
assertEquals (expectedResult , name );
247
245
}
248
246
249
- public void testTestJar () {
247
+ @ Test
248
+ void testTestJar () {
250
249
ArtifactHandler ah = new DefaultArtifactHandlerStub ("test-jar" , null );
251
250
VersionRange vr = VersionRange .createFromVersion ("1.1-SNAPSHOT" );
252
251
Artifact artifact =
@@ -257,7 +256,8 @@ public void testTestJar() {
257
256
assertEquals (expectedResult , name );
258
257
}
259
258
260
- public void testFileNameClassifier () {
259
+ @ Test
260
+ void testFileNameClassifier () {
261
261
ArtifactHandler ah = new DefaultArtifactHandlerStub ("jar" , "sources" );
262
262
VersionRange vr = VersionRange .createFromVersion ("1.1-SNAPSHOT" );
263
263
Artifact artifact =
@@ -282,7 +282,8 @@ public void testFileNameClassifier() {
282
282
assertEquals (expectedResult , name );
283
283
}
284
284
285
- public void testFileNameClassifierWithFile () {
285
+ @ Test
286
+ void testFileNameClassifierWithFile () {
286
287
// specifically testing the default operation that getFormattedFileName
287
288
// returns
288
289
// the actual name of the file if available unless remove version is
@@ -319,7 +320,8 @@ public void testFileNameClassifierWithFile() {
319
320
assertEquals (expectedResult , name );
320
321
}
321
322
322
- public void testTokenizer () {
323
+ @ Test
324
+ void testTokenizer () {
323
325
String [] tokens = DependencyUtil .tokenizer (" alpha,bravo, charlie , delta kappa, theta" );
324
326
String [] expected = new String [] {"alpha" , "bravo" , "charlie" , "delta kappa" , "theta" };
325
327
// easier to see in the JUnit reports
@@ -337,4 +339,28 @@ public void testTokenizer() {
337
339
tokens = DependencyUtil .tokenizer (" " );
338
340
assertEquals (0 , tokens .length );
339
341
}
342
+
343
+ @ Test
344
+ void outputFileShouldBeOverridden () throws IOException {
345
+ File file = new File (temDir , "file1.out" );
346
+ assertThat (file ).doesNotExist ();
347
+
348
+ DependencyUtil .write (TEST_CONTENT , file , false , "UTF-8" );
349
+ assertThat (file ).hasContent (TEST_CONTENT );
350
+
351
+ DependencyUtil .write (TEST_CONTENT , file , false , "UTF-8" );
352
+ assertThat (file ).hasContent (TEST_CONTENT );
353
+ }
354
+
355
+ @ Test
356
+ void outputFileShouldBeAppended () throws IOException {
357
+ File file = new File (temDir , "file2.out" );
358
+ assertThat (file ).doesNotExist ();
359
+
360
+ DependencyUtil .write (TEST_CONTENT , file , true , "UTF-8" );
361
+ assertThat (file ).hasContent (TEST_CONTENT );
362
+
363
+ DependencyUtil .write (TEST_CONTENT , file , true , "UTF-8" );
364
+ assertThat (file ).hasContent (TEST_CONTENT + TEST_CONTENT );
365
+ }
340
366
}
0 commit comments