View Javadoc
1   package org.apache.maven.plugins.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  
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   * Tests for the {@link ArtifactTypeMappingService}
32   * 
33   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
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              // That's good
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             // OK
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             // OK
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             // OK
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             // OK
186         }
187         catch ( PlexusConfigurationException e )
188         {
189             e.printStackTrace();
190             fail( "Unexpected " + e.getMessage() );
191         }
192     }
193 
194     // Utilities
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         // Won't occur
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         // Won't occur
246         return null;
247     }
248 }