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.impl.model.profile;
20  
21  import java.nio.file.Path;
22  import java.util.Map;
23  
24  import org.apache.maven.api.model.Model;
25  import org.apache.maven.api.model.Profile;
26  import org.apache.maven.api.services.model.ProfileActivationContext;
27  import org.apache.maven.api.services.model.ProfileActivator;
28  import org.apache.maven.impl.model.DefaultInterpolator;
29  import org.apache.maven.impl.model.DefaultPathTranslator;
30  import org.apache.maven.impl.model.DefaultProfileActivationContext;
31  import org.apache.maven.impl.model.rootlocator.DefaultRootLocator;
32  import org.junit.jupiter.api.AfterEach;
33  import org.junit.jupiter.api.BeforeEach;
34  
35  import static org.junit.jupiter.api.Assertions.assertEquals;
36  
37  /**
38   * Provides common services to test {@link ProfileActivator} implementations.
39   *
40   * @param <T> the type of {@link ProfileActivator} being tested
41   */
42  public abstract class AbstractProfileActivatorTest<T extends ProfileActivator> {
43      static class FakeRootLocator extends DefaultRootLocator {
44          @Override
45          protected boolean isRootDirectory(Path dir) {
46              return true;
47          }
48      }
49  
50      protected T activator;
51  
52      @BeforeEach
53      abstract void setUp() throws Exception;
54  
55      @AfterEach
56      void tearDown() throws Exception {
57          activator = null;
58      }
59  
60      protected DefaultProfileActivationContext newContext() {
61          return new DefaultProfileActivationContext(
62                  new DefaultPathTranslator(), new FakeRootLocator(), new DefaultInterpolator());
63      }
64  
65      protected ProfileActivationContext newContext(
66              Map<String, String> userProperties, Map<String, String> systemProperties) {
67          return newContext()
68                  .setUserProperties(userProperties)
69                  .setSystemProperties(systemProperties)
70                  .setModel(Model.newInstance());
71      }
72  
73      protected void assertActivation(boolean active, Profile profile, ProfileActivationContext context) {
74          SimpleProblemCollector problems = new SimpleProblemCollector();
75  
76          boolean res = activator.isActive(profile, context, problems);
77          assertEquals(0, problems.getErrors().size(), problems.getErrors().toString());
78          assertEquals(0, problems.getWarnings().size(), problems.getWarnings().toString());
79          assertEquals(active, res);
80      }
81  }