View Javadoc
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.plugins.source.stubs;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.maven.model.Build;
27  import org.apache.maven.model.Model;
28  import org.apache.maven.model.Resource;
29  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
30  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
31  import org.codehaus.plexus.util.IOUtil;
32  import org.codehaus.plexus.util.ReaderFactory;
33  import org.codehaus.plexus.util.xml.XmlStreamReader;
34  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
35  
36  /**
37   * @author <a href="mailto:oching@exist.com">Maria Odea Ching</a>
38   */
39  public class Project001Stub extends MavenProjectStub {
40      private Build build;
41  
42      private List<Resource> resources;
43  
44      private List<Resource> testResources;
45  
46      public Project001Stub() {
47          Model model;
48  
49          try {
50              model = readModelFromFile(new File(getBasedir(), "target/test-classes/unit/project-001/pom.xml"));
51              setModel(model);
52  
53              setGroupId(model.getGroupId());
54              setArtifactId(model.getArtifactId());
55              setVersion(model.getVersion());
56              setName(model.getName());
57              setUrl(model.getUrl());
58              setPackaging(model.getPackaging());
59  
60              Build build = new Build();
61              build.setFinalName(getArtifactId() + "-" + getVersion());
62              build.setDirectory(getBasedir() + "/target/test/unit/project-001/target");
63              setBuild(build);
64  
65              String basedir = getBasedir().getAbsolutePath();
66              List<String> compileSourceRoots = new ArrayList<>();
67              compileSourceRoots.add(basedir + "/target/test-classes/unit/project-001/src/main/java");
68              setCompileSourceRoots(compileSourceRoots);
69  
70              List<String> testCompileSourceRoots = new ArrayList<>();
71              testCompileSourceRoots.add(basedir + "/target/test-classes/unit/project-001/src/test/java");
72              setTestCompileSourceRoots(testCompileSourceRoots);
73  
74              setResources(model.getBuild().getResources());
75              setTestResources(model.getBuild().getTestResources());
76  
77              SourcePluginArtifactStub artifact =
78                      new SourcePluginArtifactStub(getGroupId(), getArtifactId(), getVersion(), getPackaging(), null);
79              artifact.setArtifactHandler(new DefaultArtifactHandlerStub());
80              artifact.setType("jar");
81              artifact.setBaseVersion("1.0-SNAPSHOT");
82              setArtifact(artifact);
83  
84          } catch (Exception e) {
85              e.printStackTrace();
86          }
87      }
88  
89      public Build getBuild() {
90          return build;
91      }
92  
93      public void setBuild(Build build) {
94          this.build = build;
95      }
96  
97      public List<Resource> getResources() {
98          return this.resources;
99      }
100 
101     public void setResources(List<Resource> resources) {
102         this.resources = resources;
103     }
104 
105     public List<Resource> getTestResources() {
106         return testResources;
107     }
108 
109     public void setTestResources(List<Resource> testResources) {
110         this.testResources = testResources;
111     }
112 
113     static Model readModelFromFile(File file) throws IOException, XmlPullParserException {
114         MavenXpp3Reader pomReader = new MavenXpp3Reader();
115         XmlStreamReader reader = null;
116         try {
117             reader = ReaderFactory.newXmlReader(file);
118             final Model model = pomReader.read(reader);
119             reader.close();
120             reader = null;
121             return model;
122         } finally {
123             IOUtil.close(reader);
124         }
125     }
126 }