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 java.util.Properties;
23  
24  import org.apache.commons.lang3.Validate;
25  import org.apache.maven.model.Profile;
26  import org.apache.maven.model.building.SimpleProblemCollector;
27  import org.apache.maven.model.profile.DefaultProfileActivationContext;
28  import org.apache.maven.model.profile.ProfileActivationContext;
29  import org.codehaus.plexus.PlexusTestCase;
30  import org.codehaus.plexus.component.annotations.Component;
31  
32  /**
33   * Provides common services to test {@link ProfileActivator} implementations.
34   *
35   * @author Benjamin Bentmann
36   */
37  public abstract class AbstractProfileActivatorTest<T extends ProfileActivator>
38      extends PlexusTestCase
39  {
40  
41      private Class<T> activatorClass;
42  
43      private String roleHint;
44  
45      protected T activator;
46  
47      public AbstractProfileActivatorTest( Class<T> activatorClass )
48      {
49          this.activatorClass = Validate.notNull( activatorClass, "activatorClass cannot be null" );;
50  
51          roleHint = activatorClass.getAnnotation( Component.class ).hint();
52      }
53  
54      @Override
55      protected void setUp()
56          throws Exception
57      {
58          super.setUp();
59  
60          activator = activatorClass.cast( lookup( ProfileActivator.class, roleHint ) );
61      }
62  
63      @Override
64      protected void tearDown()
65          throws Exception
66      {
67          activator = null;
68  
69          super.tearDown();
70      }
71  
72      protected ProfileActivationContext newContext( final Properties userProperties, final Properties systemProperties )
73      {
74          DefaultProfileActivationContext context = new DefaultProfileActivationContext();
75          return context.setUserProperties( userProperties ).setSystemProperties( systemProperties );
76      }
77  
78      protected void assertActivation( boolean active, Profile profile, ProfileActivationContext context )
79      {
80          SimpleProblemCollector problems = new SimpleProblemCollector();
81  
82          assertEquals( active, activator.isActive( profile, context, problems ) );
83  
84          assertEquals( problems.getErrors().toString(), 0, problems.getErrors().size() );
85          assertEquals( problems.getWarnings().toString(), 0, problems.getWarnings().size() );
86      }
87  
88  }