001package 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 022import java.util.Properties; 023 024import org.apache.commons.lang3.Validate; 025import org.apache.maven.model.Profile; 026import org.apache.maven.model.building.SimpleProblemCollector; 027import org.apache.maven.model.profile.DefaultProfileActivationContext; 028import org.apache.maven.model.profile.ProfileActivationContext; 029import org.codehaus.plexus.PlexusTestCase; 030import org.codehaus.plexus.component.annotations.Component; 031 032/** 033 * Provides common services to test {@link ProfileActivator} implementations. 034 * 035 * @author Benjamin Bentmann 036 */ 037public abstract class AbstractProfileActivatorTest<T extends ProfileActivator> 038 extends PlexusTestCase 039{ 040 041 private Class<T> activatorClass; 042 043 private String roleHint; 044 045 protected T activator; 046 047 public AbstractProfileActivatorTest( Class<T> activatorClass ) 048 { 049 this.activatorClass = Validate.notNull( activatorClass, "activatorClass cannot be null" );; 050 051 roleHint = activatorClass.getAnnotation( Component.class ).hint(); 052 } 053 054 @Override 055 protected void setUp() 056 throws Exception 057 { 058 super.setUp(); 059 060 activator = activatorClass.cast( lookup( ProfileActivator.class, roleHint ) ); 061 } 062 063 @Override 064 protected void tearDown() 065 throws Exception 066 { 067 activator = null; 068 069 super.tearDown(); 070 } 071 072 protected ProfileActivationContext newContext( final Properties userProperties, final Properties systemProperties ) 073 { 074 DefaultProfileActivationContext context = new DefaultProfileActivationContext(); 075 return context.setUserProperties( userProperties ).setSystemProperties( systemProperties ); 076 } 077 078 protected void assertActivation( boolean active, Profile profile, ProfileActivationContext context ) 079 { 080 SimpleProblemCollector problems = new SimpleProblemCollector(); 081 082 assertEquals( active, activator.isActive( profile, context, problems ) ); 083 084 assertEquals( problems.getErrors().toString(), 0, problems.getErrors().size() ); 085 assertEquals( problems.getWarnings().toString(), 0, problems.getWarnings().size() ); 086 } 087 088}