View Javadoc

1   package org.apache.maven.continuum.core.action;
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.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.maven.continuum.execution.ContinuumBuildExecutorConstants;
26  import org.apache.maven.continuum.model.project.BuildDefinition;
27  import org.apache.maven.continuum.model.project.BuildDefinitionTemplate;
28  import org.apache.maven.continuum.model.project.Project;
29  import org.apache.maven.continuum.project.builder.ContinuumProjectBuilder;
30  import org.apache.maven.continuum.project.builder.ContinuumProjectBuildingResult;
31  import org.apache.maven.continuum.project.builder.manager.ContinuumProjectBuilderManager;
32  import org.apache.maven.continuum.utils.ContinuumUrlValidator;
33  import org.apache.maven.settings.MavenSettingsBuilder;
34  import org.apache.maven.settings.Settings;
35  import org.codehaus.plexus.logging.Logger;
36  import org.codehaus.plexus.logging.console.ConsoleLogger;
37  import org.jmock.Mock;
38  import org.jmock.MockObjectTestCase;
39  
40  public class CreateProjectsFromMetadataTest
41      extends MockObjectTestCase
42  {
43  
44      private CreateProjectsFromMetadataAction action;
45  
46      private ContinuumProjectBuildingResult result;
47  
48      protected void setUp()
49          throws Exception
50      {
51          result = new ContinuumProjectBuildingResult();
52          action = new CreateProjectsFromMetadataAction();
53          action.enableLogging( new ConsoleLogger( Logger.LEVEL_DEBUG, "" ) );
54          Mock projectBuilderManagerMock = mock( ContinuumProjectBuilderManager.class );
55          Mock mavenSettingsBuilderMock = mock( MavenSettingsBuilder.class );
56          action.setProjectBuilderManager( (ContinuumProjectBuilderManager) projectBuilderManagerMock.proxy() );
57          action.setMavenSettingsBuilder( (MavenSettingsBuilder) mavenSettingsBuilderMock.proxy() );
58          action.setUrlValidator( new ContinuumUrlValidator() );
59          Mock projectBuilder = mock( ContinuumProjectBuilder.class );
60  
61          projectBuilderManagerMock.expects( once() ).method( "getProjectBuilder" ).will(
62              returnValue( projectBuilder.proxy() ) );
63          projectBuilder.expects( once() ).method( "buildProjectsFromMetadata" ).will(
64              returnValue( result ) );
65  
66          projectBuilder.expects( once() ).method( "getDefaultBuildDefinitionTemplate" ).will(
67              returnValue( getDefaultBuildDefinitionTemplate() ) );
68  
69          mavenSettingsBuilderMock.expects( once() ).method( "buildSettings" ).will( returnValue( new Settings() ) );
70  
71      }
72  
73      private BuildDefinitionTemplate getDefaultBuildDefinitionTemplate()
74          throws Exception
75      {
76          BuildDefinition bd = new BuildDefinition();
77  
78          bd.setDefaultForProject( true );
79  
80          bd.setGoals( "clean install" );
81  
82          bd.setArguments( "-B" );
83  
84          bd.setBuildFile( "pom.xml" );
85  
86          bd.setType( ContinuumBuildExecutorConstants.MAVEN_TWO_BUILD_EXECUTOR );
87  
88          BuildDefinitionTemplate bdt = new BuildDefinitionTemplate();
89          bdt.addBuildDefinition( bd );
90          return bdt;
91      }
92  
93      @SuppressWarnings("unchecked")
94      public void testExecuteWithNonRecursiveMode()
95          throws Exception
96      {
97          Map<String, Object> context = new HashMap<String, Object>();
98          CreateProjectsFromMetadataAction.setUrl( context,
99                                                   "http://svn.apache.org/repos/asf/maven/continuum/trunk/pom.xml" );
100         CreateProjectsFromMetadataAction.setProjectBuilderId( context, "id" );
101         CreateProjectsFromMetadataAction.setLoadRecursiveProject( context, true );
102 
103         action.execute( context );
104 
105         ContinuumProjectBuildingResult result = CreateProjectsFromMetadataAction.getProjectBuildingResult( context );
106 
107         assertFalse(
108             "Should not have errors but had " + result.getErrorsAsString() + " (this test requires internet access)",
109             result.hasErrors() );
110     }
111 
112     public void testExecuteWithRecursiveMode()
113         throws Exception
114     {
115         Map<String, Object> context = new HashMap<String, Object>();
116         CreateProjectsFromMetadataAction.setUrl( context,
117                                                  "http://svn.apache.org/repos/asf/maven/archiva/trunk/pom.xml" );
118         CreateProjectsFromMetadataAction.setProjectBuilderId( context, "id" );
119         CreateProjectsFromMetadataAction.setLoadRecursiveProject( context, false );
120 
121         action.execute( context );
122 
123         ContinuumProjectBuildingResult result = CreateProjectsFromMetadataAction.getProjectBuildingResult( context );
124 
125         assertFalse(
126             "Should not have errors but had " + result.getErrorsAsString() + " (this test requires internet access)",
127             result.hasErrors() );
128     }
129 
130     public void testExecuteFlatMultiModuleProjectThatStartsWithTheSameLetter()
131         throws Exception
132     {
133         Project project = new Project();
134         project.setGroupId( "com.example.flat" );
135         project.setArtifactId( "flat-parent" );
136         project.setVersion( "1.0-SNAPSHOT" );
137         project.setId( 6 );
138         project.setName( "Flat Example" );
139         project.setScmUrl( "scm:svn:http://svn.apache.org/repos/asf/continuum/sandbox/flat-example/flat-parent" );
140 
141         this.result.addProject( project );
142 
143         project = new Project();
144         project.setGroupId( "com.example.flat" );
145         project.setArtifactId( "flat-core" );
146         project.setVersion( "1.0-SNAPSHOT" );
147         project.setId( 7 );
148         project.setName( "flat-core" );
149         project.setScmUrl( "scm:svn:http://svn.apache.org/repos/asf/continuum/sandbox/flat-example/flat-core" );
150 
151         this.result.addProject( project );
152 
153         project = new Project();
154         project.setGroupId( "com.example.flat" );
155         project.setArtifactId( "flat-webapp" );
156         project.setVersion( "1.0-SNAPSHOT" );
157         project.setId( 8 );
158         project.setName( "flat-webapp Maven Webapp" );
159         project.setScmUrl( "scm:svn:http://svn.apache.org/repos/asf/continuum/sandbox/flat-example/flat-webapp" );
160 
161         this.result.addProject( project );
162 
163         Map<String, Object> context = new HashMap<String, Object>();
164         CreateProjectsFromMetadataAction.setUrl( context,
165                                                  "http://svn.apache.org/repos/asf/continuum/sandbox/flat-example/flat-parent/pom.xml" );
166         CreateProjectsFromMetadataAction.setProjectBuilderId( context, "id" );
167         CreateProjectsFromMetadataAction.setLoadRecursiveProject( context, true );
168 
169         action.execute( context );
170 
171         ContinuumProjectBuildingResult result = CreateProjectsFromMetadataAction.getProjectBuildingResult( context );
172 
173         assertFalse(
174             "Should not have errors but had " + result.getErrorsAsString() + " (this test requires internet access)",
175             result.hasErrors() );
176 
177         assertEquals(
178             "Wrong scm root url created", "scm:svn:http://svn.apache.org/repos/asf/continuum/sandbox/flat-example/",
179             CreateProjectsFromMetadataAction.getUrl( context ) );
180     }
181 }