View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.assembly.interpolation;
20  
21  import java.util.Properties;
22  
23  import org.apache.maven.model.Build;
24  import org.apache.maven.model.Model;
25  import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
26  import org.apache.maven.plugins.assembly.testutils.PojoConfigSource;
27  import org.apache.maven.project.MavenProject;
28  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
29  import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator;
30  import org.codehaus.plexus.interpolation.fixed.PropertiesBasedValueSource;
31  import org.junit.Test;
32  import org.junit.runner.RunWith;
33  import org.mockito.junit.MockitoJUnitRunner;
34  
35  import static org.junit.Assert.assertEquals;
36  import static org.mockito.Mockito.mock;
37  import static org.mockito.Mockito.verify;
38  import static org.mockito.Mockito.when;
39  
40  @RunWith(MockitoJUnitRunner.class)
41  public class AssemblyExpressionEvaluatorTest {
42      private final PojoConfigSource configSourceStub = new PojoConfigSource();
43  
44      @Test
45      public void testShouldResolveModelGroupId() throws ExpressionEvaluationException {
46          final Model model = new Model();
47          model.setArtifactId("artifact-id");
48          model.setGroupId("group.id");
49          model.setVersion("1");
50          model.setPackaging("jar");
51  
52          configSourceStub.setMavenProject(new MavenProject(model));
53          setupInterpolation();
54  
55          final Object result = new AssemblyExpressionEvaluator(configSourceStub).evaluate("assembly.${groupId}");
56  
57          assertEquals("assembly.group.id", result);
58      }
59  
60      private void setupInterpolation() {
61          configSourceStub.setRootInterpolator(FixedStringSearchInterpolator.create());
62          configSourceStub.setEnvironmentInterpolator(FixedStringSearchInterpolator.create());
63          configSourceStub.setEnvInterpolator(FixedStringSearchInterpolator.create());
64      }
65  
66      @Test
67      public void testShouldResolveModelPropertyBeforeModelGroupId() throws ExpressionEvaluationException {
68          final Model model = new Model();
69          model.setArtifactId("artifact-id");
70          model.setGroupId("group.id");
71          model.setVersion("1");
72          model.setPackaging("jar");
73  
74          final Properties props = new Properties();
75          props.setProperty("groupId", "other.id");
76  
77          model.setProperties(props);
78  
79          configSourceStub.setMavenProject(new MavenProject(model));
80          setupInterpolation();
81  
82          final Object result = new AssemblyExpressionEvaluator(configSourceStub).evaluate("assembly.${groupId}");
83  
84          assertEquals("assembly.other.id", result);
85      }
86  
87      @Test
88      public void testShouldResolveContextValueBeforeModelPropertyOrModelGroupIdInAssemblyId()
89              throws ExpressionEvaluationException {
90          final Model model = new Model();
91          model.setArtifactId("artifact-id");
92          model.setGroupId("group.id");
93          model.setVersion("1");
94          model.setPackaging("jar");
95  
96          final Properties props = new Properties();
97          props.setProperty("groupId", "other.id");
98  
99          model.setProperties(props);
100 
101         final Properties execProps = new Properties();
102         execProps.setProperty("groupId", "still.another.id");
103 
104         PropertiesBasedValueSource cliProps = new PropertiesBasedValueSource(execProps);
105 
106         AssemblerConfigurationSource cs = mock(AssemblerConfigurationSource.class);
107         when(cs.getCommandLinePropsInterpolator()).thenReturn(FixedStringSearchInterpolator.create(cliProps));
108         when(cs.getRepositoryInterpolator()).thenReturn(FixedStringSearchInterpolator.create());
109         when(cs.getEnvInterpolator()).thenReturn(FixedStringSearchInterpolator.create());
110         when(cs.getProject()).thenReturn(new MavenProject(model));
111 
112         final Object result = new AssemblyExpressionEvaluator(cs).evaluate("assembly.${groupId}");
113 
114         assertEquals("assembly.still.another.id", result);
115 
116         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
117         verify(cs).getCommandLinePropsInterpolator();
118         verify(cs).getRepositoryInterpolator();
119         verify(cs).getEnvInterpolator();
120         verify(cs).getProject();
121     }
122 
123     @Test
124     public void testShouldReturnUnchangedInputForUnresolvedExpression() throws ExpressionEvaluationException {
125         final Model model = new Model();
126         model.setArtifactId("artifact-id");
127         model.setGroupId("group.id");
128         model.setVersion("1");
129         model.setPackaging("jar");
130 
131         configSourceStub.setMavenProject(new MavenProject(model));
132         setupInterpolation();
133 
134         final Object result = new AssemblyExpressionEvaluator(configSourceStub).evaluate("assembly.${unresolved}");
135 
136         assertEquals("assembly.${unresolved}", result);
137     }
138 
139     @Test
140     public void testShouldInterpolateMultiDotProjectExpression() throws ExpressionEvaluationException {
141         final Build build = new Build();
142         build.setFinalName("final-name");
143 
144         final Model model = new Model();
145         model.setBuild(build);
146 
147         configSourceStub.setMavenProject(new MavenProject(model));
148         setupInterpolation();
149 
150         final Object result =
151                 new AssemblyExpressionEvaluator(configSourceStub).evaluate("assembly.${project.build.finalName}");
152 
153         assertEquals("assembly.final-name", result);
154     }
155 }