/
CbusTurnoutManagerTest.java
379 lines (316 loc) · 13.9 KB
/
CbusTurnoutManagerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package jmri.jmrix.can.cbus;
import jmri.jmrix.can.CanSystemConnectionMemo;
import jmri.jmrix.can.TrafficControllerScaffold;
import jmri.Manager.NameValidity;
import jmri.JmriException;
import jmri.Turnout;
import jmri.util.JUnitAppender;
import jmri.util.JUnitUtil;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
// import org.slf4j.Logger;
// import org.slf4j.LoggerFactory;
/**
*
* @author Paul Bender Copyright (C) 2017
* @author Steve Young Copyright (C) 2018
*/
public class CbusTurnoutManagerTest extends jmri.managers.AbstractTurnoutMgrTestBase {
CanSystemConnectionMemo memo = null;
@Test
public void testCTor() {
TrafficControllerScaffold tc = new TrafficControllerScaffold();
CanSystemConnectionMemo memo = new CanSystemConnectionMemo();
memo.setTrafficController(tc);
CbusTurnoutManager t = new CbusTurnoutManager(memo);
Assert.assertNotNull("exists", t);
}
public String getSystemName(int i) {
return "MTX0A;+N123E3" + i;
}
@Override
protected int getNumToTest1() {
return 19;
}
@Override
protected int getNumToTest2() {
return 7269;
}
@Override
@Test
public void testDefaultSystemName() {
// create
Turnout t = l.provideTurnout("MTX0A;+N15E741");
// check
Assert.assertTrue("real object returned ", t != null);
Assert.assertTrue("system name correct ", t == l.getBySystemName("MTX0A;+N15E741"));
}
@Test
public void testProvideName() {
// create
Turnout t = l.provide("MT+123");
// check
Assert.assertTrue("real object returned ", t != null);
Assert.assertTrue("system name correct ", t == l.getBySystemName("MT+123"));
}
@Test
public void testBadCbusTurnoutAddresses() {
try {
Turnout t1 = l.provideTurnout("MT+N15E6");
Assert.assertTrue(t1 != null);
} catch (Exception e) {
Assert.fail("Should NOT have thrown an exception");
}
try {
l.provideTurnout("MTX;+N15E6");
Assert.fail("X Should have thrown an exception");
} catch (Exception e) {
JUnitAppender.assertErrorMessageStartsWith("java.lang.IllegalArgumentException: ");
}
try {
l.provideTurnout("MTXA;+N15E6");
Assert.fail("A Should have thrown an exception");
} catch (Exception e) {
JUnitAppender.assertErrorMessageStartsWith("java.lang.IllegalArgumentException: ");
}
try {
l.provideTurnout("MTXABC;+N15E6");
Assert.fail("AC Should have thrown an exception");
} catch (Exception e) {
JUnitAppender.assertErrorMessageStartsWith("java.lang.IllegalArgumentException: ");
}
try {
l.provideTurnout("MTXABCDE;+N15E6");
Assert.fail("ABCDE Should have thrown an exception");
} catch (Exception e) {
JUnitAppender.assertErrorMessageStartsWith("java.lang.IllegalArgumentException: ");
}
try {
l.provideTurnout("MTXABCDEF0;+N15E6");
Assert.fail("ABCDEF0 Should have thrown an exception");
} catch (Exception e) {
JUnitAppender.assertErrorMessageStartsWith("java.lang.IllegalArgumentException: ");
}
try {
l.provideTurnout("MTXABCDEF");
Assert.fail("Single hex Should have thrown an exception");
} catch (Exception e) {
JUnitAppender.assertErrorMessageStartsWith("java.lang.IllegalArgumentException: can't make 2nd event");
}
try {
l.provideTurnout("MT;XABCDEF");
Assert.fail("Single hex ; Should have thrown an exception");
} catch (Exception e) {
JUnitAppender.assertErrorMessageStartsWith("java.lang.IllegalArgumentException: ");
}
try {
l.provideTurnout("MTXABCDEF;");
Assert.fail("Single hex ; Should have thrown an exception");
} catch (Exception e) {
JUnitAppender.assertErrorMessageStartsWith("java.lang.IllegalArgumentException: ");
}
try {
l.provideTurnout("MT;");
Assert.fail("; no arg Should have thrown an exception");
} catch (Exception e) {
JUnitAppender.assertErrorMessageStartsWith("java.lang.IllegalArgumentException: ");
}
try {
l.provideTurnout("MT;+N15E6");
Assert.fail("MS Should have thrown an exception");
} catch (Exception e) {
JUnitAppender.assertErrorMessageStartsWith("java.lang.IllegalArgumentException: ");
}
}
@Test
public void testBadCbusTurnoutAddressesPt2() {
try {
l.provideTurnout(";+N15E62");
Assert.fail("; Should have thrown an exception");
} catch (Exception ex) {
Assert.assertEquals("Invalid system name for Turnout: name \"MT;+N15E62\" has incorrect format",
ex.getMessage());
}
try {
l.provideTurnout("T+N156E77;+N123E456");
Assert.fail("Missing M Should have thrown an exception");
} catch (Exception ex) {
Assert.assertEquals("Invalid system name for Turnout: name \"MTT+N156E77;+N123E456\" has incorrect format",
ex.getMessage());
}
}
@Test
public void testBadCbusTurnoutAddressesPt3() {
try {
l.provideTurnout("M+N156E77;+N15E60");
Assert.fail("M Should have thrown an exception");
} catch (Exception ex) {
Assert.assertEquals("Invalid system name for Turnout: name \"MTM+N156E77;+N15E60\" has incorrect format",
ex.getMessage());
}
}
public void testBadCbusTurnoutAddressesPt4() {
try {
l.provideTurnout("MT++N156E78");
Assert.fail("++ Should have thrown an exception");
} catch (Exception e) {
JUnitAppender.assertErrorMessageStartsWith("java.lang.IllegalArgumentException: Did not find usabl");
}
}
public void testBadCbusTurnoutAddressesPt5() {
try {
l.provideTurnout("MT--N156E78");
Assert.fail("-- Should have thrown an exception");
} catch (Exception e) {
JUnitAppender.assertErrorMessageStartsWith("java.lang.IllegalArgumentException: Did not find usabl");
}
try {
l.provideTurnout("MTN156E+80");
Assert.fail("E+ Should have thrown an exception");
} catch (Exception e) {
JUnitAppender.assertErrorMessageStartsWith("java.lang.IllegalArgumentException: Did not find");
}
try {
l.provideTurnout("MTN156+E77");
Assert.fail("+E Should have thrown an exception");
} catch (Exception e) {
JUnitAppender.assertErrorMessageStartsWith("java.lang.IllegalArgumentException: Did not find");
}
try {
l.provideTurnout("MTXLKJK;XLKJK");
Assert.fail("LKJK Should have thrown an exception");
} catch (Exception e) {
JUnitAppender.assertErrorMessageStartsWith("java.lang.IllegalArgumentException: Did not find");
}
try {
l.provideTurnout("MT+7;-5;+11");
Assert.fail("3 split Should have thrown an exception");
} catch (Exception e) {
JUnitAppender.assertErrorMessageStartsWith("java.lang.IllegalArgumentException: Wrong number of events");
}
}
@Test
public void testLowerLower() {
Turnout t = l.provideTurnout("mt+n1e77;-n1e45");
Assert.assertNotNull("exists", t);
Assert.assertTrue("Retrievable lowercase", t == l.getTurnout(t.getSystemName()));
}
@Test
public void testLowerUpper() {
Turnout t = l.provideTurnout("mt+n1e77;-n1e45");
Assert.assertNotNull("exists", t);
Assert.assertTrue("Retrievable lowercase", t == l.getTurnout(t.getSystemName())); // case not changed
}
@Test
public void testUpperLower() {
Turnout t = l.provideTurnout("MTXABCDEF01;XFFEDCCBA");
Assert.assertTrue("Same hex getTurnout", t == l.getTurnout(t.getSystemName().toLowerCase()));
Turnout t2 = l.provideTurnout("MT-N66E1;+N15E789");
Assert.assertTrue("Same long getTurnout", t2 == l.getTurnout(t2.getSystemName().toLowerCase()));
}
@Test
public void testgetEntryToolTip() {
String x = l.getEntryToolTip();
Assert.assertTrue(x.contains("<html>"));
Assert.assertTrue(l.allowMultipleAdditions("M77"));
}
@Test
public void testvalidSystemNameFormat() {
Assert.assertEquals("MT+123", NameValidity.VALID, l.validSystemNameFormat("MT+123"));
Assert.assertEquals("MT+N123E123", NameValidity.VALID, l.validSystemNameFormat("MT+N123E123"));
Assert.assertEquals("MT+123;456", NameValidity.VALID, l.validSystemNameFormat("MT+123;456"));
Assert.assertEquals("MT1", NameValidity.VALID, l.validSystemNameFormat("MT1"));
Assert.assertEquals("MT1;2", NameValidity.VALID, l.validSystemNameFormat("MT1;2"));
Assert.assertEquals("MT65535", NameValidity.VALID, l.validSystemNameFormat("MT65535"));
Assert.assertEquals("MT-65535", NameValidity.VALID, l.validSystemNameFormat("MT-65535"));
Assert.assertEquals("MT100001", NameValidity.VALID, l.validSystemNameFormat("MT100001"));
Assert.assertEquals("MT-100001", NameValidity.VALID, l.validSystemNameFormat("MT-100001"));
Assert.assertEquals("M", NameValidity.INVALID, l.validSystemNameFormat("M"));
Assert.assertEquals("MT", NameValidity.INVALID, l.validSystemNameFormat("MT"));
Assert.assertEquals("MT-65536", NameValidity.INVALID, l.validSystemNameFormat("MT-65536"));
Assert.assertEquals("MT65536", NameValidity.INVALID, l.validSystemNameFormat("MT65536"));
Assert.assertEquals("MT+1;+0", NameValidity.INVALID, l.validSystemNameFormat("MT+1;+0"));
Assert.assertEquals("MT+1;-0", NameValidity.INVALID, l.validSystemNameFormat("MT+1;-0"));
Assert.assertEquals("MT+0;+17", NameValidity.INVALID, l.validSystemNameFormat("MT+0;+17"));
Assert.assertEquals("MT+0;-17", NameValidity.INVALID, l.validSystemNameFormat("MT+0;-17"));
Assert.assertEquals("MT+0", NameValidity.INVALID, l.validSystemNameFormat("MT+0"));
Assert.assertEquals("MT-0", NameValidity.INVALID, l.validSystemNameFormat("MT-0"));
Assert.assertEquals("MT7;0", NameValidity.INVALID, l.validSystemNameFormat("MT7;0"));
Assert.assertEquals("MT0;7", NameValidity.INVALID, l.validSystemNameFormat("MT0;7"));
}
@Test
public void testgetNextValidAddress() throws JmriException {
Assert.assertEquals("+17", "+17", l.getNextValidAddress("+17", "M"));
Turnout t = l.provideTurnout("MT+17");
Assert.assertNotNull("exists", t);
Assert.assertEquals("+18", "+18", l.getNextValidAddress("+17", "M"));
Assert.assertEquals("+N45E22", "+N45E22", l.getNextValidAddress("+N45E22", "M"));
Turnout ta = l.provideTurnout("MT+N45E22");
Assert.assertNotNull("exists", ta);
Assert.assertEquals("+N45E23", "+N45E23", l.getNextValidAddress("+N45E22", "M"));
try {
Assert.assertEquals("null", null, l.getNextValidAddress(null, "M"));
} catch (JmriException ex) {
Assert.assertEquals("java.lang.IllegalArgumentException: No address Passed ", ex.getMessage());
}
}
@Test
public void testgetNextValidAddressPt2() throws JmriException {
Turnout t = l.provideTurnout("MT+65535");
Assert.assertNotNull("exists", t);
Assert.assertEquals("+65535", null, l.getNextValidAddress("+65535", "M"));
JUnitAppender.assertErrorMessageStartsWith("java.lang.IllegalArgumentException: ");
}
@Test
public void testgetNextValidAddressPt3() throws JmriException {
Turnout t = l.provideTurnout("MT+10");
Assert.assertNotNull("exists", t);
Assert.assertEquals("+10", "+11", l.getNextValidAddress("+10", "M"));
}
@Test
public void testgetNextValidAddressPt4() throws JmriException {
Turnout t = l.provideTurnout("MT+9");
Turnout ta = l.provideTurnout("MT+10");
Assert.assertNotNull("exists", t);
Assert.assertNotNull("exists", ta);
Assert.assertEquals(" null +9 +10", "+11", l.getNextValidAddress("+9", "M"));
}
@Test
public void testcreateSystemName() throws JmriException {
Assert.assertEquals("MT+10", "MT+10", l.createSystemName("10", "M"));
Assert.assertEquals("MT+N34E610", "MT+N34E610", l.createSystemName("+N34E610", "M"));
Assert.assertEquals("MT5;6", "MT+5;-6", l.createSystemName("5;6", "M"));
Assert.assertEquals("M2T+10", "M2T+10", l.createSystemName("+10", "M2"));
Assert.assertEquals("ZZZZZZZZZ2T+10", "ZZZZZZZZZT+10", l.createSystemName("+10", "ZZZZZZZZZ"));
try {
Assert.assertEquals("MTT", null, l.createSystemName("S", "M"));
} catch (JmriException ex) {
Assert.assertEquals("java.lang.IllegalArgumentException: Wrong number of events in address: S", ex.getMessage());
}
}
@Test
public void testProvideswhenNotNull() {
Turnout t = l.provideTurnout("+4");
Turnout ta = l.provideTurnout("+4");
Assert.assertTrue(t == ta);
t = null;
ta = null;
}
// The minimal setup for log4J
@Before
public void setUp() {
JUnitUtil.setUp();
memo = new CanSystemConnectionMemo();
memo.setTrafficController(new TrafficControllerScaffold());
l = new CbusTurnoutManager(memo);
}
@After
public void tearDown() {
l.dispose();
memo.dispose();
JUnitUtil.tearDown();
}
// private final static Logger log = LoggerFactory.getLogger(CbusTurnoutManagerTest.class);
}