View Javadoc
1   package org.apache.archiva.configuration.functors;
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.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  import org.apache.archiva.configuration.ProxyConnectorConfiguration;
26  import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
27  import org.apache.commons.lang.StringUtils;
28  import static org.junit.Assert.*;
29  import org.junit.Test;
30  import org.junit.runner.RunWith;
31  
32  /**
33   * ProxyConnectorConfigurationOrderComparatorTest
34   *
35   *
36   */
37  @RunWith( ArchivaBlockJUnit4ClassRunner.class )
38  public class ProxyConnectorConfigurationOrderComparatorTest
39  {
40      @Test
41      public void testSortOfAllZeros()
42      {
43          List<ProxyConnectorConfiguration> proxies = new ArrayList<>();
44  
45          proxies.add( createConnector( "corporate", 0 ) );
46          proxies.add( createConnector( "snapshots", 0 ) );
47          proxies.add( createConnector( "3rdparty", 0 ) );
48          proxies.add( createConnector( "sandbox", 0 ) );
49  
50          Collections.sort( proxies, ProxyConnectorConfigurationOrderComparator.getInstance() );
51  
52          assertProxyOrder( new String[]{ "corporate", "snapshots", "3rdparty", "sandbox" }, proxies );
53      }
54  
55      @Test
56      public void testSortNormal()
57      {
58          List<ProxyConnectorConfiguration> proxies = new ArrayList<>();
59  
60          proxies.add( createConnector( "corporate", 3 ) );
61          proxies.add( createConnector( "snapshots", 1 ) );
62          proxies.add( createConnector( "3rdparty", 2 ) );
63          proxies.add( createConnector( "sandbox", 4 ) );
64  
65          Collections.sort( proxies, new ProxyConnectorConfigurationOrderComparator() );
66  
67          assertProxyOrder( new String[]{ "snapshots", "3rdparty", "corporate", "sandbox" }, proxies );
68      }
69  
70      @Test
71      public void testSortPartial()
72      {
73          List<ProxyConnectorConfiguration> proxies = new ArrayList<>();
74  
75          proxies.add( createConnector( "corporate", 3 ) );
76          proxies.add( createConnector( "snapshots", 0 ) );
77          proxies.add( createConnector( "3rdparty", 2 ) );
78          proxies.add( createConnector( "sandbox", 0 ) );
79  
80          Collections.sort( proxies, new ProxyConnectorConfigurationOrderComparator() );
81  
82          assertProxyOrder( new String[]{ "3rdparty", "corporate", "snapshots", "sandbox" }, proxies );
83      }
84  
85      private void assertProxyOrder( String[] ids, List<ProxyConnectorConfiguration> proxies )
86      {
87          assertEquals( "Proxies.size() == ids.length", ids.length, proxies.size() );
88  
89          int orderFailedAt = -1;
90  
91          for ( int i = 0; i < ids.length; i++ )
92          {
93              if ( !StringUtils.equals( ids[i], proxies.get( i ).getProxyId() ) )
94              {
95                  orderFailedAt = i;
96                  break;
97              }
98          }
99  
100         if ( orderFailedAt >= 0 )
101         {
102             StringBuilder msg = new StringBuilder();
103 
104             msg.append( "Failed expected order of the proxies <" );
105             msg.append( StringUtils.join( ids, ", " ) );
106             msg.append( ">, actual <" );
107 
108             boolean needsComma = false;
109             for ( ProxyConnectorConfiguration proxy : proxies )
110             {
111                 if ( needsComma )
112                 {
113                     msg.append( ", " );
114                 }
115                 msg.append( proxy.getProxyId() );
116                 needsComma = true;
117             }
118             msg.append( "> failure at index <" ).append( orderFailedAt ).append( ">." );
119 
120             fail( msg.toString() );
121         }
122     }
123 
124     private ProxyConnectorConfiguration createConnector( String id, int order )
125     {
126         ProxyConnectorConfiguration proxy = new ProxyConnectorConfiguration();
127         proxy.setProxyId( id );
128         proxy.setOrder( order );
129         proxy.setSourceRepoId( id + "_m" );
130         proxy.setTargetRepoId( id + "_r" );
131 
132         return proxy;
133     }
134 }