1   package org.apache.maven.report.projectinfo.stubs;
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.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Set;
27  
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.artifact.DefaultArtifact;
30  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
31  import org.apache.maven.artifact.repository.ArtifactRepository;
32  import org.apache.maven.artifact.repository.DefaultArtifactRepository;
33  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
34  import org.apache.maven.artifact.versioning.VersionRange;
35  import org.apache.maven.model.Build;
36  import org.apache.maven.model.DependencyManagement;
37  import org.apache.maven.model.Model;
38  import org.apache.maven.model.PluginManagement;
39  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
40  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
41  import org.codehaus.plexus.util.ReaderFactory;
42  
43  /**
44   * @author Edwin Punzalan
45   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
46   * @author Nick Stolwijk
47   * @version $Id: ProjectInfoProjectStub.java 728546 2008-12-21 22:56:51Z bentmann $
48   */
49  public abstract class ProjectInfoProjectStub
50      extends MavenProjectStub
51  {
52      private Model model;
53  
54      private Build build;
55  
56      /**
57       * Default constructor
58       */
59      public ProjectInfoProjectStub()
60      {
61          MavenXpp3Reader pomReader = new MavenXpp3Reader();
62          try
63          {
64              model = pomReader.read( ReaderFactory.newXmlReader( new File( getBasedir(), getPOM() ) ) );
65              setModel( model );
66          }
67          catch ( Exception e )
68          {
69              throw new RuntimeException( e );
70          }
71  
72          setGroupId( model.getGroupId() );
73          setArtifactId( model.getArtifactId() );
74          setVersion( model.getVersion() );
75          setName( model.getName() );
76          setUrl( model.getUrl() );
77          setPackaging( model.getPackaging() );
78  
79          Artifact artifact = new ProjectInfoPluginArtifactStub( getGroupId(), getArtifactId(), getVersion(),
80                                                                 getPackaging() );
81          artifact.setArtifactHandler( new DefaultArtifactHandlerStub() );
82          setArtifact( artifact );
83  
84          Build build = new Build();
85          build.setFinalName( model.getArtifactId() );
86          build.setDirectory( super.getBasedir() + "/target/test/unit/" + model.getArtifactId() + "/target" );
87          build.setSourceDirectory( getBasedir() + "/src/main/java" );
88          build.setOutputDirectory( super.getBasedir() + "/target/test/unit/" + model.getArtifactId()
89                                    + "/target/classes" );
90          build.setTestSourceDirectory( getBasedir() + "/src/test/java" );
91          build.setTestOutputDirectory( super.getBasedir() + "/target/test/unit/" + model.getArtifactId()
92              + "/target/test-classes" );
93          setBuild( build );
94  
95          List compileSourceRoots = new ArrayList();
96          compileSourceRoots.add( getBasedir() + "/src/main/java" );
97          setCompileSourceRoots( compileSourceRoots );
98  
99          List testCompileSourceRoots = new ArrayList();
100         testCompileSourceRoots.add( getBasedir() + "/src/test/java" );
101         setTestCompileSourceRoots( testCompileSourceRoots );
102     }
103 
104     /**
105      * @return the POM file name
106      */
107     protected abstract String getPOM();
108 
109     /** {@inheritDoc} */
110     public Model getModel()
111     {
112         return model;
113     }
114 
115     /** {@inheritDoc} */
116     public Build getBuild()
117     {
118         return build;
119     }
120 
121     /** {@inheritDoc} */
122     public void setBuild( Build build )
123     {
124         this.build = build;
125     }
126 
127     /** {@inheritDoc} */
128     public File getBasedir()
129     {
130         return new File( super.getBasedir() + "/src/test/resources/plugin-configs/" );
131     }
132 
133     /** {@inheritDoc} */
134     public Set getArtifacts()
135     {
136         return Collections.EMPTY_SET;
137     }
138 
139     /** {@inheritDoc} */
140     public List getRemoteArtifactRepositories()
141     {
142         ArtifactRepository repository = new DefaultArtifactRepository( "central", "http://repo1.maven.org/maven2",
143                                                                        new DefaultRepositoryLayout() );
144 
145         return Collections.singletonList( repository );
146     }
147 
148     /** {@inheritDoc} */
149     public Set getDependencyArtifacts()
150     {
151         return Collections.singleton( new DefaultArtifact( "junit", "junit", VersionRange.createFromVersion( "3.8.1" ),
152                                                            Artifact.SCOPE_TEST, "jar", null,
153                                                            new DefaultArtifactHandler( "jar" ), false ) );
154     }
155 
156     /** {@inheritDoc} */
157     public DependencyManagement getDependencyManagement()
158     {
159         return model.getDependencyManagement();
160     }
161 
162     /** {@inheritDoc} */
163     public PluginManagement getPluginManagement()
164     {
165         PluginManagement pluginMgmt = null;
166 
167         Build build = model.getBuild();
168         if ( build != null )
169         {
170             pluginMgmt = build.getPluginManagement();
171         }
172 
173         return pluginMgmt;
174     }
175 
176 }