View Javadoc
1   package org.apache.maven.model.profile.activation;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Tests {@link FileProfileActivator}.
40   *
41   * @author Ravil Galeyev
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 }