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