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.Files;
24  import java.nio.file.Path;
25  
26  import org.apache.maven.model.Activation;
27  import org.apache.maven.model.ActivationFile;
28  import org.apache.maven.model.Profile;
29  import org.apache.maven.model.path.DefaultPathTranslator;
30  import org.apache.maven.model.path.ProfileActivationFilePathInterpolator;
31  import org.apache.maven.model.profile.DefaultProfileActivationContext;
32  import org.junit.Before;
33  import org.junit.Test;
34  
35  /**
36   * Tests {@link FileProfileActivator}.
37   *
38   * @author Ravil Galeyev
39   */
40  public class FileProfileActivatorTest extends AbstractProfileActivatorTest<FileProfileActivator> {
41      Path tempDir;
42  
43      private final DefaultProfileActivationContext context = new DefaultProfileActivationContext();
44  
45      public FileProfileActivatorTest() {
46          super(FileProfileActivator.class);
47      }
48  
49      @Before
50      public void setUp() throws Exception {
51          super.setUp();
52  
53          tempDir = Files.createTempDirectory(null);
54  
55          activator.setProfileActivationFilePathInterpolator(
56                  new ProfileActivationFilePathInterpolator().setPathTranslator(new DefaultPathTranslator()));
57  
58          context.setProjectDirectory(new File(tempDir.toString()));
59  
60          File file = new File(tempDir.resolve("file.txt").toString());
61          if (!file.createNewFile()) {
62              throw new IOException("Can't create " + file);
63          }
64      }
65  
66      @Test
67      public void testIsActiveNoFile() {
68          assertActivation(false, newExistsProfile(null), context);
69          assertActivation(false, newExistsProfile("someFile.txt"), context);
70          assertActivation(false, newExistsProfile("${basedir}/someFile.txt"), context);
71  
72          assertActivation(false, newMissingProfile(null), context);
73          assertActivation(true, newMissingProfile("someFile.txt"), context);
74          assertActivation(true, newMissingProfile("${basedir}/someFile.txt"), context);
75      }
76  
77      @Test
78      public void testIsActiveExistsFileExists() {
79          assertActivation(true, newExistsProfile("file.txt"), context);
80          assertActivation(true, newExistsProfile("${basedir}"), context);
81          assertActivation(true, newExistsProfile("${basedir}/" + "file.txt"), context);
82  
83          assertActivation(false, newMissingProfile("file.txt"), context);
84          assertActivation(false, newMissingProfile("${basedir}"), context);
85          assertActivation(false, newMissingProfile("${basedir}/" + "file.txt"), context);
86      }
87  
88      @Test
89      public void testIsActiveExistsLeavesFileUnchanged() {
90          Profile profile = newExistsProfile("file.txt");
91          assertEquals("file.txt", profile.getActivation().getFile().getExists());
92  
93          assertActivation(true, profile, context);
94  
95          assertEquals("file.txt", profile.getActivation().getFile().getExists());
96      }
97  
98      private Profile newExistsProfile(String filePath) {
99          ActivationFile activationFile = new ActivationFile();
100         activationFile.setExists(filePath);
101         return newProfile(activationFile);
102     }
103 
104     private Profile newMissingProfile(String filePath) {
105         ActivationFile activationFile = new ActivationFile();
106         activationFile.setMissing(filePath);
107         return newProfile(activationFile);
108     }
109 
110     private Profile newProfile(ActivationFile activationFile) {
111         Activation activation = new Activation();
112         activation.setFile(activationFile);
113 
114         Profile profile = new Profile();
115         profile.setActivation(activation);
116 
117         return profile;
118     }
119 }