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