View Javadoc

1   package org.apache.continuum.buildagent.manager;
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.continuum.buildagent.configuration.BuildAgentConfigurationService;
23  import org.apache.continuum.buildagent.model.LocalRepository;
24  import org.apache.continuum.buildagent.utils.ContinuumBuildAgentUtil;
25  import org.apache.maven.continuum.release.ContinuumReleaseException;
26  import org.codehaus.plexus.spring.PlexusInSpringTestCase;
27  import org.jmock.Expectations;
28  import org.jmock.Mockery;
29  import org.jmock.integration.junit3.JUnit3Mockery;
30  import org.jmock.lib.legacy.ClassImposteriser;
31  
32  import java.io.File;
33  import java.util.ArrayList;
34  import java.util.HashMap;
35  import java.util.List;
36  import java.util.Map;
37  import java.util.Properties;
38  
39  /**
40   * For the CONTINUUM-2391 tests, checking of the local repository details is in ContinuumReleaseManagerStub. An
41   * exception is thrown if the set local repository in the repository map is incorrect.
42   */
43  public class BuildAgentReleaseManagerTest
44      extends PlexusInSpringTestCase
45  {
46      private Mockery context;
47  
48      private BuildAgentConfigurationService buildAgentConfigurationService;
49  
50      private DefaultBuildAgentReleaseManager releaseManager;
51  
52      protected void setUp()
53          throws Exception
54      {
55          super.setUp();
56  
57          context = new JUnit3Mockery();
58          context.setImposteriser( ClassImposteriser.INSTANCE );
59  
60          releaseManager = (DefaultBuildAgentReleaseManager) lookup( BuildAgentReleaseManager.class );
61  
62          buildAgentConfigurationService = context.mock( BuildAgentConfigurationService.class );
63  
64          releaseManager.setBuildAgentConfigurationService( buildAgentConfigurationService );
65      }
66  
67      protected void tearDown()
68          throws Exception
69      {
70          releaseManager = null;
71  
72          super.tearDown();
73      }
74  
75      // CONTINUUM-2391
76      public void testLocalRepositoryInReleasePrepare()
77          throws Exception
78      {
79          final List<LocalRepository> localRepos = createLocalRepositories();
80          final File workingDir = new File( getBasedir(), "target/test-classes/working-dir" );
81  
82          context.checking( new Expectations()
83          {
84              {
85                  one( buildAgentConfigurationService ).getLocalRepositories();
86                  will( returnValue( localRepos ) );
87  
88                  one( buildAgentConfigurationService ).getWorkingDirectory( 1 );
89                  will( returnValue( workingDir ) );
90  
91                  one( buildAgentConfigurationService ).getAvailableInstallations();
92                  will( returnValue( null ) );
93              }
94          } );
95  
96          try
97          {
98              releaseManager.releasePrepare( createProjectMap(), createProperties(), createReleaseVersionMap(),
99                                             createDevVersionMap(), createEnvironmentsMap(), "user" );
100         }
101         catch ( ContinuumReleaseException e )
102         {
103             fail( "An exception should not have been thrown!" );
104         }
105     }
106 
107     // CONTINUUM-2391
108     public void testLocalRepositoryNameMismatchedCaseInReleasePrepare()
109         throws Exception
110     {
111         final List<LocalRepository> localRepos = createLocalRepositories();
112         final File workingDir = new File( getBasedir(), "target/test-classes/working-dir" );
113 
114         context.checking( new Expectations()
115         {
116             {
117                 one( buildAgentConfigurationService ).getLocalRepositories();
118                 will( returnValue( localRepos ) );
119 
120                 one( buildAgentConfigurationService ).getWorkingDirectory( 1 );
121                 will( returnValue( workingDir ) );
122 
123                 one( buildAgentConfigurationService ).getAvailableInstallations();
124                 will( returnValue( null ) );
125             }
126         } );
127 
128         Map<String, Object> map = createProjectMap();
129 
130         try
131         {
132             releaseManager.releasePrepare( map, createProperties(), createReleaseVersionMap(), createDevVersionMap(),
133                                            createEnvironmentsMap(), "user" );
134         }
135         catch ( ContinuumReleaseException e )
136         {
137             fail( "An exception should not have been thrown!" );
138         }
139     }
140 
141     // CONTINUUM-2391
142     @SuppressWarnings( "unchecked" )
143     public void testLocalRepositoryInReleasePerform()
144         throws Exception
145     {
146         final List<LocalRepository> localRepos = createLocalRepositories();
147         final File workingDir = new File( getBasedir(), "target/test-classes/working-dir" );
148 
149         context.checking( new Expectations()
150         {
151             {
152                 one( buildAgentConfigurationService ).getLocalRepositories();
153                 will( returnValue( localRepos ) );
154 
155                 one( buildAgentConfigurationService ).getWorkingDirectory();
156                 will( returnValue( workingDir ) );
157             }
158         } );
159 
160         Map repository = createRepositoryMap();
161         repository.put( ContinuumBuildAgentUtil.KEY_LOCAL_REPOSITORY_NAME, "DEFAULT" );
162 
163         try
164         {
165             releaseManager.releasePerform( "1", "clean deploy", "", true, repository, "user" );
166         }
167         catch ( ContinuumReleaseException e )
168         {
169             fail( "An exception should not have been thrown!" );
170         }
171     }
172 
173     // CONTINUUM-2391
174     public void testLocalRepositoryNameMismatchedCaseInReleasePerform()
175         throws Exception
176     {
177         final List<LocalRepository> localRepos = createLocalRepositories();
178         final File workingDir = new File( getBasedir(), "target/test-classes/working-dir" );
179 
180         context.checking( new Expectations()
181         {
182             {
183                 one( buildAgentConfigurationService ).getLocalRepositories();
184                 will( returnValue( localRepos ) );
185 
186                 one( buildAgentConfigurationService ).getWorkingDirectory();
187                 will( returnValue( workingDir ) );
188             }
189         } );
190 
191         try
192         {
193             releaseManager.releasePerform( "1", "clean deploy", "", true, createRepositoryMap(), "user" );
194         }
195         catch ( ContinuumReleaseException e )
196         {
197             fail( "An exception should not have been thrown!" );
198         }
199     }
200 
201     // CONTINUUM-2391
202     @SuppressWarnings( "unchecked" )
203     public void testLocalRepositoryInReleasePerformFromScm()
204         throws Exception
205     {
206         final List<LocalRepository> localRepos = createLocalRepositories();
207         final File workingDir = new File( getBasedir(), "target/test-classes/working-dir" );
208 
209         context.checking( new Expectations()
210         {
211             {
212                 one( buildAgentConfigurationService ).getLocalRepositories();
213                 will( returnValue( localRepos ) );
214 
215                 one( buildAgentConfigurationService ).getWorkingDirectory();
216                 will( returnValue( workingDir ) );
217             }
218         } );
219 
220         Map repository = new HashMap();
221         repository.put( ContinuumBuildAgentUtil.KEY_USERNAME, "user" );
222         repository.put( ContinuumBuildAgentUtil.KEY_LOCAL_REPOSITORY_NAME, "default" );
223 
224         try
225         {
226             releaseManager.releasePerformFromScm( "clean deploy", "", true, repository,
227                                                   "scm:svn:http://svn.example.com/repos/test-project", "user",
228                                                   "mypasswrd",
229                                                   "scm:svn:http://svn.example.com/repos/test-project/tags/test-project-1.0",
230                                                   "scm:svn:http://svn.example.com/repos/test-project/tags", null,
231                                                   "user" );
232         }
233         catch ( ContinuumReleaseException e )
234         {
235             fail( "An exception should not have been thrown!" );
236         }
237     }
238 
239     private List<LocalRepository> createLocalRepositories()
240     {
241         List<LocalRepository> localRepos = new ArrayList<LocalRepository>();
242         LocalRepository localRepo = new LocalRepository();
243         localRepo.setName( "temp" );
244         localRepo.setLocation( "/tmp/.m2/repository" );
245         localRepo.setLayout( "default" );
246 
247         localRepos.add( localRepo );
248 
249         localRepo = new LocalRepository();
250         localRepo.setName( "default" );
251         localRepo.setLocation( "/home/user/.m2/repository" );
252         localRepo.setLayout( "default" );
253 
254         localRepos.add( localRepo );
255 
256         return localRepos;
257     }
258 
259     private Map<String, String> createEnvironmentsMap()
260     {
261         Map<String, String> environments = new HashMap<String, String>();
262         environments.put( "M2_HOME", "/tmp/bin/apache-maven-2.2.1" );
263 
264         return environments;
265     }
266 
267     private Map<String, String> createDevVersionMap()
268     {
269         Map<String, String> devVersion = new HashMap<String, String>();
270         devVersion.put( "1.1-SNAPSHOT", "1.1-SNAPSHOT" );
271 
272         return devVersion;
273     }
274 
275     private Map<String, String> createReleaseVersionMap()
276     {
277         Map<String, String> releaseVersion = new HashMap<String, String>();
278         releaseVersion.put( "1.0", "1.0" );
279 
280         return releaseVersion;
281     }
282 
283     private Properties createProperties()
284     {
285         Properties properties = new Properties();
286         properties.put( ContinuumBuildAgentUtil.KEY_SCM_USERNAME, "scmusername" );
287         properties.put( ContinuumBuildAgentUtil.KEY_SCM_PASSWORD, "scmpassword" );
288         properties.put( ContinuumBuildAgentUtil.KEY_SCM_TAGBASE,
289                         "scm:svn:http://svn.example.com/repos/test-project/tags" );
290         properties.put( ContinuumBuildAgentUtil.KEY_PREPARE_GOALS, "clean install" );
291         properties.put( ContinuumBuildAgentUtil.KEY_ARGUMENTS, "" );
292         properties.put( ContinuumBuildAgentUtil.KEY_SCM_TAG, "test-project-1.0" );
293 
294         return properties;
295     }
296 
297     private Map<String, Object> createProjectMap()
298     {
299         Map<String, Object> map = new HashMap<String, Object>();
300         map.put( ContinuumBuildAgentUtil.KEY_LOCAL_REPOSITORY_NAME, "default" );
301         map.put( ContinuumBuildAgentUtil.KEY_PROJECT_ID, 1 );
302         map.put( ContinuumBuildAgentUtil.KEY_GROUP_ID, "1" );
303         map.put( ContinuumBuildAgentUtil.KEY_ARTIFACT_ID, "test-project" );
304         map.put( ContinuumBuildAgentUtil.KEY_SCM_URL, "scm:svn:http://svn.example.com/repos/test-project/trunk" );
305 
306         return map;
307     }
308 
309     @SuppressWarnings( "unchecked" )
310     private Map createRepositoryMap()
311     {
312         Map repository = new HashMap();
313         repository.put( ContinuumBuildAgentUtil.KEY_USERNAME, "user" );
314         repository.put( ContinuumBuildAgentUtil.KEY_LOCAL_REPOSITORY_NAME, "default" );
315 
316         return repository;
317     }
318 }