1   package org.apache.maven.shared.test.plugin;
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 java.io.File;
23  import java.io.IOException;
24  import java.util.Collection;
25  import java.util.Iterator;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.metadata.ArtifactMetadata;
29  import org.apache.maven.project.MavenProject;
30  import org.apache.maven.project.artifact.ProjectArtifactMetadata;
31  import org.apache.maven.shared.test.plugin.ProjectTool.PomInfo;
32  import org.codehaus.plexus.PlexusTestCase;
33  import org.codehaus.plexus.util.FileUtils;
34  import org.codehaus.plexus.util.StringUtils;
35  
36  /**
37   * @version $Id: ProjectToolTest.java 881038 2009-11-16 23:14:09Z bentmann $
38   */
39  public class ProjectToolTest
40      extends PlexusTestCase
41  {
42  
43      private File getPom( String test )
44          throws IOException
45      {
46          File src = new File( "src/test/resources/projects/" + test );
47          File dst = new File( "target/unit/projects/" + test );
48  
49          FileUtils.copyDirectoryStructureIfModified( src, dst );
50  
51          return new File( dst, "pom.xml" );
52      }
53  
54      public void testManglePomForTesting_ShouldPopulateOutDirAndFinalName()
55          throws Exception
56      {
57          ProjectTool tool = (ProjectTool) lookup( ProjectTool.ROLE, "default" );
58  
59          File pomFile = getPom( "basic" );
60  
61          PomInfo info = tool.manglePomForTesting( pomFile, "test", true );
62  
63          assertEquals( "target"+File.separatorChar+"it-build-target", info.getBuildDirectory() );
64          assertEquals( "maven-it-plugin-test.jar", info.getFinalName() );
65          assertEquals( "target"+File.separatorChar+"it-build-target"+File.separatorChar+"classes",info.getBuildOutputDirectory() );
66      }
67  
68      public void testPackageProjectArtifact_ShouldPopulateArtifactFileWithJarLocation()
69          throws Exception
70      {
71          ProjectTool tool = (ProjectTool) lookup( ProjectTool.ROLE, "default" );
72  
73          File pomFile = getPom( "basic" );
74  
75          MavenProject project = tool.packageProjectArtifact( pomFile, "test", true );
76  
77          String expectedPath = "target/unit/projects/basic/target/it-build-target/maven-it-plugin-test.jar";
78  
79          // be nice with windows
80          String actualPath = StringUtils.replace( project.getArtifact().getFile().getPath(), "\\", "/" );
81  
82          assertEquals( expectedPath, actualPath );
83      }
84  
85      public void testPackageProjectArtifact_ShouldPopulateWithCorrectArtifactAndMetadata()
86          throws Exception
87      {
88          ProjectTool tool = (ProjectTool) lookup( ProjectTool.ROLE, "default" );
89  
90          File pomFile = getPom( "basic" );
91  
92          MavenProject project = tool.packageProjectArtifact( pomFile, "test", true );
93  
94          Artifact artifact = project.getArtifact();
95  
96          assertEquals( "maven-plugin", artifact.getType() );
97          assertTrue( "Missing " + artifact.getFile(), artifact.getFile().exists() );
98  
99          Collection metadata = artifact.getMetadataList();
100 
101         boolean foundPomMetadata = false;
102 
103         for ( Iterator it = metadata.iterator(); it.hasNext(); )
104         {
105             ArtifactMetadata metadataItem = (ArtifactMetadata) it.next();
106 
107             if ( metadataItem instanceof ProjectArtifactMetadata )
108             {
109                 foundPomMetadata = true;
110             }
111         }
112 
113         assertTrue( foundPomMetadata );
114     }
115 }