1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.test.plugin;
20  
21  import java.io.File;
22  import java.util.Collection;
23  import java.util.Iterator;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.metadata.ArtifactMetadata;
27  import org.apache.maven.project.MavenProject;
28  import org.apache.maven.project.artifact.ProjectArtifactMetadata;
29  import org.apache.maven.shared.test.plugin.ProjectTool.PomInfo;
30  import org.codehaus.plexus.PlexusTestCase;
31  import org.codehaus.plexus.util.StringUtils;
32  
33  public class ProjectToolTest
34      extends PlexusTestCase
35  {
36  
37      public void testManglePomForTesting_ShouldPopulateOutDirAndFinalName()
38          throws Exception
39      {
40          ProjectTool tool = (ProjectTool) lookup( ProjectTool.ROLE, "default" );
41  
42          File pomFile = new File( "pom.xml" );
43  
44          PomInfo info = tool.manglePomForTesting( pomFile, "test", true );
45  
46          assertEquals( "target", info.getBuildOutputDirectory() );
47          assertEquals( "maven-plugin-testing-tools-test.jar", info.getFinalName() );
48      }
49  
50      public void testPackageProjectArtifact_ShouldPopulateArtifactFileWithJarLocation()
51          throws Exception
52      {
53          ProjectTool tool = (ProjectTool) lookup( ProjectTool.ROLE, "default" );
54  
55          File pomFile = new File( "pom.xml" );
56  
57          MavenProject project = tool.packageProjectArtifact( pomFile, "test", true );
58  
59          String expectedPath = "target/maven-plugin-testing-tools-test.jar";
60          // be nice with windows
61          String actualPath = StringUtils.replace( project.getArtifact().getFile().getPath(), "\\", "/" );
62  
63          assertEquals( expectedPath, actualPath );
64      }
65  
66      public void testPackageProjectArtifact_ShouldPopulateWithCorrectArtifactAndMetadata()
67          throws Exception
68      {
69          ProjectTool tool = (ProjectTool) lookup( ProjectTool.ROLE, "default" );
70  
71          File pomFile = new File( "pom.xml" );
72  
73          MavenProject project = tool.packageProjectArtifact( pomFile, "test", true );
74  
75          Artifact artifact = project.getArtifact();
76  
77          assertEquals( "jar", artifact.getType() );
78          assertTrue( artifact.getFile().exists() );
79  
80          Collection metadata = artifact.getMetadataList();
81  
82          boolean foundPomMetadata = false;
83  
84          for ( Iterator it = metadata.iterator(); it.hasNext(); )
85          {
86              ArtifactMetadata metadataItem = (ArtifactMetadata) it.next();
87  
88              if ( metadataItem instanceof ProjectArtifactMetadata )
89              {
90                  foundPomMetadata = true;
91              }
92          }
93  
94          assertTrue( foundPomMetadata );
95      }
96  }