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 .util ;
19+
20+ import org .junit .jupiter .api .Test ;
21+
22+ import java .util .concurrent .atomic .AtomicInteger ;
23+
24+ import static org .junit .jupiter .api .Assertions .*;
25+
26+ /**
27+ * @author Silvio Giebl
28+ */
29+ class TypeSwitchTest {
30+
31+ @ Test
32+ void never_alwaysSame () {
33+ final TypeSwitch <Interface > never1 = TypeSwitch .never ();
34+ final TypeSwitch <String > never2 = TypeSwitch .never ();
35+ assertSame (never1 , never2 );
36+ }
37+
38+ @ Test
39+ void when_true () {
40+ final Interface i = new Impl1 ();
41+ final AtomicInteger counter = new AtomicInteger ();
42+ TypeSwitch .when (i ).is (Impl1 .class , impl1 -> counter .incrementAndGet ());
43+ assertEquals (1 , counter .get ());
44+ }
45+
46+ @ Test
47+ void when_false () {
48+ final Interface i = new Impl2 ();
49+ final AtomicInteger counter = new AtomicInteger ();
50+ TypeSwitch .when (i ).is (Impl1 .class , impl1 -> counter .incrementAndGet ());
51+ assertEquals (0 , counter .get ());
52+ }
53+
54+ @ Test
55+ void when_true_false () {
56+ final Interface i = new Impl1 ();
57+ final AtomicInteger counter = new AtomicInteger ();
58+ TypeSwitch .when (i ).is (Impl1 .class , impl1 -> counter .incrementAndGet ()).is (Impl2 .class , impl2 -> fail ());
59+ assertEquals (1 , counter .get ());
60+ }
61+
62+ @ Test
63+ void when_false_true () {
64+ final Interface i = new Impl1 ();
65+ final AtomicInteger counter = new AtomicInteger ();
66+ TypeSwitch .when (i ).is (Impl2 .class , impl2 -> fail ()).is (Impl1 .class , impl1 -> counter .incrementAndGet ());
67+ assertEquals (1 , counter .get ());
68+ }
69+
70+ private interface Interface {}
71+
72+ private static class Impl1 implements Interface {}
73+
74+ private static class Impl2 implements Interface {}
75+ }
0 commit comments