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.util.Arrays;
23  import java.util.HashSet;
24  import java.util.Map;
25  import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
26  import static org.junit.Assert.*;
27  import org.junit.Test;
28  import org.junit.runner.RunWith;
29  
30  /**
31   * Test the generated Configuration class from Modello. This is primarily to test the hand coded methods.
32   */
33  @RunWith( ArchivaBlockJUnit4ClassRunner.class )
34  public class ConfigurationTest
35  {
36      private Configuration configuration = new Configuration();
37  
38      @Test
39      public void testNetworkProxyRetrieval()
40      {
41          NetworkProxyConfiguration proxy1 = createNetworkProxy( "id1", "host1", 8080 );
42          configuration.addNetworkProxy( proxy1 );
43          NetworkProxyConfiguration proxy2 = createNetworkProxy( "id2", "host2", 9090 );
44          configuration.addNetworkProxy( proxy2 );
45  
46          Map<String, NetworkProxyConfiguration> map = configuration.getNetworkProxiesAsMap();
47          assertNotNull( map );
48          assertEquals( 2, map.size() );
49          assertEquals( new HashSet<String>( Arrays.asList( "id1", "id2" ) ), map.keySet() );
50          assertEquals( new HashSet<NetworkProxyConfiguration>( Arrays.asList( proxy1, proxy2 ) ),
51                        new HashSet<NetworkProxyConfiguration>( map.values() ) );
52      }
53  
54      private NetworkProxyConfiguration createNetworkProxy( String id, String host, int port )
55      {
56          NetworkProxyConfiguration proxy = new NetworkProxyConfiguration();
57          proxy.setId( id );
58          proxy.setHost( host );
59          proxy.setPort( port );
60          proxy.setProtocol( "http" );
61          return proxy;
62      }
63  
64      @Test
65      public void testRemoteRepositoryRetrieval()
66      {
67          RemoteRepositoryConfiguration repo1 = createRemoteRepository( "id1", "name 1", "url 1" );
68          configuration.addRemoteRepository( repo1 );
69          RemoteRepositoryConfiguration repo2 = createRemoteRepository( "id2", "name 2", "url 2" );
70          configuration.addRemoteRepository( repo2 );
71  
72          Map<String, RemoteRepositoryConfiguration> map = configuration.getRemoteRepositoriesAsMap();
73          assertNotNull( map );
74          assertEquals( 2, map.size() );
75          assertEquals( new HashSet<String>( Arrays.asList( "id1", "id2" ) ), map.keySet() );
76          assertEquals( new HashSet<RemoteRepositoryConfiguration>( Arrays.asList( repo1, repo2 ) ),
77                        new HashSet<RemoteRepositoryConfiguration>( map.values() ) );
78  
79          assertEquals( repo1, configuration.findRemoteRepositoryById( "id1" ) );
80          assertEquals( repo2, configuration.findRemoteRepositoryById( "id2" ) );
81          assertNull( configuration.findRemoteRepositoryById( "id3" ) );
82      }
83  
84      private RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
85      {
86          RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
87          repo.setId( id );
88          repo.setName( name );
89          repo.setLayout( "default" );
90          repo.setUrl( url );
91          return repo;
92      }
93  
94      @Test
95      public void testManagedRepositoryRetrieval()
96      {
97          ManagedRepositoryConfiguration repo1 = createManagedRepository( "id1", "name 1", "path 1", false );
98          configuration.addManagedRepository( repo1 );
99          ManagedRepositoryConfiguration repo2 = createManagedRepository( "id2", "name 2", "path 2", true );
100         configuration.addManagedRepository( repo2 );
101 
102         Map<String, ManagedRepositoryConfiguration> map = configuration.getManagedRepositoriesAsMap();
103         assertNotNull( map );
104         assertEquals( 2, map.size() );
105         assertEquals( new HashSet<String>( Arrays.asList( "id1", "id2" ) ), map.keySet() );
106         assertEquals( new HashSet<ManagedRepositoryConfiguration>( Arrays.asList( repo1, repo2 ) ),
107                       new HashSet<ManagedRepositoryConfiguration>( map.values() ) );
108 
109         assertEquals( repo1, configuration.findManagedRepositoryById( "id1" ) );
110         assertEquals( repo2, configuration.findManagedRepositoryById( "id2" ) );
111         assertNull( configuration.findManagedRepositoryById( "id3" ) );
112     }
113 
114     private ManagedRepositoryConfiguration createManagedRepository( String id, String name, String location,
115                                                                     boolean scanned )
116     {
117         ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
118         repo.setId( id );
119         repo.setName( name );
120         repo.setLocation( location );
121         repo.setScanned( scanned );
122         return repo;
123     }
124 
125     @Test
126     public void testNetworkProxyRetrievalWhenEmpty()
127     {
128         Map<String, NetworkProxyConfiguration> map = configuration.getNetworkProxiesAsMap();
129         assertNotNull( map );
130         assertTrue( map.isEmpty() );
131     }
132 
133     @Test
134     public void testRemoteRepositoryRetrievalWhenEmpty()
135     {
136         Map<String, RemoteRepositoryConfiguration> map = configuration.getRemoteRepositoriesAsMap();
137         assertNotNull( map );
138         assertTrue( map.isEmpty() );
139 
140         assertNull( configuration.findRemoteRepositoryById( "id" ) );
141     }
142 
143     @Test
144     public void testManagedRepositoryRetrievalWhenEmpty()
145     {
146         Map<String, ManagedRepositoryConfiguration> map = configuration.getManagedRepositoriesAsMap();
147         assertNotNull( map );
148         assertTrue( map.isEmpty() );
149 
150         assertNull( configuration.findManagedRepositoryById( "id" ) );
151     }
152 }