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 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
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 }