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