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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package org.apache.archiva.configuration.provider.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. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import org.apache.archiva.configuration.model.ProxyConnectorConfiguration;
  20. import org.apache.archiva.configuration.model.functors.ProxyConnectorConfigurationOrderComparator;
  21. import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
  22. import org.apache.commons.lang3.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. @SuppressWarnings( "deprecation" )
  36. @RunWith( ArchivaBlockJUnit4ClassRunner.class )
  37. public class ProxyConnectorConfigurationOrderComparatorTest
  38. {
  39. @Test
  40. public void testSortOfAllZeros()
  41. {
  42. List<ProxyConnectorConfiguration> proxies = new ArrayList<>();
  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. Collections.sort( proxies, ProxyConnectorConfigurationOrderComparator.getInstance() );
  48. assertProxyOrder( new String[]{ "corporate", "snapshots", "3rdparty", "sandbox" }, proxies );
  49. }
  50. @Test
  51. public void testSortNormal()
  52. {
  53. List<ProxyConnectorConfiguration> proxies = new ArrayList<>();
  54. proxies.add( createConnector( "corporate", 3 ) );
  55. proxies.add( createConnector( "snapshots", 1 ) );
  56. proxies.add( createConnector( "3rdparty", 2 ) );
  57. proxies.add( createConnector( "sandbox", 4 ) );
  58. Collections.sort( proxies, new ProxyConnectorConfigurationOrderComparator() );
  59. assertProxyOrder( new String[]{ "snapshots", "3rdparty", "corporate", "sandbox" }, proxies );
  60. }
  61. @Test
  62. public void testSortPartial()
  63. {
  64. List<ProxyConnectorConfiguration> proxies = new ArrayList<>();
  65. proxies.add( createConnector( "corporate", 3 ) );
  66. proxies.add( createConnector( "snapshots", 0 ) );
  67. proxies.add( createConnector( "3rdparty", 2 ) );
  68. proxies.add( createConnector( "sandbox", 0 ) );
  69. Collections.sort( proxies, new ProxyConnectorConfigurationOrderComparator() );
  70. assertProxyOrder( new String[]{ "3rdparty", "corporate", "snapshots", "sandbox" }, proxies );
  71. }
  72. private void assertProxyOrder( String[] ids, List<ProxyConnectorConfiguration> proxies )
  73. {
  74. assertEquals( "Proxies.size() == ids.length", ids.length, proxies.size() );
  75. int orderFailedAt = -1;
  76. for ( int i = 0; i < ids.length; i++ )
  77. {
  78. if ( !StringUtils.equals( ids[i], proxies.get( i ).getProxyId() ) )
  79. {
  80. orderFailedAt = i;
  81. break;
  82. }
  83. }
  84. if ( orderFailedAt >= 0 )
  85. {
  86. StringBuilder msg = new StringBuilder();
  87. msg.append( "Failed expected order of the proxies <" );
  88. msg.append( StringUtils.join( ids, ", " ) );
  89. msg.append( ">, actual <" );
  90. boolean needsComma = false;
  91. for ( ProxyConnectorConfiguration proxy : proxies )
  92. {
  93. if ( needsComma )
  94. {
  95. msg.append( ", " );
  96. }
  97. msg.append( proxy.getProxyId() );
  98. needsComma = true;
  99. }
  100. msg.append( "> failure at index <" ).append( orderFailedAt ).append( ">." );
  101. fail( msg.toString() );
  102. }
  103. }
  104. private ProxyConnectorConfiguration createConnector( String id, int order )
  105. {
  106. ProxyConnectorConfiguration proxy = new ProxyConnectorConfiguration();
  107. proxy.setProxyId( id );
  108. proxy.setOrder( order );
  109. proxy.setSourceRepoId( id + "_m" );
  110. proxy.setTargetRepoId( id + "_r" );
  111. return proxy;
  112. }
  113. }