From 67384d1ea6969347b87a8ec7a4f8dcc972f543a7 Mon Sep 17 00:00:00 2001 From: Alex Rodriguez Date: Fri, 4 Jan 2013 08:52:19 +0100 Subject: [PATCH] OAK-539: Fixed compareTo in microkernel Id class. --- .../java/org/apache/jackrabbit/mk/model/Id.java | 4 +- .../org/apache/jackrabbit/mk/model/IdTest.java | 62 ++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 oak-mk/src/test/java/org/apache/jackrabbit/mk/model/IdTest.java diff --git a/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/Id.java b/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/Id.java index a263ddb..021b5ef 100644 --- a/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/Id.java +++ b/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/Id.java @@ -113,7 +113,9 @@ public int compareTo(Id o) { for (int i = 0; i < len; i++) { if (raw[i] != other[i]) { - return raw[i] - other[i]; + final int rawValue = raw[i] & 0xFF; // unsigned value + final int otherValue = other[i] & 0xFF; // unsigned value + return rawValue - otherValue; } } return raw.length - other.length; diff --git a/oak-mk/src/test/java/org/apache/jackrabbit/mk/model/IdTest.java b/oak-mk/src/test/java/org/apache/jackrabbit/mk/model/IdTest.java new file mode 100644 index 0000000..14ea7f6 --- /dev/null +++ b/oak-mk/src/test/java/org/apache/jackrabbit/mk/model/IdTest.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.jackrabbit.mk.model; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class IdTest +{ + + @Test + public void idCompareToEqualsTest() + { + final Id id1 = Id.fromString( "00000000000002cc" ); + final Id id2 = Id.fromString( "00000000000002cc" ); + + assertTrue( id1 + " should be equals to " + id2, id1.compareTo( id2 ) == 0 ); + } + + @Test + public void idCompareToGreaterThanOneDigitTest() + { + final Id id1 = Id.fromString( "0000000000000007" ); + final Id id2 = Id.fromString( "000000000000000c" ); + + assertTrue( id1 + " should be less than " + id2, id1.compareTo( id2 ) < 0 ); + } + + @Test + public void idCompareToGreaterThanOneByteTest() + { + final Id id1 = Id.fromString( "0000000000000070" ); + final Id id2 = Id.fromString( "00000000000000c0" ); + + assertTrue( id1 + " should be less than " + id2, id1.compareTo( id2 ) < 0 ); + } + + @Test + public void idCompareToGreaterThanTwoBytesTest() + { + final Id id1 = Id.fromString( "0000000000000270" ); + final Id id2 = Id.fromString( "00000000000002c0" ); + + assertTrue( id1 + " should be less than " + id2, id1.compareTo( id2 ) < 0 ); + } + +} -- 1.7.10