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