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 org.apache.archiva.configuration.ProxyConnectorConfiguration;
23 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
24 import org.apache.commons.lang.StringUtils;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
32 import static org.junit.Assert.assertEquals;
33 import static org.junit.Assert.fail;
36 * ProxyConnectorConfigurationOrderComparatorTest
40 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
41 public class ProxyConnectorConfigurationOrderComparatorTest
44 public void testSortOfAllZeros()
46 List<ProxyConnectorConfiguration> proxies = new ArrayList<>();
48 proxies.add( createConnector( "corporate", 0 ) );
49 proxies.add( createConnector( "snapshots", 0 ) );
50 proxies.add( createConnector( "3rdparty", 0 ) );
51 proxies.add( createConnector( "sandbox", 0 ) );
53 Collections.sort( proxies, ProxyConnectorConfigurationOrderComparator.getInstance() );
55 assertProxyOrder( new String[]{ "corporate", "snapshots", "3rdparty", "sandbox" }, proxies );
59 public void testSortNormal()
61 List<ProxyConnectorConfiguration> proxies = new ArrayList<>();
63 proxies.add( createConnector( "corporate", 3 ) );
64 proxies.add( createConnector( "snapshots", 1 ) );
65 proxies.add( createConnector( "3rdparty", 2 ) );
66 proxies.add( createConnector( "sandbox", 4 ) );
68 Collections.sort( proxies, new ProxyConnectorConfigurationOrderComparator() );
70 assertProxyOrder( new String[]{ "snapshots", "3rdparty", "corporate", "sandbox" }, proxies );
74 public void testSortPartial()
76 List<ProxyConnectorConfiguration> proxies = new ArrayList<>();
78 proxies.add( createConnector( "corporate", 3 ) );
79 proxies.add( createConnector( "snapshots", 0 ) );
80 proxies.add( createConnector( "3rdparty", 2 ) );
81 proxies.add( createConnector( "sandbox", 0 ) );
83 Collections.sort( proxies, new ProxyConnectorConfigurationOrderComparator() );
85 assertProxyOrder( new String[]{ "3rdparty", "corporate", "snapshots", "sandbox" }, proxies );
88 private void assertProxyOrder( String[] ids, List<ProxyConnectorConfiguration> proxies )
90 assertEquals( "Proxies.size() == ids.length", ids.length, proxies.size() );
92 int orderFailedAt = -1;
94 for ( int i = 0; i < ids.length; i++ )
96 if ( !StringUtils.equals( ids[i], proxies.get( i ).getProxyId() ) )
103 if ( orderFailedAt >= 0 )
105 StringBuilder msg = new StringBuilder();
107 msg.append( "Failed expected order of the proxies <" );
108 msg.append( StringUtils.join( ids, ", " ) );
109 msg.append( ">, actual <" );
111 boolean needsComma = false;
112 for ( ProxyConnectorConfiguration proxy : proxies )
118 msg.append( proxy.getProxyId() );
121 msg.append( "> failure at index <" ).append( orderFailedAt ).append( ">." );
123 fail( msg.toString() );
127 private ProxyConnectorConfiguration createConnector( String id, int order )
129 ProxyConnectorConfiguration proxy = new ProxyConnectorConfiguration();
130 proxy.setProxyId( id );
131 proxy.setOrder( order );
132 proxy.setSourceRepoId( id + "_m" );
133 proxy.setTargetRepoId( id + "_r" );