1 package org.apache.maven.archetype.ui.generation;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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 }