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.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   * Tests for the {@link ArtifactTypeMappingService}
33   *
34   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
35   * @version $Id: ArtifactTypeMappingServiceTest.java 992370 2010-09-03 16:48:59Z snicoll $
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              // That's good
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             //OK
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             //OK
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             //OK
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             //OK
190         }
191         catch ( PlexusConfigurationException e )
192         {
193             e.printStackTrace();
194             fail( "Unexpected " + e.getMessage() );
195         }
196     }
197 
198     // Utilities
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         // Won't occur
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         // Won't occur
251         return null;
252     }
253 }