1 package org.apache.maven.model.profile.activation;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.api.model.Activation;
23 import org.apache.maven.api.model.ActivationFile;
24 import org.apache.maven.api.model.Profile;
25 import org.apache.maven.model.path.DefaultPathTranslator;
26 import org.apache.maven.model.path.ProfileActivationFilePathInterpolator;
27 import org.apache.maven.model.profile.DefaultProfileActivationContext;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.api.io.TempDir;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.nio.file.Path;
35
36 import static org.junit.jupiter.api.Assertions.assertEquals;
37
38
39
40
41
42
43 public class FileProfileActivatorTest extends AbstractProfileActivatorTest<FileProfileActivator>
44 {
45
46 @TempDir
47 Path tempDir;
48
49 private final DefaultProfileActivationContext context = new DefaultProfileActivationContext();
50
51 @BeforeEach
52 @Override
53 void setUp() throws Exception
54 {
55 activator = new FileProfileActivator( new ProfileActivationFilePathInterpolator( new DefaultPathTranslator() ) );
56
57 context.setProjectDirectory( new File( tempDir.toString() ) );
58
59 File file = new File( tempDir.resolve( "file.txt" ).toString() );
60 if ( !file.createNewFile() )
61 {
62 throw new IOException( "Can't create " + file );
63 }
64 }
65
66 @Test
67 public void testIsActiveNoFile()
68 {
69 assertActivation( false, newExistsProfile( null ), context );
70 assertActivation( false, newExistsProfile( "someFile.txt" ), context );
71 assertActivation( false, newExistsProfile( "${basedir}/someFile.txt" ), context );
72
73 assertActivation( false, newMissingProfile( null ), context );
74 assertActivation( true, newMissingProfile( "someFile.txt" ), context );
75 assertActivation( true, newMissingProfile( "${basedir}/someFile.txt" ), context );
76 }
77
78 @Test
79 public void testIsActiveExistsFileExists()
80 {
81 assertActivation( true, newExistsProfile( "file.txt" ), context );
82 assertActivation( true, newExistsProfile( "${basedir}" ), context );
83 assertActivation( true, newExistsProfile( "${basedir}/" + "file.txt" ), context );
84
85 assertActivation( false, newMissingProfile( "file.txt" ), context );
86 assertActivation( false, newMissingProfile( "${basedir}" ), context );
87 assertActivation( false, newMissingProfile( "${basedir}/" + "file.txt" ), context );
88 }
89
90 @Test
91 public void testIsActiveExistsLeavesFileUnchanged()
92 {
93 Profile profile = newExistsProfile( "file.txt" );
94 assertEquals( "file.txt", profile.getActivation().getFile().getExists() );
95
96 assertActivation( true, profile, context );
97
98 assertEquals( "file.txt", profile.getActivation().getFile().getExists() );
99 }
100
101 private Profile newExistsProfile( String filePath )
102 {
103 ActivationFile activationFile = ActivationFile.newBuilder()
104 .exists( filePath ).build();
105 return newProfile( activationFile );
106 }
107
108 private Profile newMissingProfile( String filePath )
109 {
110 ActivationFile activationFile = ActivationFile.newBuilder()
111 .missing( filePath ).build();
112 return newProfile( activationFile );
113 }
114
115 private Profile newProfile( ActivationFile activationFile )
116 {
117 Activation activation = Activation.newBuilder()
118 .file( activationFile ).build();
119 return Profile.newBuilder().activation( activation ).build();
120 }
121 }