001    package org.apache.maven.model.profile.activation;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.util.Properties;
023    
024    import org.apache.maven.model.Profile;
025    import org.apache.maven.model.building.SimpleProblemCollector;
026    import org.apache.maven.model.profile.DefaultProfileActivationContext;
027    import org.apache.maven.model.profile.ProfileActivationContext;
028    import org.codehaus.plexus.PlexusTestCase;
029    import org.codehaus.plexus.component.annotations.Component;
030    
031    /**
032     * Provides common services to test {@link ProfileActivator} implementations.
033     * 
034     * @author Benjamin Bentmann
035     */
036    public abstract class AbstractProfileActivatorTest<T extends ProfileActivator>
037        extends PlexusTestCase
038    {
039    
040        private Class<T> activatorClass;
041    
042        private String roleHint;
043    
044        protected T activator;
045    
046        public AbstractProfileActivatorTest( Class<T> activatorClass )
047        {
048            if ( activatorClass == null )
049            {
050                throw new IllegalArgumentException( "class of profile activator to test is not specified" );
051            }
052    
053            this.activatorClass = activatorClass;
054    
055            roleHint = activatorClass.getAnnotation( Component.class ).hint();
056        }
057    
058        @Override
059        protected void setUp()
060            throws Exception
061        {
062            super.setUp();
063    
064            activator = activatorClass.cast( lookup( ProfileActivator.class, roleHint ) );
065        }
066    
067        @Override
068        protected void tearDown()
069            throws Exception
070        {
071            activator = null;
072    
073            super.tearDown();
074        }
075    
076        protected ProfileActivationContext newContext( final Properties userProperties, final Properties systemProperties )
077        {
078            DefaultProfileActivationContext context = new DefaultProfileActivationContext();
079            return context.setUserProperties( userProperties ).setSystemProperties( systemProperties );
080        }
081    
082        protected void assertActivation( boolean active, Profile profile, ProfileActivationContext context )
083        {
084            SimpleProblemCollector problems = new SimpleProblemCollector();
085    
086            assertEquals( active, activator.isActive( profile, context, problems ) );
087    
088            assertEquals( problems.getErrors().toString(), 0, problems.getErrors().size() );
089            assertEquals( problems.getWarnings().toString(), 0, problems.getWarnings().size() );
090        }
091    
092    }