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