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.common.ArchetypeArtifactManager;
24  import org.apache.maven.archetype.exception.ArchetypeGenerationConfigurationFailure;
25  import org.apache.maven.archetype.exception.ArchetypeNotConfigured;
26  import org.apache.maven.archetype.exception.ArchetypeNotDefined;
27  import org.apache.maven.archetype.exception.ArchetypeSelectionFailure;
28  import org.apache.maven.archetype.exception.UnknownArchetype;
29  import org.apache.maven.archetype.exception.UnknownGroup;
30  import org.apache.maven.archetype.old.descriptor.ArchetypeDescriptor;
31  import org.codehaus.plexus.PlexusTestCase;
32  import org.codehaus.plexus.components.interactivity.PrompterException;
33  import org.easymock.MockControl;
34  
35  import java.io.IOException;
36  import java.util.Properties;
37  
38  /**
39   * @todo probably testing a little deep, could just test ArchetypeConfiguration
40   */
41  public class DefaultArchetypeGenerationConfiguratorTest
42      extends PlexusTestCase
43  {
44      private DefaultArchetypeGenerationConfigurator configurator;
45  
46      public void setUp()
47          throws Exception
48      {
49          super.setUp();
50  
51          configurator = (DefaultArchetypeGenerationConfigurator) lookup( ArchetypeGenerationConfigurator.ROLE );
52  
53          MockControl control = MockControl.createControl( ArchetypeArtifactManager.class );
54          control.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
55  
56          ArchetypeArtifactManager manager = (ArchetypeArtifactManager) control.getMock();
57          manager.exists( "archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null, null, null );
58          control.setReturnValue( true );
59          manager.isFileSetArchetype( "archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null, null, null );
60          control.setReturnValue( false );
61          manager.isOldArchetype( "archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null, null, null );
62          control.setReturnValue( true );
63          manager.getOldArchetypeDescriptor( "archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null,
64                                                 null, null );
65          control.setReturnValue( new ArchetypeDescriptor() );
66          control.replay();
67          configurator.setArchetypeArtifactManager( manager );
68      }
69  
70      public void testOldArchetypeGeneratedFieldsInRequestBatchMode()
71          throws PrompterException, ArchetypeGenerationConfigurationFailure, IOException, ArchetypeNotConfigured,
72          UnknownArchetype, ArchetypeNotDefined
73      {
74          ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
75          request.setArchetypeGroupId( "archetypeGroupId" );
76          request.setArchetypeArtifactId( "archetypeArtifactId" );
77          request.setArchetypeVersion( "archetypeVersion" );
78          Properties properties = new Properties();
79          properties.setProperty( "groupId", "preset-groupId" );
80          properties.setProperty( "artifactId", "preset-artifactId" );
81          properties.setProperty( "version", "preset-gen-version" );
82          properties.setProperty( "package", "preset-gen-package" );
83  
84          configurator.configureArchetype( request, Boolean.FALSE, properties );
85  
86          assertEquals( "preset-groupId", request.getGroupId() );
87          assertEquals( "preset-artifactId", request.getArtifactId() );
88          assertEquals( "preset-gen-version", request.getVersion() );
89          assertEquals( "preset-gen-package", request.getPackage() );
90      }
91  
92      public void testOldArchetypeGeneratedFieldsDefaultsBatchMode()
93          throws PrompterException, IOException, UnknownGroup, ArchetypeSelectionFailure, UnknownArchetype,
94          ArchetypeNotDefined, ArchetypeGenerationConfigurationFailure, ArchetypeNotConfigured
95      {
96          ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
97          request.setArchetypeGroupId( "archetypeGroupId" );
98          request.setArchetypeArtifactId( "archetypeArtifactId" );
99          request.setArchetypeVersion( "archetypeVersion" );
100         Properties properties = new Properties();
101         properties.setProperty( "groupId", "preset-groupId" );
102         properties.setProperty( "artifactId", "preset-artifactId" );
103 
104         configurator.configureArchetype( request, Boolean.FALSE, properties );
105 
106         assertEquals( "preset-groupId", request.getGroupId() );
107         assertEquals( "preset-artifactId", request.getArtifactId() );
108         assertEquals( "1.0-SNAPSHOT", request.getVersion() );
109         assertEquals( "preset-groupId", request.getPackage() );
110     }
111 
112     // TODO: should test this in interactive mode to check for prompting
113     public void testOldArchetypeGeneratedFieldsDefaultsMissingGroupId()
114         throws PrompterException, IOException, UnknownGroup, ArchetypeSelectionFailure, UnknownArchetype,
115         ArchetypeNotDefined, ArchetypeGenerationConfigurationFailure, ArchetypeNotConfigured
116     {
117         ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
118         request.setArchetypeGroupId( "archetypeGroupId" );
119         request.setArchetypeArtifactId( "archetypeArtifactId" );
120         request.setArchetypeVersion( "archetypeVersion" );
121         Properties properties = new Properties();
122         properties.setProperty( "artifactId", "preset-artifactId" );
123 
124         try
125         {
126             configurator.configureArchetype( request, Boolean.FALSE, properties );
127             fail( "An exception must be thrown" );
128         }
129         catch ( ArchetypeNotConfigured e )
130         {
131 
132         }
133     }
134 }