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;
20  
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.apache.maven.model.Activation;
25  import org.apache.maven.model.Profile;
26  import org.apache.maven.model.building.ModelProblemCollector;
27  import org.apache.maven.model.building.SimpleProblemCollector;
28  import org.apache.maven.model.profile.activation.ProfileActivator;
29  import org.junit.jupiter.api.Test;
30  
31  import static org.junit.jupiter.api.Assertions.assertEquals;
32  import static org.junit.jupiter.api.Assertions.assertTrue;
33  
34  /**
35   * Tests {@link DefaultProfileSelector}.
36   */
37  public class DefaultProfileSelectorTest {
38      private Profile newProfile(String id) {
39          Activation activation = new Activation();
40          Profile profile = new Profile();
41          profile.setId(id);
42          profile.setActivation(activation);
43          return profile;
44      }
45  
46      @Test
47      void testThrowingActivator() {
48          DefaultProfileSelector selector = new DefaultProfileSelector();
49          selector.addProfileActivator(new ProfileActivator() {
50              @Override
51              public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
52                  throw new RuntimeException("BOOM");
53              }
54  
55              @Override
56              public boolean presentInConfig(
57                      Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
58                  return true;
59              }
60          });
61  
62          List<Profile> profiles = Collections.singletonList(newProfile("one"));
63          DefaultProfileActivationContext context = new DefaultProfileActivationContext();
64          SimpleProblemCollector problems = new SimpleProblemCollector();
65          List<Profile> active = selector.getActiveProfiles(profiles, context, problems);
66          assertTrue(active.isEmpty());
67          assertEquals(1, problems.getErrors().size());
68          assertEquals(
69                  "Failed to determine activation for profile one: BOOM",
70                  problems.getErrors().get(0));
71      }
72  }