1   package org.apache.maven.plugin.war.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.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.war.AbstractWarMojo;
26  import org.apache.maven.plugin.war.stub.AbstractArtifactStub;
27  import org.codehaus.plexus.interpolation.InterpolationException;
28  
29  /**
30   * Tests the mapping of file names.
31   *
32   * @author Stephane Nicoll
33   */
34  public class MappingUtilsTest
35      extends TestCase
36  {
37  
38      public void testCompleteMapping()
39          throws MojoExecutionException, InterpolationException
40      {
41          TestArtifactStub jar = new TestArtifactStub();
42          jar.setGroupId( "org.apache.sample" );
43          jar.setArtifactId( "maven-test-lib" );
44          jar.setVersion( "1.0" );
45          assertEquals( "maven-test-lib-1.0.jar",
46                        MappingUtils.evaluateFileNameMapping( "@{artifactId}@-@{version}@.@{extension}@", jar ) );
47  
48      }
49  
50      public void testNoVersionMapping()
51          throws MojoExecutionException, InterpolationException
52      {
53          TestArtifactStub jar = new TestArtifactStub();
54          jar.setGroupId( "org.apache.sample" );
55          jar.setArtifactId( "maven-test-lib" );
56          jar.setVersion( "1.0" );
57          assertEquals( "maven-test-lib.jar", MappingUtils.evaluateFileNameMapping( "@{artifactId}@.@{extension}@", jar ) );
58  
59      }
60  
61      public void testMappingWithGroupId()
62          throws MojoExecutionException, InterpolationException
63      {
64          TestArtifactStub jar = new TestArtifactStub();
65          jar.setGroupId( "org.apache.sample" );
66          jar.setArtifactId( "maven-test-lib" );
67          jar.setVersion( "1.0" );
68          assertEquals( "org.apache.sample-maven-test-lib-1.0.jar",
69                        MappingUtils.evaluateFileNameMapping( "@{groupId}@-@{artifactId}@-@{version}@.@{extension}@", jar ) );
70  
71      }
72  
73      public void testMappingWithClassifier()
74          throws MojoExecutionException, InterpolationException
75      {
76          TestArtifactStub jar = new TestArtifactStub();
77          jar.setGroupId( "org.apache.sample" );
78          jar.setArtifactId( "maven-test-lib" );
79          jar.setVersion( "1.0" );
80          jar.setClassifier( "classifier" );
81          assertEquals( "maven-test-lib-1.0-classifier.jar",
82                        MappingUtils.evaluateFileNameMapping( AbstractWarMojo.DEFAULT_FILE_NAME_MAPPING_CLASSIFIER, jar ) );
83      }
84  
85      /**
86       * Test for MWAR-212.
87       */
88      public void testMappingWithOptionalClassifier()
89          throws MojoExecutionException, InterpolationException
90      {
91          final String MAPPING_WITH_OPTIONAL_CLASSIFIER_1 = "@{artifactId}@-@{version}@@{dashClassifier}@.@{extension}@";
92          final String MAPPING_WITH_OPTIONAL_CLASSIFIER_2 = "@{artifactId}@-@{version}@@{dashClassifier?}@.@{extension}@";
93  
94          TestArtifactStub jar = new TestArtifactStub();
95          jar.setGroupId( "org.apache.sample" );
96          jar.setArtifactId( "maven-test-lib" );
97          jar.setVersion( "1.0" );
98          assertEquals( "maven-test-lib-1.0.jar",
99                        MappingUtils.evaluateFileNameMapping( MAPPING_WITH_OPTIONAL_CLASSIFIER_1, jar ) );
100         assertEquals( "maven-test-lib-1.0.jar",
101                       MappingUtils.evaluateFileNameMapping( MAPPING_WITH_OPTIONAL_CLASSIFIER_2, jar ) );
102 
103         jar = new TestArtifactStub();
104         jar.setGroupId( "org.apache.sample" );
105         jar.setArtifactId( "maven-test-lib" );
106         jar.setVersion( "1.0" );
107         jar.setClassifier( "classifier" );
108         assertEquals( "maven-test-lib-1.0-classifier.jar",
109                       MappingUtils.evaluateFileNameMapping( MAPPING_WITH_OPTIONAL_CLASSIFIER_1, jar ) );
110         assertEquals( "maven-test-lib-1.0-classifier.jar",
111                       MappingUtils.evaluateFileNameMapping( MAPPING_WITH_OPTIONAL_CLASSIFIER_2, jar ) );
112     }
113 
114     // A very dumb stub used to test the mappings
115     class TestArtifactStub
116         extends AbstractArtifactStub
117     {
118 
119         protected String groupId;
120 
121         protected String artifactId;
122 
123         protected String version;
124 
125         protected String classifier;
126 
127         protected String type = "jar";
128 
129         public TestArtifactStub()
130         {
131             super( null );
132         }
133 
134 
135         public String getGroupId()
136         {
137             return groupId;
138         }
139 
140         public void setGroupId( String groupId )
141         {
142             this.groupId = groupId;
143         }
144 
145         public String getArtifactId()
146         {
147             return artifactId;
148         }
149 
150         public void setArtifactId( String artifactId )
151         {
152             this.artifactId = artifactId;
153         }
154 
155         public String getVersion()
156         {
157             return version;
158         }
159 
160         public void setVersion( String version )
161         {
162             this.version = version;
163         }
164 
165         public String getClassifier()
166         {
167             return classifier;
168         }
169 
170         public void setClassifier( String classifier )
171         {
172             this.classifier = classifier;
173         }
174 
175 
176         public String getType()
177         {
178             return type;
179         }
180 
181         public void setType( String type )
182         {
183             this.type = type;
184         }
185     }
186 }