1 package org.apache.archiva.configuration.functors;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import junit.framework.TestCase;
23 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
24 import org.apache.commons.lang.StringUtils;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
34 * ProxyConnectorConfigurationOrderComparatorTest
38 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
39 public class ProxyConnectorConfigurationOrderComparatorTest
43 public void testSortOfAllZeros()
45 List<ProxyConnectorConfiguration> proxies = new ArrayList<ProxyConnectorConfiguration>();
47 proxies.add( createConnector( "corporate", 0 ) );
48 proxies.add( createConnector( "snapshots", 0 ) );
49 proxies.add( createConnector( "3rdparty", 0 ) );
50 proxies.add( createConnector( "sandbox", 0 ) );
52 Collections.sort( proxies, ProxyConnectorConfigurationOrderComparator.getInstance() );
54 assertProxyOrder( new String[]{ "corporate", "snapshots", "3rdparty", "sandbox" }, proxies );
58 public void testSortNormal()
60 List<ProxyConnectorConfiguration> proxies = new ArrayList<ProxyConnectorConfiguration>();
62 proxies.add( createConnector( "corporate", 3 ) );
63 proxies.add( createConnector( "snapshots", 1 ) );
64 proxies.add( createConnector( "3rdparty", 2 ) );
65 proxies.add( createConnector( "sandbox", 4 ) );
67 Collections.sort( proxies, new ProxyConnectorConfigurationOrderComparator() );
69 assertProxyOrder( new String[]{ "snapshots", "3rdparty", "corporate", "sandbox" }, proxies );
73 public void testSortPartial()
75 List<ProxyConnectorConfiguration> proxies = new ArrayList<ProxyConnectorConfiguration>();
77 proxies.add( createConnector( "corporate", 3 ) );
78 proxies.add( createConnector( "snapshots", 0 ) );
79 proxies.add( createConnector( "3rdparty", 2 ) );
80 proxies.add( createConnector( "sandbox", 0 ) );
82 Collections.sort( proxies, new ProxyConnectorConfigurationOrderComparator() );
84 assertProxyOrder( new String[]{ "3rdparty", "corporate", "snapshots", "sandbox" }, proxies );
87 private void assertProxyOrder( String[] ids, List<ProxyConnectorConfiguration> proxies )
89 assertEquals( "Proxies.size() == ids.length", ids.length, proxies.size() );
91 int orderFailedAt = -1;
93 for ( int i = 0; i < ids.length; i++ )
95 if ( !StringUtils.equals( ids[i], proxies.get( i ).getProxyId() ) )
102 if ( orderFailedAt >= 0 )
104 StringBuffer msg = new StringBuffer();
106 msg.append( "Failed expected order of the proxies <" );
107 msg.append( StringUtils.join( ids, ", " ) );
108 msg.append( ">, actual <" );
110 boolean needsComma = false;
111 for ( ProxyConnectorConfiguration proxy : proxies )
117 msg.append( proxy.getProxyId() );
120 msg.append( "> failure at index <" ).append( orderFailedAt ).append( ">." );
122 fail( msg.toString() );
126 private ProxyConnectorConfiguration createConnector( String id, int order )
128 ProxyConnectorConfiguration proxy = new ProxyConnectorConfiguration();
129 proxy.setProxyId( id );
130 proxy.setOrder( order );
131 proxy.setSourceRepoId( id + "_m" );
132 proxy.setTargetRepoId( id + "_r" );