View Javadoc

1   package org.apache.maven.archiva.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 java.io.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  import java.util.Map;
26  import java.util.Properties;
27  
28  import org.codehaus.plexus.spring.PlexusInSpringTestCase;
29  
30  /**
31   */
32  public class MavenProxyPropertyLoaderTest
33      extends PlexusInSpringTestCase
34  {
35      private MavenProxyPropertyLoader loader;
36  
37      /**
38       * {@inheritDoc}
39       * @see org.codehaus.plexus.spring.PlexusInSpringTestCase#getSpringConfigLocation()
40       */
41      protected String getSpringConfigLocation()
42      {
43          return "org/apache/maven/archiva/configuration/spring-context.xml";
44      }
45  
46      public void testLoadValidMavenProxyConfiguration()
47          throws IOException, InvalidConfigurationException
48      {
49          File confFile = getTestFile( "src/test/conf/maven-proxy-complete.conf" );
50  
51          Configuration configuration = new Configuration();
52          NetworkProxyConfiguration proxy = new NetworkProxyConfiguration();
53          proxy.setHost( "original-host" );
54          configuration.addNetworkProxy( proxy ); // overwritten
55  
56          loader.load( new FileInputStream( confFile ), configuration );
57  
58          Map<String, ManagedRepositoryConfiguration> repositoryIdMap = configuration.getManagedRepositoriesAsMap();
59          assertEquals( "Count repositories", 1, repositoryIdMap.size() );
60          assertRepositoryExists( "maven-proxy", "target", repositoryIdMap.get( "maven-proxy" ) );
61  
62          Map<String, RemoteRepositoryConfiguration> remoteRepositoryMap = configuration.getRemoteRepositoriesAsMap();
63          assertEquals( "Count repositories", 4, remoteRepositoryMap.size() );
64          assertRepositoryExists( "local-repo", "file://target", remoteRepositoryMap.get( "local-repo" ) );
65          assertRepositoryExists( "www-ibiblio-org", "http://www.ibiblio.org/maven2",
66                                  remoteRepositoryMap.get( "www-ibiblio-org" ) );
67          assertRepositoryExists( "dist-codehaus-org", "http://dist.codehaus.org",
68                                  remoteRepositoryMap.get( "dist-codehaus-org" ) );
69          assertRepositoryExists( "private-example-com", "http://private.example.com/internal",
70                                  remoteRepositoryMap.get( "private-example-com" ) );
71      }
72  
73      private void assertRepositoryExists( String id, String expectedLocation, ManagedRepositoryConfiguration repo )
74      {
75          assertNotNull( "Repository id [" + id + "] should not be null", repo );
76          assertEquals( "Repository id", id, repo.getId() );
77          assertEquals( "Repository url", expectedLocation, repo.getLocation() );
78      }
79  
80      private void assertRepositoryExists( String id, String expectedUrl, RemoteRepositoryConfiguration repo )
81      {
82          assertNotNull( "Repository id [" + id + "] should not be null", repo );
83          assertEquals( "Repository id", id, repo.getId() );
84          assertEquals( "Repository url", expectedUrl, repo.getUrl() );
85      }
86  
87      public void testInvalidConfiguration()
88      {
89          Configuration configuration = new Configuration();
90          try
91          {
92              loader.load( new Properties(), configuration );
93              fail( "Incomplete config should have failed" );
94          }
95          catch ( InvalidConfigurationException e )
96          {
97              assertTrue( true );
98          }
99      }
100 
101     protected void setUp()
102         throws Exception
103     {
104         super.setUp();
105         loader = new MavenProxyPropertyLoader();
106     }
107 }