Skip to content

Commit dcc6e9d

Browse files
committed
Use assertThrows in terminal tests
Improves the tests and also the model to consistently throw IllegalArgumentException rather than a mixture of IllegalArgumentException and ArrayIndexOutOfBoundsException depending on which exact store is used..
1 parent b7ae22a commit dcc6e9d

File tree

3 files changed

+71
-133
lines changed

3 files changed

+71
-133
lines changed

terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextDataStore.java

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,14 @@ public LineSegment[] getLineSegments(int line, int column, int len) {
140140

141141
@Override
142142
public char getChar(int line, int column) {
143-
if (column >= fWidth) {
143+
if (column < 0 || column >= fWidth) {
144144
throw new IllegalArgumentException(
145145
"Parameter 'column' must be >= 0 and less than 'width' (current value '" + fWidth + "')"); //$NON-NLS-1$ //$NON-NLS-2$
146146
}
147+
if (line < 0 || line >= fHeight) {
148+
throw new IllegalArgumentException(
149+
"Parameter 'line' must be >= 0 and less than 'height' (current value '" + fHeight + "')"); //$NON-NLS-1$ //$NON-NLS-2$
150+
}
147151
if (fChars[line] == null || column >= fChars[line].length) {
148152
return 0;
149153
}
@@ -162,19 +166,24 @@ public TerminalStyle getStyle(int line, int column) {
162166
return fStyle[line][column];
163167
}
164168

165-
void ensureLineLength(int iLine, int length) {
169+
void ensureLineLength(int line, int length) {
170+
if (line < 0 || line >= fHeight) {
171+
throw new IllegalArgumentException(
172+
"Parameter 'line' must be >= 0 and less than 'height' (current value '" + fHeight + "')"); //$NON-NLS-1$ //$NON-NLS-2$
173+
}
166174
if (length > fWidth) {
167-
throw new RuntimeException();
175+
throw new IllegalArgumentException(
176+
"Parameter 'length'(value='" + length + "') exceeds 'width'(value='" + fWidth + "')"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
168177
}
169-
if (fChars[iLine] == null) {
170-
fChars[iLine] = new char[length];
171-
} else if (fChars[iLine].length < length) {
172-
fChars[iLine] = (char[]) resizeArray(fChars[iLine], length);
178+
if (fChars[line] == null) {
179+
fChars[line] = new char[length];
180+
} else if (fChars[line].length < length) {
181+
fChars[line] = (char[]) resizeArray(fChars[line], length);
173182
}
174-
if (fStyle[iLine] == null) {
175-
fStyle[iLine] = new TerminalStyle[length];
176-
} else if (fStyle[iLine].length < length) {
177-
fStyle[iLine] = (TerminalStyle[]) resizeArray(fStyle[iLine], length);
183+
if (fStyle[line] == null) {
184+
fStyle[line] = new TerminalStyle[length];
185+
} else if (fStyle[line].length < length) {
186+
fStyle[line] = (TerminalStyle[]) resizeArray(fStyle[line], length);
178187
}
179188
}
180189

@@ -192,6 +201,10 @@ public void setChars(int line, int column, char[] chars, TerminalStyle style) {
192201

193202
@Override
194203
public void setChars(int line, int column, char[] chars, int start, int len, TerminalStyle style) {
204+
if (column < 0 || column >= fWidth) {
205+
throw new IllegalArgumentException(
206+
"Parameter 'column' must be >= 0 and less than 'width' (current value '" + fWidth + "')"); //$NON-NLS-1$ //$NON-NLS-2$
207+
}
195208
ensureLineLength(line, column + len);
196209
for (int i = 0; i < len; i++) {
197210
fChars[line][column + i] = chars[i + start];
@@ -287,6 +300,20 @@ public void copy(ITerminalTextData source) {
287300

288301
@Override
289302
public void copyRange(ITerminalTextData source, int sourceStartLine, int destStartLine, int length) {
303+
if (destStartLine < 0 || destStartLine + length > fHeight) {
304+
throw new IllegalArgumentException(
305+
"Value of 'destStartLine'+'length' parameters must be valid line (range [0-" //$NON-NLS-1$
306+
+ fHeight + "). Parameter values: 'destStartLine'=" + destStartLine //$NON-NLS-1$
307+
+ ", 'size'=" //$NON-NLS-1$
308+
+ length);
309+
}
310+
if (sourceStartLine < 0 || sourceStartLine + length > source.getHeight()) {
311+
throw new IllegalArgumentException(
312+
"Value of 'sourceStartLine'+'length' parameters must be valid line (range [0-" //$NON-NLS-1$
313+
+ source.getHeight() + "). Parameter values: 'sourceStartLine'=" + sourceStartLine //$NON-NLS-1$
314+
+ ", 'size'=" //$NON-NLS-1$
315+
+ length);
316+
}
290317
for (int i = 0; i < length; i++) {
291318
copyLine(source, i + sourceStartLine, i + destStartLine);
292319
}

terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/connector/TerminalToRemoteInjectionOutputStreamTest.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2008, 2018 Wind River Systems, Inc. and others.
2+
* Copyright (c) 2008, 2026 Wind River Systems, Inc. and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License 2.0
55
* which accompanies this distribution, and is available at
@@ -13,7 +13,7 @@
1313
package org.eclipse.terminal.internal.connector;
1414

1515
import static org.junit.jupiter.api.Assertions.assertEquals;
16-
import static org.junit.jupiter.api.Assertions.fail;
16+
import static org.junit.jupiter.api.Assertions.assertThrows;
1717

1818
import java.io.ByteArrayOutputStream;
1919
import java.io.IOException;
@@ -72,20 +72,12 @@ public void testClose() throws IOException {
7272
s.write('-');
7373
OutputStream os = s.grabOutput();
7474
// make sure the closed output does not inject anything
75-
try {
76-
os1.write('k');
77-
fail("...");
78-
} catch (Exception e) {
79-
}
75+
assertThrows(IOException.class, () -> os1.write('k'));
8076
os.write('X');
8177
s.write('a');
8278
os.write('Y');
8379
// make sure the closed output does not inject anything
84-
try {
85-
os1.write('l');
86-
fail("...");
87-
} catch (Exception e) {
88-
}
80+
assertThrows(IOException.class, () -> os1.write('l'));
8981
s.write('b');
9082
os.close();
9183
assertEquals("begin:xyAB-XYab", new String(bs.toByteArray(), ENCODING));
@@ -127,14 +119,9 @@ public void testGrabOutput() throws IOException {
127119
s.write("begin:".getBytes(ENCODING));
128120
assertEquals("begin:", new String(bs.toByteArray(), ENCODING));
129121
OutputStream os1 = s.grabOutput();
130-
OutputStream os2;
131-
try {
132-
os2 = s.grabOutput();
133-
fail("should fail until the foirst output is closed");
134-
} catch (IOException e) {
135-
}
122+
assertThrows(IOException.class, () -> s.grabOutput(), "should fail until the first output is closed");
136123
os1.close();
137-
os2 = s.grabOutput();
124+
OutputStream os2 = s.grabOutput();
138125
assertEquals("begin:", new String(bs.toByteArray(), ENCODING));
139126
os2.write("Test".getBytes(ENCODING));
140127
assertEquals("begin:Test", new String(bs.toByteArray(), ENCODING));
@@ -147,6 +134,7 @@ public void testGrabOutput() throws IOException {
147134
s.write('!');
148135
assertEquals("begin:Test the west!", new String(bs.toByteArray(), ENCODING));
149136
}
137+
150138
}
151139

152140
@Test

terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/model/AbstractITerminalTextDataTest.java

Lines changed: 26 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
import static org.junit.jupiter.api.Assertions.assertEquals;
1515
import static org.junit.jupiter.api.Assertions.assertFalse;
1616
import static org.junit.jupiter.api.Assertions.assertSame;
17+
import static org.junit.jupiter.api.Assertions.assertThrows;
1718
import static org.junit.jupiter.api.Assertions.assertTrue;
18-
import static org.junit.jupiter.api.Assertions.fail;
1919

2020
import org.eclipse.terminal.model.ITerminalTextData;
2121
import org.eclipse.terminal.model.ITerminalTextDataReadOnly;
@@ -188,12 +188,7 @@ public void testResizeFailure() {
188188
String s = "12345\n" + "abcde\n" + "ABCDE";
189189
fill(term, 0, 0, s);
190190
assertEqualsTerm(s, toMultiLineText(term));
191-
try {
192-
term.setDimensions(-3, 4);
193-
fail();
194-
} catch (RuntimeException e) {
195-
// OK
196-
}
191+
assertThrows(IllegalArgumentException.class, () -> term.setDimensions(-3, 4));
197192
// assertEquals(5, term.getWidth());
198193
// assertEquals(3, term.getHeight());
199194
// assertEquals(s, toSimpleText(term));
@@ -303,36 +298,12 @@ public void testGetChar() {
303298
assertEquals('C', term.getChar(2, 2));
304299
assertEquals('D', term.getChar(2, 3));
305300
assertEquals('E', term.getChar(2, 4));
306-
try {
307-
term.getChar(0, -1);
308-
fail();
309-
} catch (RuntimeException e) {
310-
}
311-
try {
312-
term.getChar(-1, -1);
313-
fail();
314-
} catch (RuntimeException e) {
315-
}
316-
try {
317-
term.getChar(-1, 0);
318-
fail();
319-
} catch (RuntimeException e) {
320-
}
321-
try {
322-
term.getChar(0, 5);
323-
fail();
324-
} catch (RuntimeException e) {
325-
}
326-
try {
327-
term.getChar(3, 5);
328-
fail();
329-
} catch (RuntimeException e) {
330-
}
331-
try {
332-
term.getChar(3, 0);
333-
fail();
334-
} catch (RuntimeException e) {
335-
}
301+
assertThrows(IllegalArgumentException.class, () -> term.getChar(0, -1));
302+
assertThrows(IllegalArgumentException.class, () -> term.getChar(-1, -1));
303+
assertThrows(IllegalArgumentException.class, () -> term.getChar(-1, 0));
304+
assertThrows(IllegalArgumentException.class, () -> term.getChar(0, 5));
305+
assertThrows(IllegalArgumentException.class, () -> term.getChar(3, 5));
306+
assertThrows(IllegalArgumentException.class, () -> term.getChar(3, 0));
336307
}
337308

338309
@Test
@@ -398,12 +369,9 @@ public void testSetChars() {
398369

399370
term.setChars(3, 1, new char[] { '1', '2' }, null);
400371
assertEqualsTerm("abc\n" + "bcd\n" + "cde\n" + "d12\n" + "efg\n" + "fgh", toMultiLineText(term));
401-
try {
402-
// check if we cannot exceed the range
403-
term.setChars(4, 1, new char[] { '1', '2', '3', '4', '5' }, null);
404-
fail();
405-
} catch (RuntimeException e) {
406-
}
372+
// check if we cannot exceed the range
373+
assertThrows(IllegalArgumentException.class,
374+
() -> term.setChars(4, 1, new char[] { '1', '2', '3', '4', '5' }, null));
407375

408376
}
409377

@@ -433,33 +401,12 @@ public void testSetCharsLen() {
433401
assertEqualsTerm("ZYXWVU\n" + "ab4567\n" + "ABCDEF", toMultiLineText(term));
434402

435403
fill(term, s);
436-
try {
437-
term.setChars(1, 0, chars, 7, 10, null);
438-
fail();
439-
} catch (RuntimeException e) {
440-
}
404+
assertThrows(IllegalArgumentException.class, () -> term.setChars(1, 0, chars, 7, 10, null));
441405
fill(term, s);
442-
try {
443-
term.setChars(1, -1, chars, 0, 2, null);
444-
fail();
445-
} catch (RuntimeException e) {
446-
}
447-
try {
448-
term.setChars(-1, 1, chars, 0, 2, null);
449-
fail();
450-
} catch (RuntimeException e) {
451-
}
452-
try {
453-
term.setChars(1, 10, chars, 0, 2, null);
454-
fail();
455-
} catch (RuntimeException e) {
456-
}
457-
try {
458-
term.setChars(10, 1, chars, 0, 2, null);
459-
fail();
460-
} catch (RuntimeException e) {
461-
}
462-
// assertEquals(s, toSimpleText(term));
406+
assertThrows(IllegalArgumentException.class, () -> term.setChars(1, -1, chars, 0, 2, null));
407+
assertThrows(IllegalArgumentException.class, () -> term.setChars(-1, 1, chars, 0, 2, null));
408+
assertThrows(IllegalArgumentException.class, () -> term.setChars(1, 10, chars, 0, 2, null));
409+
assertThrows(IllegalArgumentException.class, () -> term.setChars(10, 1, chars, 0, 2, null));
463410
}
464411

465412
@Test
@@ -527,30 +474,14 @@ public void testSetCopyLines() {
527474
assertEqualsSimple(s, toSimple(term));
528475
assertEqualsSimple("a2345", toSimple(termCopy));
529476

530-
try {
531-
fillSimple(termCopy, sCopy);
532-
termCopy.copyRange(term, 1, 1, 5);
533-
fail();
534-
} catch (RuntimeException e) {
535-
}
536-
try {
537-
fillSimple(termCopy, sCopy);
538-
termCopy.copyRange(term, 0, 0, 6);
539-
fail();
540-
} catch (RuntimeException e) {
541-
}
542-
try {
543-
fillSimple(termCopy, sCopy);
544-
termCopy.copyRange(term, 7, 0, 1);
545-
fail();
546-
} catch (RuntimeException e) {
547-
}
548-
try {
549-
fillSimple(termCopy, sCopy);
550-
termCopy.copyRange(term, 0, 7, 1);
551-
fail();
552-
} catch (RuntimeException e) {
553-
}
477+
fillSimple(termCopy, sCopy);
478+
assertThrows(IllegalArgumentException.class, () -> termCopy.copyRange(term, 1, 1, 5));
479+
fillSimple(termCopy, sCopy);
480+
assertThrows(IllegalArgumentException.class, () -> termCopy.copyRange(term, 0, 0, 6));
481+
fillSimple(termCopy, sCopy);
482+
assertThrows(IllegalArgumentException.class, () -> termCopy.copyRange(term, 7, 0, 1));
483+
fillSimple(termCopy, sCopy);
484+
assertThrows(IllegalArgumentException.class, () -> termCopy.copyRange(term, 0, 7, 1));
554485
}
555486

556487
@Test
@@ -690,16 +621,8 @@ public void testScrollPositive() {
690621

691622
@Test
692623
public void testScrollFail() {
693-
try {
694-
scrollTest(5, 2, -1, "012345", "012345");
695-
fail();
696-
} catch (RuntimeException e) {
697-
}
698-
try {
699-
scrollTest(0, 7, 1, "012345", " ");
700-
fail();
701-
} catch (RuntimeException e) {
702-
}
624+
assertThrows(IllegalArgumentException.class, () -> scrollTest(5, 2, -1, "012345", "012345"));
625+
assertThrows(IllegalArgumentException.class, () -> scrollTest(0, 7, 1, "012345", " "));
703626
}
704627

705628
/**

0 commit comments

Comments
 (0)