1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.mockito.junit.jupiter.MockitoExtension;
34 import org.mockito.junit.jupiter.MockitoSettings;
35 import org.mockito.quality.Strictness;
36
37 import static org.junit.jupiter.api.Assertions.assertEquals;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.verify;
40 import static org.mockito.Mockito.when;
41
42 @MockitoSettings(strictness = Strictness.WARN)
43 @ExtendWith(MockitoExtension.class)
44 public class AssemblyExpressionEvaluatorTest {
45 private final PojoConfigSource configSourceStub = new PojoConfigSource();
46
47 @Test
48 public void testShouldResolveModelGroupId() throws ExpressionEvaluationException {
49 final Model model = new Model();
50 model.setArtifactId("artifact-id");
51 model.setGroupId("group.id");
52 model.setVersion("1");
53 model.setPackaging("jar");
54
55 configSourceStub.setMavenProject(new MavenProject(model));
56 setupInterpolation();
57
58 final Object result = new AssemblyExpressionEvaluator(configSourceStub).evaluate("assembly.${groupId}");
59
60 assertEquals("assembly.group.id", result);
61 }
62
63 private void setupInterpolation() {
64 configSourceStub.setRootInterpolator(FixedStringSearchInterpolator.create());
65 configSourceStub.setEnvironmentInterpolator(FixedStringSearchInterpolator.create());
66 configSourceStub.setEnvInterpolator(FixedStringSearchInterpolator.create());
67 }
68
69 @Test
70 public void testShouldResolveModelPropertyBeforeModelGroupId() throws ExpressionEvaluationException {
71 final Model model = new Model();
72 model.setArtifactId("artifact-id");
73 model.setGroupId("group.id");
74 model.setVersion("1");
75 model.setPackaging("jar");
76
77 final Properties props = new Properties();
78 props.setProperty("groupId", "other.id");
79
80 model.setProperties(props);
81
82 configSourceStub.setMavenProject(new MavenProject(model));
83 setupInterpolation();
84
85 final Object result = new AssemblyExpressionEvaluator(configSourceStub).evaluate("assembly.${groupId}");
86
87 assertEquals("assembly.other.id", result);
88 }
89
90 @Test
91 public void testShouldResolveContextValueBeforeModelPropertyOrModelGroupIdInAssemblyId()
92 throws ExpressionEvaluationException {
93 final Model model = new Model();
94 model.setArtifactId("artifact-id");
95 model.setGroupId("group.id");
96 model.setVersion("1");
97 model.setPackaging("jar");
98
99 final Properties props = new Properties();
100 props.setProperty("groupId", "other.id");
101
102 model.setProperties(props);
103
104 final Properties execProps = new Properties();
105 execProps.setProperty("groupId", "still.another.id");
106
107 PropertiesBasedValueSource cliProps = new PropertiesBasedValueSource(execProps);
108
109 AssemblerConfigurationSource cs = mock(AssemblerConfigurationSource.class);
110 when(cs.getCommandLinePropsInterpolator()).thenReturn(FixedStringSearchInterpolator.create(cliProps));
111 when(cs.getRepositoryInterpolator()).thenReturn(FixedStringSearchInterpolator.create());
112 when(cs.getEnvInterpolator()).thenReturn(FixedStringSearchInterpolator.create());
113 when(cs.getProject()).thenReturn(new MavenProject(model));
114
115 final Object result = new AssemblyExpressionEvaluator(cs).evaluate("assembly.${groupId}");
116
117 assertEquals("assembly.still.another.id", result);
118
119
120 verify(cs).getCommandLinePropsInterpolator();
121 verify(cs).getRepositoryInterpolator();
122 verify(cs).getEnvInterpolator();
123 verify(cs).getProject();
124 }
125
126 @Test
127 public void testShouldReturnUnchangedInputForUnresolvedExpression() throws ExpressionEvaluationException {
128 final Model model = new Model();
129 model.setArtifactId("artifact-id");
130 model.setGroupId("group.id");
131 model.setVersion("1");
132 model.setPackaging("jar");
133
134 configSourceStub.setMavenProject(new MavenProject(model));
135 setupInterpolation();
136
137 final Object result = new AssemblyExpressionEvaluator(configSourceStub).evaluate("assembly.${unresolved}");
138
139 assertEquals("assembly.${unresolved}", result);
140 }
141
142 @Test
143 public void testShouldInterpolateMultiDotProjectExpression() throws ExpressionEvaluationException {
144 final Build build = new Build();
145 build.setFinalName("final-name");
146
147 final Model model = new Model();
148 model.setBuild(build);
149
150 configSourceStub.setMavenProject(new MavenProject(model));
151 setupInterpolation();
152
153 final Object result =
154 new AssemblyExpressionEvaluator(configSourceStub).evaluate("assembly.${project.build.finalName}");
155
156 assertEquals("assembly.final-name", result);
157 }
158 }