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