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