1   package org.apache.maven.plugin.ear.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  import org.codehaus.plexus.configuration.PlexusConfigurationException;
24  import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
25  import org.apache.maven.plugin.ear.util.ArtifactTypeMappingService;
26  import org.apache.maven.plugin.ear.EarModuleFactory;
27  import org.apache.maven.plugin.ear.UnknownArtifactTypeException;
28  import org.apache.maven.plugin.ear.EarPluginException;
29  
30  import java.util.Iterator;
31  
32  /**
33   * Tests for the {@link ArtifactTypeMappingService}
34   *
35   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
36   * @version $Id: ArtifactTypeMappingServiceTest.java 728546 2008-12-21 22:56:51Z bentmann $
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              // That's good
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 = ArtifactTypeMappingService.getInstance();
109             service.configure( rootConfig );
110             fail( "Should have failed" );
111         }
112         catch ( EarPluginException e )
113         {
114             //OK
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 = ArtifactTypeMappingService.getInstance();
135             service.configure( rootConfig );
136             fail( "Should have failed" );
137         }
138         catch ( EarPluginException e )
139         {
140             //OK
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 = ArtifactTypeMappingService.getInstance();
160             service.configure( rootConfig );
161             fail( "Should have failed" );
162         }
163         catch ( EarPluginException e )
164         {
165             //OK
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 = ArtifactTypeMappingService.getInstance();
185             service.configure( rootConfig );
186             fail( "Should have failed" );
187         }
188         catch ( EarPluginException e )
189         {
190             //OK
191         }
192         catch ( PlexusConfigurationException e )
193         {
194             e.printStackTrace();
195             fail( "Unexpected " + e.getMessage() );
196         }
197     }
198 
199     // Utilities
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 = ArtifactTypeMappingService.getInstance();
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         // Won't occur
227         return null;
228 
229 
230     }
231 
232     protected ArtifactTypeMappingService getDefaultService()
233     {
234         try
235         {
236             ArtifactTypeMappingService service = ArtifactTypeMappingService.getInstance();
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         // Won't occur
252         return null;
253     }
254 }