View Javadoc
1   package org.apache.maven.archetype.ui.generation;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   /*
7    * Licensed to the Apache Software Foundation (ASF) under one
8    * or more contributor license agreements.  See the NOTICE file
9    * distributed with this work for additional information
10   * regarding copyright ownership.  The ASF licenses this file
11   * to you under the Apache License, Version 2.0 (the
12   * "License"); you may not use this file except in compliance
13   * with the License.  You may obtain a copy of the License at
14   *
15   *   http://www.apache.org/licenses/LICENSE-2.0
16   *
17   * Unless required by applicable law or agreed to in writing,
18   * software distributed under the License is distributed on an
19   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20   * KIND, either express or implied.  See the License for the
21   * specific language governing permissions and limitations
22   * under the License.
23   */
24  
25  import java.util.Properties;
26  import java.util.regex.Pattern;
27  import org.apache.maven.archetype.ArchetypeGenerationRequest;
28  import org.apache.maven.archetype.common.ArchetypeArtifactManager;
29  import org.apache.maven.archetype.metadata.ArchetypeDescriptor;
30  import org.apache.maven.archetype.metadata.RequiredProperty;
31  import org.apache.maven.archetype.ui.ArchetypeConfiguration;
32  import org.apache.maven.artifact.repository.ArtifactRepository;
33  import org.apache.maven.project.ProjectBuildingRequest;
34  import org.codehaus.plexus.PlexusTestCase;
35  import org.easymock.EasyMock;
36  import org.easymock.IAnswer;
37  
38  /**
39   * Tests the ability to use variables in default fields in batch mode.
40   */
41  public class DefaultArchetypeGenerationConfigurator2Test
42      extends PlexusTestCase
43  {
44      private DefaultArchetypeGenerationConfigurator configurator;
45      private ArchetypeGenerationQueryer queryer;
46      private ArchetypeDescriptor descriptor;
47  
48      @Override
49      public void setUp()
50          throws Exception
51      {
52          super.setUp();
53  
54          configurator = (DefaultArchetypeGenerationConfigurator) lookup( ArchetypeGenerationConfigurator.ROLE );
55  
56          ProjectBuildingRequest buildingRequest = null;
57          
58          descriptor = new ArchetypeDescriptor();
59          RequiredProperty groupId = new RequiredProperty();
60          groupId.setKey( "groupId" );
61          groupId.setDefaultValue( "com.example.${groupName}" );
62          RequiredProperty artifactId = new RequiredProperty();
63          artifactId.setKey( "artifactId" );
64          artifactId.setDefaultValue( "${serviceName}" );
65          RequiredProperty thePackage = new RequiredProperty();
66          thePackage.setKey( "package" );
67          thePackage.setDefaultValue( "com.example.${groupName}" );
68          RequiredProperty groupName = new RequiredProperty();
69          groupName.setKey( "groupName" );
70          groupName.setDefaultValue( null );
71          RequiredProperty serviceName = new RequiredProperty();
72          serviceName.setKey( "serviceName" );
73          serviceName.setDefaultValue( null );
74          descriptor.addRequiredProperty( groupId );
75          descriptor.addRequiredProperty( artifactId );
76          descriptor.addRequiredProperty( thePackage );
77          descriptor.addRequiredProperty( groupName );
78          descriptor.addRequiredProperty( serviceName );
79          
80          ArchetypeArtifactManager manager = EasyMock.createMock ( ArchetypeArtifactManager.class );
81          
82          List<ArtifactRepository> x = new ArrayList<>();
83          EasyMock.expect( manager.exists( "archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null, null, x,
84                                           buildingRequest ) ).andReturn( true );
85          EasyMock.expect( manager.isFileSetArchetype( "archetypeGroupId", "archetypeArtifactId", "archetypeVersion",
86                                                       null, null, x, buildingRequest ) ).andReturn( true );
87          EasyMock.expect( manager.isOldArchetype( "archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null,
88                                                   null, x, buildingRequest ) ).andReturn( false );
89          EasyMock.expect( manager.getFileSetArchetypeDescriptor( "archetypeGroupId", "archetypeArtifactId",
90                                                                  "archetypeVersion", null, null, x,
91                                                                  buildingRequest ) ).andReturn( descriptor );
92         
93          EasyMock.replay( manager );
94          configurator.setArchetypeArtifactManager( manager );
95     
96          queryer = EasyMock.mock( ArchetypeGenerationQueryer.class );
97          configurator.setArchetypeGenerationQueryer( queryer );
98      }
99  
100     public void testJIRA_509_FileSetArchetypeDefaultsWithVariables() throws Exception
101     {
102         ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
103         request.setArchetypeGroupId( "archetypeGroupId" );
104         request.setArchetypeArtifactId( "archetypeArtifactId" );
105         request.setArchetypeVersion( "archetypeVersion" );
106         Properties properties = new Properties();
107         properties.setProperty( "groupName", "myGroupName" );
108         properties.setProperty( "serviceName", "myServiceName" );
109         
110         configurator.configureArchetype( request, Boolean.FALSE, properties );
111         
112         assertEquals( "com.example.myGroupName", request.getGroupId() );
113         assertEquals( "myServiceName", request.getArtifactId() );
114         assertEquals( "1.0-SNAPSHOT", request.getVersion() );
115         assertEquals( "com.example.myGroupName", request.getPackage() );
116     }
117 
118     public void testInteractive() throws Exception
119     {
120         ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
121         request.setArchetypeGroupId( "archetypeGroupId" );
122         request.setArchetypeArtifactId( "archetypeArtifactId" );
123         request.setArchetypeVersion( "archetypeVersion" );
124         Properties properties = new Properties();
125 
126         EasyMock.expect( queryer.getPropertyValue( EasyMock.eq("groupName"), EasyMock.anyString(),
127                         EasyMock.<Pattern> isNull() ) ).andReturn( "myGroupName" );
128 
129         EasyMock.expect( queryer.getPropertyValue( EasyMock.eq("serviceName"), EasyMock.anyString(),
130                         EasyMock.<Pattern> isNull() ) ).andReturn( "myServiceName" );
131 
132         EasyMock.expect( queryer.getPropertyValue( EasyMock.anyString(), EasyMock.anyString(),
133                         EasyMock.<Pattern> anyObject())).andAnswer( new IAnswer<String>() {
134 
135                             @Override
136                             public String answer() throws Throwable {
137                                 return (String) EasyMock.getCurrentArguments()[1];
138                             }}
139                         ).anyTimes();
140 
141         EasyMock.expect( queryer.confirmConfiguration( EasyMock.<ArchetypeConfiguration> anyObject() ) )
142                         .andReturn( Boolean.TRUE );
143 
144         EasyMock.replay( queryer );
145         configurator.configureArchetype( request, Boolean.TRUE, properties );
146 
147         assertEquals( "com.example.myGroupName", request.getGroupId() );
148         assertEquals( "myServiceName", request.getArtifactId() );
149         assertEquals( "1.0-SNAPSHOT", request.getVersion() );
150         assertEquals( "com.example.myGroupName", request.getPackage() );
151     }
152 
153     public void testArchetype406ComplexCustomPropertyValue() throws Exception
154     {
155         RequiredProperty custom = new RequiredProperty();
156         custom.setKey( "serviceUpper" );
157         custom.setDefaultValue( "${serviceName.toUpperCase()}" );
158         descriptor.addRequiredProperty( custom );
159 
160         ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
161         request.setArchetypeGroupId( "archetypeGroupId" );
162         request.setArchetypeArtifactId( "archetypeArtifactId" );
163         request.setArchetypeVersion( "archetypeVersion" );
164         Properties properties = new Properties();
165 
166         EasyMock.expect( queryer.getPropertyValue( EasyMock.eq("groupName"), EasyMock.anyString(),
167                         EasyMock.<Pattern> isNull() ) ).andReturn( "myGroupName" );
168 
169         EasyMock.expect( queryer.getPropertyValue( EasyMock.eq("serviceName"), EasyMock.anyString(),
170                         EasyMock.<Pattern> isNull() ) ).andReturn( "myServiceName" );
171 
172         EasyMock.expect( queryer.getPropertyValue( EasyMock.anyString(), EasyMock.anyString(),
173                         EasyMock.<Pattern> anyObject())).andAnswer( new IAnswer<String>() {
174 
175                             @Override
176                             public String answer() throws Throwable {
177                                 return (String) EasyMock.getCurrentArguments()[1];
178                             }}
179                         ).anyTimes();
180 
181         EasyMock.expect( queryer.confirmConfiguration( EasyMock.<ArchetypeConfiguration> anyObject() ) )
182                         .andReturn( Boolean.TRUE );
183 
184         EasyMock.replay( queryer );
185         configurator.configureArchetype( request, Boolean.TRUE, properties );
186 
187         assertEquals( "MYSERVICENAME", request.getProperties().get( "serviceUpper" ) );
188     }
189 
190     public void testArchetype618() throws Exception
191     {
192         RequiredProperty custom = getRequiredProperty( "serviceName" );
193         custom.setKey( "camelArtifact" );
194         custom.setDefaultValue( "${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', '')}" );
195         descriptor.addRequiredProperty( custom );
196 
197         getRequiredProperty( "artifactId" ).setDefaultValue( null );
198 
199         ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
200         request.setArchetypeGroupId( "archetypeGroupId" );
201         request.setArchetypeArtifactId( "archetypeArtifactId" );
202         request.setArchetypeVersion( "archetypeVersion" );
203         Properties properties = new Properties();
204 
205         EasyMock.expect( queryer.getPropertyValue( EasyMock.eq("groupName"), EasyMock.anyString(),
206                         EasyMock.<Pattern> isNull() ) ).andReturn( "myGroupName" );
207 
208         EasyMock.expect( queryer.getPropertyValue( EasyMock.eq("artifactId"), EasyMock.anyString(),
209                         EasyMock.<Pattern> isNull() ) ).andReturn( "my-service-name" );
210 
211         EasyMock.expect( queryer.getPropertyValue( EasyMock.anyString(), EasyMock.anyString(),
212                         EasyMock.<Pattern> anyObject())).andAnswer( new IAnswer<String>() {
213 
214                             @Override
215                             public String answer() throws Throwable {
216                                 return (String) EasyMock.getCurrentArguments()[1];
217                             }}
218                         ).anyTimes();
219 
220         EasyMock.expect( queryer.confirmConfiguration( EasyMock.<ArchetypeConfiguration> anyObject() ) )
221                         .andReturn( Boolean.TRUE );
222 
223         EasyMock.replay( queryer );
224         configurator.configureArchetype( request, Boolean.TRUE, properties );
225 
226         assertEquals( "MyServiceName", request.getProperties().get( "camelArtifact" ) );
227     }
228 
229     private RequiredProperty getRequiredProperty( String propertyName )
230     {
231         if ( propertyName != null )
232         {
233             for ( RequiredProperty candidate : descriptor.getRequiredProperties() )
234             {
235                 if ( propertyName.equals( candidate.getKey() ) )
236                 {
237                     return candidate;
238                 }
239             }
240         }
241         return null;
242     }
243 }