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.plugin.resources.remote.stub;
20  
21  import java.io.File;
22  import java.util.HashSet;
23  import java.util.LinkedList;
24  import java.util.Properties;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.artifact.repository.ArtifactRepository;
28  import org.apache.maven.model.Profile;
29  import org.apache.maven.project.MavenProject;
30  import org.codehaus.plexus.PlexusTestCase;
31  import org.codehaus.plexus.util.FileUtils;
32  
33  /**
34   * Stub
35   */
36  public class MavenProjectBasicStub extends MavenProject {
37      protected String identifier;
38  
39      protected String testRootDir;
40  
41      protected Properties properties;
42  
43      protected String description;
44  
45      protected ModelStub modelStub;
46  
47      protected File file;
48  
49      protected ArtifactStub artifact;
50  
51      public MavenProjectBasicStub(String id) throws Exception {
52          // most values are hardcoded to have a controlled environment
53          super(new ModelStub());
54  
55          modelStub = (ModelStub) getModel();
56          properties = new Properties();
57          artifact = new ArtifactStub();
58          identifier = id;
59  
60          // set isolated root directory
61          testRootDir = PlexusTestCase.getBasedir() + "/target/test-classes/unit/test-dir/" + identifier;
62  
63          if (!FileUtils.fileExists(testRootDir)) {
64              FileUtils.mkdir(testRootDir);
65          }
66  
67          artifact.populate(this);
68  
69          // this is ugly but needed to ensure that the copy constructor
70          // works correctly
71          initializeParentFields();
72      }
73  
74      @Override
75      public String getName() {
76          return "Test Project " + identifier;
77      }
78  
79      @Override
80      public void setDescription(String desc) {
81          description = desc;
82      }
83  
84      @Override
85      public String getDescription() {
86          if (description == null) {
87              return "this is a test project";
88          } else {
89              return description;
90          }
91      }
92  
93      @Override
94      public File getBasedir() {
95          // create an isolated environment
96          // see setupTestEnvironment for details
97          return new File(testRootDir);
98      }
99  
100     @Override
101     public Artifact getArtifact() {
102         return artifact;
103     }
104 
105     @Override
106     public String getGroupId() {
107         return "org.apache.maven.plugin.test";
108     }
109 
110     @Override
111     public String getArtifactId() {
112         return "maven-resource-plugin-test#" + identifier;
113     }
114 
115     @Override
116     public String getPackaging() {
117         return "ejb";
118     }
119 
120     @Override
121     public String getVersion() {
122         return identifier;
123     }
124 
125     public void addProperty(String key, String value) {
126         properties.put(key, value);
127     }
128 
129     @Override
130     public Properties getProperties() {
131         return properties;
132     }
133 
134     // to prevent the MavenProject copy constructor from blowing up
135     private void initializeParentFields() {
136         // the pom should be located in the isolated dummy root
137         super.setFile(new File(getBasedir(), "pom.xml"));
138         super.setDependencyArtifacts(new HashSet<Artifact>());
139         super.setArtifacts(new HashSet<Artifact>());
140         super.setReportArtifacts(new HashSet<Artifact>());
141         super.setExtensionArtifacts(new HashSet<Artifact>());
142         super.setRemoteArtifactRepositories(new LinkedList<ArtifactRepository>());
143         super.setPluginArtifactRepositories(new LinkedList<ArtifactRepository>());
144         super.setCollectedProjects(new LinkedList<MavenProject>());
145         super.setActiveProfiles(new LinkedList<Profile>());
146         super.setOriginalModel(null);
147         super.setExecutionProject(this);
148         super.setArtifact(artifact);
149     }
150 }