1 package org.apache.maven.archetype.ui;
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.codehaus.plexus.PlexusTestCase;
32 import org.codehaus.plexus.components.interactivity.PrompterException;
33 import org.easymock.MockControl;
34
35 import java.io.IOException;
36 import java.util.Properties;
37
38
39
40
41 public class DefaultArchetypeGenerationConfiguratorTest
42 extends PlexusTestCase
43 {
44 private DefaultArchetypeGenerationConfigurator configurator;
45
46 public void setUp()
47 throws Exception
48 {
49 super.setUp();
50
51 configurator = (DefaultArchetypeGenerationConfigurator) lookup( ArchetypeGenerationConfigurator.ROLE );
52
53 MockControl control = MockControl.createControl( ArchetypeArtifactManager.class );
54 control.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
55
56 ArchetypeArtifactManager manager = (ArchetypeArtifactManager) control.getMock();
57 manager.exists( "archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null, null, null );
58 control.setReturnValue( true );
59 manager.isFileSetArchetype( "archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null, null, null );
60 control.setReturnValue( false );
61 manager.isOldArchetype( "archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null, null, null );
62 control.setReturnValue( true );
63 manager.getOldArchetypeDescriptor( "archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null,
64 null, null );
65 control.setReturnValue( new ArchetypeDescriptor() );
66 control.replay();
67 configurator.setArchetypeArtifactManager( manager );
68 }
69
70 public void testOldArchetypeGeneratedFieldsInRequestBatchMode()
71 throws PrompterException, ArchetypeGenerationConfigurationFailure, IOException, ArchetypeNotConfigured,
72 UnknownArchetype, ArchetypeNotDefined
73 {
74 ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
75 request.setArchetypeGroupId( "archetypeGroupId" );
76 request.setArchetypeArtifactId( "archetypeArtifactId" );
77 request.setArchetypeVersion( "archetypeVersion" );
78 Properties properties = new Properties();
79 properties.setProperty( "groupId", "preset-groupId" );
80 properties.setProperty( "artifactId", "preset-artifactId" );
81 properties.setProperty( "version", "preset-gen-version" );
82 properties.setProperty( "package", "preset-gen-package" );
83
84 configurator.configureArchetype( request, Boolean.FALSE, properties );
85
86 assertEquals( "preset-groupId", request.getGroupId() );
87 assertEquals( "preset-artifactId", request.getArtifactId() );
88 assertEquals( "preset-gen-version", request.getVersion() );
89 assertEquals( "preset-gen-package", request.getPackage() );
90 }
91
92 public void testOldArchetypeGeneratedFieldsDefaultsBatchMode()
93 throws PrompterException, IOException, UnknownGroup, ArchetypeSelectionFailure, UnknownArchetype,
94 ArchetypeNotDefined, ArchetypeGenerationConfigurationFailure, ArchetypeNotConfigured
95 {
96 ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
97 request.setArchetypeGroupId( "archetypeGroupId" );
98 request.setArchetypeArtifactId( "archetypeArtifactId" );
99 request.setArchetypeVersion( "archetypeVersion" );
100 Properties properties = new Properties();
101 properties.setProperty( "groupId", "preset-groupId" );
102 properties.setProperty( "artifactId", "preset-artifactId" );
103
104 configurator.configureArchetype( request, Boolean.FALSE, properties );
105
106 assertEquals( "preset-groupId", request.getGroupId() );
107 assertEquals( "preset-artifactId", request.getArtifactId() );
108 assertEquals( "1.0-SNAPSHOT", request.getVersion() );
109 assertEquals( "preset-groupId", request.getPackage() );
110 }
111
112
113 public void testOldArchetypeGeneratedFieldsDefaultsMissingGroupId()
114 throws PrompterException, IOException, UnknownGroup, ArchetypeSelectionFailure, UnknownArchetype,
115 ArchetypeNotDefined, ArchetypeGenerationConfigurationFailure, ArchetypeNotConfigured
116 {
117 ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
118 request.setArchetypeGroupId( "archetypeGroupId" );
119 request.setArchetypeArtifactId( "archetypeArtifactId" );
120 request.setArchetypeVersion( "archetypeVersion" );
121 Properties properties = new Properties();
122 properties.setProperty( "artifactId", "preset-artifactId" );
123
124 try{
125 configurator.configureArchetype( request, Boolean.FALSE, properties );
126 fail("An exception must be thrown");
127 }catch (ArchetypeNotConfigured e){
128
129 }
130 }
131 }