Skip to content

Commit a66efa1

Browse files
committed
Added unit tests for internal.util package
Fixed a message of Checks
1 parent c176564 commit a66efa1

File tree

13 files changed

+1100
-26
lines changed

13 files changed

+1100
-26
lines changed

src/main/java/com/hivemq/client/internal/util/Checks.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public static void indexRange(final int start, final int end, final int size) {
154154
throw new IndexOutOfBoundsException("Start index must not be smaller than 0, but was " + start + ".");
155155
} else if (start > end) {
156156
throw new IndexOutOfBoundsException(
157-
"Start index must be greater than the end index, but " + start + " > " + end + ".");
157+
"Start index must not be greater than the end index, but " + start + " > " + end + ".");
158158
} else {
159159
throw new IndexOutOfBoundsException(
160160
"End index must not be greater than or equal to the size (" + size + "), but was " + end + ".");
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright 2018 dc-square and the HiveMQ MQTT Client Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.hivemq.client.internal.util;
19+
20+
import org.jetbrains.annotations.NotNull;
21+
import org.jetbrains.annotations.Nullable;
22+
import org.junit.jupiter.api.Test;
23+
24+
import static org.junit.jupiter.api.Assertions.*;
25+
26+
/**
27+
* @author Silvio Giebl
28+
*/
29+
class AsyncRuntimeExceptionTest {
30+
31+
@Test
32+
void constructor_message() {
33+
final TestAsyncRuntimeException exception = new TestAsyncRuntimeException("message");
34+
assertEquals("message", exception.getMessage());
35+
assertNull(exception.getCause());
36+
assertEquals(0, exception.getStackTrace().length);
37+
}
38+
39+
@Test
40+
void constructor_cause() {
41+
final TestAsyncRuntimeException exception = new TestAsyncRuntimeException(new RuntimeException("cause"));
42+
assertEquals("java.lang.RuntimeException: cause", exception.getMessage());
43+
assertNotNull(exception.getCause());
44+
assertEquals("cause", exception.getCause().getMessage());
45+
assertEquals(0, exception.getStackTrace().length);
46+
}
47+
48+
@Test
49+
void constructor_messageAndCause() {
50+
final TestAsyncRuntimeException exception =
51+
new TestAsyncRuntimeException("message", new RuntimeException("cause"));
52+
assertEquals("message", exception.getMessage());
53+
assertNotNull(exception.getCause());
54+
assertEquals("cause", exception.getCause().getMessage());
55+
assertEquals(0, exception.getStackTrace().length);
56+
}
57+
58+
@Test
59+
void noStackTrace() {
60+
final TestAsyncRuntimeException exception = new TestAsyncRuntimeException("message");
61+
assertEquals(0, exception.getStackTrace().length);
62+
final TestAsyncRuntimeException thrownException =
63+
assertThrows(TestAsyncRuntimeException.class, () -> { throw exception; });
64+
assertEquals(0, thrownException.getStackTrace().length);
65+
}
66+
67+
@Test
68+
void fillInStackTrace_newStackTrace() {
69+
final TestAsyncRuntimeException exception = new TestAsyncRuntimeException("message");
70+
assertEquals(0, exception.getStackTrace().length);
71+
final RuntimeException filledException = AsyncRuntimeException.fillInStackTrace(exception);
72+
assertTrue(filledException instanceof TestAsyncRuntimeException);
73+
assertTrue(filledException.getStackTrace().length > 0);
74+
assertEquals("fillInStackTrace_newStackTrace", filledException.getStackTrace()[0].getMethodName());
75+
}
76+
77+
@Test
78+
void fillInStackTrace_otherException() {
79+
final RuntimeException exception = new RuntimeException("message");
80+
final StackTraceElement[] stackTrace = exception.getStackTrace();
81+
assertAll(() -> {
82+
final RuntimeException filledException = AsyncRuntimeException.fillInStackTrace(exception);
83+
assertSame(exception, filledException);
84+
assertEquals(stackTrace.length, filledException.getStackTrace().length);
85+
assertEquals(stackTrace[0].getMethodName(), filledException.getStackTrace()[0].getMethodName());
86+
});
87+
}
88+
89+
private static class TestAsyncRuntimeException extends AsyncRuntimeException {
90+
91+
TestAsyncRuntimeException(final @Nullable String message) {
92+
super(message);
93+
}
94+
95+
TestAsyncRuntimeException(final @Nullable String message, final @Nullable Throwable cause) {
96+
super(message, cause);
97+
}
98+
99+
TestAsyncRuntimeException(final @Nullable Throwable cause) {
100+
super(cause);
101+
}
102+
103+
TestAsyncRuntimeException(final @NotNull AsyncRuntimeException e) {
104+
super(e);
105+
}
106+
107+
@Override
108+
protected @NotNull AsyncRuntimeException copy() {
109+
return new TestAsyncRuntimeException(this);
110+
}
111+
}
112+
}

src/test/java/com/hivemq/client/internal/util/ByteArrayTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,25 @@
1919

2020
import nl.jqno.equalsverifier.EqualsVerifier;
2121
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.params.ParameterizedTest;
23+
import org.junit.jupiter.params.provider.ValueSource;
24+
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
2226

2327
/**
2428
* @author Silvio Giebl
2529
*/
2630
class ByteArrayTest {
2731

32+
@ParameterizedTest
33+
@ValueSource(ints = {0, 1, 10})
34+
void length(final int length) {
35+
final ByteArray empty = new ByteArray(new byte[length]);
36+
assertEquals(length, empty.length());
37+
assertEquals(0, empty.getStart());
38+
assertEquals(length, empty.getEnd());
39+
}
40+
2841
@Test
2942
void equals() {
3043
EqualsVerifier.forClass(ByteArray.class).verify();
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright 2018 dc-square and the HiveMQ MQTT Client Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.hivemq.client.internal.util;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.util.Arrays;
23+
import java.util.Random;
24+
25+
import static org.junit.jupiter.api.Assertions.*;
26+
27+
/**
28+
* @author Silvio Giebl
29+
*/
30+
class ByteArrayUtilTest {
31+
32+
@Test
33+
void equals() {
34+
final byte[] bytes = new byte[100];
35+
final Random random = new Random();
36+
random.nextBytes(bytes);
37+
final byte[] clone = bytes.clone();
38+
assertTrue(ByteArrayUtil.equals(bytes, 0, bytes.length, bytes, 0, bytes.length));
39+
assertTrue(ByteArrayUtil.equals(bytes, 0, bytes.length, clone, 0, clone.length));
40+
assertTrue(ByteArrayUtil.equals(bytes, 10, bytes.length, clone, 10, clone.length));
41+
assertTrue(ByteArrayUtil.equals(bytes, 0, 20, clone, 0, 20));
42+
assertTrue(ByteArrayUtil.equals(bytes, 10, 20, clone, 10, 20));
43+
}
44+
45+
@Test
46+
void equals_differentOffset() {
47+
final byte[] bytes = new byte[100];
48+
final Random random = new Random();
49+
random.nextBytes(bytes);
50+
final byte[] clone = Arrays.copyOfRange(bytes, 50, 100);
51+
assertTrue(ByteArrayUtil.equals(bytes, 50, 100, clone, 0, 50));
52+
assertTrue(ByteArrayUtil.equals(bytes, 60, 90, clone, 10, 40));
53+
assertFalse(ByteArrayUtil.equals(bytes, 0, 50, clone, 0, 50));
54+
assertFalse(ByteArrayUtil.equals(bytes, 10, 40, clone, 10, 40));
55+
}
56+
57+
@Test
58+
void equals_differentLength() {
59+
final byte[] bytes = new byte[100];
60+
final Random random = new Random();
61+
random.nextBytes(bytes);
62+
final byte[] clone = bytes.clone();
63+
assertFalse(ByteArrayUtil.equals(bytes, 0, bytes.length, bytes, 0, bytes.length - 1));
64+
assertFalse(ByteArrayUtil.equals(bytes, 0, bytes.length - 1, bytes, 0, bytes.length));
65+
assertFalse(ByteArrayUtil.equals(bytes, 0, bytes.length, clone, 0, clone.length - 1));
66+
assertFalse(ByteArrayUtil.equals(bytes, 0, bytes.length - 1, clone, 0, clone.length));
67+
assertFalse(ByteArrayUtil.equals(bytes, 10, bytes.length, clone, 10, clone.length - 1));
68+
assertFalse(ByteArrayUtil.equals(bytes, 10, bytes.length - 1, clone, 10, clone.length));
69+
assertFalse(ByteArrayUtil.equals(bytes, 0, 20, clone, 0, 20 - 1));
70+
assertFalse(ByteArrayUtil.equals(bytes, 0, 20 - 1, clone, 0, 20));
71+
assertFalse(ByteArrayUtil.equals(bytes, 10, 20, clone, 10, 20 - 1));
72+
assertFalse(ByteArrayUtil.equals(bytes, 10, 20 - 1, clone, 10, 20));
73+
}
74+
75+
@Test
76+
void hashCode_sameAsArrays() {
77+
final byte[] bytes = new byte[100];
78+
final Random random = new Random();
79+
random.nextBytes(bytes);
80+
assertEquals(
81+
Arrays.hashCode(Arrays.copyOfRange(bytes, 0, bytes.length)),
82+
ByteArrayUtil.hashCode(bytes, 0, bytes.length));
83+
assertEquals(
84+
Arrays.hashCode(Arrays.copyOfRange(bytes, 10, bytes.length)),
85+
ByteArrayUtil.hashCode(bytes, 10, bytes.length));
86+
assertEquals(Arrays.hashCode(Arrays.copyOfRange(bytes, 0, 20)), ByteArrayUtil.hashCode(bytes, 0, 20));
87+
assertEquals(Arrays.hashCode(Arrays.copyOfRange(bytes, 10, 20)), ByteArrayUtil.hashCode(bytes, 10, 20));
88+
}
89+
90+
@Test
91+
void indexOf() {
92+
final byte[] bytes = {0, 1, 2, 3, 4, 5};
93+
for (byte b = 0; b < 6; b++) {
94+
assertEquals(b, ByteArrayUtil.indexOf(bytes, 0, b));
95+
}
96+
assertEquals(bytes.length, ByteArrayUtil.indexOf(bytes, 0, (byte) 123));
97+
}
98+
99+
@Test
100+
void indexOf_offset() {
101+
final byte[] bytes = {12, 12, 12, 0, 1, 2, 3, 4, 5};
102+
for (byte b = 0; b < 6; b++) {
103+
assertEquals(3 + b, ByteArrayUtil.indexOf(bytes, 3, b));
104+
}
105+
assertEquals(bytes.length, ByteArrayUtil.indexOf(bytes, 0, (byte) 123));
106+
}
107+
}

0 commit comments

Comments
 (0)