Skip to content

Commit d96c6f2

Browse files
committed
Added unit tests for mqtt.exceptions package
1 parent 7383b5a commit d96c6f2

File tree

6 files changed

+356
-0
lines changed

6 files changed

+356
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.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 ConnectionClosedExceptionTest {
29+
30+
@Test
31+
void constructor_message() {
32+
final ConnectionClosedException exception = new ConnectionClosedException("message");
33+
assertEquals("message", exception.getMessage());
34+
assertNull(exception.getCause());
35+
assertEquals(0, exception.getStackTrace().length);
36+
}
37+
38+
@Test
39+
void constructor_cause() {
40+
final ConnectionClosedException exception = new ConnectionClosedException(new RuntimeException("cause"));
41+
assertEquals("java.lang.RuntimeException: cause", exception.getMessage());
42+
assertNotNull(exception.getCause());
43+
assertEquals("cause", exception.getCause().getMessage());
44+
assertEquals(0, exception.getStackTrace().length);
45+
}
46+
47+
@Test
48+
void noStackTrace() {
49+
final ConnectionClosedException exception = new ConnectionClosedException("message");
50+
assertEquals(0, exception.getStackTrace().length);
51+
final ConnectionClosedException thrownException =
52+
assertThrows(ConnectionClosedException.class, () -> { throw exception; });
53+
assertEquals(0, thrownException.getStackTrace().length);
54+
}
55+
56+
@Test
57+
void fillInStackTrace_newStackTrace() {
58+
final ConnectionClosedException exception = new ConnectionClosedException("message");
59+
assertEquals(0, exception.getStackTrace().length);
60+
final RuntimeException filledException = AsyncRuntimeException.fillInStackTrace(exception);
61+
assertTrue(filledException instanceof ConnectionClosedException);
62+
assertTrue(filledException.getStackTrace().length > 0);
63+
assertEquals("fillInStackTrace_newStackTrace", filledException.getStackTrace()[0].getMethodName());
64+
}
65+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.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 ConnectionFailedExceptionTest {
29+
30+
@Test
31+
void constructor_message() {
32+
final ConnectionFailedException exception = new ConnectionFailedException("message");
33+
assertEquals("message", exception.getMessage());
34+
assertNull(exception.getCause());
35+
assertEquals(0, exception.getStackTrace().length);
36+
}
37+
38+
@Test
39+
void constructor_cause() {
40+
final ConnectionFailedException exception = new ConnectionFailedException(new RuntimeException("cause"));
41+
assertEquals("java.lang.RuntimeException: cause", exception.getMessage());
42+
assertNotNull(exception.getCause());
43+
assertEquals("cause", exception.getCause().getMessage());
44+
assertEquals(0, exception.getStackTrace().length);
45+
}
46+
47+
@Test
48+
void noStackTrace() {
49+
final ConnectionFailedException exception = new ConnectionFailedException("message");
50+
assertEquals(0, exception.getStackTrace().length);
51+
final ConnectionFailedException thrownException =
52+
assertThrows(ConnectionFailedException.class, () -> { throw exception; });
53+
assertEquals(0, thrownException.getStackTrace().length);
54+
}
55+
56+
@Test
57+
void fillInStackTrace_newStackTrace() {
58+
final ConnectionFailedException exception = new ConnectionFailedException("message");
59+
assertEquals(0, exception.getStackTrace().length);
60+
final RuntimeException filledException = AsyncRuntimeException.fillInStackTrace(exception);
61+
assertTrue(filledException instanceof ConnectionFailedException);
62+
assertTrue(filledException.getStackTrace().length > 0);
63+
assertEquals("fillInStackTrace_newStackTrace", filledException.getStackTrace()[0].getMethodName());
64+
}
65+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.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 MqttClientStateExceptionTest {
29+
30+
@Test
31+
void constructor_message() {
32+
final MqttClientStateException exception = new MqttClientStateException("message");
33+
assertEquals("message", exception.getMessage());
34+
assertNull(exception.getCause());
35+
assertEquals(0, exception.getStackTrace().length);
36+
}
37+
38+
@Test
39+
void noStackTrace() {
40+
final MqttClientStateException exception = new MqttClientStateException("message");
41+
assertEquals(0, exception.getStackTrace().length);
42+
final MqttClientStateException thrownException =
43+
assertThrows(MqttClientStateException.class, () -> { throw exception; });
44+
assertEquals(0, thrownException.getStackTrace().length);
45+
}
46+
47+
@Test
48+
void fillInStackTrace_newStackTrace() {
49+
final MqttClientStateException exception = new MqttClientStateException("message");
50+
assertEquals(0, exception.getStackTrace().length);
51+
final RuntimeException filledException = AsyncRuntimeException.fillInStackTrace(exception);
52+
assertTrue(filledException instanceof MqttClientStateException);
53+
assertTrue(filledException.getStackTrace().length > 0);
54+
assertEquals("fillInStackTrace_newStackTrace", filledException.getStackTrace()[0].getMethodName());
55+
}
56+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.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 MqttDecodeExceptionTest {
29+
30+
@Test
31+
void constructor_message() {
32+
final MqttDecodeException exception = new MqttDecodeException("message");
33+
assertEquals("message", exception.getMessage());
34+
assertNull(exception.getCause());
35+
assertEquals(0, exception.getStackTrace().length);
36+
}
37+
38+
@Test
39+
void noStackTrace() {
40+
final MqttDecodeException exception = new MqttDecodeException("message");
41+
assertEquals(0, exception.getStackTrace().length);
42+
final MqttDecodeException thrownException = assertThrows(MqttDecodeException.class, () -> { throw exception; });
43+
assertEquals(0, thrownException.getStackTrace().length);
44+
}
45+
46+
@Test
47+
void fillInStackTrace_newStackTrace() {
48+
final MqttDecodeException exception = new MqttDecodeException("message");
49+
assertEquals(0, exception.getStackTrace().length);
50+
final RuntimeException filledException = AsyncRuntimeException.fillInStackTrace(exception);
51+
assertTrue(filledException instanceof MqttDecodeException);
52+
assertTrue(filledException.getStackTrace().length > 0);
53+
assertEquals("fillInStackTrace_newStackTrace", filledException.getStackTrace()[0].getMethodName());
54+
}
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.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 MqttEncodeExceptionTest {
29+
30+
@Test
31+
void constructor_message() {
32+
final MqttEncodeException exception = new MqttEncodeException("message");
33+
assertEquals("message", exception.getMessage());
34+
assertNull(exception.getCause());
35+
assertEquals(0, exception.getStackTrace().length);
36+
}
37+
38+
@Test
39+
void noStackTrace() {
40+
final MqttEncodeException exception = new MqttEncodeException("message");
41+
assertEquals(0, exception.getStackTrace().length);
42+
final MqttEncodeException thrownException = assertThrows(MqttEncodeException.class, () -> { throw exception; });
43+
assertEquals(0, thrownException.getStackTrace().length);
44+
}
45+
46+
@Test
47+
void fillInStackTrace_newStackTrace() {
48+
final MqttEncodeException exception = new MqttEncodeException("message");
49+
assertEquals(0, exception.getStackTrace().length);
50+
final RuntimeException filledException = AsyncRuntimeException.fillInStackTrace(exception);
51+
assertTrue(filledException instanceof MqttEncodeException);
52+
assertTrue(filledException.getStackTrace().length > 0);
53+
assertEquals("fillInStackTrace_newStackTrace", filledException.getStackTrace()[0].getMethodName());
54+
}
55+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.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 MqttSessionExpiredExceptionTest {
29+
30+
@Test
31+
void constructor_messageAndCause() {
32+
final MqttSessionExpiredException exception =
33+
new MqttSessionExpiredException("message", new RuntimeException("cause"));
34+
assertEquals("message", exception.getMessage());
35+
assertNotNull(exception.getCause());
36+
assertEquals("cause", exception.getCause().getMessage());
37+
assertEquals(0, exception.getStackTrace().length);
38+
}
39+
40+
@Test
41+
void noStackTrace() {
42+
final MqttSessionExpiredException exception =
43+
new MqttSessionExpiredException("message", new RuntimeException("cause"));
44+
assertEquals(0, exception.getStackTrace().length);
45+
final MqttSessionExpiredException thrownException =
46+
assertThrows(MqttSessionExpiredException.class, () -> { throw exception; });
47+
assertEquals(0, thrownException.getStackTrace().length);
48+
}
49+
50+
@Test
51+
void fillInStackTrace_newStackTrace() {
52+
final MqttSessionExpiredException exception =
53+
new MqttSessionExpiredException("message", new RuntimeException("cause"));
54+
assertEquals(0, exception.getStackTrace().length);
55+
final RuntimeException filledException = AsyncRuntimeException.fillInStackTrace(exception);
56+
assertTrue(filledException instanceof MqttSessionExpiredException);
57+
assertTrue(filledException.getStackTrace().length > 0);
58+
assertEquals("fillInStackTrace_newStackTrace", filledException.getStackTrace()[0].getMethodName());
59+
}
60+
}

0 commit comments

Comments
 (0)