Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changes make MessageId Comparable (#1065)
- Loading branch information
Showing
4 changed files
with
149 additions
and
14 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
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
123 changes: 123 additions & 0 deletions
123
pulsar-client/src/test/java/org/apache/pulsar/client/impl/MessageIdCompareToTest.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,123 @@ | |||
/** | |||
* 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.pulsar.client.impl; | |||
|
|||
import static org.testng.Assert.assertTrue; | |||
import static org.testng.Assert.fail; | |||
|
|||
import org.testng.annotations.Test; | |||
|
|||
/** | |||
* Test compareTo method in MessageIdImpl and BatchMessageIdImpl | |||
*/ | |||
public class MessageIdCompareToTest { | |||
|
|||
@Test | |||
public void testEqual() { | |||
MessageIdImpl messageIdImpl1 = new MessageIdImpl(123L, 345L, 567); | |||
MessageIdImpl messageIdImpl2 = new MessageIdImpl(123L, 345L, 567); | |||
|
|||
BatchMessageIdImpl batchMessageId1 = new BatchMessageIdImpl(234L, 345L, 456, 567); | |||
BatchMessageIdImpl batchMessageId2 = new BatchMessageIdImpl(234L, 345L, 456, 567); | |||
|
|||
assertTrue(messageIdImpl1.compareTo(messageIdImpl2) == 0, "Expected to be equal"); | |||
assertTrue(batchMessageId1.compareTo(batchMessageId2) == 0, "Expected to be equal"); | |||
} | |||
|
|||
@Test | |||
public void testGreaterThan() { | |||
MessageIdImpl messageIdImpl1 = new MessageIdImpl(124L, 345L, 567); | |||
MessageIdImpl messageIdImpl2 = new MessageIdImpl(123L, 345L, 567); | |||
MessageIdImpl messageIdImpl3 = new MessageIdImpl(123L, 344L, 567); | |||
MessageIdImpl messageIdImpl4 = new MessageIdImpl(123L, 344L, 566); | |||
|
|||
BatchMessageIdImpl batchMessageId1 = new BatchMessageIdImpl(235L, 345L, 456, 567); | |||
BatchMessageIdImpl batchMessageId2 = new BatchMessageIdImpl(234L, 346L, 456, 567); | |||
BatchMessageIdImpl batchMessageId3 = new BatchMessageIdImpl(234L, 345L, 456, 568); | |||
BatchMessageIdImpl batchMessageId4 = new BatchMessageIdImpl(234L, 345L, 457, 567); | |||
BatchMessageIdImpl batchMessageId5 = new BatchMessageIdImpl(234L, 345L, 456, 567); | |||
|
|||
assertTrue(messageIdImpl1.compareTo(messageIdImpl2) > 0, "Expected to be greater than"); | |||
assertTrue(messageIdImpl1.compareTo(messageIdImpl3) > 0, "Expected to be greater than"); | |||
assertTrue(messageIdImpl1.compareTo(messageIdImpl4) > 0, "Expected to be greater than"); | |||
assertTrue(messageIdImpl2.compareTo(messageIdImpl3) > 0, "Expected to be greater than"); | |||
assertTrue(messageIdImpl2.compareTo(messageIdImpl4) > 0, "Expected to be greater than"); | |||
assertTrue(messageIdImpl3.compareTo(messageIdImpl4) > 0, "Expected to be greater than"); | |||
|
|||
assertTrue(batchMessageId1.compareTo(batchMessageId2) > 0, "Expected to be greater than"); | |||
assertTrue(batchMessageId1.compareTo(batchMessageId3) > 0, "Expected to be greater than"); | |||
assertTrue(batchMessageId1.compareTo(batchMessageId4) > 0, "Expected to be greater than"); | |||
assertTrue(batchMessageId1.compareTo(batchMessageId5) > 0, "Expected to be greater than"); | |||
assertTrue(batchMessageId2.compareTo(batchMessageId3) > 0, "Expected to be greater than"); | |||
assertTrue(batchMessageId2.compareTo(batchMessageId4) > 0, "Expected to be greater than"); | |||
assertTrue(batchMessageId2.compareTo(batchMessageId5) > 0, "Expected to be greater than"); | |||
assertTrue(batchMessageId3.compareTo(batchMessageId4) > 0, "Expected to be greater than"); | |||
assertTrue(batchMessageId3.compareTo(batchMessageId5) > 0, "Expected to be greater than"); | |||
assertTrue(batchMessageId4.compareTo(batchMessageId5) > 0, "Expected to be greater than"); | |||
} | |||
|
|||
@Test | |||
public void testLessThan() { | |||
MessageIdImpl messageIdImpl1 = new MessageIdImpl(124L, 345L, 567); | |||
MessageIdImpl messageIdImpl2 = new MessageIdImpl(123L, 345L, 567); | |||
MessageIdImpl messageIdImpl3 = new MessageIdImpl(123L, 344L, 567); | |||
MessageIdImpl messageIdImpl4 = new MessageIdImpl(123L, 344L, 566); | |||
|
|||
BatchMessageIdImpl batchMessageId1 = new BatchMessageIdImpl(235L, 345L, 456, 567); | |||
BatchMessageIdImpl batchMessageId2 = new BatchMessageIdImpl(234L, 346L, 456, 567); | |||
BatchMessageIdImpl batchMessageId3 = new BatchMessageIdImpl(234L, 345L, 456, 568); | |||
BatchMessageIdImpl batchMessageId4 = new BatchMessageIdImpl(234L, 345L, 457, 567); | |||
BatchMessageIdImpl batchMessageId5 = new BatchMessageIdImpl(234L, 345L, 456, 567); | |||
|
|||
assertTrue(messageIdImpl2.compareTo(messageIdImpl1) < 0, "Expected to be less than"); | |||
assertTrue(messageIdImpl3.compareTo(messageIdImpl1) < 0, "Expected to be less than"); | |||
assertTrue(messageIdImpl4.compareTo(messageIdImpl1) < 0, "Expected to be less than"); | |||
assertTrue(messageIdImpl3.compareTo(messageIdImpl2) < 0, "Expected to be less than"); | |||
assertTrue(messageIdImpl4.compareTo(messageIdImpl2) < 0, "Expected to be less than"); | |||
assertTrue(messageIdImpl4.compareTo(messageIdImpl3) < 0, "Expected to be less than"); | |||
|
|||
assertTrue(batchMessageId2.compareTo(batchMessageId1) < 0, "Expected to be less than"); | |||
assertTrue(batchMessageId3.compareTo(batchMessageId1) < 0, "Expected to be less than"); | |||
assertTrue(batchMessageId4.compareTo(batchMessageId1) < 0, "Expected to be less than"); | |||
assertTrue(batchMessageId5.compareTo(batchMessageId1) < 0, "Expected to be less than"); | |||
assertTrue(batchMessageId3.compareTo(batchMessageId2) < 0, "Expected to be less than"); | |||
assertTrue(batchMessageId4.compareTo(batchMessageId2) < 0, "Expected to be less than"); | |||
assertTrue(batchMessageId5.compareTo(batchMessageId2) < 0, "Expected to be less than"); | |||
assertTrue(batchMessageId4.compareTo(batchMessageId3) < 0, "Expected to be less than"); | |||
assertTrue(batchMessageId5.compareTo(batchMessageId3) < 0, "Expected to be less than"); | |||
assertTrue(batchMessageId5.compareTo(batchMessageId4) < 0, "Expected to be less than"); | |||
} | |||
|
|||
@Test | |||
public void testCompareDifferentType() { | |||
// Expected throw IllegalArgumentException | |||
MessageIdImpl messageIdImpl = new MessageIdImpl(123L, 345L, 567); | |||
BatchMessageIdImpl batchMessageId = new BatchMessageIdImpl(123L, 345L, 567, 789); | |||
|
|||
assertTrue( messageIdImpl.compareTo(batchMessageId) == 0, "Expected to be equal"); | |||
|
|||
try { | |||
batchMessageId.compareTo(messageIdImpl); | |||
fail("Should throw IllegalArgumentException when compare different type"); | |||
} catch (IllegalArgumentException e) { | |||
// expected | |||
} | |||
} | |||
|
|||
} |