View Javadoc

1   package org.apache.maven.plugin.assembly.interpolation;
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 org.apache.maven.artifact.repository.ArtifactRepository;
23  import org.apache.maven.execution.MavenSession;
24  import org.apache.maven.model.Build;
25  import org.apache.maven.model.Model;
26  import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
27  import org.apache.maven.plugin.assembly.testutils.ConfigSourceStub;
28  import org.apache.maven.plugin.assembly.testutils.MockManager;
29  import org.apache.maven.project.MavenProject;
30  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
31  import org.codehaus.plexus.logging.Logger;
32  import org.codehaus.plexus.logging.console.ConsoleLogger;
33  import org.easymock.MockControl;
34  import org.easymock.classextension.MockClassControl;
35  
36  import java.io.IOException;
37  import java.util.Properties;
38  
39  import junit.framework.TestCase;
40  
41  public class AssemblyExpressionEvaluatorTest
42      extends TestCase
43  {
44  
45      private AssemblyInterpolator interpolator;
46  
47      private final ConfigSourceStub configSourceStub = new ConfigSourceStub();
48  
49      @Override
50      public void setUp()
51          throws IOException
52      {
53          interpolator = new AssemblyInterpolator();
54  
55          interpolator.enableLogging( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
56      }
57  
58      public void testShouldResolveModelGroupId()
59          throws ExpressionEvaluationException
60      {
61          final Model model = new Model();
62          model.setArtifactId( "artifact-id" );
63          model.setGroupId( "group.id" );
64          model.setVersion( "1" );
65          model.setPackaging( "jar" );
66  
67          configSourceStub.setProject( new MavenProject( model ) );
68  
69          final Object result = new AssemblyExpressionEvaluator( configSourceStub ).evaluate( "assembly.${groupId}" );
70  
71          assertEquals( "assembly.group.id", result );
72      }
73  
74      public void testShouldResolveModelPropertyBeforeModelGroupId()
75          throws ExpressionEvaluationException
76      {
77          final Model model = new Model();
78          model.setArtifactId( "artifact-id" );
79          model.setGroupId( "group.id" );
80          model.setVersion( "1" );
81          model.setPackaging( "jar" );
82  
83          final Properties props = new Properties();
84          props.setProperty( "groupId", "other.id" );
85  
86          model.setProperties( props );
87  
88          configSourceStub.setProject( new MavenProject( model ) );
89  
90          final Object result = new AssemblyExpressionEvaluator( configSourceStub ).evaluate( "assembly.${groupId}" );
91  
92          assertEquals( "assembly.other.id", result );
93      }
94  
95      public void testShouldResolveContextValueBeforeModelPropertyOrModelGroupIdInAssemblyId()
96          throws ExpressionEvaluationException
97      {
98          final Model model = new Model();
99          model.setArtifactId( "artifact-id" );
100         model.setGroupId( "group.id" );
101         model.setVersion( "1" );
102         model.setPackaging( "jar" );
103 
104         final Properties props = new Properties();
105         props.setProperty( "groupId", "other.id" );
106 
107         model.setProperties( props );
108 
109         final MockManager mm = new MockManager();
110 
111         final MockControl sessionControl = MockClassControl.createControl( MavenSession.class );
112         final MavenSession session = (MavenSession) sessionControl.getMock();
113 
114         mm.add( sessionControl );
115 
116         final Properties execProps = new Properties();
117         execProps.setProperty( "groupId", "still.another.id" );
118 
119         session.getExecutionProperties();
120         sessionControl.setReturnValue( execProps, MockControl.ZERO_OR_MORE );
121 
122         session.getUserProperties();
123         sessionControl.setReturnValue( new Properties(), MockControl.ZERO_OR_MORE );
124 
125         final MockControl csControl = MockControl.createControl( AssemblerConfigurationSource.class );
126         final AssemblerConfigurationSource cs = (AssemblerConfigurationSource) csControl.getMock();
127 
128         mm.add( csControl );
129 
130         cs.getMavenSession();
131         csControl.setReturnValue( session, MockControl.ZERO_OR_MORE );
132 
133         cs.getProject();
134         csControl.setReturnValue( new MavenProject( model ), MockControl.ZERO_OR_MORE );
135 
136         final MockControl lrCtl = MockControl.createControl( ArtifactRepository.class );
137         final ArtifactRepository lr = (ArtifactRepository) lrCtl.getMock();
138         mm.add( lrCtl );
139 
140         lr.getBasedir();
141         lrCtl.setReturnValue( "/path/to/local/repo", MockControl.ZERO_OR_MORE );
142 
143         cs.getLocalRepository();
144         csControl.setReturnValue( lr, MockControl.ZERO_OR_MORE );
145 
146         mm.replayAll();
147 
148         final Object result = new AssemblyExpressionEvaluator( cs ).evaluate( "assembly.${groupId}" );
149 
150         assertEquals( "assembly.still.another.id", result );
151 
152         mm.verifyAll();
153         mm.clear();
154     }
155 
156     public void testShouldReturnUnchangedInputForUnresolvedExpression()
157         throws ExpressionEvaluationException
158     {
159         final Model model = new Model();
160         model.setArtifactId( "artifact-id" );
161         model.setGroupId( "group.id" );
162         model.setVersion( "1" );
163         model.setPackaging( "jar" );
164 
165         configSourceStub.setProject( new MavenProject( model ) );
166 
167         final Object result = new AssemblyExpressionEvaluator( configSourceStub ).evaluate( "assembly.${unresolved}" );
168 
169         assertEquals( "assembly.${unresolved}", result );
170     }
171 
172     public void testShouldInterpolateMultiDotProjectExpression()
173         throws ExpressionEvaluationException
174     {
175         final Build build = new Build();
176         build.setFinalName( "final-name" );
177 
178         final Model model = new Model();
179         model.setBuild( build );
180 
181         configSourceStub.setProject( new MavenProject( model ) );
182 
183         final Object result =
184             new AssemblyExpressionEvaluator( configSourceStub ).evaluate( "assembly.${project.build.finalName}" );
185 
186         assertEquals( "assembly.final-name", result );
187     }
188 
189 }