Skip to content

Commit

Permalink
Replace assertion and use lambda (#270)
Browse files Browse the repository at this point in the history
* Replace assertion

* Use lambdas
  • Loading branch information
offa committed Feb 26, 2022
1 parent e0fde81 commit ecacc09
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 87 deletions.
104 changes: 51 additions & 53 deletions src/main/java/hudson/plugins/sshslaves/SSHLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -419,60 +419,58 @@ public void launch(final SlaveComputer computer, final TaskListener listener) th
launcherExecutorService = Executors.newSingleThreadExecutor(
new NamingThreadFactory(Executors.defaultThreadFactory(), "SSHLauncher.launch for '" + computer.getName() + "' node"));
Set<Callable<Boolean>> callables = new HashSet<>();
callables.add(new Callable<Boolean>() {
public Boolean call() throws InterruptedException {
Boolean rval = Boolean.FALSE;
try {
String[] preferredKeyAlgorithms = getSshHostKeyVerificationStrategyDefaulted().getPreferredKeyAlgorithms(computer);
if (preferredKeyAlgorithms != null && preferredKeyAlgorithms.length > 0) { // JENKINS-44832
connection.setServerHostKeyAlgorithms(preferredKeyAlgorithms);
} else {
listener.getLogger().println("Warning: no key algorithms provided; JENKINS-42959 disabled");
}

listener.getLogger().println(logConfiguration());

openConnection(listener, computer);

verifyNoHeaderJunk(listener);
reportEnvironment(listener);

final String workingDirectory = getWorkingDirectory(computer);
if (workingDirectory == null) {
listener.error("Cannot get the working directory for " + computer);
return Boolean.FALSE;
}

String java = null;
if (StringUtils.isNotBlank(javaPath)) {
java = expandExpression(computer, javaPath);
} else {
checkJavaIsInPath(listener);
//FIXME deprecated on 2020-12-10, it will removed after 2021-09-01
JavaVersionChecker javaVersionChecker = new JavaVersionChecker(computer, listener, getJvmOptions(),
connection);
java = javaVersionChecker.resolveJava();
}

copyAgentJar(listener, workingDirectory);

startAgent(computer, listener, java, workingDirectory);

PluginImpl.register(connection);
rval = Boolean.TRUE;
} catch (RuntimeException|Error e) {
String msg = Messages.SSHLauncher_UnexpectedError();
if(StringUtils.isNotBlank(e.getMessage())){
msg = e.getMessage();
}
e.printStackTrace(listener.error(msg));
} catch (AbortException e) {
listener.getLogger().println(e.getMessage());
} catch (IOException e) {
e.printStackTrace(listener.getLogger());
} finally {
return rval;
callables.add(() -> {
Boolean rval = Boolean.FALSE;
try {
String[] preferredKeyAlgorithms = getSshHostKeyVerificationStrategyDefaulted().getPreferredKeyAlgorithms(computer);
if (preferredKeyAlgorithms != null && preferredKeyAlgorithms.length > 0) { // JENKINS-44832
connection.setServerHostKeyAlgorithms(preferredKeyAlgorithms);
} else {
listener.getLogger().println("Warning: no key algorithms provided; JENKINS-42959 disabled");
}

listener.getLogger().println(logConfiguration());

openConnection(listener, computer);

verifyNoHeaderJunk(listener);
reportEnvironment(listener);

final String workingDirectory = getWorkingDirectory(computer);
if (workingDirectory == null) {
listener.error("Cannot get the working directory for " + computer);
return Boolean.FALSE;
}

String java = null;
if (StringUtils.isNotBlank(javaPath)) {
java = expandExpression(computer, javaPath);
} else {
checkJavaIsInPath(listener);
//FIXME deprecated on 2020-12-10, it will removed after 2021-09-01
JavaVersionChecker javaVersionChecker = new JavaVersionChecker(computer, listener, getJvmOptions(),
connection);
java = javaVersionChecker.resolveJava();
}

copyAgentJar(listener, workingDirectory);

startAgent(computer, listener, java, workingDirectory);

PluginImpl.register(connection);
rval = Boolean.TRUE;
} catch (RuntimeException|Error e) {
String msg = Messages.SSHLauncher_UnexpectedError();
if(StringUtils.isNotBlank(e.getMessage())){
msg = e.getMessage();
}
e.printStackTrace(listener.error(msg));
} catch (AbortException e) {
listener.getLogger().println(e.getMessage());
} catch (IOException e) {
e.printStackTrace(listener.getLogger());
} finally {
return rval;
}
});

Expand Down
12 changes: 4 additions & 8 deletions src/test/java/hudson/plugins/sshslaves/SSHLauncherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -154,12 +155,7 @@ private static boolean checkSupported(final String testVersionOutput) throws IOE
}

private static void assertNotSupported(final String testVersionOutput) throws AssertionError {
try {
checkSupported(testVersionOutput);
fail("Expected version " + testVersionOutput + " to be not supported, but it is supported");
} catch (IOException e) {
// expected
}
assertThrows(IOException.class, () -> checkSupported(testVersionOutput));
}

private void checkRoundTrip(String host) throws Exception {
Expand Down Expand Up @@ -400,7 +396,7 @@ public void getMd5Hash() {
try {
byte[] bytes = "Leave me alone!".getBytes();
String result = SSHLauncher.getMd5Hash(bytes);
assertTrue("1EB226C8E950BAC1494BE197E84A264C".equals(result));
assertEquals("1EB226C8E950BAC1494BE197E84A264C", result);
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -420,7 +416,7 @@ public void readInputStreamIntoByteArrayAndClose() {
byte[] bytes = SSHLauncher.readInputStreamIntoByteArrayAndClose(inputStream);
assertNotNull(bytes);
assertTrue(bytes.length > 0);
assertTrue("Don't change me or add newlines!".equals(new String(bytes)));
assertEquals("Don't change me or add newlines!", new String(bytes));

} catch (Exception e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,25 +187,21 @@ private Object newCommandFactory(Class commandFactoryClass) throws ClassNotFound
return java.lang.reflect.Proxy.newProxyInstance(
commandFactoryClass.getClassLoader(),
new java.lang.Class[]{commandFactoryClass},
new java.lang.reflect.InvocationHandler() {
(proxy, method, args) -> {

@Override
public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws java.lang.Throwable {
if (method.getName().equals("createCommand")) {
Class commandClass;
try {
commandClass = Class.forName("org.apache.sshd.server.command.UnknownCommand");
} catch (ClassNotFoundException e) {
commandClass = Class.forName("org.apache.sshd.server.scp.UnknownCommand");
}

if (method.getName().equals("createCommand")) {
Class commandClass;
try {
commandClass = Class.forName("org.apache.sshd.server.command.UnknownCommand");
} catch (ClassNotFoundException e) {
commandClass = Class.forName("org.apache.sshd.server.scp.UnknownCommand");
}
return commandClass.getConstructor(String.class).newInstance(args[0]);
}

return commandClass.getConstructor(String.class).newInstance(args[0]);
}

return null;
}
});
return null;
});
}

private Class newCommandAuthenticatorClass() throws ClassNotFoundException {
Expand All @@ -223,18 +219,14 @@ private Object newAuthenticator(Class passwordAuthenticatorClass) throws ClassNo
return java.lang.reflect.Proxy.newProxyInstance(
passwordAuthenticatorClass.getClassLoader(),
new java.lang.Class[]{passwordAuthenticatorClass},
new java.lang.reflect.InvocationHandler() {

@Override
public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws java.lang.Throwable {
(proxy, method, args) -> {

if (method.getName().equals("authenticate")) {
return Boolean.TRUE;
}
if (method.getName().equals("authenticate")) {
return Boolean.TRUE;
}

return null;
}
});
return null;
});
}

private Object invoke(Object target, String methodName, Class[] parameterTypes, Object[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Expand Down

0 comments on commit ecacc09

Please sign in to comment.