View Javadoc

1   package org.apache.maven.plugin.idea.stubs;
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 org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.repository.ArtifactRepository;
24  import org.apache.maven.artifact.repository.DefaultArtifactRepository;
25  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
26  import org.apache.maven.model.Build;
27  import org.apache.maven.model.Dependency;
28  import org.apache.maven.model.Resource;
29  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
30  import org.codehaus.plexus.PlexusTestCase;
31  
32  import java.io.File;
33  import java.util.ArrayList;
34  import java.util.Collections;
35  import java.util.List;
36  
37  /**
38   * @author Edwin Punzalan
39   */
40  public class SimpleMavenProjectStub
41      extends MavenProjectStub
42  {
43  
44      private String testId;
45  
46      private List collectedProjects;
47  
48      private Build build;
49  
50      private List testArtifacts;
51  
52      private List remoteRepositories;
53  
54      protected String getTestId()
55      {
56          if ( testId == null )
57          {
58              throw new IllegalStateException( "missing test id, project stub has not been properly initialized" );
59          }
60          return testId;
61      }
62  
63      public String getGroupId()
64      {
65          return "org.apache.maven.plugin.test";
66      }
67  
68      public String getArtifactId()
69      {
70          return "plugin-test-" + getTestId();
71      }
72  
73      public String getVersion()
74      {
75          return "1.0";
76      }
77  
78      public File getBasedir()
79      {
80          File basedir = new File( PlexusTestCase.getBasedir(), "target/test-harness/" + getTestId() );
81  
82          if ( !basedir.exists() )
83          {
84              basedir.mkdirs();
85          }
86  
87          return basedir;
88      }
89  
90      public List getCollectedProjects()
91      {
92          if ( collectedProjects == null )
93          {
94              collectedProjects = new ArrayList();
95          }
96          return collectedProjects;
97      }
98  
99      public void setCollectedProjects( List list )
100     {
101         collectedProjects = list;
102     }
103 
104     public boolean isExecutionRoot()
105     {
106         return true;
107     }
108 
109     public List getDependencies()
110     {
111         List dependencies = new ArrayList();
112 
113         Dependency dep = new Dependency();
114         dep.setGroupId( "org.apache.maven" );
115         dep.setArtifactId( "maven-model" );
116         dep.setVersion( "2.0.1" );
117         dep.setScope( Artifact.SCOPE_COMPILE );
118         dependencies.add( dep );
119 
120         dep = new Dependency();
121         dep.setGroupId( "junit" );
122         dep.setArtifactId( "junit" );
123         dep.setVersion( "3.8.1" );
124         dep.setScope( Artifact.SCOPE_TEST );
125         dependencies.add( dep );
126 
127         return dependencies;
128     }
129 
130     public Artifact getArtifact()
131     {
132         Artifact artifact = new IdeaArtifactStub();
133 
134         artifact.setGroupId( getGroupId() );
135 
136         artifact.setArtifactId( getArtifactId() );
137 
138         artifact.setVersion( getVersion() );
139 
140         return artifact;
141     }
142 
143     public Build getBuild()
144     {
145         if ( build == null )
146         {
147             build = new Build();
148             build.setDirectory( getBasedir().getAbsolutePath() + "/target" );
149             build.setOutputDirectory( getBasedir().getAbsolutePath() + "/target/classes" );
150             build.setTestOutputDirectory( getBasedir().getAbsolutePath() + "/target/test-classes" );
151 
152             Resource resource = new Resource();
153             resource.setDirectory( getBasedir().getAbsolutePath() + "/src/main/resources" );
154             resource.setFiltering( false );
155             build.setResources( Collections.singletonList( resource ) );
156 
157             resource = new Resource();
158             resource.setFiltering( false );
159             resource.setDirectory( getBasedir().getAbsolutePath() + "/src/test/resources" );
160             build.setTestResources( Collections.singletonList( resource ) );
161         }
162 
163         return build;
164     }
165 
166     public List getRemoteArtifactRepositories()
167     {
168         if ( remoteRepositories == null )
169         {
170             File testRepo = new File( PlexusTestCase.getBasedir(), "src/test/remote-repo" );
171             ArtifactRepository repository = new DefaultArtifactRepository( "test-repo",
172                                                                            "file://" + testRepo.getAbsolutePath(),
173                                                                            new DefaultRepositoryLayout() );
174             remoteRepositories = Collections.singletonList( repository );
175         }
176 
177         return remoteRepositories;
178     }
179 
180     public List getCompileSourceRoots()
181     {
182         File src = new File( getBasedir().getAbsolutePath() + "/src/main/java" );
183 
184         src.mkdirs();
185 
186         return Collections.singletonList( src.getAbsolutePath() );
187     }
188 
189     public List getTestArtifacts()
190     {
191         if ( testArtifacts == null )
192         {
193             testArtifacts = new ArrayList();
194 
195             testArtifacts.add( createArtifact( "org.apache.maven", "maven-model", "2.0.1" ) );
196 
197             testArtifacts.add( createArtifact( "junit", "junit", "3.8.1" ) );
198         }
199 
200         return testArtifacts;
201     }
202 
203     public void setTestArtifacts( List artifacts )
204     {
205         testArtifacts = artifacts;
206     }
207 
208     public List getTestCompileSourceRoots()
209     {
210         File src = new File( getBasedir().getAbsolutePath() + "/src/test/java" );
211 
212         src.mkdirs();
213 
214         return Collections.singletonList( src.getAbsolutePath() );
215     }
216 
217     protected Artifact createArtifact( String groupId, String artifactId, String version )
218     {
219         Artifact artifact = new IdeaArtifactStub();
220 
221         artifact.setGroupId( groupId );
222         artifact.setArtifactId( artifactId );
223         artifact.setVersion( version );
224         artifact.setFile( new File( PlexusTestCase.getBasedir(), "target/local-repo/" +
225             artifact.getGroupId().replace( '.', '/' ) + "/" + artifact.getArtifactId() + "/" + artifact.getVersion() +
226             "/" + artifact.getArtifactId() + "-" + artifact.getVersion() + ".jar" ) );
227 
228         return artifact;
229     }
230 
231     public List getBuildPlugins()
232     {
233         return getBuild().getPlugins();
234     }
235 }