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