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