1 package org.apache.maven.plugin.ear.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.framework.TestCase;
23 import org.apache.maven.plugin.ear.EarModuleFactory;
24 import org.apache.maven.plugin.ear.EarPluginException;
25 import org.apache.maven.plugin.ear.UnknownArtifactTypeException;
26 import org.codehaus.plexus.configuration.PlexusConfigurationException;
27 import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
28
29 import java.util.Iterator;
30
31
32
33
34
35
36
37 public class ArtifactTypeMappingServiceTest
38 extends TestCase
39 {
40
41 public void testDefaultConfiguration()
42 {
43 ArtifactTypeMappingService service = getDefaultService();
44 final Iterator it = EarModuleFactory.getStandardArtifactTypes().iterator();
45 while ( it.hasNext() )
46 {
47 String type = (String) it.next();
48 assertTrue( "Standard type could not be found", service.isMappedToType( type, type ) );
49 }
50 }
51
52 public void testIsMappedToTypeForUnknownType()
53 {
54 ArtifactTypeMappingService service = getDefaultService();
55 assertFalse( service.isMappedToType( "rar", "MyKoolCustomType" ) );
56 }
57
58 public void testIsMappedToTypeForKnownType()
59 {
60 ArtifactTypeMappingService service = getServiceWithRarMappingToMyRar();
61 assertTrue( service.isMappedToType( "rar", "MyRar" ) );
62 }
63
64 public void testGetStandardTypeForUknonwnType()
65 {
66 try
67 {
68 ArtifactTypeMappingService service = getDefaultService();
69 service.getStandardType( "MyKoolCustomType" );
70 fail( "Should have failed to retrieve a unknwon custom type" );
71 }
72 catch ( UnknownArtifactTypeException e )
73 {
74
75 }
76 }
77
78 public void testGetStandardTypeForKnownType()
79 {
80 try
81 {
82 ArtifactTypeMappingService service = getServiceWithRarMappingToMyRar();
83 assertEquals( "rar", service.getStandardType( "MyRar" ) );
84 }
85 catch ( UnknownArtifactTypeException e )
86 {
87 fail( "Should not have failed to retrieve a knwon custom type " + e.getMessage() );
88 }
89 }
90
91 public void testConfigWithSameCustomType()
92 {
93 try
94 {
95 XmlPlexusConfiguration rootConfig = new XmlPlexusConfiguration( "dummy" );
96 XmlPlexusConfiguration childConfig =
97 new XmlPlexusConfiguration( ArtifactTypeMappingService.ARTIFACT_TYPE_MAPPING_ELEMENT );
98 childConfig.setAttribute( "type", "generic" );
99 childConfig.setAttribute( "mapping", "rar" );
100 XmlPlexusConfiguration childConfig2 =
101 new XmlPlexusConfiguration( ArtifactTypeMappingService.ARTIFACT_TYPE_MAPPING_ELEMENT );
102 childConfig.setAttribute( "type", "generic" );
103 childConfig.setAttribute( "mapping", "ejb" );
104
105 rootConfig.addChild( childConfig );
106 rootConfig.addChild( childConfig2 );
107 ArtifactTypeMappingService service = new ArtifactTypeMappingService();
108 service.configure( rootConfig );
109 fail( "Should have failed" );
110 }
111 catch ( EarPluginException e )
112 {
113
114 }
115 catch ( PlexusConfigurationException e )
116 {
117 e.printStackTrace();
118 fail( "Unexpected " + e.getMessage() );
119 }
120 }
121
122 public void testConfigWithUnknownStandardType()
123 {
124 try
125 {
126 XmlPlexusConfiguration rootConfig = new XmlPlexusConfiguration( "dummy" );
127 XmlPlexusConfiguration childConfig =
128 new XmlPlexusConfiguration( ArtifactTypeMappingService.ARTIFACT_TYPE_MAPPING_ELEMENT );
129 childConfig.setAttribute( "type", "generic" );
130 childConfig.setAttribute( "mapping", "notAStandardType" );
131
132 rootConfig.addChild( childConfig );
133 ArtifactTypeMappingService service = new ArtifactTypeMappingService();
134 service.configure( rootConfig );
135 fail( "Should have failed" );
136 }
137 catch ( EarPluginException e )
138 {
139
140 }
141 catch ( PlexusConfigurationException e )
142 {
143 e.printStackTrace();
144 fail( "Unexpected " + e.getMessage() );
145 }
146 }
147
148 public void testConfigWithNoType()
149 {
150 try
151 {
152 XmlPlexusConfiguration rootConfig = new XmlPlexusConfiguration( "dummy" );
153 XmlPlexusConfiguration childConfig =
154 new XmlPlexusConfiguration( ArtifactTypeMappingService.ARTIFACT_TYPE_MAPPING_ELEMENT );
155 childConfig.setAttribute( "mapping", "ejb" );
156
157 rootConfig.addChild( childConfig );
158 ArtifactTypeMappingService service = new ArtifactTypeMappingService();
159 service.configure( rootConfig );
160 fail( "Should have failed" );
161 }
162 catch ( EarPluginException e )
163 {
164
165 }
166 catch ( PlexusConfigurationException e )
167 {
168 e.printStackTrace();
169 fail( "Unexpected " + e.getMessage() );
170 }
171 }
172
173 public void testConfigWithNoMapping()
174 {
175 try
176 {
177 XmlPlexusConfiguration rootConfig = new XmlPlexusConfiguration( "dummy" );
178 XmlPlexusConfiguration childConfig =
179 new XmlPlexusConfiguration( ArtifactTypeMappingService.ARTIFACT_TYPE_MAPPING_ELEMENT );
180 childConfig.setAttribute( "type", "generic" );
181
182 rootConfig.addChild( childConfig );
183 ArtifactTypeMappingService service = new ArtifactTypeMappingService();
184 service.configure( rootConfig );
185 fail( "Should have failed" );
186 }
187 catch ( EarPluginException e )
188 {
189
190 }
191 catch ( PlexusConfigurationException e )
192 {
193 e.printStackTrace();
194 fail( "Unexpected " + e.getMessage() );
195 }
196 }
197
198
199
200 protected ArtifactTypeMappingService getServiceWithRarMappingToMyRar()
201 {
202 try
203 {
204 XmlPlexusConfiguration rootConfig = new XmlPlexusConfiguration( "artifact-type-mappings" );
205 XmlPlexusConfiguration childConfig =
206 new XmlPlexusConfiguration( ArtifactTypeMappingService.ARTIFACT_TYPE_MAPPING_ELEMENT );
207 childConfig.setAttribute( "type", "MyRar" );
208 childConfig.setAttribute( "mapping", "rar" );
209 rootConfig.addChild( childConfig );
210 ArtifactTypeMappingService service = new ArtifactTypeMappingService();
211 service.configure( rootConfig );
212
213 return service;
214 }
215 catch ( EarPluginException e )
216 {
217 e.printStackTrace();
218 fail( e.getMessage() );
219 }
220 catch ( PlexusConfigurationException e )
221 {
222 e.printStackTrace();
223 fail( e.getMessage() );
224 }
225
226 return null;
227
228
229 }
230
231 protected ArtifactTypeMappingService getDefaultService()
232 {
233 try
234 {
235 ArtifactTypeMappingService service = new ArtifactTypeMappingService();
236 service.configure( new XmlPlexusConfiguration( "dummy" ) );
237
238 return service;
239 }
240 catch ( EarPluginException e )
241 {
242 e.printStackTrace();
243 fail( e.getMessage() );
244 }
245 catch ( PlexusConfigurationException e )
246 {
247 e.printStackTrace();
248 fail( e.getMessage() );
249 }
250
251 return null;
252 }
253 }