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