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