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

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