3636import hudson .slaves .DumbSlave ;
3737import jenkins .security .MasterToSlaveCallable ;
3838
39- import org .junit .After ;
40- import org .junit .Before ;
41- import org .junit .Rule ;
42- import org .junit .Test ;
39+ import org .junit .jupiter . api . AfterEach ;
40+ import org .junit .jupiter . api . BeforeEach ;
41+ import org .junit .jupiter . api . Disabled ;
42+ import org .junit .jupiter . api . Test ;
4343import org .jvnet .hudson .test .JenkinsRule ;
44+ import org .jvnet .hudson .test .junit .jupiter .WithJenkins ;
4445
46+ import java .io .Serial ;
4547import java .lang .reflect .InvocationTargetException ;
4648import java .lang .reflect .Proxy ;
4749import java .util .Collections ;
5052import java .util .logging .Logger ;
5153
5254import static org .hamcrest .CoreMatchers .is ;
53- import static org .junit .Assert .assertNotNull ;
5455import static org .hamcrest .MatcherAssert .assertThat ;
56+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
5557
56- public class TrileadSSHPasswordAuthenticatorTest {
58+ @ WithJenkins
59+ class TrileadSSHPasswordAuthenticatorTest {
5760
5861 private Connection connection ;
5962 private StandardUsernamePasswordCredentials user ;
6063 private Object sshd ;
6164
62- @ Rule public JenkinsRule r = new JenkinsRule ();
63-
64- @ After
65- public void tearDown () {
65+ private JenkinsRule r ;
66+
67+ @ BeforeEach
68+ void setUp (JenkinsRule rule ) {
69+ r = rule ;
70+ user = (StandardUsernamePasswordCredentials ) Items .XSTREAM .fromXML (Items .XSTREAM .toXML (new BasicSSHUserPassword (CredentialsScope .SYSTEM , null , "foobar" , "foomanchu" , null )));
71+ }
72+
73+ @ AfterEach
74+ void tearDown () {
6675 if (connection != null ) {
6776 connection .close ();
6877 connection = null ;
6978 }
70- if (sshd != null ) {
79+ if (sshd != null ) {
7180 try {
7281 invoke (sshd , "stop" , new Class <?>[] {Boolean .TYPE }, new Object [] {true });
7382 } catch (Throwable t ) {
@@ -76,9 +85,10 @@ public void tearDown() {
7685 }
7786 }
7887
79- // disabled as Apache MINA sshd does not provide easy mech for giving a Keyboard Interactive authenticator
80- // so this test relies on having a local sshd which is keyboard interactive only
81- public void dontTestKeyboardInteractive () throws Exception {
88+ @ Test
89+ @ Disabled ("Apache MINA sshd does not provide easy mech for giving a Keyboard Interactive authenticator " +
90+ "so this test relies on having a local sshd which is keyboard interactive only" )
91+ void dontTestKeyboardInteractive () throws Exception {
8292 connection = new Connection ("localhost" );
8393 connection .connect (new NoVerifier ());
8494 TrileadSSHPasswordAuthenticator instance =
@@ -91,13 +101,8 @@ public void dontTestKeyboardInteractive() throws Exception {
91101 assertThat (instance .isAuthenticated (), is (true ));
92102 }
93103
94- @ Before
95- public void setUp () {
96- user =(StandardUsernamePasswordCredentials ) Items .XSTREAM .fromXML (Items .XSTREAM .toXML (new BasicSSHUserPassword (CredentialsScope .SYSTEM , null , "foobar" , "foomanchu" , null )));
97- }
98-
99104 @ Test
100- public void testPassword () throws Exception {
105+ void testPassword () throws Exception {
101106 sshd = createPasswordAuthenticatedSshServer ();
102107 invoke (sshd , "start" , null , null );
103108 int port = (Integer )invoke (sshd , "getPort" , null , null );
@@ -111,11 +116,11 @@ public void testPassword() throws Exception {
111116 assertThat (instance .isAuthenticated (), is (true ));
112117 }
113118
114- private Object createPasswordAuthenticatedSshServer () throws InvocationTargetException , NoSuchMethodException , ClassNotFoundException , InstantiationException , IllegalAccessException {
119+ private Object createPasswordAuthenticatedSshServer () throws Exception {
115120 return createPasswordAuthenticatedSshServer (null );
116121 }
117122
118- private Object createPasswordAuthenticatedSshServer (final String username ) throws NoSuchMethodException , IllegalAccessException , InvocationTargetException , ClassNotFoundException , InstantiationException {
123+ private Object createPasswordAuthenticatedSshServer (final String username ) throws Exception {
119124 Object sshd = newDefaultSshServer ();
120125 Class <?> keyPairProviderClass = newKeyPairProviderClass ();
121126 Object provider = newProvider ();
@@ -132,7 +137,7 @@ private Object createPasswordAuthenticatedSshServer(final String username) throw
132137 }
133138
134139 @ Test
135- public void testFactory () throws Exception {
140+ void testFactory () throws Exception {
136141 sshd = createPasswordAuthenticatedSshServer ();
137142 invoke (sshd , "start" , null , null );
138143 int port = (Integer )invoke (sshd , "getPort" , null , null );
@@ -146,7 +151,7 @@ public void testFactory() throws Exception {
146151 }
147152
148153 @ Test
149- public void testFactoryAltUsername () throws Exception {
154+ void testFactoryAltUsername () throws Exception {
150155 sshd = createPasswordAuthenticatedSshServer ("bill" );
151156 invoke (sshd , "start" , null , null );
152157 int port = (Integer )invoke (sshd , "getPort" , null , null );
@@ -170,7 +175,7 @@ public void testFactoryAltUsername() throws Exception {
170175 * Brings the {@link SSHAuthenticatorFactory} to a slave.
171176 */
172177 @ Test
173- public void testSlave () throws Exception {
178+ void testSlave () throws Exception {
174179 Object sshd = createPasswordAuthenticatedSshServer ();
175180 invoke (sshd , "start" , null , null );
176181
@@ -216,15 +221,16 @@ public Void call() throws Exception {
216221 return null ;
217222 }
218223
224+ @ Serial
219225 private static final long serialVersionUID = 1L ;
220226 }
221227
222- private Object invoke (Object target , String methodName , Class <?>[] parameterTypes , Object [] args ) throws NoSuchMethodException , InvocationTargetException , IllegalAccessException {
228+ private Object invoke (Object target , String methodName , Class <?>[] parameterTypes , Object [] args ) throws Exception {
223229 return target .getClass ().getMethod (methodName , parameterTypes ).invoke (target , args );
224230 }
225231
226- private Object newDefaultSshServer () throws ClassNotFoundException , NoSuchMethodException , InvocationTargetException , IllegalAccessException {
227- Object server = null ;
232+ private Object newDefaultSshServer () throws Exception {
233+ Object server ;
228234 Class <?> serverClass ;
229235 try {
230236 serverClass = Class .forName ("org.apache.sshd.SshServer" );
@@ -238,7 +244,7 @@ private Object newDefaultSshServer() throws ClassNotFoundException, NoSuchMethod
238244 return server ;
239245 }
240246
241- private Class <?> newKeyPairProviderClass () throws ClassNotFoundException {
247+ private Class <?> newKeyPairProviderClass () throws Exception {
242248 Class <?> keyPairProviderClass ;
243249 try {
244250 keyPairProviderClass = Class .forName ("org.apache.sshd.common.KeyPairProvider" );
@@ -249,15 +255,15 @@ private Class<?> newKeyPairProviderClass() throws ClassNotFoundException {
249255 return keyPairProviderClass ;
250256 }
251257
252- private Object newProvider () throws ClassNotFoundException , NoSuchMethodException , IllegalAccessException , InvocationTargetException , InstantiationException {
258+ private Object newProvider () throws Exception {
253259 Class <?> providerClass = Class .forName ("org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider" );
254260 Object provider = providerClass .getConstructor ().newInstance ();
255261 assertNotNull (provider );
256262
257263 return provider ;
258264 }
259265
260- private Class <?> newAuthenticatorClass () throws ClassNotFoundException {
266+ private Class <?> newAuthenticatorClass () throws Exception {
261267 Class <?> authenticatorClass ;
262268 try {
263269 authenticatorClass = Class .forName ("org.apache.sshd.server.auth.password.PasswordAuthenticator" );
@@ -268,7 +274,7 @@ private Class<?> newAuthenticatorClass() throws ClassNotFoundException {
268274 return authenticatorClass ;
269275 }
270276
271- private Object newAuthenticator (Class <?> authenticatorClass , final String userName ) throws IllegalArgumentException {
277+ private Object newAuthenticator (Class <?> authenticatorClass , final String userName ) {
272278 Object authenticator = Proxy .newProxyInstance (
273279 authenticatorClass .getClassLoader (), new Class <?>[]{authenticatorClass }, (proxy , method , args ) ->
274280 method .getName ().equals ("authenticate" ) ?
@@ -278,8 +284,8 @@ private Object newAuthenticator(Class<?> authenticatorClass, final String userNa
278284 return authenticator ;
279285 }
280286
281- private Object newFactory () throws ClassNotFoundException , NoSuchMethodException , IllegalAccessException , InvocationTargetException , InstantiationException {
282- Object factory = null ;
287+ private Object newFactory () throws Exception {
288+ Object factory ;
283289 Class <?> factoryClass ;
284290 try {
285291 factoryClass = Class .forName ("org.apache.sshd.server.auth.UserAuthPassword$Factory" );
0 commit comments