1919package org .apache .maven .plugins .dependency .utils ;
2020
2121import java .io .File ;
22- import java .util .ArrayList ;
23- import java .util .List ;
24- import java .util .Random ;
22+ import java .io .IOException ;
2523
26- import junit .framework .TestCase ;
2724import org .apache .maven .artifact .Artifact ;
2825import org .apache .maven .artifact .DefaultArtifact ;
2926import org .apache .maven .artifact .handler .ArtifactHandler ;
3027import org .apache .maven .artifact .versioning .VersionRange ;
31- import org .apache .maven .plugin .logging .Log ;
32- import org .apache .maven .plugin .testing .SilentLog ;
3328import 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 ;
3436
3537/**
3638 * @author brianf
3739 */
38- public class TestDependencyUtil extends TestCase {
39- List <Artifact > artifacts = new ArrayList <>();
40+ class TestDependencyUtil {
4041
41- Log log = new SilentLog ();
42+ private static final String TEST_CONTENT =
43+ "Test line 1" + System .lineSeparator () + "Test line 2" + System .lineSeparator ();
4244
43- File outputFolder ;
45+ @ TempDir
46+ File temDir ;
4447
4548 Artifact snap ;
4649
@@ -50,40 +53,31 @@ public class TestDependencyUtil extends TestCase {
5053
5154 Artifact sources ;
5255
53- @ Override
54- protected void setUp () throws Exception {
55- super .setUp ();
56+ @ BeforeEach
57+ protected void setUp () {
5658
5759 ArtifactHandler ah = new DefaultArtifactHandlerStub ("jar" , null );
5860 VersionRange vr = VersionRange .createFromVersion ("1.1" );
5961 release = new DefaultArtifact ("test" , "one" , vr , Artifact .SCOPE_COMPILE , "jar" , "sources" , ah , false );
60- artifacts .add (release );
6162
6263 ah = new DefaultArtifactHandlerStub ("war" , null );
6364 vr = VersionRange .createFromVersion ("1.1-SNAPSHOT" );
6465 snap = new DefaultArtifact ("test" , "two" , vr , Artifact .SCOPE_PROVIDED , "war" , null , ah , false );
65- artifacts .add (snap );
6666
6767 ah = new DefaultArtifactHandlerStub ("war" , null );
6868 vr = VersionRange .createFromVersion ("1.1-SNAPSHOT" );
6969 snapResolvedVersion = new DefaultArtifact ("test" , "three" , vr , Artifact .SCOPE_PROVIDED , "war" , null , ah , false );
7070 snapResolvedVersion .setResolvedVersion ("1.1-20121003.035531-117" );
71- artifacts .add (snapResolvedVersion );
7271
7372 ah = new DefaultArtifactHandlerStub ("war" , null );
7473 vr = VersionRange .createFromVersion ("1.1-SNAPSHOT" );
7574 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 ());
8275 }
8376
84- public void testDirectoryName () {
77+ @ Test
78+ void testDirectoryName () {
8579 File folder = new File ("target/a" );
86- final Artifact artifact = artifacts . get ( 0 ) ;
80+ final Artifact artifact = release ;
8781 File name =
8882 DependencyUtil .getFormattedOutputDirectory (false , false , false , false , false , false , folder , artifact );
8983 // object is the same.
@@ -153,9 +147,10 @@ public void testDirectoryName() {
153147 assertEquals (expectedResult , name .getAbsolutePath ());
154148 }
155149
156- public void testDirectoryName2 () {
150+ @ Test
151+ void testDirectoryName2 () {
157152 File folder = new File ("target/a" );
158- final Artifact artifact = artifacts . get ( 1 ) ;
153+ final Artifact artifact = snap ;
159154 File name =
160155 DependencyUtil .getFormattedOutputDirectory (false , false , false , false , false , false , folder , artifact );
161156 // object is the same.
@@ -205,7 +200,8 @@ public void testDirectoryName2() {
205200 assertEquals (expectedResult , name .getAbsolutePath ());
206201 }
207202
208- public void testDirectoryNameSources () {
203+ @ Test
204+ void testDirectoryNameSources () {
209205 File folder = new File ("target/a" );
210206 File name = DependencyUtil .getFormattedOutputDirectory (false , false , true , false , true , false , folder , sources );
211207 String expectedResult = folder .getAbsolutePath () + File .separatorChar + "two-sources" ;
@@ -224,8 +220,9 @@ public void testDirectoryNameSources() {
224220 assertEquals (expectedResult , name .getAbsolutePath ());
225221 }
226222
227- public void testFileName () {
228- Artifact artifact = artifacts .get (0 );
223+ @ Test
224+ void testFileName () {
225+ Artifact artifact = release ;
229226
230227 String name = DependencyUtil .getFormattedFileName (artifact , false );
231228 String expectedResult = "one-1.1-sources.jar" ;
@@ -235,7 +232,8 @@ public void testFileName() {
235232 assertEquals (expectedResult , name );
236233 }
237234
238- public void testFileNameUseBaseVersion () {
235+ @ Test
236+ void testFileNameUseBaseVersion () {
239237 Artifact artifact = snapResolvedVersion ;
240238
241239 String name = DependencyUtil .getFormattedFileName (artifact , false , false , true );
@@ -246,7 +244,8 @@ public void testFileNameUseBaseVersion() {
246244 assertEquals (expectedResult , name );
247245 }
248246
249- public void testTestJar () {
247+ @ Test
248+ void testTestJar () {
250249 ArtifactHandler ah = new DefaultArtifactHandlerStub ("test-jar" , null );
251250 VersionRange vr = VersionRange .createFromVersion ("1.1-SNAPSHOT" );
252251 Artifact artifact =
@@ -257,7 +256,8 @@ public void testTestJar() {
257256 assertEquals (expectedResult , name );
258257 }
259258
260- public void testFileNameClassifier () {
259+ @ Test
260+ void testFileNameClassifier () {
261261 ArtifactHandler ah = new DefaultArtifactHandlerStub ("jar" , "sources" );
262262 VersionRange vr = VersionRange .createFromVersion ("1.1-SNAPSHOT" );
263263 Artifact artifact =
@@ -282,7 +282,8 @@ public void testFileNameClassifier() {
282282 assertEquals (expectedResult , name );
283283 }
284284
285- public void testFileNameClassifierWithFile () {
285+ @ Test
286+ void testFileNameClassifierWithFile () {
286287 // specifically testing the default operation that getFormattedFileName
287288 // returns
288289 // the actual name of the file if available unless remove version is
@@ -319,7 +320,8 @@ public void testFileNameClassifierWithFile() {
319320 assertEquals (expectedResult , name );
320321 }
321322
322- public void testTokenizer () {
323+ @ Test
324+ void testTokenizer () {
323325 String [] tokens = DependencyUtil .tokenizer (" alpha,bravo, charlie , delta kappa, theta" );
324326 String [] expected = new String [] {"alpha" , "bravo" , "charlie" , "delta kappa" , "theta" };
325327 // easier to see in the JUnit reports
@@ -337,4 +339,28 @@ public void testTokenizer() {
337339 tokens = DependencyUtil .tokenizer (" " );
338340 assertEquals (0 , tokens .length );
339341 }
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+ }
340366}
0 commit comments