View Javadoc
1   package org.apache.maven.plugin.resources.remote.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.artifact.repository.ArtifactRepository;
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
38      extends MavenProject
39  {
40      protected String identifier;
41  
42      protected String testRootDir;
43  
44      protected Properties properties;
45  
46      protected String description;
47  
48      protected ModelStub modelStub;
49  
50      protected File file;
51  
52      protected ArtifactStub artifact;
53  
54      public MavenProjectBasicStub( String id )
55          throws Exception
56      {
57          // most values are hardcoded to have a controlled environment
58          super( new ModelStub() );
59  
60          modelStub = (ModelStub) getModel();
61          properties = new Properties();
62          artifact = new ArtifactStub();
63          identifier = id;
64  
65          // set isolated root directory
66          testRootDir = PlexusTestCase.getBasedir() + "/target/test-classes/unit/test-dir/" + identifier;
67  
68          if ( !FileUtils.fileExists( testRootDir ) )
69          {
70              FileUtils.mkdir( testRootDir );
71          }
72  
73          artifact.populate( this );
74  
75          // this is ugly but needed to ensure that the copy constructor
76          // works correctly
77          initializeParentFields();
78      }
79  
80      @Override
81      public String getName()
82      {
83          return "Test Project " + identifier;
84      }
85  
86      @Override
87      public void setDescription( String desc )
88      {
89          description = desc;
90      }
91  
92      @Override
93      public String getDescription()
94      {
95          if ( description == null )
96          {
97              return "this is a test project";
98          }
99          else
100         {
101             return description;
102         }
103     }
104 
105     @Override
106     public File getBasedir()
107     {
108         // create an isolated environment
109         // see setupTestEnvironment for details
110         return new File( testRootDir );
111     }
112 
113     @Override
114     public Artifact getArtifact()
115     {
116         return artifact;
117     }
118 
119     @Override
120     public String getGroupId()
121     {
122         return "org.apache.maven.plugin.test";
123     }
124 
125     @Override
126     public String getArtifactId()
127     {
128         return "maven-resource-plugin-test#" + identifier;
129     }
130 
131     @Override
132     public String getPackaging()
133     {
134         return "ejb";
135     }
136 
137     @Override
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     @Override
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( new HashSet<Artifact>() );
160         super.setArtifacts( new HashSet<Artifact>() );
161         super.setReportArtifacts( new HashSet<Artifact>() );
162         super.setExtensionArtifacts( new HashSet<Artifact>() );
163         super.setRemoteArtifactRepositories( new LinkedList<ArtifactRepository>() );
164         super.setPluginArtifactRepositories( new LinkedList<ArtifactRepository>() );
165         super.setCollectedProjects( new LinkedList<MavenProject>() );
166         super.setActiveProfiles( new LinkedList<Profile>() );
167         super.setOriginalModel( null );
168         super.setExecutionProject( this );
169         super.setArtifact( artifact );
170     }
171 }