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;
31 * ProxyConnectorConfigurationOrderComparatorTest
35 public class ProxyConnectorConfigurationOrderComparatorTest
38 public void testSortOfAllZeros()
40 List<ProxyConnectorConfiguration> proxies = new ArrayList<ProxyConnectorConfiguration>();
42 proxies.add( createConnector( "corporate", 0 ) );
43 proxies.add( createConnector( "snapshots", 0 ) );
44 proxies.add( createConnector( "3rdparty", 0 ) );
45 proxies.add( createConnector( "sandbox", 0 ) );
47 Collections.sort( proxies, ProxyConnectorConfigurationOrderComparator.getInstance() );
49 assertProxyOrder( new String[]{ "corporate", "snapshots", "3rdparty", "sandbox" }, proxies );
52 public void testSortNormal()
54 List<ProxyConnectorConfiguration> proxies = new ArrayList<ProxyConnectorConfiguration>();
56 proxies.add( createConnector( "corporate", 3 ) );
57 proxies.add( createConnector( "snapshots", 1 ) );
58 proxies.add( createConnector( "3rdparty", 2 ) );
59 proxies.add( createConnector( "sandbox", 4 ) );
61 Collections.sort( proxies, new ProxyConnectorConfigurationOrderComparator() );
63 assertProxyOrder( new String[]{ "snapshots", "3rdparty", "corporate", "sandbox" }, proxies );
66 public void testSortPartial()
68 List<ProxyConnectorConfiguration> proxies = new ArrayList<ProxyConnectorConfiguration>();
70 proxies.add( createConnector( "corporate", 3 ) );
71 proxies.add( createConnector( "snapshots", 0 ) );
72 proxies.add( createConnector( "3rdparty", 2 ) );
73 proxies.add( createConnector( "sandbox", 0 ) );
75 Collections.sort( proxies, new ProxyConnectorConfigurationOrderComparator() );
77 assertProxyOrder( new String[]{ "3rdparty", "corporate", "snapshots", "sandbox" }, proxies );
80 private void assertProxyOrder( String[] ids, List<ProxyConnectorConfiguration> proxies )
82 assertEquals( "Proxies.size() == ids.length", ids.length, proxies.size() );
84 int orderFailedAt = -1;
86 for ( int i = 0; i < ids.length; i++ )
88 if ( !StringUtils.equals( ids[i], proxies.get( i ).getProxyId() ) )
95 if ( orderFailedAt >= 0 )
97 StringBuffer msg = new StringBuffer();
99 msg.append( "Failed expected order of the proxies <" );
100 msg.append( StringUtils.join( ids, ", " ) );
101 msg.append( ">, actual <" );
103 boolean needsComma = false;
104 for ( ProxyConnectorConfiguration proxy : proxies )
110 msg.append( proxy.getProxyId() );
113 msg.append( "> failure at index <" ).append( orderFailedAt ).append( ">." );
115 fail( msg.toString() );
119 private ProxyConnectorConfiguration createConnector( String id, int order )
121 ProxyConnectorConfiguration proxy = new ProxyConnectorConfiguration();
122 proxy.setProxyId( id );
123 proxy.setOrder( order );
124 proxy.setSourceRepoId( id + "_m" );
125 proxy.setTargetRepoId( id + "_r" );