This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rhbz1110627 - check server is compatible to init command
- Loading branch information
Patrick Huang
committed
Aug 21, 2014
1 parent
61fe130
commit 55b3c80
Showing
7 changed files
with
202 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
zanata-client-commands/src/main/java/org/zanata/client/util/VersionComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright 2014, Red Hat, Inc. and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
|
||
package org.zanata.client.util; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.common.base.Splitter; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
/** | ||
* This class will compare version strings i.e. 3.3.1 to 3.3. It can also | ||
* compare maven snapshot version i.e. 3.3.1-SNAPSHOT is newer than 3.3.1. | ||
* It can NOT handle version like 1.1a or 1.1.Final or 1.1.Alpha. | ||
* | ||
* @author Patrick Huang <a | ||
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a> | ||
*/ | ||
public class VersionComparator implements Comparator<String> { | ||
|
||
private static final Splitter splitter = | ||
Splitter.on(".").omitEmptyStrings().trimResults(); | ||
public static final String SNAPSHOT_SUFFIX = "-SNAPSHOT"; | ||
|
||
@Override | ||
public int compare(String left, String right) { | ||
Preconditions.checkNotNull(left); | ||
Preconditions.checkNotNull(right); | ||
|
||
List<String> leftOrdinals = | ||
ImmutableList.copyOf(splitter.split(left.replace( | ||
SNAPSHOT_SUFFIX, ""))); | ||
List<String> rightOrdinals = | ||
ImmutableList.copyOf(splitter.split(right.replace( | ||
SNAPSHOT_SUFFIX, ""))); | ||
int compareIndex = | ||
firstDiffOrdinalOrLastOrdinalInShortestVer(leftOrdinals, | ||
rightOrdinals); | ||
// compare first different ordinal | ||
if (compareIndex < leftOrdinals.size() | ||
&& compareIndex < rightOrdinals.size()) { | ||
Integer leftOrdinal = | ||
getOrdinalAsInteger(leftOrdinals, compareIndex); | ||
Integer rightOrdinal = | ||
getOrdinalAsInteger(rightOrdinals, compareIndex); | ||
|
||
int ordinalDiff = leftOrdinal.compareTo(rightOrdinal); | ||
if (ordinalDiff == 0) { | ||
return compareSnapshot(left, right); | ||
} | ||
return ordinalDiff; | ||
} | ||
|
||
// either equal or one is a substring of another | ||
int sizeDiff = leftOrdinals.size() - rightOrdinals.size(); | ||
if (sizeDiff == 0) { | ||
return compareSnapshot(left, right); | ||
} | ||
return Integer.signum(sizeDiff); | ||
} | ||
|
||
private static int firstDiffOrdinalOrLastOrdinalInShortestVer( | ||
List<String> ver1Ordinals, | ||
List<String> ver2Ordinals) { | ||
int result = 0; | ||
while (result < ver1Ordinals.size() && result < ver2Ordinals.size() | ||
&& ver1Ordinals.get(result).equals(ver2Ordinals.get(result))) { | ||
result++; | ||
} | ||
return result; | ||
} | ||
|
||
private static Integer getOrdinalAsInteger(List<String> ordinals, | ||
int index) { | ||
return Integer.valueOf(ordinals.get(index)); | ||
} | ||
|
||
private static int compareSnapshot(String ver1, String ver2) { | ||
boolean ver1IsSnapshot = ver1.endsWith(SNAPSHOT_SUFFIX); | ||
boolean ver2IsSnapshot = ver2.endsWith(SNAPSHOT_SUFFIX); | ||
if (ver1IsSnapshot && !ver2IsSnapshot) { | ||
return -1; | ||
} else if (!ver1IsSnapshot && ver2IsSnapshot) { | ||
return 1; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
zanata-client-commands/src/test/java/org/zanata/client/util/VersionComparatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2014, Red Hat, Inc. and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
|
||
package org.zanata.client.util; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
import org.junit.Test; | ||
|
||
public class VersionComparatorTest { | ||
public static final int GREATER = 1; | ||
public static final int EQUAL = 0; | ||
public static final int LESS = -1; | ||
private VersionComparator comparator = new VersionComparator(); | ||
|
||
@Test | ||
public void canCompareVersions() { | ||
assertThat(comparator.compare("3.3.1", "3.3"), equalTo(GREATER)); | ||
assertThat(comparator.compare("3.3.1", "3.3.1"), equalTo(EQUAL)); | ||
assertThat(comparator.compare("3.3.1", "3.3.2"), equalTo(LESS)); | ||
assertThat(comparator.compare("3.3.1-SNAPSHOT", "3.3"), equalTo(GREATER)); | ||
assertThat(comparator.compare("3.3.1-SNAPSHOT", "3.3-SNAPSHOT"), equalTo(GREATER)); | ||
assertThat(comparator.compare("3.3.1-SNAPSHOT", "3.3.1"), equalTo(LESS)); | ||
assertThat(comparator.compare("3.3.1-SNAPSHOT", "3.3.2-SNAPSHOT"), equalTo(LESS)); | ||
assertThat(comparator.compare("3.3.1-SNAPSHOT", "3.3.1-SNAPSHOT"), equalTo(EQUAL)); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
zanata-client-commands/src/test/resources/serverresponse/version.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?xml version='1.0' encoding='UTF-8' standalone='yes'?> | ||
<ns2:versionInfo xmlns:ns2='http://zanata.org/namespace/api/'> | ||
<versionNo>3.3.1</versionNo> | ||
<versionNo>3.4.0</versionNo> | ||
<buildTimeStamp>unknown</buildTimeStamp> | ||
<ns2:scmDescribe>unknown</ns2:scmDescribe> | ||
</ns2:versionInfo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters