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