View Javadoc
1   package org.apache.maven.archetype.ui.generation;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.archetype.ArchetypeGenerationRequest;
23  import org.apache.maven.archetype.catalog.Archetype;
24  import org.apache.maven.archetype.exception.ArchetypeNotDefined;
25  import org.apache.maven.archetype.exception.ArchetypeSelectionFailure;
26  import org.apache.maven.archetype.exception.UnknownArchetype;
27  import org.apache.maven.archetype.exception.UnknownGroup;
28  import org.apache.maven.archetype.ui.ArchetypeDefinition;
29  import org.codehaus.plexus.PlexusTestCase;
30  import org.codehaus.plexus.components.interactivity.PrompterException;
31  import org.easymock.AbstractMatcher;
32  import org.easymock.ArgumentsMatcher;
33  import org.easymock.MockControl;
34  
35  import java.io.IOException;
36  import java.util.Collections;
37  import java.util.List;
38  import java.util.Map;
39  
40  public class DefaultArchetypeSelectorTest
41      extends PlexusTestCase
42  {
43      private DefaultArchetypeSelector selector;
44  
45      public void setUp()
46          throws Exception
47      {
48          super.setUp();
49  
50          selector = (DefaultArchetypeSelector) lookup( ArchetypeSelector.ROLE );
51      }
52  
53      public void testArchetypeCoordinatesInRequest()
54          throws PrompterException, IOException, UnknownGroup, ArchetypeSelectionFailure, UnknownArchetype,
55          ArchetypeNotDefined
56      {
57          ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
58          request.setArchetypeArtifactId( "preset-artifactId" );
59          request.setArchetypeGroupId( "preset-groupId" );
60          request.setArchetypeVersion( "preset-version" );
61  
62          MockControl control = MockControl.createControl( ArchetypeSelectionQueryer.class );
63  
64          ArchetypeSelectionQueryer queryer = (ArchetypeSelectionQueryer) control.getMock();
65          // expect it to not be called
66  
67          control.replay();
68  
69          selector.setArchetypeSelectionQueryer( queryer );
70  
71          selector.selectArchetype( request, Boolean.TRUE, "" );
72  
73          control.verify();
74  
75          assertEquals( "preset-groupId", request.getArchetypeGroupId() );
76          assertEquals( "preset-artifactId", request.getArchetypeArtifactId() );
77          assertEquals( "preset-version", request.getArchetypeVersion() );
78      }
79  
80      public void testArchetypeArtifactIdInRequest()
81          throws PrompterException, IOException, UnknownGroup, ArchetypeSelectionFailure, UnknownArchetype,
82          ArchetypeNotDefined
83      {
84          ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
85          request.setArchetypeArtifactId( "preset-artifactId" );
86  
87          MockControl control = MockControl.createControl( ArchetypeSelectionQueryer.class );
88  
89          ArchetypeSelectionQueryer queryer = (ArchetypeSelectionQueryer) control.getMock();
90          // expect it to not be called
91  
92          control.replay();
93  
94          selector.setArchetypeSelectionQueryer( queryer );
95  
96          selector.selectArchetype( request, Boolean.TRUE, "" );
97  
98          control.verify();
99  
100         assertEquals( DefaultArchetypeSelector.DEFAULT_ARCHETYPE_GROUPID, request.getArchetypeGroupId() );
101         assertEquals( "preset-artifactId", request.getArchetypeArtifactId() );
102         assertEquals( DefaultArchetypeSelector.DEFAULT_ARCHETYPE_VERSION, request.getArchetypeVersion() );
103     }
104 
105     public void testArchetypeArtifactIdNotInRequest()
106         throws PrompterException, IOException, UnknownGroup, ArchetypeSelectionFailure, UnknownArchetype,
107         ArchetypeNotDefined
108     {
109         ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
110 
111         MockControl control = MockControl.createControl( ArchetypeSelectionQueryer.class );
112 
113         ArchetypeSelectionQueryer queryer = (ArchetypeSelectionQueryer) control.getMock();
114         queryer.selectArchetype( Collections.<String, List<Archetype>> emptyMap(), new ArchetypeDefinition() );
115         control.setMatcher( MockControl.ALWAYS_MATCHER );
116         Archetype archetype = new Archetype();
117         archetype.setArtifactId( "set-artifactId" );
118         archetype.setGroupId( "set-groupId" );
119         archetype.setVersion( "set-version" );
120         control.setReturnValue( archetype );
121 
122         control.replay();
123 
124         selector.setArchetypeSelectionQueryer( queryer );
125 
126         selector.selectArchetype( request, Boolean.TRUE, "" );
127 
128         control.verify();
129 
130         assertEquals( "set-groupId", request.getArchetypeGroupId() );
131         assertEquals( "set-artifactId", request.getArchetypeArtifactId() );
132         assertEquals( "set-version", request.getArchetypeVersion() );
133     }
134 
135     public void testArchetypeNotInRequestDefaultsInBatchMode()
136         throws PrompterException, IOException, UnknownGroup, ArchetypeSelectionFailure, UnknownArchetype,
137         ArchetypeNotDefined
138     {
139         ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
140 
141         MockControl control = MockControl.createControl( ArchetypeSelectionQueryer.class );
142 
143         ArchetypeSelectionQueryer queryer = (ArchetypeSelectionQueryer) control.getMock();
144         // expect it to not be called
145 
146         control.replay();
147 
148         selector.setArchetypeSelectionQueryer( queryer );
149 
150         selector.selectArchetype( request, Boolean.FALSE, "" );
151 
152         control.verify();
153 
154         assertEquals( DefaultArchetypeSelector.DEFAULT_ARCHETYPE_GROUPID, request.getArchetypeGroupId() );
155         assertEquals( DefaultArchetypeSelector.DEFAULT_ARCHETYPE_ARTIFACTID, request.getArchetypeArtifactId() );
156         assertEquals( DefaultArchetypeSelector.DEFAULT_ARCHETYPE_VERSION, request.getArchetypeVersion() );
157     }
158 
159     public void testArchetypeNotInRequestDefaults()
160         throws PrompterException, IOException, UnknownGroup, ArchetypeSelectionFailure, UnknownArchetype,
161         ArchetypeNotDefined
162     {
163         ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
164 
165         MockControl control = MockControl.createControl( ArchetypeSelectionQueryer.class );
166 
167         ArchetypeSelectionQueryer queryer = (ArchetypeSelectionQueryer) control.getMock();
168         queryer.selectArchetype( Collections.<String, List<Archetype>> emptyMap(), createDefaultArchetypeDefinition() );
169         control.setMatcher( createArgumentMatcher() );
170         Archetype archetype = new Archetype();
171         archetype.setArtifactId( "set-artifactId" );
172         archetype.setGroupId( "set-groupId" );
173         archetype.setVersion( "set-version" );
174         control.setReturnValue( archetype );
175 
176         control.replay();
177 
178         selector.setArchetypeSelectionQueryer( queryer );
179 
180         selector.selectArchetype( request, Boolean.TRUE, "" );
181 
182         control.verify();
183 
184         assertEquals( "set-groupId", request.getArchetypeGroupId() );
185         assertEquals( "set-artifactId", request.getArchetypeArtifactId() );
186         assertEquals( "set-version", request.getArchetypeVersion() );
187     }
188 
189     private ArchetypeDefinition createDefaultArchetypeDefinition()
190     {
191         ArchetypeDefinition definition = new ArchetypeDefinition();
192         definition.setGroupId( DefaultArchetypeSelector.DEFAULT_ARCHETYPE_GROUPID );
193         definition.setArtifactId( DefaultArchetypeSelector.DEFAULT_ARCHETYPE_ARTIFACTID );
194         definition.setVersion( DefaultArchetypeSelector.DEFAULT_ARCHETYPE_VERSION );
195         return definition;
196     }
197 
198     private static ArgumentsMatcher createArgumentMatcher()
199     {
200         return new AbstractMatcher()
201         {
202             protected boolean argumentMatches( Object o, Object o1 )
203             {
204                 return !( o instanceof ArchetypeDefinition ) ||
205                     compareArchetypeDefinition( (ArchetypeDefinition) o, (ArchetypeDefinition) o1 );
206             }
207 
208             private boolean compareArchetypeDefinition( ArchetypeDefinition o, ArchetypeDefinition o1 )
209             {
210                 if ( o1 == o )
211                 {
212                     return true;
213                 }
214                 if ( o == null )
215                 {
216                     return false;
217                 }
218 
219                 if ( o1.getArtifactId() != null
220                     ? !o1.getArtifactId().equals( o.getArtifactId() )
221                     : o.getArtifactId() != null )
222                 {
223                     return false;
224                 }
225                 if ( o1.getGroupId() != null ? !o1.getGroupId().equals( o.getGroupId() ) : o.getGroupId() != null )
226                 {
227                     return false;
228                 }
229                 if ( o1.getName() != null ? !o1.getName().equals( o.getName() ) : o.getName() != null )
230                 {
231                     return false;
232                 }
233                 if ( o1.getVersion() != null ? !o1.getVersion().equals( o.getVersion() ) : o.getVersion() != null )
234                 {
235                     return false;
236                 }
237                 if ( o1.getGoals() != null ? !o1.getGoals().equals( o.getGoals() ) : o.getGoals() != null )
238                 {
239                     return false;
240                 }
241                 if ( o1.getRepository() != null
242                     ? !o1.getRepository().equals( o.getRepository() )
243                     : o.getRepository() != null )
244                 {
245                     return false;
246                 }
247 
248                 return true;
249             }
250 
251             protected String argumentToString( Object o )
252             {
253                 return o instanceof Map ? "..." : toString( (ArchetypeDefinition) o );
254             }
255 
256             private String toString( ArchetypeDefinition archetypeDefinition )
257             {
258                 return "groupId = " + archetypeDefinition.getGroupId() + ", " + "artifactId = " +
259                     archetypeDefinition.getArtifactId() + ", " + "version = " + archetypeDefinition.getVersion();
260             }
261         };
262     }
263 }