]> git.basschouten.com Git - openhab-addons.git/blob
ddc787c25def0877b18d895d4d04545e5d16187a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.dmx.internal.multiverse;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
18 import java.util.List;
19
20 import org.junit.jupiter.api.Test;
21
22 /**
23  * Tests cases for BaseChannel
24  *
25  * @author Jan N. Klug - Initial contribution
26  */
27 public class BaseChannelTest {
28
29     @Test
30     public void creatingBaseChannelFromIntegers() {
31         // overrange
32         BaseDmxChannel channel = new BaseDmxChannel(0, 600);
33         assertThat(channel.getChannelId(), is(BaseDmxChannel.MAX_CHANNEL_ID));
34
35         // underrange
36         channel = new BaseDmxChannel(0, -1);
37         assertThat(channel.getChannelId(), is(BaseDmxChannel.MIN_CHANNEL_ID));
38
39         // inrange & universe
40         channel = new BaseDmxChannel(5, 100);
41         assertThat(channel.getChannelId(), is(100));
42         assertThat(channel.getUniverseId(), is(5));
43
44         // set universe
45         channel.setUniverseId(1);
46         assertThat(channel.getUniverseId(), is(1));
47     }
48
49     @Test
50     public void creatingBaseChannelfromBaseChannel() {
51         BaseDmxChannel baseChannel = new BaseDmxChannel(5, 100);
52         BaseDmxChannel copyChannel = new BaseDmxChannel(baseChannel);
53
54         assertThat(copyChannel.getChannelId(), is(100));
55         assertThat(copyChannel.getUniverseId(), is(5));
56     }
57
58     @Test
59     public void comparingChannels() {
60         BaseDmxChannel channel1 = new BaseDmxChannel(5, 100);
61         BaseDmxChannel channel2 = new BaseDmxChannel(7, 140);
62
63         assertThat(channel1.compareTo(channel2), is(-1));
64         assertThat(channel2.compareTo(channel1), is(1));
65         assertThat(channel1.compareTo(channel1), is(0));
66     }
67
68     @Test
69     public void stringConversion() {
70         // to string
71         BaseDmxChannel baseChannel = new BaseDmxChannel(5, 100);
72         assertThat(baseChannel.toString(), is(equalTo("5:100")));
73
74         // single channel from string with universe
75         String parseString = new String("2:100");
76         List<BaseDmxChannel> channelList = BaseDmxChannel.fromString(parseString, 0);
77         assertThat(channelList.size(), is(1));
78         assertThat(channelList.get(0).toString(), is(equalTo("2:100")));
79
80         // single channel from string without universe
81         parseString = new String("100");
82         channelList = BaseDmxChannel.fromString(parseString, 2);
83         assertThat(channelList.size(), is(1));
84         assertThat(channelList.get(0).toString(), is(equalTo("2:100")));
85
86         // two channels with channel width
87         parseString = new String("100/2");
88         channelList = BaseDmxChannel.fromString(parseString, 2);
89         assertThat(channelList.size(), is(2));
90         assertThat(channelList.get(0).toString(), is(equalTo("2:100")));
91         assertThat(channelList.get(1).toString(), is(equalTo("2:101")));
92
93         // to channels with comma
94         parseString = new String("100,102");
95         channelList = BaseDmxChannel.fromString(parseString, 2);
96         assertThat(channelList.size(), is(2));
97         assertThat(channelList.get(0).toString(), is(equalTo("2:100")));
98         assertThat(channelList.get(1).toString(), is(equalTo("2:102")));
99
100         // complex string
101         parseString = new String("257,100/3,426");
102         channelList = BaseDmxChannel.fromString(parseString, 2);
103         assertThat(channelList.size(), is(5));
104         assertThat(channelList.get(0).toString(), is(equalTo("2:257")));
105         assertThat(channelList.get(1).toString(), is(equalTo("2:100")));
106         assertThat(channelList.get(2).toString(), is(equalTo("2:101")));
107         assertThat(channelList.get(3).toString(), is(equalTo("2:102")));
108         assertThat(channelList.get(4).toString(), is(equalTo("2:426")));
109     }
110 }