Skip to content

Commit 962f862

Browse files
committed
Streamline JUnit asserts
Change-Id: Id3663c1054b7040da9e566adf80a73ee6a087981 Reviewed-on: https://git.eclipse.org/r/c/platform/eclipse.platform.swt/+/186314 Tested-by: Platform Bot <[email protected]> Reviewed-by: Alexander Kurtakov <[email protected]>
1 parent 8932346 commit 962f862

File tree

4 files changed

+87
-126
lines changed

4 files changed

+87
-126
lines changed

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_SWT.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*******************************************************************************/
1515
package org.eclipse.swt.tests.junit;
1616

17+
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertNotNull;
1719
import static org.junit.Assert.assertTrue;
1820
import static org.junit.Assert.fail;
1921

@@ -109,17 +111,17 @@ public void test_getMessageLjava_lang_String() {
109111
} catch (Throwable t) {
110112
fail ("exception " + t + " generated for SWT_Yes");
111113
}
112-
assertTrue (
114+
assertEquals (
113115
"invalid key did not return as itself",
114-
"_NOT_FOUND_IN_PROPERTIES_".equals(SWT.getMessage("_NOT_FOUND_IN_PROPERTIES_")));
116+
"_NOT_FOUND_IN_PROPERTIES_", SWT.getMessage("_NOT_FOUND_IN_PROPERTIES_"));
115117

116118
}
117119

118120
@Test
119121
public void test_getPlatform() {
120122
// Can't test the list of platforms, since this may change,
121123
// so just test to see it returns something.
122-
assertTrue ("returned null platform name", SWT.getPlatform() != null);
124+
assertNotNull ("returned null platform name", SWT.getPlatform());
123125
}
124126

125127
@Test

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_SWTError.java

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2015 IBM Corporation and others.
2+
* Copyright (c) 2000, 2021 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -14,6 +14,7 @@
1414
*******************************************************************************/
1515
package org.eclipse.swt.tests.junit;
1616

17+
import static org.junit.Assert.assertEquals;
1718
import static org.junit.Assert.assertTrue;
1819

1920
import java.io.ByteArrayOutputStream;
@@ -32,31 +33,31 @@ public class Test_org_eclipse_swt_SWTError {
3233

3334
@Test
3435
public void test_Constructor() {
35-
assertTrue (
36+
assertEquals (
3637
"did not fill in code properly",
37-
new SWTError().code == SWT.ERROR_UNSPECIFIED);
38+
SWT.ERROR_UNSPECIFIED, new SWTError().code);
3839
}
3940

4041
@Test
4142
public void test_ConstructorI() {
42-
assertTrue (
43-
"did not fill in code properly",
44-
new SWTError(SWT.ERROR_CANNOT_BE_ZERO).code == SWT.ERROR_CANNOT_BE_ZERO);
43+
assertEquals(
44+
"did not fill in code properly",SWT.ERROR_CANNOT_BE_ZERO,
45+
new SWTError(SWT.ERROR_CANNOT_BE_ZERO).code );
4546
}
4647

4748
@Test
4849
public void test_ConstructorILjava_lang_String() {
49-
assertTrue (
50-
"did not fill in code properly",
50+
assertEquals (
51+
"did not fill in code properly",SWT.ERROR_CANNOT_BE_ZERO,
5152
new SWTError(SWT.ERROR_CANNOT_BE_ZERO, "An uninteresting message").code
52-
== SWT.ERROR_CANNOT_BE_ZERO);
53+
);
5354
}
5455

5556
@Test
5657
public void test_ConstructorLjava_lang_String() {
57-
assertTrue (
58-
"did not fill in code properly",
59-
new SWTError("An uninteresting message").code == SWT.ERROR_UNSPECIFIED);
58+
assertEquals(
59+
"did not fill in code properly",SWT.ERROR_UNSPECIFIED,
60+
new SWTError("An uninteresting message").code);
6061
}
6162

6263
@Test
@@ -70,15 +71,6 @@ public void test_getMessage() {
7071
@Test
7172
public void test_printStackTrace() {
7273

73-
// WARNING: this test is not CLDC safe, because it requires java.io.PrintStream
74-
75-
try {
76-
Class.forName("java.io.PrintStream");
77-
} catch (ClassNotFoundException e) {
78-
// ignore test if running on CLDC
79-
return;
80-
}
81-
8274
// test default SWTError
8375

8476
ByteArrayOutputStream out = new ByteArrayOutputStream();

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_CCombo.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2020 IBM Corporation and others.
2+
* Copyright (c) 2000, 2021 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -17,6 +17,7 @@
1717
import static org.junit.Assert.assertArrayEquals;
1818
import static org.junit.Assert.assertEquals;
1919
import static org.junit.Assert.assertFalse;
20+
import static org.junit.Assert.assertNotEquals;
2021
import static org.junit.Assert.assertNotNull;
2122
import static org.junit.Assert.assertNull;
2223
import static org.junit.Assert.assertThrows;
@@ -84,7 +85,7 @@ public void test_copy() {
8485
ccombo.copy();
8586
ccombo.setSelection(new Point(0,0));
8687
ccombo.paste();
87-
assertTrue(":a:", ccombo.getText().equals("23123456"));
88+
assertEquals(":a:", "23123456", ccombo.getText());
8889
}
8990

9091
@Test
@@ -100,7 +101,7 @@ public void test_cut() {
100101
ccombo.setText("123456");
101102
ccombo.setSelection(new Point(1,3));
102103
ccombo.cut();
103-
assertTrue(":a:", ccombo.getText().equals("1456"));
104+
assertEquals(":a:", "1456", ccombo.getText());
104105
}
105106

106107
@Override
@@ -137,9 +138,9 @@ public void test_paste() {
137138
ccombo.setText("123456");
138139
ccombo.setSelection(new Point(1,3));
139140
ccombo.cut();
140-
assertTrue(":a:", ccombo.getText().equals("1456"));
141+
assertEquals(":a:", "1456", ccombo.getText());
141142
ccombo.paste();
142-
assertTrue(":a:", ccombo.getText().equals("123456"));
143+
assertEquals(":a:", "123456", ccombo.getText());
143144
}
144145

145146
@Override
@@ -247,7 +248,7 @@ public void test_setToolTipTextLjava_lang_String() {
247248
String[] cases = {"", "fang", "fang0"};
248249
for (int i = 0; i < cases.length; i++) {
249250
ccombo.setText(cases[i]);
250-
assertTrue(":a:" + i, ccombo.getText().equals(cases[i]));
251+
assertEquals(":a:" + i, cases[i], ccombo.getText());
251252
}
252253
}
253254

@@ -382,12 +383,12 @@ public void test_clearSelection() {
382383
for (int i = 0; i < number; i++)
383384
ccombo.add("fred" + i);
384385
ccombo.clearSelection();
385-
assertTrue(":a:", ccombo.getSelection().equals(new Point(0, 0)));
386+
assertEquals(":a:", new Point(0, 0), ccombo.getSelection());
386387
ccombo.setSelection(new Point(0, 5));
387-
assertTrue(":b:", ccombo.getSelection().equals(new Point(0, 0))); //nothing is selected
388+
assertEquals(":b:", new Point(0, 0), ccombo.getSelection()); //nothing is selected
388389
ccombo.setText("some text");
389390
ccombo.setSelection(new Point(0, 5));
390-
assertTrue(":c:", ccombo.getSelection().equals(new Point(0, 5)));
391+
assertEquals(":c:", new Point(0, 5), ccombo.getSelection());
391392
ccombo.clearSelection();
392393
assertEquals(":d:", ccombo.getSelection().x, ccombo.getSelection().y);
393394
}
@@ -467,7 +468,7 @@ public void test_getItemI() {
467468
ccombo.add("fred" + i);
468469
}
469470
for (int i = 0; i < number; i++)
470-
assertTrue(ccombo.getItem(i).equals("fred" + i));
471+
assertEquals("fred" + i, ccombo.getItem(i));
471472
}
472473

473474
@Test
@@ -478,17 +479,17 @@ public void test_getItems() {
478479
ccombo.add("3");
479480
String[] items = ccombo.getItems();
480481
assertEquals(":a:",3, items.length);
481-
assertTrue(":a:", items[0].equals("1"));
482-
assertTrue(":a:", items[1].equals("2"));
483-
assertTrue(":a:", items[2].equals("3"));
482+
assertEquals(":a:", "1", items[0]);
483+
assertEquals(":a:", "2", items[1]);
484+
assertEquals(":a:", "3", items[2]);
484485
}
485486

486487
@Test
487488
public void test_getSelection() {
488489
ccombo.setText("123456");
489490
ccombo.setSelection(new Point(1,3));
490491
ccombo.getSelection();
491-
assertTrue(":a:", ccombo.getSelection().equals(new Point(1,3)));
492+
assertEquals(":a:", new Point(1,3), ccombo.getSelection());
492493
}
493494

494495
@Test
@@ -520,15 +521,15 @@ public void test_getStyle() {
520521
int style = 0;
521522
ccombo.setEditable(false);
522523
style = ccombo.getStyle();
523-
assertTrue((style & SWT.READ_ONLY) != 0);
524+
assertNotEquals(0, (style & SWT.READ_ONLY));
524525
}
525526

526527
@Test
527528
public void test_getText() {
528529
String[] cases = {"", "fred", "fredfred"};
529530
for (int i = 0; i < cases.length; i++) {
530531
ccombo.setText(cases[i]);
531-
assertTrue(":a:" + String.valueOf(i), cases[i].equals(ccombo.getText()));
532+
assertEquals(":a:" + String.valueOf(i), cases[i], ccombo.getText());
532533
}
533534
}
534535

@@ -768,7 +769,7 @@ public void test_setItemILjava_lang_String() {
768769

769770
ccombo.add("joe");
770771
ccombo.setItem(0, "fang");
771-
assertTrue("fang", ccombo.getItem(0).equals("fang"));
772+
assertEquals("fang", "fang", ccombo.getItem(0));
772773

773774
assertThrows("No exception thrown for illegal index argument", IllegalArgumentException.class, () ->ccombo.setItem(4, "fang"));
774775

@@ -806,10 +807,10 @@ public void test_setSelectionLorg_eclipse_swt_graphics_Point() {
806807
for (int i = 0; i < number; i++)
807808
ccombo.add("fang" + i);
808809
ccombo.setSelection(new Point(0, 5));
809-
assertTrue(":a:", ccombo.getSelection().equals(new Point(0, 0)));
810+
assertEquals(":a:", new Point(0, 0), ccombo.getSelection());
810811
ccombo.setText("some text");
811812
ccombo.setSelection(new Point(0, 5));
812-
assertTrue("Has not been implemented :b:", ccombo.getSelection().equals(new Point(0, 5)));
813+
assertEquals("Has not been implemented :b:", new Point(0, 5), ccombo.getSelection());
813814
}
814815

815816
@Test
@@ -827,21 +828,21 @@ public void test_setTextLjava_lang_String() {
827828
String[] cases = {"", "fang", "fang0"};
828829
for (int i = 0; i < cases.length; i++) {
829830
ccombo.setText(cases[i]);
830-
assertTrue(":a:" + i, ccombo.getText().equals(cases[i]));
831+
assertEquals(":a:" + i, cases[i], ccombo.getText());
831832
}
832833
for (int i = 0; i < 5; i++) {
833834
ccombo.add("fang");
834835
}
835836
for (int i = 0; i < cases.length; i++) {
836837
ccombo.setText(cases[i]);
837-
assertTrue(":b:" + i, ccombo.getText().equals(cases[i]));
838+
assertEquals(":b:" + i, cases[i], ccombo.getText());
838839
}
839840
for (int i = 0; i < 5; i++) {
840841
ccombo.add("fang" + i);
841842
}
842843
for (int i = 0; i < cases.length; i++) {
843844
ccombo.setText(cases[i]);
844-
assertTrue(":c:" + i, ccombo.getText().equals(cases[i]));
845+
assertEquals(":c:" + i, cases[i], ccombo.getText());
845846
}
846847
}
847848

0 commit comments

Comments
 (0)