1 package org.apache.maven.plugins.assembly.interpolation;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.junit.Assert.assertEquals;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25 import static org.mockito.Mockito.verify;
26
27 import java.util.Properties;
28
29 import org.apache.maven.model.Build;
30 import org.apache.maven.model.Model;
31 import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
32 import org.apache.maven.plugins.assembly.testutils.PojoConfigSource;
33 import org.apache.maven.project.MavenProject;
34 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
35 import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator;
36 import org.codehaus.plexus.interpolation.fixed.PropertiesBasedValueSource;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.junit.MockitoJUnitRunner;
40
41 @RunWith( MockitoJUnitRunner.class )
42 public class AssemblyExpressionEvaluatorTest
43 {
44 private final PojoConfigSource configSourceStub = new PojoConfigSource();
45
46 @Test
47 public void testShouldResolveModelGroupId()
48 throws ExpressionEvaluationException
49 {
50 final Model model = new Model();
51 model.setArtifactId( "artifact-id" );
52 model.setGroupId( "group.id" );
53 model.setVersion( "1" );
54 model.setPackaging( "jar" );
55
56 configSourceStub.setMavenProject( new MavenProject( model ) );
57 setupInterpolation();
58
59 final Object result = new AssemblyExpressionEvaluator( configSourceStub ).evaluate( "assembly.${groupId}" );
60
61 assertEquals( "assembly.group.id", result );
62 }
63
64 private void setupInterpolation()
65 {
66 configSourceStub.setRootInterpolator( FixedStringSearchInterpolator.create() );
67 configSourceStub.setEnvironmentInterpolator( FixedStringSearchInterpolator.create() );
68 configSourceStub.setEnvInterpolator( FixedStringSearchInterpolator.create() );
69 }
70
71 @Test
72 public void testShouldResolveModelPropertyBeforeModelGroupId()
73 throws ExpressionEvaluationException
74 {
75 final Model model = new Model();
76 model.setArtifactId( "artifact-id" );
77 model.setGroupId( "group.id" );
78 model.setVersion( "1" );
79 model.setPackaging( "jar" );
80
81 final Properties props = new Properties();
82 props.setProperty( "groupId", "other.id" );
83
84 model.setProperties( props );
85
86 configSourceStub.setMavenProject( new MavenProject( model ) );
87 setupInterpolation();
88
89 final Object result = new AssemblyExpressionEvaluator( configSourceStub ).evaluate( "assembly.${groupId}" );
90
91 assertEquals( "assembly.other.id", result );
92 }
93
94 @Test
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 Properties execProps = new Properties();
110 execProps.setProperty( "groupId", "still.another.id" );
111
112 PropertiesBasedValueSource cliProps = new PropertiesBasedValueSource( execProps );
113
114 AssemblerConfigurationSource cs = mock( AssemblerConfigurationSource.class );
115 when( cs.getCommandLinePropsInterpolator() ).thenReturn( FixedStringSearchInterpolator.create( cliProps ) );
116 when( cs.getRepositoryInterpolator() ).thenReturn( FixedStringSearchInterpolator.create() );
117 when( cs.getEnvInterpolator() ).thenReturn( FixedStringSearchInterpolator.create() );
118 when( cs.getProject() ).thenReturn( new MavenProject( model ) );
119
120 final Object result = new AssemblyExpressionEvaluator( cs ).evaluate( "assembly.${groupId}" );
121
122 assertEquals( "assembly.still.another.id", result );
123
124
125 verify( cs ).getCommandLinePropsInterpolator();
126 verify( cs ).getRepositoryInterpolator();
127 verify( cs ).getEnvInterpolator();
128 verify( cs ).getProject();
129 }
130
131 @Test
132 public void testShouldReturnUnchangedInputForUnresolvedExpression()
133 throws ExpressionEvaluationException
134 {
135 final Model model = new Model();
136 model.setArtifactId( "artifact-id" );
137 model.setGroupId( "group.id" );
138 model.setVersion( "1" );
139 model.setPackaging( "jar" );
140
141 configSourceStub.setMavenProject( new MavenProject( model ) );
142 setupInterpolation();
143
144 final Object result = new AssemblyExpressionEvaluator( configSourceStub ).evaluate( "assembly.${unresolved}" );
145
146 assertEquals( "assembly.${unresolved}", result );
147 }
148
149 @Test
150 public void testShouldInterpolateMultiDotProjectExpression()
151 throws ExpressionEvaluationException
152 {
153 final Build build = new Build();
154 build.setFinalName( "final-name" );
155
156 final Model model = new Model();
157 model.setBuild( build );
158
159 configSourceStub.setMavenProject( new MavenProject( model ) );
160 setupInterpolation();
161
162 final Object result =
163 new AssemblyExpressionEvaluator( configSourceStub ).evaluate( "assembly.${project.build.finalName}" );
164
165 assertEquals( "assembly.final-name", result );
166 }
167
168 }