|
1 | 1 | /*******************************************************************************
|
2 |
| - * Copyright (c) 2000, 2015 IBM Corporation and others. |
| 2 | + * Copyright (c) 2000, 2025 IBM Corporation and others. |
3 | 3 | *
|
4 | 4 | * This program and the accompanying materials
|
5 | 5 | * are made available under the terms of the Eclipse Public License 2.0
|
|
14 | 14 | *******************************************************************************/
|
15 | 15 | package org.eclipse.swt.tests.junit;
|
16 | 16 |
|
17 |
| -import static org.junit.Assert.assertEquals; |
18 |
| -import static org.junit.Assert.assertNotNull; |
19 |
| -import static org.junit.Assert.assertTrue; |
20 |
| -import static org.junit.Assert.fail; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | +import static org.junit.jupiter.api.Assertions.fail; |
21 | 22 |
|
22 | 23 | import java.nio.file.Paths;
|
23 | 24 | import java.security.CodeSigner;
|
|
29 | 30 | import org.eclipse.swt.SWT;
|
30 | 31 | import org.eclipse.swt.SWTError;
|
31 | 32 | import org.eclipse.swt.SWTException;
|
32 |
| -import org.junit.Assume; |
33 |
| -import org.junit.Test; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; |
34 | 35 |
|
35 | 36 | /**
|
36 |
| - * Automated Test Suite for class org.eclipse.swt.SWT |
37 |
| - * |
38 |
| - * @see org.eclipse.swt.SWT |
| 37 | + * Automated Test Suite for class {@link org.eclipse.swt.SWT}. |
39 | 38 | */
|
40 | 39 | public class Test_org_eclipse_swt_SWT {
|
41 | 40 |
|
42 |
| -@Test |
43 |
| -public void test_Constructor() { |
44 |
| - // Do nothing. Class SWT is not intended to be subclassed. |
45 |
| -} |
| 41 | + @Test |
| 42 | + public void test_errorI() { |
| 43 | + // Test that we throw the expected kinds of errors for the given error types. |
| 44 | + assertThrows(IllegalArgumentException.class, () -> SWT.error(SWT.ERROR_NULL_ARGUMENT), |
| 45 | + "did not correctly throw exception for ERROR_NULL_ARGUMENT"); |
| 46 | + assertThrows(SWTException.class, () -> SWT.error(SWT.ERROR_FAILED_EXEC), |
| 47 | + "did not correctly throw exception for ERROR_FAILED_EXEC"); |
| 48 | + assertThrows(SWTError.class, () -> SWT.error(SWT.ERROR_NO_HANDLES), |
| 49 | + "did not correctly throw exception for ERROR_NO_HANDLES"); |
| 50 | + assertThrows(SWTError.class, () -> SWT.error(-1), |
| 51 | + "did not correctly throw exception for error(-1)"); |
| 52 | + } |
46 | 53 |
|
47 |
| -@Test |
48 |
| -public void test_errorI() { |
49 |
| - // Test that we throw the expected kinds of errors for the given error types. |
50 |
| - boolean passed = false; |
51 |
| - try { |
52 |
| - SWT.error(SWT.ERROR_NULL_ARGUMENT); |
53 |
| - } catch (IllegalArgumentException ex) { |
54 |
| - passed = true; |
55 |
| - } catch (Throwable t) { } |
56 |
| - assertTrue ("did not correctly throw exception for ERROR_NULL_ARGUMENT", passed); |
57 |
| - passed = false; |
58 |
| - try { |
59 |
| - SWT.error(SWT.ERROR_FAILED_EXEC); |
60 |
| - } catch (SWTException ex) { |
61 |
| - passed = true; |
62 |
| - } catch (Throwable t) { } |
63 |
| - assertTrue ("did not correctly throw exception for ERROR_FAILED_EXEC", passed); |
64 |
| - passed = false; |
65 |
| - try { |
66 |
| - SWT.error(SWT.ERROR_NO_HANDLES); |
67 |
| - } catch (SWTError ex) { |
68 |
| - passed = true; |
69 |
| - } catch (Throwable t) { } |
70 |
| - assertTrue ("did not correctly throw exception for ERROR_NO_HANDLES", passed); |
71 |
| - passed = false; |
72 |
| - try { |
73 |
| - SWT.error(-1); |
74 |
| - } catch (SWTError ex) { |
75 |
| - passed = true; |
76 |
| - } catch (Throwable t) { } |
77 |
| - assertTrue ("did not correctly throw exception for error(-1)", passed); |
78 |
| -} |
| 54 | + @Test |
| 55 | + public void test_errorILjava_lang_Throwable() { |
| 56 | + // Test that the causing throwable is filled in. |
| 57 | + Throwable cause = new RuntimeException("Just for testing"); |
| 58 | + SWTException e1 = assertThrows(SWTException.class, () -> SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT, cause)); |
| 59 | + |
| 60 | + assertTrue(e1.throwable == cause, "did not correctly throw exception for ERROR_UNSUPPORTED_FORMAT"); |
| 61 | + SWTError e2 = assertThrows(SWTError.class, () -> SWT.error(SWT.ERROR_NOT_IMPLEMENTED, cause)); |
| 62 | + assertTrue(e2.throwable == cause, "did not correctly throw exception for ERROR_NOT_IMPLEMENTED"); |
| 63 | + SWTError e3 = assertThrows(SWTError.class, () -> SWT.error(-1, cause)); |
| 64 | + assertTrue(e3.throwable == cause, "did not correctly throw exception for error(-1)"); |
| 65 | + } |
79 | 66 |
|
80 |
| -@Test |
81 |
| -public void test_errorILjava_lang_Throwable() { |
82 |
| - // Test that the causing throwable is filled in. |
83 |
| - Throwable cause = new RuntimeException("Just for testing"); |
84 |
| - boolean passed = false; |
85 |
| - try { |
86 |
| - SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT, cause); |
87 |
| - } catch (SWTException ex) { |
88 |
| - passed = ex.throwable == cause; |
89 |
| - } catch (Throwable t) { } |
90 |
| - assertTrue ("did not correctly throw exception for ERROR_UNSUPPORTED_FORMAT", passed); |
91 |
| - passed = false; |
92 |
| - try { |
93 |
| - SWT.error(SWT.ERROR_NOT_IMPLEMENTED, cause); |
94 |
| - } catch (SWTError ex) { |
95 |
| - passed = ex.throwable == cause; |
96 |
| - } catch (Throwable t) { } |
97 |
| - assertTrue ("did not correctly throw exception for ERROR_NOT_IMPLEMENTED", passed); |
98 |
| - passed = false; |
99 |
| - try { |
100 |
| - SWT.error(-1, cause); |
101 |
| - } catch (SWTError ex) { |
102 |
| - passed = ex.throwable == cause; |
103 |
| - } catch (Throwable t) { } |
104 |
| - assertTrue ("did not correctly throw exception for error(-1)", passed); |
105 |
| -} |
| 67 | + @Test |
| 68 | + public void test_getMessageLjava_lang_String() { |
| 69 | + assertThrows(IllegalArgumentException.class, () -> SWT.getMessage(null), |
| 70 | + "did not correctly throw exception with null argument"); |
| 71 | + assertNotNull(SWT.getMessage("SWT_Yes")); |
| 72 | + assertEquals( |
| 73 | + "_NOT_FOUND_IN_PROPERTIES_", SWT.getMessage("_NOT_FOUND_IN_PROPERTIES_"), |
| 74 | + "invalid key did not return as itself"); |
106 | 75 |
|
107 |
| -@Test |
108 |
| -public void test_getMessageLjava_lang_String() { |
109 |
| - boolean passed = false; |
110 |
| - try { |
111 |
| - passed = false; |
112 |
| - SWT.getMessage(null); |
113 |
| - } catch (IllegalArgumentException ex) { |
114 |
| - passed = true; |
115 | 76 | }
|
116 |
| - assertTrue ("did not correctly throw exception with null argument", passed); |
117 |
| - try { |
118 |
| - SWT.getMessage("SWT_Yes"); |
119 |
| - } catch (Throwable t) { |
120 |
| - fail ("exception " + t + " generated for SWT_Yes"); |
121 |
| - } |
122 |
| - assertEquals ( |
123 |
| - "invalid key did not return as itself", |
124 |
| - "_NOT_FOUND_IN_PROPERTIES_", SWT.getMessage("_NOT_FOUND_IN_PROPERTIES_")); |
125 | 77 |
|
126 |
| -} |
| 78 | + private String pathFromClass(Class<?> classValue) { |
| 79 | + String filePrefix = SwtTestUtil.isWindows ? "file:/" : "file:"; |
127 | 80 |
|
128 |
| -private String pathFromClass(Class<?> classValue) { |
129 |
| - String filePrefix = SwtTestUtil.isWindows ? "file:/" : "file:"; |
| 81 | + String url = classValue.getProtectionDomain().getCodeSource().getLocation().toString(); |
| 82 | + assertTrue(url.startsWith(filePrefix)); |
130 | 83 |
|
131 |
| - String url = classValue.getProtectionDomain().getCodeSource().getLocation().toString(); |
132 |
| - assertTrue(url.startsWith(filePrefix)); |
| 84 | + String urlPath = url.substring(filePrefix.length()); |
| 85 | + String path = Paths.get(urlPath).toAbsolutePath().toString(); |
133 | 86 |
|
134 |
| - String urlPath = url.substring(filePrefix.length()); |
135 |
| - String path = Paths.get(urlPath).toAbsolutePath().toString(); |
| 87 | + // On Windows, use / for consistency |
| 88 | + return path.replace('\\', '/'); |
| 89 | + } |
136 | 90 |
|
137 |
| - // On Windows, use / for consistency |
138 |
| - return path.replace('\\', '/'); |
139 |
| -} |
| 91 | + private List<String> signersFromClass(Class<?> classValue) { |
| 92 | + List<String> result = new ArrayList<>(); |
140 | 93 |
|
141 |
| -private List<String> signersFromClass(Class<?> classValue) { |
142 |
| - List<String> result = new ArrayList<>(); |
| 94 | + CodeSigner[] signers = classValue.getProtectionDomain().getCodeSource().getCodeSigners(); |
| 95 | + if (signers == null) { |
| 96 | + return result; |
| 97 | + } |
143 | 98 |
|
144 |
| - CodeSigner[] signers = classValue.getProtectionDomain().getCodeSource().getCodeSigners(); |
145 |
| - if (signers == null) { |
146 |
| - return result; |
147 |
| - } |
| 99 | + for (CodeSigner signer : signers) { |
| 100 | + Certificate cert = signer.getSignerCertPath().getCertificates().get(0); |
| 101 | + if (!(cert instanceof X509Certificate x509cert)) { |
| 102 | + continue; |
| 103 | + } |
148 | 104 |
|
149 |
| - for (CodeSigner signer : signers) { |
150 |
| - Certificate cert = signer.getSignerCertPath().getCertificates().get(0); |
151 |
| - if (!(cert instanceof X509Certificate)) { |
152 |
| - continue; |
| 105 | + result.add(x509cert.getSubjectX500Principal().getName()); |
153 | 106 | }
|
154 | 107 |
|
155 |
| - X509Certificate x509cert = (X509Certificate)cert; |
156 |
| - result.add(x509cert.getSubjectX500Principal().getName()); |
| 108 | + return result; |
157 | 109 | }
|
158 | 110 |
|
159 |
| - return result; |
160 |
| -} |
| 111 | + /** |
| 112 | + * Ensure that SWT being tested was built together with the tests. This |
| 113 | + * is a test designed for build scripts, such as the one that checks |
| 114 | + * GitHub Pull Requests. |
| 115 | + */ |
| 116 | + @Test |
| 117 | + @EnabledIfSystemProperty(named = "org.eclipse.swt.tests.junit.disable.test_isLocal", matches = "true") |
| 118 | + public void test_isLocal() { |
| 119 | + String swtPath = pathFromClass(SWT.class); |
| 120 | + String tstPath = pathFromClass(Test_org_eclipse_swt_SWT.class); |
| 121 | + List<String> swtSigners = signersFromClass(SWT.class); |
| 122 | + |
| 123 | + // If SWT is signed by Eclipse, that's a good sign that it wasn't built locally |
| 124 | + for (String signer : swtSigners) { |
| 125 | + if (signer.toLowerCase().contains("cn=eclipse.org")) { |
| 126 | + fail("Tested SWT is signed by Eclipse: (SWT='" + swtPath + "' signer='" + signer + "')"); |
| 127 | + } |
| 128 | + } |
161 | 129 |
|
162 |
| -/** |
163 |
| - * Ensure that SWT being tested was built together with the tests. This |
164 |
| - * is a test designed for build scripts, such as the one that checks |
165 |
| - * GitHub Pull Requests. |
166 |
| - */ |
167 |
| -@Test |
168 |
| -public void test_isLocal() { |
169 |
| - // If you change default to NO, make sure that this test runs on GitHub |
170 |
| - Assume.assumeFalse(Boolean.getBoolean("org.eclipse.swt.tests.junit.disable.test_isLocal")); |
171 |
| - |
172 |
| - String swtPath = pathFromClass(SWT.class); |
173 |
| - String tstPath = pathFromClass(Test_org_eclipse_swt_SWT.class); |
174 |
| - List<String> swtSigners = signersFromClass(SWT.class); |
175 |
| - |
176 |
| - // If SWT is signed by Eclipse, that's a good sign that it wasn't built locally |
177 |
| - for (String signer : swtSigners) { |
178 |
| - if (signer.toLowerCase().contains("cn=eclipse.org")) { |
179 |
| - fail("Tested SWT is signed by Eclipse: (SWT='" + swtPath + "' signer='" + signer + "')"); |
| 130 | + // It's hard to devise a condition that would hold for all possible |
| 131 | + // locations of compiled classes. For example, my own project uses |
| 132 | + // output directories different from what Eclipse does by default. |
| 133 | + |
| 134 | + // SWT shouldn't be published in maven. Note that SWT's local build |
| 135 | + // may still be cached in maven. We observed the following paths: |
| 136 | + // GitHub, bad: |
| 137 | + // $HOME/.m2/repository/p2/osgi/bundle/org.eclipse.swt.gtk.linux.x86_64/3.121.0.v20220701-1002/org.eclipse.swt.gtk.linux.x86_64-3.121.0.v20220701-1002.jar |
| 138 | + // Jenkins, good: |
| 139 | + // $HOME/.m2/repository/org/eclipse/swt/org.eclipse.swt.gtk.linux.x86_64/3.121.0-SNAPSHOT/org.eclipse.swt.gtk.linux.x86_64-3.121.0-SNAPSHOT.jar |
| 140 | + // The difference here is '/p2/osgi'. |
| 141 | + if (swtPath.contains("/.m2/repository/p2/osgi/")) { |
| 142 | + fail("Tested SWT path is on maven OSGI: (SWT='" + swtPath + "' Test='" + tstPath + "'"); |
180 | 143 | }
|
181 | 144 | }
|
182 | 145 |
|
183 |
| - // It's hard to devise a condition that would hold for all possible |
184 |
| - // locations of compiled classes. For example, my own project uses |
185 |
| - // output directories different from what Eclipse does by default. |
186 |
| - |
187 |
| - // SWT shouldn't be published in maven. Note that SWT's local build |
188 |
| - // may still be cached in maven. We observed the following paths: |
189 |
| - // GitHub, bad: $HOME/.m2/repository/p2/osgi/bundle/org.eclipse.swt.gtk.linux.x86_64/3.121.0.v20220701-1002/org.eclipse.swt.gtk.linux.x86_64-3.121.0.v20220701-1002.jar |
190 |
| - // Jenkins, good: $HOME/.m2/repository/org/eclipse/swt/org.eclipse.swt.gtk.linux.x86_64/3.121.0-SNAPSHOT/org.eclipse.swt.gtk.linux.x86_64-3.121.0-SNAPSHOT.jar |
191 |
| - // The difference here is '/p2/osgi'. |
192 |
| - if (swtPath.contains("/.m2/repository/p2/osgi/")) { |
193 |
| - fail("Tested SWT path is on maven OSGI: (SWT='" + swtPath + "' Test='" + tstPath + "'"); |
| 146 | + @Test |
| 147 | + public void test_getPlatform() { |
| 148 | + // Can't test the list of platforms, since this may change, |
| 149 | + // so just test to see it returns something. |
| 150 | + assertNotNull(SWT.getPlatform(), "returned null platform name"); |
194 | 151 | }
|
195 |
| -} |
196 |
| - |
197 |
| -@Test |
198 |
| -public void test_getPlatform() { |
199 |
| - // Can't test the list of platforms, since this may change, |
200 |
| - // so just test to see it returns something. |
201 |
| - assertNotNull ("returned null platform name", SWT.getPlatform()); |
202 |
| -} |
203 | 152 |
|
204 |
| -@Test |
205 |
| -public void test_getVersion() { |
206 |
| - // Test that the version number which is returned is reasonable. |
207 |
| - int ver = SWT.getVersion(); |
208 |
| - assertTrue ("unreasonable value returned", ver > 0 && ver < 1000000); |
209 |
| - System.out.println("SWT.getVersion(): " + ver); |
210 |
| -} |
| 153 | + @Test |
| 154 | + public void test_getVersion() { |
| 155 | + // Test that the version number which is returned is reasonable. |
| 156 | + int ver = SWT.getVersion(); |
| 157 | + assertTrue(ver > 0 && ver < 1000000, "unreasonable value returned:" + ver); |
| 158 | + } |
211 | 159 | }
|
0 commit comments