Skip to content

Commit c7cf80b

Browse files
committed
Add unit tests for mqtt message exceptions
1 parent cdef16c commit c7cf80b

13 files changed

+989
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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.mqtt.mqtt3.exceptions;
19+
20+
import com.hivemq.client.internal.util.AsyncRuntimeException;
21+
import com.hivemq.client.mqtt.mqtt3.message.connect.connack.Mqtt3ConnAck;
22+
import org.junit.jupiter.api.Test;
23+
24+
import static org.junit.jupiter.api.Assertions.*;
25+
import static org.mockito.Mockito.mock;
26+
27+
/**
28+
* @author Silvio Giebl
29+
*/
30+
class Mqtt3ConnAckExceptionTest {
31+
32+
@Test
33+
void constructor() {
34+
final Mqtt3ConnAck connAck = mock(Mqtt3ConnAck.class);
35+
final Mqtt3ConnAckException exception = new Mqtt3ConnAckException(connAck, null, null);
36+
assertNull(exception.getMessage());
37+
assertNull(exception.getCause());
38+
assertEquals(connAck, exception.getMqttMessage());
39+
assertEquals(0, exception.getStackTrace().length);
40+
}
41+
42+
@Test
43+
void constructor_message() {
44+
final Mqtt3ConnAck connAck = mock(Mqtt3ConnAck.class);
45+
final Mqtt3ConnAckException exception = new Mqtt3ConnAckException(connAck, "message", null);
46+
assertEquals("message", exception.getMessage());
47+
assertNull(exception.getCause());
48+
assertEquals(connAck, exception.getMqttMessage());
49+
assertEquals(0, exception.getStackTrace().length);
50+
}
51+
52+
@Test
53+
void constructor_cause() {
54+
final Mqtt3ConnAck connAck = mock(Mqtt3ConnAck.class);
55+
final RuntimeException cause = new RuntimeException("cause");
56+
final Mqtt3ConnAckException exception = new Mqtt3ConnAckException(connAck, null, cause);
57+
assertNull(exception.getMessage());
58+
assertEquals(cause, exception.getCause());
59+
assertEquals(connAck, exception.getMqttMessage());
60+
assertEquals(0, exception.getStackTrace().length);
61+
}
62+
63+
@Test
64+
void constructor_message_cause() {
65+
final Mqtt3ConnAck connAck = mock(Mqtt3ConnAck.class);
66+
final RuntimeException cause = new RuntimeException("cause");
67+
final Mqtt3ConnAckException exception = new Mqtt3ConnAckException(connAck, "message", cause);
68+
assertEquals("message", exception.getMessage());
69+
assertEquals(cause, exception.getCause());
70+
assertEquals(connAck, exception.getMqttMessage());
71+
assertEquals(0, exception.getStackTrace().length);
72+
}
73+
74+
@Test
75+
void noStackTrace() {
76+
final Mqtt3ConnAck connAck = mock(Mqtt3ConnAck.class);
77+
final RuntimeException cause = new RuntimeException("cause");
78+
final Mqtt3ConnAckException exception = new Mqtt3ConnAckException(connAck, "message", cause);
79+
assertEquals(0, exception.getStackTrace().length);
80+
final Mqtt3ConnAckException thrownException =
81+
assertThrows(Mqtt3ConnAckException.class, () -> { throw exception; });
82+
assertEquals(0, thrownException.getStackTrace().length);
83+
}
84+
85+
@Test
86+
void fillInStackTrace_newStackTrace() {
87+
final Mqtt3ConnAck connAck = mock(Mqtt3ConnAck.class);
88+
final RuntimeException cause = new RuntimeException("cause");
89+
final Mqtt3ConnAckException exception = new Mqtt3ConnAckException(connAck, "message", cause);
90+
assertEquals(0, exception.getStackTrace().length);
91+
final RuntimeException filledException = AsyncRuntimeException.fillInStackTrace(exception);
92+
assertTrue(filledException instanceof Mqtt3ConnAckException);
93+
assertTrue(filledException.getStackTrace().length > 0);
94+
assertEquals("fillInStackTrace_newStackTrace", filledException.getStackTrace()[0].getMethodName());
95+
}
96+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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.mqtt.mqtt3.exceptions;
19+
20+
import com.hivemq.client.internal.util.AsyncRuntimeException;
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.junit.jupiter.api.Assertions.*;
24+
25+
/**
26+
* @author Silvio Giebl
27+
*/
28+
class Mqtt3DisconnectExceptionTest {
29+
30+
@Test
31+
void constructor() {
32+
final Mqtt3DisconnectException exception = new Mqtt3DisconnectException(null, null);
33+
assertNull(exception.getMessage());
34+
assertNull(exception.getCause());
35+
assertNotNull(exception.getMqttMessage());
36+
assertEquals(0, exception.getStackTrace().length);
37+
}
38+
39+
@Test
40+
void constructor_message() {
41+
final Mqtt3DisconnectException exception = new Mqtt3DisconnectException("message", null);
42+
assertEquals("message", exception.getMessage());
43+
assertNull(exception.getCause());
44+
assertNotNull(exception.getMqttMessage());
45+
assertEquals(0, exception.getStackTrace().length);
46+
}
47+
48+
@Test
49+
void constructor_cause() {
50+
final RuntimeException cause = new RuntimeException("cause");
51+
final Mqtt3DisconnectException exception = new Mqtt3DisconnectException(null, cause);
52+
assertNull(exception.getMessage());
53+
assertEquals(cause, exception.getCause());
54+
assertNotNull(exception.getMqttMessage());
55+
assertEquals(0, exception.getStackTrace().length);
56+
}
57+
58+
@Test
59+
void constructor_message_cause() {
60+
final RuntimeException cause = new RuntimeException("cause");
61+
final Mqtt3DisconnectException exception = new Mqtt3DisconnectException("message", cause);
62+
assertEquals("message", exception.getMessage());
63+
assertEquals(cause, exception.getCause());
64+
assertNotNull(exception.getMqttMessage());
65+
assertEquals(0, exception.getStackTrace().length);
66+
}
67+
68+
@Test
69+
void noStackTrace() {
70+
final RuntimeException cause = new RuntimeException("cause");
71+
final Mqtt3DisconnectException exception = new Mqtt3DisconnectException("message", cause);
72+
assertEquals(0, exception.getStackTrace().length);
73+
final Mqtt3DisconnectException thrownException =
74+
assertThrows(Mqtt3DisconnectException.class, () -> { throw exception; });
75+
assertEquals(0, thrownException.getStackTrace().length);
76+
}
77+
78+
@Test
79+
void fillInStackTrace_newStackTrace() {
80+
final RuntimeException cause = new RuntimeException("cause");
81+
final Mqtt3DisconnectException exception = new Mqtt3DisconnectException("message", cause);
82+
assertEquals(0, exception.getStackTrace().length);
83+
final RuntimeException filledException = AsyncRuntimeException.fillInStackTrace(exception);
84+
assertTrue(filledException instanceof Mqtt3DisconnectException);
85+
assertTrue(filledException.getStackTrace().length > 0);
86+
assertEquals("fillInStackTrace_newStackTrace", filledException.getStackTrace()[0].getMethodName());
87+
}
88+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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.mqtt.mqtt3.exceptions;
19+
20+
import com.hivemq.client.internal.util.AsyncRuntimeException;
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.junit.jupiter.api.Assertions.*;
24+
25+
/**
26+
* @author Silvio Giebl
27+
*/
28+
class Mqtt3PubAckExceptionTest {
29+
30+
@Test
31+
void constructor() {
32+
final Mqtt3PubAckException exception = new Mqtt3PubAckException(null, null);
33+
assertNull(exception.getMessage());
34+
assertNull(exception.getCause());
35+
assertNotNull(exception.getMqttMessage());
36+
assertEquals(0, exception.getStackTrace().length);
37+
}
38+
39+
@Test
40+
void constructor_message() {
41+
final Mqtt3PubAckException exception = new Mqtt3PubAckException("message", null);
42+
assertEquals("message", exception.getMessage());
43+
assertNull(exception.getCause());
44+
assertNotNull(exception.getMqttMessage());
45+
assertEquals(0, exception.getStackTrace().length);
46+
}
47+
48+
@Test
49+
void constructor_cause() {
50+
final RuntimeException cause = new RuntimeException("cause");
51+
final Mqtt3PubAckException exception = new Mqtt3PubAckException(null, cause);
52+
assertNull(exception.getMessage());
53+
assertEquals(cause, exception.getCause());
54+
assertNotNull(exception.getMqttMessage());
55+
assertEquals(0, exception.getStackTrace().length);
56+
}
57+
58+
@Test
59+
void constructor_message_cause() {
60+
final RuntimeException cause = new RuntimeException("cause");
61+
final Mqtt3PubAckException exception = new Mqtt3PubAckException("message", cause);
62+
assertEquals("message", exception.getMessage());
63+
assertEquals(cause, exception.getCause());
64+
assertNotNull(exception.getMqttMessage());
65+
assertEquals(0, exception.getStackTrace().length);
66+
}
67+
68+
@Test
69+
void noStackTrace() {
70+
final RuntimeException cause = new RuntimeException("cause");
71+
final Mqtt3PubAckException exception = new Mqtt3PubAckException("message", cause);
72+
assertEquals(0, exception.getStackTrace().length);
73+
final Mqtt3PubAckException thrownException =
74+
assertThrows(Mqtt3PubAckException.class, () -> { throw exception; });
75+
assertEquals(0, thrownException.getStackTrace().length);
76+
}
77+
78+
@Test
79+
void fillInStackTrace_newStackTrace() {
80+
final RuntimeException cause = new RuntimeException("cause");
81+
final Mqtt3PubAckException exception = new Mqtt3PubAckException("message", cause);
82+
assertEquals(0, exception.getStackTrace().length);
83+
final RuntimeException filledException = AsyncRuntimeException.fillInStackTrace(exception);
84+
assertTrue(filledException instanceof Mqtt3PubAckException);
85+
assertTrue(filledException.getStackTrace().length > 0);
86+
assertEquals("fillInStackTrace_newStackTrace", filledException.getStackTrace()[0].getMethodName());
87+
}
88+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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.mqtt.mqtt3.exceptions;
19+
20+
import com.hivemq.client.internal.util.AsyncRuntimeException;
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.junit.jupiter.api.Assertions.*;
24+
25+
/**
26+
* @author Silvio Giebl
27+
*/
28+
class Mqtt3PubRecExceptionTest {
29+
30+
@Test
31+
void constructor() {
32+
final Mqtt3PubRecException exception = new Mqtt3PubRecException(null, null);
33+
assertNull(exception.getMessage());
34+
assertNull(exception.getCause());
35+
assertNotNull(exception.getMqttMessage());
36+
assertEquals(0, exception.getStackTrace().length);
37+
}
38+
39+
@Test
40+
void constructor_message() {
41+
final Mqtt3PubRecException exception = new Mqtt3PubRecException("message", null);
42+
assertEquals("message", exception.getMessage());
43+
assertNull(exception.getCause());
44+
assertNotNull(exception.getMqttMessage());
45+
assertEquals(0, exception.getStackTrace().length);
46+
}
47+
48+
@Test
49+
void constructor_cause() {
50+
final RuntimeException cause = new RuntimeException("cause");
51+
final Mqtt3PubRecException exception = new Mqtt3PubRecException(null, cause);
52+
assertNull(exception.getMessage());
53+
assertEquals(cause, exception.getCause());
54+
assertNotNull(exception.getMqttMessage());
55+
assertEquals(0, exception.getStackTrace().length);
56+
}
57+
58+
@Test
59+
void constructor_message_cause() {
60+
final RuntimeException cause = new RuntimeException("cause");
61+
final Mqtt3PubRecException exception = new Mqtt3PubRecException("message", cause);
62+
assertEquals("message", exception.getMessage());
63+
assertEquals(cause, exception.getCause());
64+
assertNotNull(exception.getMqttMessage());
65+
assertEquals(0, exception.getStackTrace().length);
66+
}
67+
68+
@Test
69+
void noStackTrace() {
70+
final RuntimeException cause = new RuntimeException("cause");
71+
final Mqtt3PubRecException exception = new Mqtt3PubRecException("message", cause);
72+
assertEquals(0, exception.getStackTrace().length);
73+
final Mqtt3PubRecException thrownException =
74+
assertThrows(Mqtt3PubRecException.class, () -> { throw exception; });
75+
assertEquals(0, thrownException.getStackTrace().length);
76+
}
77+
78+
@Test
79+
void fillInStackTrace_newStackTrace() {
80+
final RuntimeException cause = new RuntimeException("cause");
81+
final Mqtt3PubRecException exception = new Mqtt3PubRecException("message", cause);
82+
assertEquals(0, exception.getStackTrace().length);
83+
final RuntimeException filledException = AsyncRuntimeException.fillInStackTrace(exception);
84+
assertTrue(filledException instanceof Mqtt3PubRecException);
85+
assertTrue(filledException.getStackTrace().length > 0);
86+
assertEquals("fillInStackTrace_newStackTrace", filledException.getStackTrace()[0].getMethodName());
87+
}
88+
}

0 commit comments

Comments
 (0)