View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.archetype.ui.generation;
20  
21  import java.io.File;
22  import java.util.Properties;
23  
24  import org.apache.maven.archetype.ArchetypeGenerationRequest;
25  import org.apache.maven.archetype.common.ArchetypeArtifactManager;
26  import org.apache.maven.archetype.exception.ArchetypeGenerationConfigurationFailure;
27  import org.apache.maven.archetype.exception.ArchetypeNotConfigured;
28  import org.apache.maven.archetype.exception.ArchetypeNotDefined;
29  import org.apache.maven.archetype.exception.UnknownArchetype;
30  import org.apache.maven.archetype.old.descriptor.ArchetypeDescriptor;
31  import org.codehaus.plexus.ContainerConfiguration;
32  import org.codehaus.plexus.PlexusTestCase;
33  import org.codehaus.plexus.components.interactivity.PrompterException;
34  import org.easymock.EasyMock;
35  
36  import static org.easymock.EasyMock.anyObject;
37  import static org.easymock.EasyMock.eq;
38  
39  /**
40   * TODO probably testing a little deep, could just test ArchetypeConfiguration
41   */
42  public class DefaultArchetypeGenerationConfiguratorTest extends PlexusTestCase {
43      private DefaultArchetypeGenerationConfigurator configurator;
44  
45      @Override
46      protected void customizeContainerConfiguration(ContainerConfiguration configuration) {
47          configuration.setClassPathScanning("index");
48      }
49  
50      @Override
51      public void setUp() throws Exception {
52          super.setUp();
53  
54          configurator = (DefaultArchetypeGenerationConfigurator) lookup(ArchetypeGenerationConfigurator.ROLE);
55  
56          ArchetypeArtifactManager manager = EasyMock.createMock(ArchetypeArtifactManager.class);
57  
58          File archetype = new File("archetype.jar");
59          EasyMock.expect(manager.exists(
60                          eq("archetypeGroupId"),
61                          eq("archetypeArtifactId"),
62                          eq("archetypeVersion"),
63                          anyObject(),
64                          anyObject()))
65                  .andReturn(true);
66          EasyMock.expect(manager.getArchetypeFile(
67                          eq("archetypeGroupId"),
68                          eq("archetypeArtifactId"),
69                          eq("archetypeVersion"),
70                          anyObject(),
71                          anyObject()))
72                  .andReturn(archetype);
73          EasyMock.expect(manager.isFileSetArchetype(archetype)).andReturn(false);
74          EasyMock.expect(manager.isOldArchetype(archetype)).andReturn(true);
75  
76          EasyMock.expect(manager.getOldArchetypeDescriptor(archetype)).andReturn(new ArchetypeDescriptor());
77  
78          EasyMock.replay(manager);
79          configurator.setArchetypeArtifactManager(manager);
80      }
81  
82      public void testOldArchetypeGeneratedFieldsInRequestBatchMode()
83              throws PrompterException, ArchetypeGenerationConfigurationFailure, ArchetypeNotConfigured, UnknownArchetype,
84                      ArchetypeNotDefined {
85          ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
86          request.setArchetypeGroupId("archetypeGroupId");
87          request.setArchetypeArtifactId("archetypeArtifactId");
88          request.setArchetypeVersion("archetypeVersion");
89          Properties properties = new Properties();
90          properties.setProperty("groupId", "preset-groupId");
91          properties.setProperty("artifactId", "preset-artifactId");
92          properties.setProperty("version", "preset-gen-version");
93          properties.setProperty("package", "preset-gen-package");
94  
95          configurator.configureArchetype(request, Boolean.FALSE, properties);
96  
97          assertEquals("preset-groupId", request.getGroupId());
98          assertEquals("preset-artifactId", request.getArtifactId());
99          assertEquals("preset-gen-version", request.getVersion());
100         assertEquals("preset-gen-package", request.getPackage());
101     }
102 
103     public void testOldArchetypeGeneratedFieldsDefaultsBatchMode()
104             throws PrompterException, UnknownArchetype, ArchetypeNotDefined, ArchetypeGenerationConfigurationFailure,
105                     ArchetypeNotConfigured {
106         ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
107         request.setArchetypeGroupId("archetypeGroupId");
108         request.setArchetypeArtifactId("archetypeArtifactId");
109         request.setArchetypeVersion("archetypeVersion");
110         Properties properties = new Properties();
111         properties.setProperty("groupId", "preset-groupId");
112         properties.setProperty("artifactId", "preset-artifactId");
113 
114         configurator.configureArchetype(request, Boolean.FALSE, properties);
115 
116         assertEquals("preset-groupId", request.getGroupId());
117         assertEquals("preset-artifactId", request.getArtifactId());
118         assertEquals("1.0-SNAPSHOT", request.getVersion());
119         assertEquals("preset-groupId", request.getPackage());
120     }
121 
122     // TODO: should test this in interactive mode to check for prompting
123     public void testOldArchetypeGeneratedFieldsDefaultsMissingGroupId()
124             throws PrompterException, UnknownArchetype, ArchetypeNotDefined, ArchetypeGenerationConfigurationFailure {
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             configurator.configureArchetype(request, Boolean.FALSE, properties);
134             fail("An exception must be thrown");
135         } catch (ArchetypeNotConfigured e) {
136 
137         }
138     }
139 }