View Javadoc
1   package org.apache.maven.plugins.ejb.stub;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.Collections;
24  import java.util.Properties;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.artifact.DefaultArtifact;
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.model.Model;
30  import org.apache.maven.model.Profile;
31  import org.apache.maven.project.MavenProject;
32  import org.codehaus.plexus.PlexusTestCase;
33  import org.codehaus.plexus.util.FileUtils;
34  
35  /**
36   * Stub
37   */
38  public class MavenProjectBasicStub
39      extends MavenProject
40  {
41      protected String identifier;
42  
43      protected String testRootDir;
44  
45      protected Properties properties;
46  
47      protected String description;
48  
49      protected ModelStub model;
50  
51      protected File file;
52  
53      protected DefaultArtifact artifact;
54  
55      public MavenProjectBasicStub( String id )
56          throws Exception
57      {
58          // most values are hardcoded to have a controlled environment
59          super( new ModelStub() );
60  
61          model = (ModelStub) super.getModel();
62          properties = new Properties();
63          identifier = id;
64  
65          artifact = new DefaultArtifact(getGroupId(), getArtifactId(), getVersion(), "compile", "jar", "", null);
66  
67          // set isolated root directory
68          testRootDir = PlexusTestCase.getBasedir() + "/target/test-classes/unit/test-dir/" + identifier;
69  
70          if ( !FileUtils.fileExists( testRootDir ) )
71          {
72              FileUtils.mkdir( testRootDir );
73          }
74  
75          // this is ugly but needed to ensure that the copy constructor
76          // works correctly
77          initializeParentFields();
78      }
79  
80      public String getName()
81      {
82          return "Test Project " + identifier;
83      }
84  
85      public void setDescription( String desc )
86      {
87          description = desc;
88      }
89  
90      public Model getModel()
91      {
92          return model;
93      }
94  
95      public String getDescription()
96      {
97          if ( description == null )
98          {
99              return "this is a test project";
100         }
101         else
102         {
103             return description;
104         }
105     }
106 
107     public File getBasedir()
108     {
109         // create an isolated environment
110         // see setupTestEnvironment for details
111         return new File( testRootDir );
112     }
113 
114     public Artifact getArtifact()
115     {
116         return artifact;
117     }
118 
119     public String getGroupId()
120     {
121         String groupId = getModel().getGroupId();
122         if ( ( groupId == null ) && ( getModel().getParent() != null ) )
123         {
124             groupId = getModel().getParent().getGroupId();
125         }
126         return groupId;
127     }
128 
129     public String getArtifactId()
130     {
131         return getModel().getArtifactId();
132     }
133 
134     public String getPackaging()
135     {
136         return "ejb";
137     }
138 
139     public String getVersion()
140     {
141         return identifier;
142     }
143 
144     public void addProperty( String key, String value )
145     {
146         properties.put( key, value );
147     }
148 
149     public Properties getProperties()
150     {
151         return properties;
152     }
153 
154     // to prevent the MavenProject copy constructor from blowing up
155     private void initializeParentFields()
156     {
157         // the pom should be located in the isolated dummy root
158         super.setFile( new File( getBasedir(), "pom.xml" ) );
159         super.setDependencyArtifacts( Collections.<Artifact>emptySet() );
160         super.setArtifacts( Collections.<Artifact>emptySet() );
161         super.setExtensionArtifacts( Collections.<Artifact>emptySet() );
162         super.setRemoteArtifactRepositories( Collections.<ArtifactRepository>emptyList() );
163         super.setPluginArtifactRepositories( Collections.<ArtifactRepository>emptyList() );
164         super.setCollectedProjects( Collections.<MavenProject>emptyList() );
165         super.setActiveProfiles( Collections.<Profile>emptyList() );
166         super.setOriginalModel( null );
167         super.setExecutionProject( this );
168         super.setArtifact( artifact );
169     }
170 }