You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProxyConnectorConfigurationOrderComparatorTest.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package org.apache.archiva.configuration.functors;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.configuration.ProxyConnectorConfiguration;
  21. import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
  22. import org.apache.commons.lang.StringUtils;
  23. import org.junit.Test;
  24. import org.junit.runner.RunWith;
  25. import java.util.ArrayList;
  26. import java.util.Collections;
  27. import java.util.List;
  28. import static org.junit.Assert.assertEquals;
  29. import static org.junit.Assert.fail;
  30. /**
  31. * ProxyConnectorConfigurationOrderComparatorTest
  32. *
  33. *
  34. */
  35. @RunWith( ArchivaBlockJUnit4ClassRunner.class )
  36. public class ProxyConnectorConfigurationOrderComparatorTest
  37. {
  38. @Test
  39. public void testSortOfAllZeros()
  40. {
  41. List<ProxyConnectorConfiguration> proxies = new ArrayList<>();
  42. proxies.add( createConnector( "corporate", 0 ) );
  43. proxies.add( createConnector( "snapshots", 0 ) );
  44. proxies.add( createConnector( "3rdparty", 0 ) );
  45. proxies.add( createConnector( "sandbox", 0 ) );
  46. Collections.sort( proxies, ProxyConnectorConfigurationOrderComparator.getInstance() );
  47. assertProxyOrder( new String[]{ "corporate", "snapshots", "3rdparty", "sandbox" }, proxies );
  48. }
  49. @Test
  50. public void testSortNormal()
  51. {
  52. List<ProxyConnectorConfiguration> proxies = new ArrayList<>();
  53. proxies.add( createConnector( "corporate", 3 ) );
  54. proxies.add( createConnector( "snapshots", 1 ) );
  55. proxies.add( createConnector( "3rdparty", 2 ) );
  56. proxies.add( createConnector( "sandbox", 4 ) );
  57. Collections.sort( proxies, new ProxyConnectorConfigurationOrderComparator() );
  58. assertProxyOrder( new String[]{ "snapshots", "3rdparty", "corporate", "sandbox" }, proxies );
  59. }
  60. @Test
  61. public void testSortPartial()
  62. {
  63. List<ProxyConnectorConfiguration> proxies = new ArrayList<>();
  64. proxies.add( createConnector( "corporate", 3 ) );
  65. proxies.add( createConnector( "snapshots", 0 ) );
  66. proxies.add( createConnector( "3rdparty", 2 ) );
  67. proxies.add( createConnector( "sandbox", 0 ) );
  68. Collections.sort( proxies, new ProxyConnectorConfigurationOrderComparator() );
  69. assertProxyOrder( new String[]{ "3rdparty", "corporate", "snapshots", "sandbox" }, proxies );
  70. }
  71. private void assertProxyOrder( String[] ids, List<ProxyConnectorConfiguration> proxies )
  72. {
  73. assertEquals( "Proxies.size() == ids.length", ids.length, proxies.size() );
  74. int orderFailedAt = -1;
  75. for ( int i = 0; i < ids.length; i++ )
  76. {
  77. if ( !StringUtils.equals( ids[i], proxies.get( i ).getProxyId() ) )
  78. {
  79. orderFailedAt = i;
  80. break;
  81. }
  82. }
  83. if ( orderFailedAt >= 0 )
  84. {
  85. StringBuilder msg = new StringBuilder();
  86. msg.append( "Failed expected order of the proxies <" );
  87. msg.append( StringUtils.join( ids, ", " ) );
  88. msg.append( ">, actual <" );
  89. boolean needsComma = false;
  90. for ( ProxyConnectorConfiguration proxy : proxies )
  91. {
  92. if ( needsComma )
  93. {
  94. msg.append( ", " );
  95. }
  96. msg.append( proxy.getProxyId() );
  97. needsComma = true;
  98. }
  99. msg.append( "> failure at index <" ).append( orderFailedAt ).append( ">." );
  100. fail( msg.toString() );
  101. }
  102. }
  103. private ProxyConnectorConfiguration createConnector( String id, int order )
  104. {
  105. ProxyConnectorConfiguration proxy = new ProxyConnectorConfiguration();
  106. proxy.setProxyId( id );
  107. proxy.setOrder( order );
  108. proxy.setSourceRepoId( id + "_m" );
  109. proxy.setTargetRepoId( id + "_r" );
  110. return proxy;
  111. }
  112. }