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.pmd.stubs;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.InputStream;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.model.Build;
29  import org.apache.maven.model.Model;
30  import org.apache.maven.model.ReportPlugin;
31  import org.apache.maven.model.Scm;
32  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
33  
34  /**
35   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
36   * @version $Id$
37   */
38  public class CustomConfigurationMavenProjectStub extends PmdProjectStub {
39      private Build build;
40  
41      private List<ReportPlugin> reportPlugins = new ArrayList<>();
42  
43      public CustomConfigurationMavenProjectStub() {
44          MavenXpp3Reader pomReader = new MavenXpp3Reader();
45          Model model = null;
46  
47          try (InputStream is = new FileInputStream(new File(getBasedir()
48                  + "/src/test/resources/unit/custom-configuration/custom-configuration-plugin-config.xml"))) {
49              model = pomReader.read(is);
50              setModel(model);
51          } catch (Exception e) {
52          }
53  
54          setGroupId(model.getGroupId());
55          setArtifactId(model.getArtifactId());
56          setVersion(model.getVersion());
57          setName(model.getName());
58          setUrl(model.getUrl());
59          setPackaging(model.getPackaging());
60  
61          Scm scm = new Scm();
62          scm.setConnection("scm:svn:http://svn.apache.org/maven/sample/trunk");
63          setScm(scm);
64  
65          Build build = new Build();
66          build.setFinalName(model.getBuild().getFinalName());
67          build.setDirectory(getBasedir() + "/target/test/unit/custom-configuration/target");
68          build.setSourceDirectory(getBasedir() + "/src/test/resources/unit/custom-configuration");
69          setBuild(build);
70  
71          setReportPlugins(model.getReporting().getPlugins());
72  
73          String basedir = getBasedir().getAbsolutePath();
74          List<String> compileSourceRoots = new ArrayList<>();
75          compileSourceRoots.add(basedir + "/src/test/resources/unit/custom-configuration/custom/configuration");
76          setCompileSourceRoots(compileSourceRoots);
77  
78          Artifact artifact = new PmdPluginArtifactStub(getGroupId(), getArtifactId(), getVersion(), getPackaging());
79          artifact.setArtifactHandler(new DefaultArtifactHandlerStub());
80          setArtifact(artifact);
81  
82          setFile(new File(getBasedir().getAbsolutePath() + "/pom.xml"));
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public void setBuild(Build build) {
88          this.build = build;
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public Build getBuild() {
94          return build;
95      }
96  
97      public void setReportPlugins(List<ReportPlugin> plugins) {
98          this.reportPlugins = plugins;
99      }
100 
101     /** {@inheritDoc} */
102     @Override
103     public List<ReportPlugin> getReportPlugins() {
104         return reportPlugins;
105     }
106 }