Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

VersionComparatorTest.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package org.apache.archiva.common.utils;
  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.Arrays;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import junit.framework.TestCase;
  25. /**
  26. * VersionComparatorTest
  27. *
  28. *
  29. */
  30. public class VersionComparatorTest
  31. extends TestCase
  32. {
  33. public void testComparator()
  34. {
  35. /* Sort order is oldest to newest */
  36. assertSort( new String[] { "1.0", "3.0", "2.0" }, new String[] { "1.0", "2.0", "3.0" } );
  37. assertSort( new String[] { "1.5", "1.2", "1.0" }, new String[] { "1.0", "1.2", "1.5" } );
  38. assertSort( new String[] { "1.5-SNAPSHOT", "1.2", "1.20" }, new String[] { "1.2", "1.5-SNAPSHOT", "1.20" } );
  39. assertSort( new String[] { "1.1", "1.0-SNAPSHOT", "1.1-m6", "1.1-rc1" }, new String[] {
  40. "1.0-SNAPSHOT",
  41. "1.1-rc1",
  42. "1.1-m6",
  43. "1.1" } );
  44. assertSort( new String[] { "1.1-m6", "1.0-SNAPSHOT", "1.1-rc1", "1.1" }, new String[] {
  45. "1.0-SNAPSHOT",
  46. "1.1-rc1",
  47. "1.1-m6",
  48. "1.1" } );
  49. assertSort( new String[] { "2.0.5", "2.0.4-SNAPSHOT", "2.0", "2.0-rc1" }, new String[] {
  50. "2.0-rc1",
  51. "2.0",
  52. "2.0.4-SNAPSHOT",
  53. "2.0.5" } );
  54. assertSort( new String[] { "1.0-alpha-1", "1.0-alpha-22", "1.0-alpha-10", "1.0-alpha-9" }, new String[] {
  55. "1.0-alpha-1",
  56. "1.0-alpha-9",
  57. "1.0-alpha-10",
  58. "1.0-alpha-22" } );
  59. assertSort( new String[] { "1.0-alpha1", "1.0-alpha22", "1.0-alpha10", "1.0-alpha9" }, new String[] {
  60. "1.0-alpha1",
  61. "1.0-alpha9",
  62. "1.0-alpha10",
  63. "1.0-alpha22" } );
  64. assertSort( new String[] { "1.0-1", "1.0-22", "1.0-10", "1.0-9" }, new String[] {
  65. "1.0-1",
  66. "1.0-9",
  67. "1.0-10",
  68. "1.0-22" } );
  69. assertSort( new String[] { "alpha-1", "alpha-22", "alpha-10", "alpha-9" }, new String[] {
  70. "alpha-1",
  71. "alpha-9",
  72. "alpha-10",
  73. "alpha-22" } );
  74. assertSort( new String[] { "1.0.1", "1.0.22", "1.0.10", "1.0.9" }, new String[] {
  75. "1.0.1",
  76. "1.0.9",
  77. "1.0.10",
  78. "1.0.22" } );
  79. // TODO: write more unit tests.
  80. }
  81. private void assertSort( String[] rawVersions, String[] expectedSort )
  82. {
  83. List<String> versions = new ArrayList<String>();
  84. versions.addAll( Arrays.asList( rawVersions ) );
  85. Collections.sort( versions, VersionComparator.getInstance() );
  86. assertEquals( "Versions.size()", expectedSort.length, versions.size() );
  87. for ( int i = 0; i < expectedSort.length; i++ )
  88. {
  89. assertEquals( "Sorted Versions[" + i + "]", expectedSort[i], (String) versions.get( i ) );
  90. }
  91. }
  92. public void testToParts()
  93. {
  94. assertParts( "1.0", new String[] { "1", "0" } );
  95. assertParts( "1.0-alpha-1", new String[] { "1", "0", "alpha", "1" } );
  96. assertParts( "2.0-rc2", new String[] { "2", "0", "rc", "2" } );
  97. assertParts( "1.3-m6", new String[] { "1", "3", "m", "6" } );
  98. }
  99. private void assertParts( String version, String[] expectedParts )
  100. {
  101. String actualParts[] = VersionComparator.toParts( version );
  102. assertEquals( "Parts.length", expectedParts.length, actualParts.length );
  103. for ( int i = 0; i < expectedParts.length; i++ )
  104. {
  105. assertEquals( "parts[" + i + "]", expectedParts[i], actualParts[i] );
  106. }
  107. }
  108. }