View Javadoc
1   package org.apache.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.nio.file.Files;
26  import java.util.Map;
27  import java.util.Properties;
28  import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
29  import static org.junit.Assert.*;
30  import org.junit.Before;
31  import org.junit.Test;
32  import org.junit.runner.RunWith;
33  
34  /**
35   */
36  @RunWith( ArchivaBlockJUnit4ClassRunner.class )
37  public class MavenProxyPropertyLoaderTest
38  {
39      private MavenProxyPropertyLoader loader;
40  
41      @Test
42      public void testLoadValidMavenProxyConfiguration()
43          throws IOException, InvalidConfigurationException
44      {
45          File confFile = ArchivaConfigurationTest.getTestFile( "src/test/conf/maven-proxy-complete.conf" );
46  
47          Configuration configuration = new Configuration();
48          NetworkProxyConfiguration proxy = new NetworkProxyConfiguration();
49          proxy.setHost( "original-host" );
50          configuration.addNetworkProxy( proxy ); // overwritten
51  
52          loader.load( Files.newInputStream(confFile.toPath()), configuration );
53  
54          Map<String, ManagedRepositoryConfiguration> repositoryIdMap = configuration.getManagedRepositoriesAsMap();
55          assertEquals( "Count repositories", 1, repositoryIdMap.size() );
56          assertRepositoryExists( "maven-proxy", "target", repositoryIdMap.get( "maven-proxy" ) );
57  
58          Map<String, RemoteRepositoryConfiguration> remoteRepositoryMap = configuration.getRemoteRepositoriesAsMap();
59          assertEquals( "Count repositories", 4, remoteRepositoryMap.size() );
60          assertRepositoryExists( "local-repo", "file://target", remoteRepositoryMap.get( "local-repo" ) );
61          assertRepositoryExists( "www-ibiblio-org", "http://www.ibiblio.org/maven2",
62                                  remoteRepositoryMap.get( "www-ibiblio-org" ) );
63          assertRepositoryExists( "dist-codehaus-org", "http://dist.codehaus.org",
64                                  remoteRepositoryMap.get( "dist-codehaus-org" ) );
65          assertRepositoryExists( "private-example-com", "http://private.example.com/internal",
66                                  remoteRepositoryMap.get( "private-example-com" ) );
67      }
68  
69      private void assertRepositoryExists( String id, String expectedLocation, ManagedRepositoryConfiguration repo )
70      {
71          assertNotNull( "Repository id [" + id + "] should not be null", repo );
72          assertEquals( "Repository id", id, repo.getId() );
73          assertEquals( "Repository url", expectedLocation, repo.getLocation() );
74      }
75  
76      private void assertRepositoryExists( String id, String expectedUrl, RemoteRepositoryConfiguration repo )
77      {
78          assertNotNull( "Repository id [" + id + "] should not be null", repo );
79          assertEquals( "Repository id", id, repo.getId() );
80          assertEquals( "Repository url", expectedUrl, repo.getUrl() );
81      }
82  
83      @Test( expected=InvalidConfigurationException.class )
84      public void testInvalidConfiguration() 
85          throws InvalidConfigurationException
86      {
87          Configuration configuration = new Configuration();
88          loader.load( new Properties(), configuration );
89          //fail( "Incomplete config should have failed" );
90      }
91  
92      @Before
93      public void setUp()
94          throws Exception
95      {
96          loader = new MavenProxyPropertyLoader();
97      }
98  }