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