Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CommandRunner and JvmCommandRunner in SystemCommandTasklet
This improves the testability of `SystemCommandTasklet` when the target command is not available during test execution. Resolves #3955
- Loading branch information
1 parent
6fbbeb8
commit eccea80
Showing
5 changed files
with
214 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...g-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/CommandRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2006-2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.batch.core.step.tasklet; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Interface for executing commands. This abstraction is only | ||
* useful in order to allow classes that make {@link Runtime#exec} calls | ||
* to be testable, since the invoked command might not be | ||
* available during tests execution. | ||
* | ||
* @author Stefano Cordio | ||
* @since FIXME | ||
*/ | ||
public interface CommandRunner { | ||
|
||
/** | ||
* Executes the specified string command in a separate process with the | ||
* specified environment and working directory. | ||
* | ||
* @param command a specified system command. | ||
* | ||
* @param envp array of strings, each element of which | ||
* has environment variable settings in the format | ||
* <i>name</i>=<i>value</i>, or | ||
* {@code null} if the subprocess should inherit | ||
* the environment of the current process. | ||
* | ||
* @param dir the working directory of the subprocess, or | ||
* {@code null} if the subprocess should inherit | ||
* the working directory of the current process. | ||
* | ||
* @return A new {@link Process} object for managing the subprocess | ||
* | ||
* @throws SecurityException | ||
* If a security manager exists and its | ||
* {@link SecurityManager#checkExec checkExec} | ||
* method doesn't allow creation of the subprocess | ||
* | ||
* @throws IOException | ||
* If an I/O error occurs | ||
* | ||
* @throws NullPointerException | ||
* If {@code command} is {@code null}, | ||
* or one of the elements of {@code envp} is {@code null} | ||
* | ||
* @throws IllegalArgumentException | ||
* If {@code command} is empty | ||
* | ||
* @see Runtime#exec(String, String[], File) | ||
*/ | ||
Process exec(String command, String[] envp, File dir) throws IOException; | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
...atch-core/src/main/java/org/springframework/batch/core/step/tasklet/JvmCommandRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2006-2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.batch.core.step.tasklet; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Implementation of the {@link CommandRunner} interface that calls the standard | ||
* {@link Runtime#exec} method. It should be noted that there is no unit tests for | ||
* this class, since there is only one line of actual code, that would only be | ||
* testable by mocking {@link Runtime}. | ||
* | ||
* @author Stefano Cordio | ||
* @since FIXME | ||
*/ | ||
public class JvmCommandRunner implements CommandRunner { | ||
|
||
/** | ||
* Delegate call to {@link Runtime#exec} with the arguments provided. | ||
* | ||
* @see CommandRunner#exec(String, String[], File) | ||
*/ | ||
@Override | ||
public Process exec(String command, String[] envp, File dir) throws IOException { | ||
return Runtime.getRuntime().exec(command, envp, dir); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters