1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.archetype.ui.generation;
20
21 import java.io.File;
22 import java.util.Properties;
23
24 import org.apache.maven.archetype.ArchetypeGenerationRequest;
25 import org.apache.maven.archetype.common.ArchetypeArtifactManager;
26 import org.apache.maven.archetype.metadata.ArchetypeDescriptor;
27 import org.apache.maven.archetype.metadata.RequiredProperty;
28 import org.codehaus.plexus.ContainerConfiguration;
29 import org.codehaus.plexus.PlexusTestCase;
30 import org.easymock.EasyMock;
31 import org.easymock.IAnswer;
32
33 import static org.easymock.EasyMock.anyObject;
34 import static org.easymock.EasyMock.anyString;
35 import static org.easymock.EasyMock.eq;
36 import static org.easymock.EasyMock.isNull;
37
38
39
40
41 public class DefaultArchetypeGenerationConfigurator2Test extends PlexusTestCase {
42 private DefaultArchetypeGenerationConfigurator configurator;
43 private ArchetypeGenerationQueryer queryer;
44 private ArchetypeDescriptor descriptor;
45
46 @Override
47 protected void customizeContainerConfiguration(ContainerConfiguration configuration) {
48 configuration.setClassPathScanning("index");
49 }
50
51 @Override
52 public void setUp() throws Exception {
53 super.setUp();
54
55 configurator = (DefaultArchetypeGenerationConfigurator) lookup(ArchetypeGenerationConfigurator.ROLE);
56
57 descriptor = new ArchetypeDescriptor();
58 RequiredProperty groupId = new RequiredProperty();
59 groupId.setKey("groupId");
60 groupId.setDefaultValue("com.example.${groupName}");
61 RequiredProperty artifactId = new RequiredProperty();
62 artifactId.setKey("artifactId");
63 artifactId.setDefaultValue("${serviceName}");
64 RequiredProperty thePackage = new RequiredProperty();
65 thePackage.setKey("package");
66 thePackage.setDefaultValue("com.example.${groupName}");
67 RequiredProperty groupName = new RequiredProperty();
68 groupName.setKey("groupName");
69 groupName.setDefaultValue(null);
70 RequiredProperty serviceName = new RequiredProperty();
71 serviceName.setKey("serviceName");
72 serviceName.setDefaultValue(null);
73 descriptor.addRequiredProperty(groupId);
74 descriptor.addRequiredProperty(artifactId);
75 descriptor.addRequiredProperty(thePackage);
76 descriptor.addRequiredProperty(groupName);
77 descriptor.addRequiredProperty(serviceName);
78
79 ArchetypeArtifactManager manager = EasyMock.createMock(ArchetypeArtifactManager.class);
80
81 File archetype = new File("archetype.jar");
82
83 EasyMock.expect(manager.exists(
84 eq("archetypeGroupId"),
85 eq("archetypeArtifactId"),
86 eq("archetypeVersion"),
87 anyObject(),
88 anyObject()))
89 .andReturn(true);
90 EasyMock.expect(manager.getArchetypeFile(
91 eq("archetypeGroupId"),
92 eq("archetypeArtifactId"),
93 eq("archetypeVersion"),
94 anyObject(),
95 anyObject()))
96 .andReturn(archetype);
97 EasyMock.expect(manager.isFileSetArchetype(archetype)).andReturn(true);
98 EasyMock.expect(manager.isOldArchetype(archetype)).andReturn(false);
99 EasyMock.expect(manager.getFileSetArchetypeDescriptor(archetype)).andReturn(descriptor);
100
101 EasyMock.replay(manager);
102 configurator.setArchetypeArtifactManager(manager);
103
104 queryer = EasyMock.mock(ArchetypeGenerationQueryer.class);
105 configurator.setArchetypeGenerationQueryer(queryer);
106 }
107
108 public void testJIRA509FileSetArchetypeDefaultsWithVariables() throws Exception {
109 ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
110 request.setArchetypeGroupId("archetypeGroupId");
111 request.setArchetypeArtifactId("archetypeArtifactId");
112 request.setArchetypeVersion("archetypeVersion");
113 Properties properties = new Properties();
114 properties.setProperty("groupName", "myGroupName");
115 properties.setProperty("serviceName", "myServiceName");
116
117 configurator.configureArchetype(request, Boolean.FALSE, properties);
118
119 assertEquals("com.example.myGroupName", request.getGroupId());
120 assertEquals("myServiceName", request.getArtifactId());
121 assertEquals("1.0-SNAPSHOT", request.getVersion());
122 assertEquals("com.example.myGroupName", request.getPackage());
123 }
124
125 public void testInteractive() throws Exception {
126 ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
127 request.setArchetypeGroupId("archetypeGroupId");
128 request.setArchetypeArtifactId("archetypeArtifactId");
129 request.setArchetypeVersion("archetypeVersion");
130 Properties properties = new Properties();
131
132 EasyMock.expect(queryer.getPropertyValue(eq("groupName"), anyString(), isNull()))
133 .andReturn("myGroupName");
134
135 EasyMock.expect(queryer.getPropertyValue(eq("serviceName"), anyString(), isNull()))
136 .andReturn("myServiceName");
137
138 EasyMock.expect(queryer.getPropertyValue(anyString(), anyString(), anyObject()))
139 .andAnswer(new IAnswer<String>() {
140
141 @Override
142 public String answer() throws Throwable {
143 return (String) EasyMock.getCurrentArguments()[1];
144 }
145 })
146 .anyTimes();
147
148 EasyMock.expect(queryer.confirmConfiguration(anyObject())).andReturn(Boolean.TRUE);
149
150 EasyMock.replay(queryer);
151 configurator.configureArchetype(request, Boolean.TRUE, properties);
152
153 assertEquals("com.example.myGroupName", request.getGroupId());
154 assertEquals("myServiceName", request.getArtifactId());
155 assertEquals("1.0-SNAPSHOT", request.getVersion());
156 assertEquals("com.example.myGroupName", request.getPackage());
157 }
158
159 public void testArchetype406ComplexCustomPropertyValue() throws Exception {
160 RequiredProperty custom = new RequiredProperty();
161 custom.setKey("serviceUpper");
162 custom.setDefaultValue("${serviceName.toUpperCase()}");
163 descriptor.addRequiredProperty(custom);
164
165 ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
166 request.setArchetypeGroupId("archetypeGroupId");
167 request.setArchetypeArtifactId("archetypeArtifactId");
168 request.setArchetypeVersion("archetypeVersion");
169 Properties properties = new Properties();
170
171 EasyMock.expect(queryer.getPropertyValue(eq("groupName"), anyString(), isNull()))
172 .andReturn("myGroupName");
173
174 EasyMock.expect(queryer.getPropertyValue(eq("serviceName"), anyString(), isNull()))
175 .andReturn("myServiceName");
176
177 EasyMock.expect(queryer.getPropertyValue(anyString(), anyString(), anyObject()))
178 .andAnswer(new IAnswer<String>() {
179
180 @Override
181 public String answer() throws Throwable {
182 return (String) EasyMock.getCurrentArguments()[1];
183 }
184 })
185 .anyTimes();
186
187 EasyMock.expect(queryer.confirmConfiguration(anyObject())).andReturn(Boolean.TRUE);
188
189 EasyMock.replay(queryer);
190 configurator.configureArchetype(request, Boolean.TRUE, properties);
191
192 assertEquals("MYSERVICENAME", request.getProperties().get("serviceUpper"));
193 }
194
195 public void testArchetype618() throws Exception {
196 RequiredProperty custom = getRequiredProperty("serviceName");
197 custom.setKey("camelArtifact");
198 custom.setDefaultValue(
199 "${artifactId.class.forName('org.codehaus.plexus.util.StringUtils').capitaliseAllWords($artifactId.replaceAll('[^A-Za-z_\\$0-9]', ' ').replaceFirst('^(\\d)', '_$1').replaceAll('\\d', '$0 ').replaceAll('[A-Z](?=[^A-Z])', ' $0').toLowerCase()).replaceAll('\\s', '')}");
200 descriptor.addRequiredProperty(custom);
201
202 getRequiredProperty("artifactId").setDefaultValue(null);
203
204 ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
205 request.setArchetypeGroupId("archetypeGroupId");
206 request.setArchetypeArtifactId("archetypeArtifactId");
207 request.setArchetypeVersion("archetypeVersion");
208 Properties properties = new Properties();
209
210 EasyMock.expect(queryer.getPropertyValue(eq("groupName"), anyString(), isNull()))
211 .andReturn("myGroupName");
212
213 EasyMock.expect(queryer.getPropertyValue(eq("artifactId"), anyString(), isNull()))
214 .andReturn("my-service-name");
215
216 EasyMock.expect(queryer.getPropertyValue(anyString(), anyString(), anyObject()))
217 .andAnswer(new IAnswer<String>() {
218
219 @Override
220 public String answer() throws Throwable {
221 return (String) EasyMock.getCurrentArguments()[1];
222 }
223 })
224 .anyTimes();
225
226 EasyMock.expect(queryer.confirmConfiguration(anyObject())).andReturn(Boolean.TRUE);
227
228 EasyMock.replay(queryer);
229 configurator.configureArchetype(request, Boolean.TRUE, properties);
230
231 assertEquals("MyServiceName", request.getProperties().get("camelArtifact"));
232 }
233
234 private RequiredProperty getRequiredProperty(String propertyName) {
235 if (propertyName != null) {
236 for (RequiredProperty candidate : descriptor.getRequiredProperties()) {
237 if (propertyName.equals(candidate.getKey())) {
238 return candidate;
239 }
240 }
241 }
242 return null;
243 }
244 }