1 package org.apache.maven.project;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.repository.ArtifactRepository;
29 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
30 import org.codehaus.plexus.util.FileUtils;
31
32 public class DefaultMavenProjectBuilderTest
33 extends AbstractMavenProjectTestCase
34 {
35
36 private List<File> filesToDelete = new ArrayList<File>();
37
38 private File localRepoDir;
39
40 @Override
41 public void setUp()
42 throws Exception
43 {
44 super.setUp();
45
46 projectBuilder = lookup( ProjectBuilder.class );
47
48 localRepoDir = new File( System.getProperty( "java.io.tmpdir" ), "local-repo." + System.currentTimeMillis() );
49 localRepoDir.mkdirs();
50
51 filesToDelete.add( localRepoDir );
52 }
53
54 @Override
55 public void tearDown()
56 throws Exception
57 {
58 super.tearDown();
59
60 if ( !filesToDelete.isEmpty() )
61 {
62 for ( Iterator<File> it = filesToDelete.iterator(); it.hasNext(); )
63 {
64 File file = it.next();
65
66 if ( file.exists() )
67 {
68 if ( file.isDirectory() )
69 {
70 FileUtils.deleteDirectory( file );
71 }
72 else
73 {
74 file.delete();
75 }
76 }
77 }
78 }
79 }
80
81 protected MavenProject getProject( Artifact pom, boolean allowStub )
82 throws Exception
83 {
84 ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
85 configuration.setLocalRepository( getLocalRepository() );
86 initRepoSession( configuration );
87
88 return projectBuilder.build( pom, allowStub, configuration ).getProject();
89 }
90
91
92
93
94
95 public void testBuildFromMiddlePom() throws Exception
96 {
97 File f1 = getTestFile( "src/test/resources/projects/grandchild-check/child/pom.xml");
98 File f2 = getTestFile( "src/test/resources/projects/grandchild-check/child/grandchild/pom.xml");
99
100 getProject( f1 );
101
102
103
104 getProject( f2 );
105 }
106
107 public void testDuplicatePluginDefinitionsMerged()
108 throws Exception
109 {
110 File f1 = getTestFile( "src/test/resources/projects/duplicate-plugins-merged-pom.xml" );
111
112 MavenProject project = getProject( f1 );
113 assertEquals( 2, project.getBuildPlugins().get( 0 ).getDependencies().size() );
114 assertEquals( 2, project.getBuildPlugins().get( 0 ).getExecutions().size() );
115 assertEquals( "first", project.getBuildPlugins().get( 0 ).getExecutions().get( 0 ).getId() );
116 }
117
118 public void testBuildStubModelForMissingRemotePom()
119 throws Exception
120 {
121 Artifact pom = repositorySystem.createProjectArtifact( "org.apache.maven.its", "missing", "0.1" );
122 MavenProject project = getProject( pom, true );
123
124 assertNotNull( project.getArtifactId() );
125
126 assertNotNull( project.getRemoteArtifactRepositories() );
127 assertFalse( project.getRemoteArtifactRepositories().isEmpty() );
128
129 assertNotNull( project.getPluginArtifactRepositories() );
130 assertFalse( project.getPluginArtifactRepositories().isEmpty() );
131
132 assertNull( project.getParent() );
133 assertNull( project.getParentArtifact() );
134
135 assertFalse( project.isExecutionRoot() );
136 }
137
138 @Override
139 protected ArtifactRepository getLocalRepository()
140 throws Exception
141 {
142 ArtifactRepositoryLayout repoLayout = lookup( ArtifactRepositoryLayout.class, "default" );
143 ArtifactRepository r =
144 repositorySystem.createArtifactRepository( "local", "file://" + localRepoDir.getAbsolutePath(), repoLayout,
145 null, null );
146 return r;
147 }
148
149 public void xtestLoop()
150 throws Exception
151 {
152 while ( true )
153 {
154 File f1 = getTestFile( "src/test/resources/projects/duplicate-plugins-merged-pom.xml" );
155 getProject( f1 );
156 }
157 }
158
159 public void testPartialResultUponBadDependencyDeclaration()
160 throws Exception
161 {
162 File pomFile = getTestFile( "src/test/resources/projects/bad-dependency.xml" );
163
164 try
165 {
166 ProjectBuildingRequest request = newBuildingRequest();
167 request.setProcessPlugins( false );
168 request.setResolveDependencies( true );
169 projectBuilder.build( pomFile, request );
170 fail( "Project building did not fail despite invalid POM" );
171 }
172 catch ( ProjectBuildingException e )
173 {
174 List<ProjectBuildingResult> results = e.getResults();
175 assertNotNull( results );
176 assertEquals( 1, results.size() );
177 ProjectBuildingResult result = results.get( 0 );
178 assertNotNull( result );
179 assertNotNull( result.getProject() );
180 assertEquals( 1, result.getProblems().size() );
181 assertEquals( 1, result.getProject().getArtifacts().size() );
182 assertNotNull( result.getDependencyResolutionResult() );
183 }
184 }
185
186 }