View Javadoc
1   package org.apache.maven.plugins.source.stubs;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.ArrayList;
6   import java.util.List;
7   
8   import org.apache.maven.model.Build;
9   import org.apache.maven.model.Model;
10  import org.apache.maven.model.Resource;
11  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
12  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
13  import org.codehaus.plexus.util.IOUtil;
14  import org.codehaus.plexus.util.ReaderFactory;
15  import org.codehaus.plexus.util.xml.XmlStreamReader;
16  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
17  
18  /*
19   * Licensed to the Apache Software Foundation (ASF) under one
20   * or more contributor license agreements.  See the NOTICE file
21   * distributed with this work for additional information
22   * regarding copyright ownership.  The ASF licenses this file
23   * to you under the Apache License, Version 2.0 (the
24   * "License"); you may not use this file except in compliance
25   * with the License.  You may obtain a copy of the License at
26   *
27   *   http://www.apache.org/licenses/LICENSE-2.0
28   *
29   * Unless required by applicable law or agreed to in writing,
30   * software distributed under the License is distributed on an
31   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
32   * KIND, either express or implied.  See the License for the
33   * specific language governing permissions and limitations
34   * under the License.
35   */
36  
37  /**
38   * @author <a href="mailto:oching@exist.com">Maria Odea Ching</a>
39   */
40  public class Project001Stub
41      extends MavenProjectStub
42  {
43      private Build build;
44  
45      private List<Resource> resources;
46  
47      private List<Resource> testResources;
48  
49      public Project001Stub()
50      {
51          Model model;
52  
53          try
54          {
55              model = readModelFromFile( new File( getBasedir(), "target/test-classes/unit/project-001/pom.xml" ) );
56              setModel( model );
57  
58              setGroupId( model.getGroupId() );
59              setArtifactId( model.getArtifactId() );
60              setVersion( model.getVersion() );
61              setName( model.getName() );
62              setUrl( model.getUrl() );
63              setPackaging( model.getPackaging() );
64  
65              Build build = new Build();
66              build.setFinalName( getArtifactId() + "-" + getVersion() );
67              build.setDirectory( getBasedir() + "/target/test/unit/project-001/target" );
68              setBuild( build );
69  
70              String basedir = getBasedir().getAbsolutePath();
71              List<String> compileSourceRoots = new ArrayList<String>();
72              compileSourceRoots.add( basedir + "/target/test-classes/unit/project-001/src/main/java" );
73              setCompileSourceRoots( compileSourceRoots );
74  
75              List<String> testCompileSourceRoots = new ArrayList<String>();
76              testCompileSourceRoots.add( basedir + "/target/test-classes/unit/project-001/src/test/java" );
77              setTestCompileSourceRoots( testCompileSourceRoots );
78  
79              setResources( model.getBuild().getResources() );
80              setTestResources( model.getBuild().getTestResources() );
81  
82              SourcePluginArtifactStub artifact =
83                  new SourcePluginArtifactStub( getGroupId(), getArtifactId(), getVersion(), getPackaging(), null );
84              artifact.setArtifactHandler( new DefaultArtifactHandlerStub() );
85              artifact.setType( "jar" );
86              artifact.setBaseVersion( "1.0-SNAPSHOT" );
87              setArtifact( artifact );
88  
89          }
90          catch ( Exception e )
91          {
92              e.printStackTrace();
93          }
94      }
95  
96      public Build getBuild()
97      {
98          return build;
99      }
100 
101     public void setBuild( Build build )
102     {
103         this.build = build;
104     }
105 
106     public List<Resource> getResources()
107     {
108         return this.resources;
109     }
110 
111     public void setResources( List<Resource> resources )
112     {
113         this.resources = resources;
114     }
115 
116     public List<Resource> getTestResources()
117     {
118         return testResources;
119     }
120 
121     public void setTestResources( List<Resource> testResources )
122     {
123         this.testResources = testResources;
124     }
125 
126     static Model readModelFromFile( File file )
127         throws IOException, XmlPullParserException
128     {
129         MavenXpp3Reader pomReader = new MavenXpp3Reader();
130         XmlStreamReader reader = null;
131         try
132         {
133             reader = ReaderFactory.newXmlReader( file );
134             final Model model = pomReader.read( reader );
135             reader.close();
136             reader = null;
137             return model;
138         }
139         finally
140         {
141             IOUtil.close( reader );
142         }
143 
144     }
145 }