Got rid of the profile injector and advisor.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@748871 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Britton Isbell 2009-02-28 16:01:20 +00:00
parent c416e704b7
commit b216d61448
8 changed files with 109 additions and 1100 deletions

View File

@ -1,145 +0,0 @@
package org.apache.maven.profiles.build;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.model.Model;
import org.apache.maven.model.Profile;
import org.apache.maven.profiles.DefaultProfileManager;
import org.apache.maven.profiles.ProfileManager;
import org.apache.maven.profiles.activation.ProfileActivationContext;
import org.apache.maven.profiles.activation.ProfileActivationException;
import org.apache.maven.profiles.injection.ProfileInjector;
import org.apache.maven.project.ProjectBuildingException;
import org.codehaus.plexus.PlexusConstants;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.context.ContextException;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
@Component(role = ProfileAdvisor.class)
public class DefaultProfileAdvisor
implements ProfileAdvisor, LogEnabled, Contextualizable
{
@Requirement
private ProfileInjector profileInjector;
@Requirement
private PlexusContainer container;
private Logger logger;
public List applyActivatedProfiles(Model model,
ProfileActivationContext activationContext)
throws ProjectBuildingException
{
ProfileManager profileManager = buildProfileManager( model, activationContext );
return applyActivatedProfiles( model, profileManager );
}
public List applyActivatedExternalProfiles(Model model, ProfileManager externalProfileManager)
throws ProjectBuildingException
{
if ( externalProfileManager == null )
{
return Collections.EMPTY_LIST;
}
return applyActivatedProfiles( model, externalProfileManager );
}
private List applyActivatedProfiles( Model model, ProfileManager profileManager )
throws ProjectBuildingException
{
List activeProfiles;
if ( profileManager != null )
{
try
{
activeProfiles = profileManager.getActiveProfiles( model );
}
catch ( ProfileActivationException e )
{
String groupId = model.getGroupId();
if ( groupId == null )
{
groupId = "unknown";
}
String artifactId = model.getArtifactId();
if ( artifactId == null )
{
artifactId = "unknown";
}
String projectId = ArtifactUtils.versionlessKey( groupId, artifactId );
throw new ProjectBuildingException(projectId, e.getMessage());
}
for ( Iterator it = activeProfiles.iterator(); it.hasNext(); )
{
Profile profile = (Profile) it.next();
profileInjector.inject( profile, model );
}
}
else
{
activeProfiles = Collections.EMPTY_LIST;
}
return activeProfiles;
}
private ProfileManager buildProfileManager(Model model,
ProfileActivationContext profileActivationContext)
throws ProjectBuildingException
{
ProfileManager profileManager = new DefaultProfileManager( container, profileActivationContext );
profileManager.addProfiles( model.getProfiles() );
return profileManager;
}
public void contextualize( Context context )
throws ContextException
{
container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
}
public void enableLogging( Logger logger )
{
this.logger = logger;
}
}

View File

@ -1,46 +0,0 @@
package org.apache.maven.profiles.build;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.model.Model;
import org.apache.maven.profiles.ProfileManager;
import org.apache.maven.profiles.activation.ProfileActivationContext;
import org.apache.maven.project.ProjectBuildingException;
import java.io.File;
import java.util.LinkedHashSet;
import java.util.List;
/**
* @author jdcasey
*/
public interface ProfileAdvisor
{
String ROLE = ProfileAdvisor.class.getName();
List applyActivatedProfiles(Model model,
ProfileActivationContext activationContext)
throws ProjectBuildingException;
List applyActivatedExternalProfiles(Model model, ProfileManager externalProfileManager)
throws ProjectBuildingException;
}

View File

@ -1,169 +0,0 @@
package org.apache.maven.profiles.injection;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.model.*;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.project.builder.*;
import org.apache.maven.shared.model.ModelProperty;
import org.apache.maven.shared.model.ModelMarshaller;
import org.apache.maven.shared.model.ModelTransformerContext;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.util.xml.pull.XmlSerializer;
import org.codehaus.plexus.util.xml.pull.MXSerializer;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.codehaus.plexus.util.WriterFactory;
import org.codehaus.plexus.util.ReaderFactory;
import java.util.*;
import java.io.*;
import java.lang.reflect.Method;
/**
* Inject profile data into a Model, using the profile as the dominant data source, and
* persisting results of the injection in the Model.
*/
@Component(role = ProfileInjector.class)
public class DefaultProfileInjector
implements ProfileInjector
{
public Model inject( Profile profile, Model model )
{
//TODO: Using reflection now. Need to replace with custom mapper
StringWriter writer = new StringWriter();
XmlSerializer serializer = new MXSerializer();
serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " " );
serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n" );
try
{
serializer.setOutput( writer );
serializer.startDocument("UTF-8", null );
} catch (IOException e) {
}
try {
MavenXpp3Writer w = new MavenXpp3Writer();
Class c = Class.forName("org.apache.maven.model.io.xpp3.MavenXpp3Writer");
Class partypes[] = new Class[3];
partypes[0] = Profile.class;
partypes[1] = String.class;
partypes[2] = XmlSerializer.class;
Method meth = c.getDeclaredMethod(
"writeProfile", partypes);
meth.setAccessible(true);
Object arglist[] = new Object[3];
arglist[0] = profile;
arglist[1] = "profile";
arglist[2] = serializer;
meth.invoke(w, arglist);
serializer.endDocument();
}
catch (Exception e)
{
return null;
}
Set<String> uris = new HashSet(PomTransformer.URIS);
uris.add(ProjectUri.Profiles.Profile.Build.Plugins.Plugin.configuration);
List<ModelProperty> p;
try
{
p = ModelMarshaller.marshallXmlToModelProperties(new ByteArrayInputStream(writer.getBuffer().toString().getBytes()),
ProjectUri.Profiles.xUri, uris);
} catch (IOException e) {
return null;
}
List<ModelProperty> transformed = new ArrayList<ModelProperty>();
for(ModelProperty mp : p)
{
if(mp.getUri().startsWith(ProjectUri.Profiles.Profile.xUri) && !mp.getUri().equals(ProjectUri.Profiles.Profile.id)
&& !mp.getUri().startsWith(ProjectUri.Profiles.Profile.Activation.xUri) )
{
transformed.add(new ModelProperty(mp.getUri().replace(ProjectUri.Profiles.Profile.xUri, ProjectUri.xUri),
mp.getResolvedValue()));
}
}
PomTransformer transformer = new PomTransformer( new PomClassicDomainModelFactory() );
ModelTransformerContext ctx = new ModelTransformerContext(PomTransformer.MODEL_CONTAINER_INFOS );
PomClassicDomainModel transformedDomainModel;
try {
transformedDomainModel = ( (PomClassicDomainModel) ctx.transform( Arrays.asList( new PomClassicDomainModel(transformed), convertToDomainModel(model)),
transformer,
transformer,
Collections.EMPTY_LIST,
null,
null ) );
return convertFromInputStreamToModel(transformedDomainModel.getInputStream());
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private PomClassicDomainModel convertToDomainModel(Model model) throws IOException
{
if ( model == null )
{
throw new IllegalArgumentException( "model: null" );
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Writer out = null;
MavenXpp3Writer writer = new MavenXpp3Writer();
try
{
out = WriterFactory.newXmlWriter( baos );
writer.write( out, model );
}
finally
{
if ( out != null )
{
out.close();
}
}
return new PomClassicDomainModel(new ByteArrayInputStream(baos.toByteArray()));
}
private static Model convertFromInputStreamToModel(InputStream inputStream) throws IOException
{
try
{
return new MavenXpp3Reader().read( ReaderFactory.newXmlReader( inputStream ) );
}
catch ( XmlPullParserException e )
{
throw new IOException( e.getMessage() );
}
}
}

View File

@ -1,31 +0,0 @@
package org.apache.maven.profiles.injection;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.model.Model;
import org.apache.maven.model.Profile;
public interface ProfileInjector
{
String ROLE = ProfileInjector.class.getName();
Model inject( Profile profile, Model model );
}

View File

@ -22,6 +22,7 @@
import java.io.*; import java.io.*;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import java.lang.reflect.Method;
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.artifact.ArtifactUtils;
@ -37,9 +38,9 @@
import org.apache.maven.model.io.xpp3.MavenXpp3Writer; import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.profiles.ProfileManager; import org.apache.maven.profiles.ProfileManager;
import org.apache.maven.profiles.DefaultProfileManager;
import org.apache.maven.profiles.activation.ProfileActivationContext; import org.apache.maven.profiles.activation.ProfileActivationContext;
import org.apache.maven.profiles.activation.ProfileActivationException; import org.apache.maven.profiles.activation.ProfileActivationException;
import org.apache.maven.profiles.build.ProfileAdvisor;
import org.apache.maven.project.artifact.InvalidDependencyVersionException; import org.apache.maven.project.artifact.InvalidDependencyVersionException;
import org.apache.maven.project.builder.*; import org.apache.maven.project.builder.*;
import org.apache.maven.project.builder.profile.ProfileContext; import org.apache.maven.project.builder.profile.ProfileContext;
@ -58,6 +59,9 @@
import org.codehaus.plexus.util.ReaderFactory; import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.codehaus.plexus.util.xml.pull.XmlSerializer;
import org.codehaus.plexus.util.xml.pull.MXSerializer;
import org.codehaus.plexus.PlexusContainer;
/** /**
@ -71,10 +75,10 @@ public class DefaultMavenProjectBuilder
private ModelValidator validator; private ModelValidator validator;
@Requirement @Requirement
private ProfileAdvisor profileAdvisor; private MavenRepositorySystem repositorySystem;
@Requirement @Requirement
private MavenRepositorySystem repositorySystem; private PlexusContainer container;
@Requirement @Requirement
List<ModelEventListener> listeners; List<ModelEventListener> listeners;
@ -306,15 +310,15 @@ private MavenProject buildWithProfiles( Model model, ProjectBuilderConfiguration
String projectId = safeVersionlessKey( model.getGroupId(), model.getArtifactId() ); String projectId = safeVersionlessKey( model.getGroupId(), model.getArtifactId() );
ProfileActivationContext profileActivationContext; ProfileActivationContext profileActivationContext;
List<Profile> projectProfiles = new ArrayList<Profile>();
ProfileManager externalProfileManager = config.getGlobalProfileManager(); ProfileManager externalProfileManager = config.getGlobalProfileManager();
if ( externalProfileManager != null ) if ( externalProfileManager != null )
{ {
// used to trigger the caching of SystemProperties in the container context...
try try
{ {
externalProfileManager.getActiveProfiles(); projectProfiles.addAll(externalProfileManager.getActiveProfiles( model ));
} }
catch ( ProfileActivationException e ) catch ( ProfileActivationException e )
{ {
@ -326,13 +330,24 @@ private MavenProject buildWithProfiles( Model model, ProjectBuilderConfiguration
else else
{ {
profileActivationContext = new ProfileActivationContext( config.getExecutionProperties(), false ); profileActivationContext = new ProfileActivationContext( config.getExecutionProperties(), false );
ProfileManager profileManager = new DefaultProfileManager( container, profileActivationContext );
profileManager.addProfiles( model.getProfiles() );
try
{
projectProfiles.addAll( profileManager.getActiveProfiles( model ));
}
catch (ProfileActivationException e)
{
throw new ProjectBuildingException( projectId, "Failed to activate external profiles.",
projectDescriptor, e );
}
} }
List<Profile> projectProfiles = new ArrayList<Profile>(); for( Profile profile : projectProfiles )
{
projectProfiles.addAll( profileAdvisor.applyActivatedProfiles( model, profileActivationContext ) ); inject( profile, model );
}
projectProfiles.addAll( profileAdvisor.applyActivatedExternalProfiles( model, externalProfileManager ) );
MavenProject project; MavenProject project;
@ -358,6 +373,89 @@ private MavenProject buildWithProfiles( Model model, ProjectBuilderConfiguration
return project; return project;
} }
private Model inject( Profile profile, Model model )
{
//TODO: Using reflection now. Need to replace with custom mapper
StringWriter writer = new StringWriter();
XmlSerializer serializer = new MXSerializer();
serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " " );
serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n" );
try
{
serializer.setOutput( writer );
serializer.startDocument("UTF-8", null );
} catch (IOException e) {
}
try {
MavenXpp3Writer w = new MavenXpp3Writer();
Class c = Class.forName("org.apache.maven.model.io.xpp3.MavenXpp3Writer");
Class partypes[] = new Class[3];
partypes[0] = Profile.class;
partypes[1] = String.class;
partypes[2] = XmlSerializer.class;
Method meth = c.getDeclaredMethod(
"writeProfile", partypes);
meth.setAccessible(true);
Object arglist[] = new Object[3];
arglist[0] = profile;
arglist[1] = "profile";
arglist[2] = serializer;
meth.invoke(w, arglist);
serializer.endDocument();
}
catch (Exception e)
{
return null;
}
Set<String> uris = new HashSet(PomTransformer.URIS);
uris.add(ProjectUri.Profiles.Profile.Build.Plugins.Plugin.configuration);
List<ModelProperty> p;
try
{
p = ModelMarshaller.marshallXmlToModelProperties(new ByteArrayInputStream(writer.getBuffer().toString().getBytes()),
ProjectUri.Profiles.xUri, uris);
} catch (IOException e) {
return null;
}
List<ModelProperty> transformed = new ArrayList<ModelProperty>();
for(ModelProperty mp : p)
{
if(mp.getUri().startsWith(ProjectUri.Profiles.Profile.xUri) && !mp.getUri().equals(ProjectUri.Profiles.Profile.id)
&& !mp.getUri().startsWith(ProjectUri.Profiles.Profile.Activation.xUri) )
{
transformed.add(new ModelProperty(mp.getUri().replace(ProjectUri.Profiles.Profile.xUri, ProjectUri.xUri),
mp.getResolvedValue()));
}
}
PomTransformer transformer = new PomTransformer( new PomClassicDomainModelFactory() );
ModelTransformerContext ctx = new ModelTransformerContext(PomTransformer.MODEL_CONTAINER_INFOS );
PomClassicDomainModel transformedDomainModel;
try {
transformedDomainModel = ( (PomClassicDomainModel) ctx.transform( Arrays.asList( new PomClassicDomainModel(transformed), convertToDomainModel(model)),
transformer,
transformer,
Collections.EMPTY_LIST,
null,
null ) );
return convertFromInputStreamToModel(transformedDomainModel.getInputStream());
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private MavenProject readModelFromLocalPath( String projectId, File projectDescriptor, PomArtifactResolver resolver, ProjectBuilderConfiguration config ) private MavenProject readModelFromLocalPath( String projectId, File projectDescriptor, PomArtifactResolver resolver, ProjectBuilderConfiguration config )
throws ProjectBuildingException throws ProjectBuildingException
{ {

View File

@ -1,293 +0,0 @@
package org.apache.maven.profiles.injection;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.model.Build;
import org.apache.maven.model.BuildBase;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginContainer;
import org.apache.maven.model.PluginExecution;
import org.apache.maven.model.Profile;
import org.apache.maven.model.Repository;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
public class DefaultProfileInjectorTest
extends TestCase
{
public void testProfilePluginConfigurationShouldOverrideCollidingModelPluginConfiguration()
{
Plugin mPlugin = new Plugin();
mPlugin.setGroupId( "test" );
mPlugin.setArtifactId( "test-artifact" );
mPlugin.setVersion( "1.0-SNAPSHOT" );
Xpp3Dom mConfigChild = new Xpp3Dom( "test" );
mConfigChild.setValue( "value" );
Xpp3Dom mConfigChild2 = new Xpp3Dom( "test2" );
mConfigChild2.setValue( "value2" );
Xpp3Dom mConfig = new Xpp3Dom( "configuration" );
mConfig.addChild( mConfigChild );
mConfig.addChild( mConfigChild2 );
mPlugin.setConfiguration( mConfig );
Build mBuild = new Build();
mBuild.addPlugin( mPlugin );
Model model = new Model();
model.setBuild( mBuild );
Plugin pPlugin = new Plugin();
pPlugin.setGroupId( "test" );
pPlugin.setArtifactId( "test-artifact" );
pPlugin.setVersion( "1.0-SNAPSHOT" );
Xpp3Dom pConfigChild = new Xpp3Dom( "test" );
pConfigChild.setValue( "replacedValue" );
Xpp3Dom pConfig = new Xpp3Dom( "configuration" );
pConfig.addChild( pConfigChild );
pPlugin.setConfiguration( pConfig );
BuildBase pBuild = new BuildBase();
pBuild.addPlugin( pPlugin );
Profile profile = new Profile();
profile.setId( "testId" );
profile.setBuild( pBuild );
model = new DefaultProfileInjector().inject( profile, model );
Build rBuild = model.getBuild();
Plugin rPlugin = (Plugin) rBuild.getPlugins().get( 0 );
Xpp3Dom rConfig = (Xpp3Dom) rPlugin.getConfiguration();
Xpp3Dom rChild = rConfig.getChild( "test" );
assertEquals( "replacedValue", rChild.getValue() );
Xpp3Dom rChild2 = rConfig.getChild( "test2" );
assertEquals( "value2", rChild2.getValue() );
}
public void testModelConfigShouldPersistWhenPluginHasExecConfigs()
{
Plugin mPlugin = new Plugin();
mPlugin.setGroupId( "test" );
mPlugin.setArtifactId( "test-artifact" );
mPlugin.setVersion( "1.0-SNAPSHOT" );
Xpp3Dom mConfigChild = new Xpp3Dom( "test" );
mConfigChild.setValue( "value" );
Xpp3Dom mConfigChild2 = new Xpp3Dom( "test2" );
mConfigChild2.setValue( "value2" );
Xpp3Dom mConfig = new Xpp3Dom( "configuration" );
mConfig.addChild( mConfigChild );
mConfig.addChild( mConfigChild2 );
mPlugin.setConfiguration( mConfig );
Build mBuild = new Build();
mBuild.addPlugin( mPlugin );
Model model = new Model();
model.setBuild( mBuild );
Plugin pPlugin = new Plugin();
pPlugin.setGroupId( "test" );
pPlugin.setArtifactId( "test-artifact" );
pPlugin.setVersion( "1.0-SNAPSHOT" );
PluginExecution pExec = new PluginExecution();
pExec.setId("profile-injected");
Xpp3Dom pConfigChild = new Xpp3Dom( "test" );
pConfigChild.setValue( "replacedValue" );
Xpp3Dom pConfig = new Xpp3Dom( "configuration" );
pConfig.addChild( pConfigChild );
pExec.setConfiguration( pConfig );
pPlugin.addExecution( pExec );
BuildBase pBuild = new BuildBase();
pBuild.addPlugin( pPlugin );
Profile profile = new Profile();
profile.setId( "testId" );
profile.setBuild( pBuild );
model = new DefaultProfileInjector().inject( profile, model );
Build rBuild = model.getBuild();
Plugin rPlugin = (Plugin) rBuild.getPlugins().get( 0 );
PluginExecution rExec = (PluginExecution) rPlugin.getExecutionsAsMap().get( "profile-injected" );
assertNotNull( rExec );
Xpp3Dom rExecConfig = (Xpp3Dom) rExec.getConfiguration();
Xpp3Dom rChild = rExecConfig.getChild( "test" );
assertEquals( "replacedValue", rChild.getValue() );
Xpp3Dom rConfig = (Xpp3Dom) rPlugin.getConfiguration();
assertNotNull( rConfig );
Xpp3Dom rChild2 = rConfig.getChild( "test2" );
assertEquals( "value2", rChild2.getValue() );
}
public void testProfileRepositoryShouldOverrideModelRepository()
{
Repository mRepository = new Repository();
mRepository.setId( "testId" );
mRepository.setName( "Test repository" );
mRepository.setUrl( "http://www.google.com" );
Model model = new Model();
model.addRepository( mRepository );
Repository pRepository = new Repository();
pRepository.setId( "testId" );
pRepository.setName( "Test repository" );
pRepository.setUrl( "http://www.yahoo.com" );
Profile profile = new Profile();
profile.setId( "testId" );
profile.addRepository( pRepository );
model = new DefaultProfileInjector().inject( profile, model );
Repository rRepository = model.getRepositories().get( 0 );
assertEquals( "http://www.yahoo.com", rRepository.getUrl() );
}
/*
public void testShouldPreserveModelModulesWhenProfileHasNone()
{
Model model = new Model();
model.addModule( "module1" );
Profile profile = new Profile();
profile.setId( "testId" );
model = new DefaultProfileInjector().inject( profile, model );
List rModules = model.getModules();
assertEquals( 1, rModules.size() );
assertEquals( "module1", rModules.get( 0 ) );
}
public void testShouldPreserveOrderingOfProfileInjectedPluginExecutions()
{
Plugin profilePlugin = new Plugin();
profilePlugin.setGroupId( "group" );
profilePlugin.setArtifactId( "artifact" );
profilePlugin.setVersion( "version" );
PluginExecution exec1 = new PluginExecution();
exec1.setId( "z" );
profilePlugin.addExecution( exec1 );
PluginExecution exec2 = new PluginExecution();
exec2.setId( "y" );
profilePlugin.addExecution( exec2 );
BuildBase buildBase = new BuildBase();
buildBase.addPlugin( profilePlugin );
Profile profile = new Profile();
profile.setBuild( buildBase );
Plugin modelPlugin = new Plugin();
modelPlugin.setGroupId( "group" );
modelPlugin.setArtifactId( "artifact" );
modelPlugin.setVersion( "version" );
PluginExecution exec3 = new PluginExecution();
exec3.setId( "w" );
modelPlugin.addExecution( exec3 );
PluginExecution exec4 = new PluginExecution();
exec4.setId( "x" );
modelPlugin.addExecution( exec4 );
Build build = new Build();
build.addPlugin( modelPlugin );
Model model = new Model();
model.setBuild( build );
model = new DefaultProfileInjector().inject( profile, model );
List plugins = model.getBuild().getPlugins();
assertNotNull( plugins );
assertEquals( 1, plugins.size() );
Plugin plugin = (Plugin) plugins.get( 0 );
List executions = plugin.getExecutions();
assertNotNull( executions );
assertEquals( 4, executions.size() );
Iterator it = executions.iterator();
PluginExecution e = (PluginExecution) it.next();
assertEquals( "w", e.getId() );
e = (PluginExecution) it.next();
assertEquals( "x", e.getId() );
e = (PluginExecution) it.next();
assertEquals( "z", e.getId() );
e = (PluginExecution) it.next();
assertEquals( "y", e.getId() );
}
*/
}

View File

@ -1,368 +0,0 @@
package org.apache.maven.project.injection;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import junit.framework.TestCase;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Exclusion;
import org.apache.maven.model.Model;
import org.apache.maven.project.artifact.TestModelDefaultsInjector;
import java.util.List;
/**
* @author jdcasey
*/
public class DefaultModelDefaultsInjectorTest
extends TestCase
{
public void testShouldConstructWithNoParams()
{
new TestModelDefaultsInjector();
}
public void testShouldMergeManagedDependencyOfTypeEJBToDependencyList()
{
Model model = new Model();
Dependency managedDep = new Dependency();
managedDep.setGroupId( "group" );
managedDep.setArtifactId( "artifact" );
managedDep.setVersion( "1.0" );
managedDep.setType( "ejb" );
DependencyManagement depMgmt = new DependencyManagement();
depMgmt.addDependency( managedDep );
model.setDependencyManagement( depMgmt );
Dependency dep = new Dependency();
dep.setGroupId( "group" );
dep.setArtifactId( "artifact" );
// looks like groupId:artifactId:type is the minimum for identification, where
// type is defaulted to "jar".
dep.setType( "ejb" );
model.addDependency( dep );
new TestModelDefaultsInjector().injectDefaults( model );
List resultingDeps = model.getDependencies();
assertEquals( 1, resultingDeps.size() );
Dependency result = (Dependency) resultingDeps.get( 0 );
assertEquals( "1.0", result.getVersion() );
}
public void testShouldSucceedInMergingDependencyWithDependency()
{
Model model = new Model();
Dependency dep = new Dependency();
dep.setGroupId( "myGroup" );
dep.setArtifactId( "myArtifact" );
model.addDependency( dep );
Dependency def = new Dependency();
def.setGroupId( dep.getGroupId() );
def.setArtifactId( dep.getArtifactId() );
def.setVersion( "1.0.1" );
def.setScope( "scope" );
DependencyManagement depMgmt = new DependencyManagement();
depMgmt.addDependency( def );
model.setDependencyManagement( depMgmt );
new TestModelDefaultsInjector().injectDefaults( model );
List deps = model.getDependencies();
assertEquals( 1, deps.size() );
Dependency result = (Dependency) deps.get( 0 );
assertEquals( def.getVersion(), result.getVersion() );
}
public void testShouldMergeDependencyExclusionsFromDefaultsToDependency()
{
Model model = new Model();
Dependency dep = new Dependency();
dep.setGroupId( "myGroup" );
dep.setArtifactId( "myArtifact" );
model.addDependency( dep );
Dependency def = new Dependency();
def.setGroupId( dep.getGroupId() );
def.setArtifactId( dep.getArtifactId() );
def.setVersion( "1.0.1" );
def.setScope( "scope" );
Exclusion exc = new Exclusion();
exc.setArtifactId( "mydep" );
exc.setGroupId( "mygrp" );
def.addExclusion( exc );
DependencyManagement depMgmt = new DependencyManagement();
depMgmt.addDependency( def );
model.setDependencyManagement( depMgmt );
new TestModelDefaultsInjector().injectDefaults( model );
List deps = model.getDependencies();
assertEquals( 1, deps.size() );
Dependency result = (Dependency) deps.get( 0 );
assertEquals( def.getVersion(), result.getVersion() );
List resultExclusions = result.getExclusions();
assertNotNull( resultExclusions );
assertEquals( 1, resultExclusions.size() );
Exclusion resultExclusion = (Exclusion) resultExclusions.get( 0 );
assertEquals( "mydep", resultExclusion.getArtifactId() );
assertEquals( "mygrp", resultExclusion.getGroupId() );
}
public void testShouldMergeDefaultUrlAndArtifactWhenDependencyDoesntSupplyVersion()
{
Model model = new Model();
Dependency dep = new Dependency();
dep.setGroupId( "myGroup" );
dep.setArtifactId( "myArtifact" );
model.addDependency( dep );
Dependency def = new Dependency();
def.setGroupId( dep.getGroupId() );
def.setArtifactId( dep.getArtifactId() );
def.setVersion( "1.0.1" );
DependencyManagement depMgmt = new DependencyManagement();
depMgmt.addDependency( def );
model.setDependencyManagement( depMgmt );
new TestModelDefaultsInjector().injectDefaults( model );
List deps = model.getDependencies();
assertEquals( 1, deps.size() );
Dependency result = (Dependency) deps.get( 0 );
assertEquals( def.getVersion(), result.getVersion() );
}
public void testShouldNotMergeDefaultUrlOrArtifactWhenDependencySuppliesVersion()
{
Model model = new Model();
Dependency dep = new Dependency();
dep.setGroupId( "myGroup" );
dep.setArtifactId( "myArtifact" );
dep.setVersion( "1.0.1" );
model.addDependency( dep );
Dependency def = new Dependency();
def.setGroupId( dep.getGroupId() );
def.setArtifactId( dep.getArtifactId() );
DependencyManagement depMgmt = new DependencyManagement();
depMgmt.addDependency( def );
model.setDependencyManagement( depMgmt );
new TestModelDefaultsInjector().injectDefaults( model );
List deps = model.getDependencies();
assertEquals( 1, deps.size() );
Dependency result = (Dependency) deps.get( 0 );
assertEquals( dep.getVersion(), result.getVersion() );
}
public void testShouldMergeDefaultPropertiesWhenDependencyDoesntSupplyProperties()
{
Model model = new Model();
Dependency dep = new Dependency();
dep.setGroupId( "myGroup" );
dep.setArtifactId( "myArtifact" );
dep.setVersion( "1.0.1" );
model.addDependency( dep );
Dependency def = new Dependency();
def.setGroupId( dep.getGroupId() );
def.setArtifactId( dep.getArtifactId() );
DependencyManagement depMgmt = new DependencyManagement();
depMgmt.addDependency( def );
model.setDependencyManagement( depMgmt );
new TestModelDefaultsInjector().injectDefaults( model );
List deps = model.getDependencies();
assertEquals( 1, deps.size() );
}
public void testShouldNotMergeDefaultPropertiesWhenDependencySuppliesProperties()
{
Model model = new Model();
Dependency dep = new Dependency();
dep.setGroupId( "myGroup" );
dep.setArtifactId( "myArtifact" );
dep.setVersion( "1.0.1" );
model.addDependency( dep );
Dependency def = new Dependency();
def.setGroupId( dep.getGroupId() );
def.setArtifactId( dep.getArtifactId() );
DependencyManagement depMgmt = new DependencyManagement();
depMgmt.addDependency( def );
model.setDependencyManagement( depMgmt );
new TestModelDefaultsInjector().injectDefaults( model );
List deps = model.getDependencies();
assertEquals( 1, deps.size() );
}
public void testShouldMergeDefaultScopeWhenDependencyDoesntSupplyScope()
{
Model model = new Model();
Dependency dep = new Dependency();
dep.setGroupId( "myGroup" );
dep.setArtifactId( "myArtifact" );
dep.setVersion( "1.0.1" );
dep.setScope( "scope" );
model.addDependency( dep );
Dependency def = new Dependency();
def.setGroupId( dep.getGroupId() );
def.setArtifactId( dep.getArtifactId() );
DependencyManagement depMgmt = new DependencyManagement();
depMgmt.addDependency( def );
model.setDependencyManagement( depMgmt );
new TestModelDefaultsInjector().injectDefaults( model );
List deps = model.getDependencies();
assertEquals( 1, deps.size() );
Dependency result = (Dependency) deps.get( 0 );
assertEquals( "scope", result.getScope() );
}
public void testShouldNotMergeDefaultScopeWhenDependencySuppliesScope()
{
Model model = new Model();
Dependency dep = new Dependency();
dep.setGroupId( "myGroup" );
dep.setArtifactId( "myArtifact" );
dep.setVersion( "1.0.1" );
dep.setScope( "scope" );
model.addDependency( dep );
Dependency def = new Dependency();
def.setGroupId( dep.getGroupId() );
def.setArtifactId( dep.getArtifactId() );
def.setScope( "default" );
DependencyManagement depMgmt = new DependencyManagement();
depMgmt.addDependency( def );
model.setDependencyManagement( depMgmt );
new TestModelDefaultsInjector().injectDefaults( model );
List deps = model.getDependencies();
assertEquals( 1, deps.size() );
Dependency result = (Dependency) deps.get( 0 );
assertEquals( "scope", result.getScope() );
}
public void testShouldRejectDependencyWhereNoVersionIsFoundAfterDefaultsInjection()
{
Model model = new Model();
Dependency dep = new Dependency();
dep.setGroupId( "myGroup" );
dep.setArtifactId( "myArtifact" );
model.addDependency( dep );
Dependency def = new Dependency();
def.setGroupId( dep.getGroupId() );
def.setArtifactId( dep.getArtifactId() );
DependencyManagement depMgmt = new DependencyManagement();
depMgmt.addDependency( def );
model.setDependencyManagement( depMgmt );
// try
// {
new TestModelDefaultsInjector().injectDefaults( model );
Dependency dependency = (Dependency) model.getDependencies().get( 0 );
assertNull( "check version is null", dependency.getVersion() );
// fail("Should fail to validate dependency without a version.");
// }
// catch ( IllegalStateException e )
// {
// // should throw when it detects a missing version in the test dependency.
// }
}
}

View File

@ -1,37 +0,0 @@
package org.apache.maven.project.injection;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.model.Model;
import org.apache.maven.model.Profile;
import org.apache.maven.profiles.injection.ProfileInjector;
import org.codehaus.plexus.component.annotations.Component;
@Component(role=ProfileInjector.class,hint="test")
public class TestProfileInjector
implements ProfileInjector
{
public Model inject( Profile profile, Model model )
{
return null;
}
}