View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.model.profile.activation;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.nio.file.Path;
24  
25  import org.apache.maven.api.model.Activation;
26  import org.apache.maven.api.model.ActivationFile;
27  import org.apache.maven.api.model.Profile;
28  import org.apache.maven.model.path.DefaultPathTranslator;
29  import org.apache.maven.model.path.ProfileActivationFilePathInterpolator;
30  import org.apache.maven.model.profile.DefaultProfileActivationContext;
31  import org.apache.maven.model.root.RootLocator;
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Test;
34  import org.junit.jupiter.api.io.TempDir;
35  
36  import static org.junit.jupiter.api.Assertions.assertEquals;
37  import static org.junit.jupiter.api.Assertions.assertThrows;
38  
39  /**
40   * Tests {@link FileProfileActivator}.
41   *
42   */
43  class FileProfileActivatorTest extends AbstractProfileActivatorTest<FileProfileActivator> {
44  
45      @TempDir
46      Path tempDir;
47  
48      private final DefaultProfileActivationContext context = new DefaultProfileActivationContext();
49  
50      @BeforeEach
51      @Override
52      void setUp() throws Exception {
53          activator = new FileProfileActivator(
54                  new ProfileActivationFilePathInterpolator(new DefaultPathTranslator(), bd -> true));
55  
56          context.setProjectDirectory(tempDir.toFile());
57  
58          File file = new File(tempDir.resolve("file.txt").toString());
59          if (!file.createNewFile()) {
60              throw new IOException("Can't create " + file);
61          }
62      }
63  
64      @Test
65      void testRootDirectoryWithNull() {
66          context.setProjectDirectory(null);
67  
68          IllegalStateException e = assertThrows(
69                  IllegalStateException.class,
70                  () -> assertActivation(false, newExistsProfile("${project.rootDirectory}"), context));
71          assertEquals(RootLocator.UNABLE_TO_FIND_ROOT_PROJECT_MESSAGE, e.getMessage());
72      }
73  
74      @Test
75      void testRootDirectory() {
76          assertActivation(false, newExistsProfile("${project.rootDirectory}/someFile.txt"), context);
77          assertActivation(true, newMissingProfile("${project.rootDirectory}/someFile.txt"), context);
78          assertActivation(true, newExistsProfile("${project.rootDirectory}"), context);
79          assertActivation(true, newExistsProfile("${project.rootDirectory}/" + "file.txt"), context);
80          assertActivation(false, newMissingProfile("${project.rootDirectory}"), context);
81          assertActivation(false, newMissingProfile("${project.rootDirectory}/" + "file.txt"), context);
82      }
83  
84      @Test
85      void testIsActiveNoFileWithShortBasedir() {
86          assertActivation(false, newExistsProfile(null), context);
87          assertActivation(false, newExistsProfile("someFile.txt"), context);
88          assertActivation(false, newExistsProfile("${basedir}/someFile.txt"), context);
89  
90          assertActivation(false, newMissingProfile(null), context);
91          assertActivation(true, newMissingProfile("someFile.txt"), context);
92          assertActivation(true, newMissingProfile("${basedir}/someFile.txt"), context);
93      }
94  
95      @Test
96      void testIsActiveNoFile() {
97          assertActivation(false, newExistsProfile(null), context);
98          assertActivation(false, newExistsProfile("someFile.txt"), context);
99          assertActivation(false, newExistsProfile("${project.basedir}/someFile.txt"), context);
100 
101         assertActivation(false, newMissingProfile(null), context);
102         assertActivation(true, newMissingProfile("someFile.txt"), context);
103         assertActivation(true, newMissingProfile("${project.basedir}/someFile.txt"), context);
104     }
105 
106     @Test
107     void testIsActiveExistsFileExists() {
108         assertActivation(true, newExistsProfile("file.txt"), context);
109         assertActivation(true, newExistsProfile("${project.basedir}"), context);
110         assertActivation(true, newExistsProfile("${project.basedir}/" + "file.txt"), context);
111 
112         assertActivation(false, newMissingProfile("file.txt"), context);
113         assertActivation(false, newMissingProfile("${project.basedir}"), context);
114         assertActivation(false, newMissingProfile("${project.basedir}/" + "file.txt"), context);
115     }
116 
117     @Test
118     void testIsActiveExistsLeavesFileUnchanged() {
119         Profile profile = newExistsProfile("file.txt");
120         assertEquals("file.txt", profile.getActivation().getFile().getExists());
121 
122         assertActivation(true, profile, context);
123 
124         assertEquals("file.txt", profile.getActivation().getFile().getExists());
125     }
126 
127     private Profile newExistsProfile(String filePath) {
128         ActivationFile activationFile =
129                 ActivationFile.newBuilder().exists(filePath).build();
130         return newProfile(activationFile);
131     }
132 
133     private Profile newMissingProfile(String filePath) {
134         ActivationFile activationFile =
135                 ActivationFile.newBuilder().missing(filePath).build();
136         return newProfile(activationFile);
137     }
138 
139     private Profile newProfile(ActivationFile activationFile) {
140         Activation activation = Activation.newBuilder().file(activationFile).build();
141         return Profile.newBuilder().activation(activation).build();
142     }
143 }