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.catalog.Archetype;
23  import org.apache.maven.archetype.ui.ArchetypeDefinition;
24  import org.codehaus.plexus.PlexusTestCase;
25  import org.codehaus.plexus.components.interactivity.Prompter;
26  import org.codehaus.plexus.components.interactivity.PrompterException;
27  import org.easymock.AbstractMatcher;
28  import org.easymock.ArgumentsMatcher;
29  import org.easymock.MockControl;
30  
31  import java.util.ArrayList;
32  import java.util.Collections;
33  import java.util.List;
34  import java.util.Map;
35  
36  public class DefaultArchetypeSelectionQueryerTest
37      extends PlexusTestCase
38  {
39      private DefaultArchetypeSelectionQueryer queryer;
40  
41      public void setUp()
42          throws Exception
43      {
44          super.setUp();
45  
46          queryer = (DefaultArchetypeSelectionQueryer) lookup( ArchetypeSelectionQueryer.ROLE );
47      }
48  
49      public void testDefaultArchetypeInMapOtherSelection()
50          throws PrompterException
51      {
52          Map<String, List<Archetype>> map = createDefaultArchetypeCatalog();
53  
54          MockControl control = MockControl.createControl( Prompter.class );
55          Prompter prompter = (Prompter) control.getMock();
56          prompter.prompt( "", "2" );
57          control.setMatcher( createArgumentMatcher() );
58          control.setReturnValue( "1" );
59          queryer.setPrompter( prompter );
60  
61          control.replay();
62  
63          ArchetypeDefinition defaultDefinition = new ArchetypeDefinition();
64          defaultDefinition.setGroupId( "default-groupId" );
65          defaultDefinition.setArtifactId( "default-artifactId" );
66          defaultDefinition.setVersion( "default-version" );
67          Archetype archetype = queryer.selectArchetype( map, defaultDefinition );
68  
69          control.verify();
70  
71          assertEquals( "set-groupId", archetype.getGroupId() );
72          assertEquals( "set-artifactId", archetype.getArtifactId() );
73          assertEquals( "set-version", archetype.getVersion() );
74      }
75  
76      public void testDefaultArchetypeInMapDefaultSelection()
77          throws PrompterException
78      {
79          Map<String, List<Archetype>> map = createDefaultArchetypeCatalog();
80  
81          MockControl control = MockControl.createControl( Prompter.class );
82          Prompter prompter = (Prompter) control.getMock();
83          prompter.prompt( "", "2" );
84          control.setMatcher( createArgumentMatcher() );
85          control.setReturnValue( "2" );
86          queryer.setPrompter( prompter );
87  
88          control.replay();
89  
90          ArchetypeDefinition defaultDefinition = new ArchetypeDefinition();
91          defaultDefinition.setGroupId( "default-groupId" );
92          defaultDefinition.setArtifactId( "default-artifactId" );
93          defaultDefinition.setVersion( "default-version" );
94          Archetype archetype = queryer.selectArchetype( map, defaultDefinition );
95  
96          control.verify();
97  
98          assertEquals( "default-groupId", archetype.getGroupId() );
99          assertEquals( "default-artifactId", archetype.getArtifactId() );
100         assertEquals( "default-version", archetype.getVersion() );
101     }
102 
103     public void testDefaultArchetypeNotInMap()
104         throws PrompterException
105     {
106         Map<String, List<Archetype>> map = createDefaultArchetypeCatalog();
107 
108         MockControl control = MockControl.createControl( Prompter.class );
109         Prompter prompter = (Prompter) control.getMock();
110         prompter.prompt( "" );
111         control.setMatcher( createArgumentMatcher() );
112         control.setReturnValue( "1" );
113         queryer.setPrompter( prompter );
114 
115         control.replay();
116 
117         ArchetypeDefinition defaultDefinition = new ArchetypeDefinition();
118         defaultDefinition.setGroupId( "invalid-groupId" );
119         defaultDefinition.setArtifactId( "invalid-artifactId" );
120         defaultDefinition.setVersion( "invalid-version" );
121         Archetype archetype = queryer.selectArchetype( map, defaultDefinition );
122 
123         control.verify();
124 
125         assertEquals( "set-groupId", archetype.getGroupId() );
126         assertEquals( "set-artifactId", archetype.getArtifactId() );
127         assertEquals( "set-version", archetype.getVersion() );
128     }
129 
130     public void testNoDefaultArchetype()
131         throws PrompterException
132     {
133         Map<String, List<Archetype>> map = createDefaultArchetypeCatalog();
134 
135         MockControl control = MockControl.createControl( Prompter.class );
136         Prompter prompter = (Prompter) control.getMock();
137         prompter.prompt( "" );
138         control.setMatcher( createArgumentMatcher() );
139         control.setReturnValue( "1" );
140         queryer.setPrompter( prompter );
141 
142         control.replay();
143 
144         Archetype archetype = queryer.selectArchetype( map );
145 
146         control.verify();
147 
148         assertEquals( "set-groupId", archetype.getGroupId() );
149         assertEquals( "set-artifactId", archetype.getArtifactId() );
150         assertEquals( "set-version", archetype.getVersion() );
151     }
152 
153     public void testArchetypeFiltering()
154         throws PrompterException
155     {
156         Map<String, List<Archetype>> map = createDefaultArchetypeCatalog();
157 
158         MockControl control = MockControl.createControl( Prompter.class );
159         Prompter prompter = (Prompter) control.getMock();
160         prompter.prompt( "" );
161         control.setMatcher( createArgumentMatcher() );
162         control.setReturnValue( "set-artifactId" );
163         prompter.prompt( "" );
164         control.setReturnValue( "1" );
165         queryer.setPrompter( prompter );
166 
167         control.replay();
168 
169         Archetype archetype = queryer.selectArchetype( map );
170 
171         control.verify();
172 
173         assertEquals( "set-groupId", archetype.getGroupId() );
174         assertEquals( "set-artifactId", archetype.getArtifactId() );
175         assertEquals( "set-version", archetype.getVersion() );
176     }
177 
178     private static Map<String, List<Archetype>> createDefaultArchetypeCatalog()
179     {
180         List<Archetype> list = new ArrayList<Archetype>();
181         list.add( createArchetype( "set-groupId", "set-artifactId", "set-version" ) );
182         list.add( createArchetype( "default-groupId", "default-artifactId", "default-version" ) );
183         return Collections.singletonMap( "internal", list );
184     }
185 
186     private static Archetype createArchetype( String groupId, String artifactId, String version )
187     {
188         Archetype a = new Archetype();
189         a.setGroupId( groupId );
190         a.setArtifactId( artifactId );
191         a.setVersion( version );
192         return a;
193     }
194 
195     private static ArgumentsMatcher createArgumentMatcher()
196     {
197         return new AbstractMatcher()
198         {
199             private boolean isPromptString( Object o )
200             {
201                 boolean value = false;
202                 if ( o instanceof String )
203                 {
204                     String s = (String) o;
205                     if ( s.startsWith( "Choose" ) )
206                     {
207                         // ignore the prompt
208                         value = true;
209                     }
210                 }
211                 return value;
212             }
213 
214             protected boolean argumentMatches( Object o, Object o1 )
215             {
216                 return isPromptString( o1 ) || super.argumentMatches( o, o1 );
217             }
218 
219             protected String argumentToString( Object o )
220             {
221                 return isPromptString( o ) ? "..." : super.argumentToString( o );
222             }
223         };
224     }
225 }