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      @Override
79      public String getName()
80      {
81          return "Test Project " + identifier;
82      }
83  
84      @Override
85      public void setDescription( String desc )
86      {
87          description = desc;
88      }
89  
90      @Override
91      public String getDescription()
92      {
93          if ( description == null )
94          {
95              return "this is a test project";
96          }
97          else
98          {
99              return description;
100         }
101     }
102 
103     @Override
104     public File getBasedir()
105     {
106         // create an isolated environment
107         // see setupTestEnvironment for details
108         return new File( testRootDir );
109     }
110 
111     @Override
112     public Artifact getArtifact()
113     {
114         return artifact;
115     }
116 
117     @Override
118     public String getGroupId()
119     {
120         return "org.apache.maven.plugin.test";
121     }
122 
123     @Override
124     public String getArtifactId()
125     {
126         return "maven-resource-plugin-test#" + identifier;
127     }
128 
129     @Override
130     public String getPackaging()
131     {
132         return "ejb";
133     }
134 
135     @Override
136     public String getVersion()
137     {
138         return identifier;
139     }
140 
141     public void addProperty( String key, String value )
142     {
143         properties.put( key, value );
144     }
145 
146     @Override
147     public Properties getProperties()
148     {
149         return properties;
150     }
151 
152     // to prevent the MavenProject copy constructor from blowing up
153     private void initializeParentFields()
154     {
155         // the pom should be located in the isolated dummy root
156         super.setFile( new File( getBasedir(), "pom.xml" ) );
157         super.setDependencyArtifacts( new HashSet<>() );
158         super.setArtifacts( new HashSet<>() );
159         super.setPluginArtifacts( new HashSet<>() );
160         super.setReportArtifacts( new HashSet<>() );
161         super.setExtensionArtifacts( new HashSet<>() );
162         super.setRemoteArtifactRepositories( new LinkedList<>() );
163         super.setPluginArtifactRepositories( new LinkedList<>() );
164         super.setCollectedProjects( new LinkedList<>() );
165         super.setActiveProfiles( new LinkedList<>() );
166         super.setOriginalModel( null );
167         super.setExecutionProject( this );
168         super.setArtifact( artifact );
169     }
170 }