View Javadoc

1   package org.apache.maven.continuum.configuration;
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.configuration.BuildAgentConfiguration;
23  import org.apache.continuum.configuration.BuildAgentGroupConfiguration;
24  import org.codehaus.plexus.spring.PlexusInSpringTestCase;
25  import org.codehaus.plexus.util.FileUtils;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import java.io.File;
30  
31  /**
32   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
33   * @version $Id: ConfigurationServiceTest.java 1372260 2012-08-13 04:29:09Z brett $
34   */
35  public class ConfigurationServiceTest
36      extends PlexusInSpringTestCase
37  {
38      private static final Logger log = LoggerFactory.getLogger( ConfigurationServiceTest.class );
39  
40      private static final String confFile = "target/test-classes/conf/continuum.xml";
41  
42      @Override
43      protected void setUp()
44          throws Exception
45      {
46          File originalConf = new File( getBasedir(), "src/test/resources/conf/continuum.xml" );
47  
48          File confUsed = new File( getBasedir(), confFile );
49          if ( confUsed.exists() )
50          {
51              confUsed.delete();
52          }
53          FileUtils.copyFile( originalConf, confUsed );
54          super.setUp();
55      }
56  
57      public void testLoad()
58          throws Exception
59      {
60          ConfigurationService service = (ConfigurationService) lookup( "configurationService" );
61          assertNotNull( service );
62  
63          assertNotNull( service.getUrl() );
64          assertEquals( "http://test", service.getUrl() );
65          log.info( "myBuildOutputDir " + new File( getBasedir(), "target/myBuildOutputDir" ).getAbsolutePath() );
66  
67          log.info( "getBuildOutputDirectory " + service.getBuildOutputDirectory().getAbsolutePath() );
68          assertEquals( new File( getBasedir(), "target/myBuildOutputDir" ).getAbsolutePath(),
69                        service.getBuildOutputDirectory().getAbsolutePath() );
70      }
71  
72      public void testConfigurationService()
73          throws Exception
74      {
75          File conf = new File( getBasedir(), confFile );
76          if ( conf.exists() )
77          {
78              conf.delete();
79          }
80  
81          ConfigurationService service = (ConfigurationService) lookup( "configurationService" );
82  
83          assertNotNull( service );
84  
85  //        service.load();
86  
87  //        assertEquals( "http://test", service.getUrl() );
88  
89  //        assertEquals( "build-output-directory", service.getBuildOutputDirectory().getName() );
90  
91  //        assertEquals( "working-directory", service.getWorkingDirectory().getName() );
92  
93          assertEquals( "check # build agents", 1, service.getBuildAgents().size() );
94  
95          service.setUrl( "http://test/zloug" );
96          service.setBuildOutputDirectory( new File( "testBuildOutputDir" ) );
97  
98          BuildAgentConfiguration buildAgent = new BuildAgentConfiguration( "http://test/xmlrpc", "windows", false );
99          service.addBuildAgent( buildAgent );
100 
101         service.store();
102 
103         String contents = FileUtils.fileRead( conf );
104         //assertTrue( contents.indexOf( "http://test/zloug" ) > 0 );
105 
106         service.reload();
107 
108         assertEquals( "http://test/zloug", service.getUrl() );
109         assertEquals( "check # build agents", 2, service.getBuildAgents().size() );
110         assertEquals( "http://test/xmlrpc", service.getBuildAgents().get( 1 ).getUrl() );
111         assertEquals( "windows", service.getBuildAgents().get( 1 ).getDescription() );
112         assertFalse( service.getBuildAgents().get( 1 ).isEnabled() );
113 
114         assertEquals( "http://test/xmlrpc", buildAgent.getUrl() );
115         service.removeBuildAgent( buildAgent );
116         service.store();
117         service.reload();
118 
119         assertEquals( "check # build agents", 1, service.getBuildAgents().size() );
120         assertEquals( "http://buildagent/xmlrpc", service.getBuildAgents().get( 0 ).getUrl() );
121         assertEquals( "linux", service.getBuildAgents().get( 0 ).getDescription() );
122         assertTrue( service.getBuildAgents().get( 0 ).isEnabled() );
123 
124         BuildAgentGroupConfiguration buildAgentGroup = new BuildAgentGroupConfiguration();
125         buildAgentGroup.setName( "group-1" );
126         buildAgentGroup.addBuildAgent( buildAgent );
127         service.addBuildAgentGroup( buildAgentGroup );
128 
129         service.store();
130         service.reload();
131         assertEquals( "check # build agent groups", 1, service.getBuildAgentGroups().size() );
132         assertEquals( "group-1", service.getBuildAgentGroups().get( 0 ).getName() );
133         assertEquals( "windows", service.getBuildAgentGroups().get( 0 ).getBuildAgents().get( 0 ).getDescription() );
134 
135         BuildAgentConfiguration buildAgent2 = new BuildAgentConfiguration( "http://machine-1/xmlrpc", "node-1", true );
136         //buildAgentGroup.addBuildAgent( buildAgent2 );
137         service.addBuildAgent( buildAgentGroup, buildAgent2 );
138 
139         service.store();
140         service.reload();
141 
142         assertEquals( "check # build agent groups", 1, service.getBuildAgentGroups().size() );
143         assertEquals( "check # build agent groups", 2, service.getBuildAgentGroups().get( 0 ).getBuildAgents().size() );
144         assertEquals( "group-1", service.getBuildAgentGroups().get( 0 ).getName() );
145         assertEquals( "windows", service.getBuildAgentGroups().get( 0 ).getBuildAgents().get( 0 ).getDescription() );
146         assertEquals( "http://machine-1/xmlrpc", service.getBuildAgentGroups().get( 0 ).getBuildAgents().get(
147             1 ).getUrl() );
148         assertEquals( "node-1", service.getBuildAgentGroups().get( 0 ).getBuildAgents().get( 1 ).getDescription() );
149         assertEquals( true, service.getBuildAgentGroups().get( 0 ).getBuildAgents().get( 1 ).isEnabled() );
150 
151         service.removeBuildAgent( buildAgentGroup, buildAgent2 );
152         service.store();
153         service.reload();
154 
155         assertEquals( "check # build agent groups", 1, service.getBuildAgentGroups().size() );
156         assertEquals( "group-1", service.getBuildAgentGroups().get( 0 ).getName() );
157         assertEquals( "windows", service.getBuildAgentGroups().get( 0 ).getBuildAgents().get( 0 ).getDescription() );
158         assertNull( service.getSharedSecretPassword() );
159 
160         service.setSharedSecretPassword( "password" );
161         service.store();
162         service.reload();
163 
164         assertEquals( "password", service.getSharedSecretPassword() );
165     }
166 
167     public void testAddDuplicateBuildAgentUrl()
168         throws Exception
169     {
170         ConfigurationService service = (ConfigurationService) lookup( "configurationService" );
171 
172         assertNotNull( service );
173 
174         BuildAgentConfiguration buildAgent = new BuildAgentConfiguration( "http://agent1/xmlrpc ", "windows", false );
175         service.addBuildAgent( buildAgent );
176         service.store();
177         service.reload();
178 
179         assertEquals( "check # build agents", 2, service.getBuildAgents().size() );
180         assertNotNull( service.getBuildAgent( "http://agent1/xmlrpc" ) );
181 
182         BuildAgentConfiguration buildAgent2 = new BuildAgentConfiguration( "http://agent1/xmlrpc", "windows", false );
183 
184         try
185         {
186             service.addBuildAgent( buildAgent2 );
187             fail( "Should have thrown an exception because of duplicate agent url" );
188         }
189         catch ( ConfigurationException e )
190         {
191             assertEquals( "Unable to add build agent: build agent already exist", e.getMessage() );
192         }
193 
194         service.removeBuildAgent( buildAgent );
195         service.store();
196     }
197 }