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.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) 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         return "org.apache.maven.plugin.test";
121     }
122 
123     public String getArtifactId()
124     {
125         return "maven-resource-plugin-test#" + identifier;
126     }
127 
128     public String getPackaging()
129     {
130         return "ejb";
131     }
132 
133     public String getVersion()
134     {
135         return identifier;
136     }
137 
138     public void addProperty( String key, String value )
139     {
140         properties.put( key, value );
141     }
142 
143     public Properties getProperties()
144     {
145         return properties;
146     }
147 
148     // to prevent the MavenProject copy constructor from blowing up
149     private void initializeParentFields()
150     {
151         // the pom should be located in the isolated dummy root         
152         super.setFile( new File( getBasedir(), "pom.xml" ) );
153         super.setDependencyArtifacts( new HashSet() );
154         super.setArtifacts( new HashSet() );
155         super.setPluginArtifacts( new HashSet() );
156         super.setReportArtifacts( new HashSet() );
157         super.setExtensionArtifacts( new HashSet() );
158         super.setRemoteArtifactRepositories( new LinkedList() );
159         super.setPluginArtifactRepositories( new LinkedList() );
160         super.setCollectedProjects( new LinkedList() );
161         super.setActiveProfiles( new LinkedList() );
162         super.setOriginalModel( null );
163         super.setExecutionProject( this );
164         super.setArtifact( artifact );
165     }
166 }