Skip to content

Commit

Permalink
Code cleanup, fixed typos
Browse files Browse the repository at this point in the history
  • Loading branch information
frimtec committed Aug 31, 2020
1 parent 18e66f0 commit 8769e4d
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 39 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.github.frimtec</groupId>
<artifactId>jpse</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>

<name>jpse</name>
<description>
Expand Down
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ API to easily execute PowerShell commands and scripts from Java.
## Example
Call PowerShell commands or scripts like this:
```java
PowerShellExecutor executor = PowerShellExecutor.instance(null);
PowerShellExecutor executor = PowerShellExecutor.instance();

System.out.println("Execute command: ");
String output = executor.execute("Write-Host Hello PowerShell!").getStandartOutput();
String output = executor.execute("Write-Host Hello PowerShell!").getStandardOutput();
System.out.println(" output = " + output);

Map<String, String> arguments = Collections.singletonMap("name", "PowerShell");

System.out.println("Execute script as file: ");
output = executor.execute(Paths.get(".\\src\\test\\resources\\test.ps1"), arguments).getStandartOutput();
output = executor.execute(Paths.get(".\\src\\test\\resources\\test.ps1"), arguments).getStandardOutput();
System.out.println(" output = " + output);

System.out.println("Execute script from classpath: ");
output = executor.execute(PowerShellTestApplication.class.getResourceAsStream("/test.ps1"), arguments).getStandartOutput();
output = executor.execute(PowerShellTestApplication.class.getResourceAsStream("/test.ps1"), arguments).getStandardOutput();
System.out.println(" output = " + output);
```

Expand All @@ -37,6 +37,6 @@ In Maven just add the following dependency to your pom.xml:
<dependency>
<groupId>com.github.frimtec</groupId>
<artifactId>jpse</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
</dependency>
```
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public interface ExecutionResult {
int getReturnCode();

/**
* Returns the executions standart output.
* Returns the executions standard output.
*
* @return standart output
* @return standard output
*/
String getStandartOutput();
String getStandardOutput();

/**
* Returns the executions error output.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public boolean isSuccess() {
}

@Override
public String getStandartOutput() {
public String getStandardOutput() {
return this.result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,17 @@ public interface PowerShellExecutor {
ExecutionResult execute(InputStream script, Map<String, String> arguments);

/**
* Creates a power shell executor.
* Creates a power shell executor using the default temp path to create and execute temporary scripts.
* @return power shell executor
*/
static PowerShellExecutor instance() {
return instance(null);
}

/**
* Creates a power shell executor using the given temp path to create and execute temporary scripts.
*
* @param tempPath path to the temp directory where JPSE can store temporary scripts to be executes or null if default temp directory is fine
* @param tempPath path to the temp directory where JPSE can store temporary scripts to be executed
* @return power shell executor
*/
static PowerShellExecutor instance(Path tempPath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ void getReturnCode() {
}

@Test
void getStandartOutput() {
void getStandardOutput() {
// arrange
ExecutionResult executionResult = new ExecutionResultImpl(2, "OUTPUT", "ERROR");

// act
String output = executionResult.getStandartOutput();
String output = executionResult.getStandardOutput();

// assert
assertThat(output).isEqualTo("OUTPUT");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class PowerShellExecutorTest {

@Test
void instance() {
PowerShellExecutor instance = PowerShellExecutor.instance(null);
PowerShellExecutor instance = PowerShellExecutor.instance();
assertThat(instance).isNotNull();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import com.github.frimtec.libraries.jpse.PowerShellExecutor;
package com.github.frimtec.libraries.jpse;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Map;
Expand All @@ -10,21 +9,21 @@
*/
class PowerShellTestApplication {

public static void main(String[] args) throws IOException {
PowerShellExecutor executor = PowerShellExecutor.instance(null);
public static void main(String[] args) {
PowerShellExecutor executor = PowerShellExecutor.instance();

System.out.println("Execute command: ");
String output = executor.execute("Write-Host Hello PowerShell!").getStandartOutput();
String output = executor.execute("Write-Host Hello PowerShell!").getStandardOutput();
System.out.println(" output = " + output);

Map<String, String> arguments = Collections.singletonMap("name", "PowerShell");

System.out.println("Execute scipt as file: ");
output = executor.execute(Paths.get(".\\src\\test\\resources\\test.ps1"), arguments).getStandartOutput();
System.out.println("Execute script as file: ");
output = executor.execute(Paths.get(".\\src\\test\\resources\\test.ps1"), arguments).getStandardOutput();
System.out.println(" output = " + output);

System.out.println("Execute scipt from classpath: ");
output = executor.execute(PowerShellTestApplication.class.getResourceAsStream("/test.ps1"), arguments).getStandartOutput();
System.out.println("Execute script from classpath: ");
output = executor.execute(PowerShellTestApplication.class.getResourceAsStream("/test.ps1"), arguments).getStandardOutput();
System.out.println(" output = " + output);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -24,30 +23,30 @@ class WindowsPowerShellExecutorTest {
private final PowerShellExecutor executor = this.osWindows ? new WindowsPowerShellExecutor(null) : new WindowsPowerShellExecutor(null, "echo");

@Test
void executeForSuccess() throws IOException {
void executeForSuccess() {
// act
ExecutionResult executionResult = this.executor.execute("Write-Host Hello PowerShell!");

// assert
assertThat(executionResult.isSuccess()).isTrue();
assertThat(executionResult.getStandartOutput()).isEqualTo(this.osWindows ? "Hello PowerShell!" : "-EncodedCommand VwByAGkAdABlAC0ASABvAHMAdAAgAEgAZQBsAGwAbwAgAFAAbwB3AGUAcgBTAGgAZQBsAGwAIQA=");
assertThat(executionResult.getStandardOutput()).isEqualTo(this.osWindows ? "Hello PowerShell!" : "-EncodedCommand VwByAGkAdABlAC0ASABvAHMAdAAgAEgAZQBsAGwAbwAgAFAAbwB3AGUAcgBTAGgAZQBsAGwAIQA=");
}

@Test
@EnabledOnOs(OS.WINDOWS)
void executeForFailure() throws IOException {
void executeForFailure() {
// act
ExecutionResult executionResult = this.executor.execute("Write-Error Problem");

// assert
assertThat(executionResult.isSuccess()).isFalse();
assertThat(executionResult.getReturnCode()).isEqualTo(1);
assertThat(executionResult.getStandartOutput()).isEmpty();
assertThat(executionResult.getStandardOutput()).isEmpty();
assertThat(executionResult.getErrorOutput()).contains("Problem");
}

@Test
void executeForInterruptThrowsException() throws IOException {
void executeForInterruptThrowsException() {
// arrange
Thread mainThread = Thread.currentThread();
new Thread(() -> {
Expand All @@ -59,7 +58,6 @@ void executeForInterruptThrowsException() throws IOException {
mainThread.interrupt();
}).start();

//noinspection MethodDoesntCallSuperMethod
PowerShellExecutor interruptedExecutor = this.osWindows ? this.executor : new WindowsPowerShellExecutor(null, "") {
@Override
protected ProcessBuilder createProcessBuilder(List<String> commandLine) {
Expand All @@ -74,7 +72,7 @@ protected ProcessBuilder createProcessBuilder(List<String> commandLine) {
}

@Test
void executeForBadPowerShellExeThrowsException() throws IOException {
void executeForBadPowerShellExeThrowsException() {
// arrange
PowerShellExecutor errorExecutor = new WindowsPowerShellExecutor(null, "unknown_command.exe");

Expand All @@ -84,7 +82,7 @@ void executeForBadPowerShellExeThrowsException() throws IOException {
}

@Test
void testExecuteForScriptFromFile() throws IOException {
void testExecuteForScriptFromFile() {
// arrange
Map<String, String> arguments = Collections.singletonMap("name", "PowerShell");

Expand All @@ -93,11 +91,11 @@ void testExecuteForScriptFromFile() throws IOException {

// assert
assertThat(executionResult.isSuccess()).isTrue();
assertThat(executionResult.getStandartOutput()).isEqualTo(this.osWindows ? "Hello PowerShell!" : "-File .\\src\\test\\resources\\test.ps1 -name \"PowerShell\"");
assertThat(executionResult.getStandardOutput()).isEqualTo(this.osWindows ? "Hello PowerShell!" : "-File .\\src\\test\\resources\\test.ps1 -name \"PowerShell\"");
}

@Test
void testExecuteForScriptFromClasspath() throws IOException {
void testExecuteForScriptFromClasspath() {
// arrange
Map<String, String> arguments = Collections.singletonMap("name", "PowerShell");

Expand All @@ -107,19 +105,19 @@ void testExecuteForScriptFromClasspath() throws IOException {
// assert
assertThat(executionResult.isSuccess()).isTrue();
if (this.osWindows) {
assertThat(executionResult.getStandartOutput()).isEqualTo("Hello PowerShell!");
assertThat(executionResult.getStandardOutput()).isEqualTo("Hello PowerShell!");
} else {
assertThat(executionResult.getStandartOutput()).matches("^-File /tmp/java-power-shell-(\\d)*.ps1 -name \"PowerShell\"$");
assertThat(executionResult.getStandardOutput()).matches("^-File /tmp/java-power-shell-(\\d)*.ps1 -name \"PowerShell\"$");
}
}

@Test
void createForNonExistingTempFolderExpectsFolderCreated(@TempDir Path tempBaseFolder) throws IOException {
void createForNonExistingTempFolderExpectsFolderCreated(@TempDir Path tempBaseFolder) {
// arrange
Path tempPath = tempBaseFolder.resolve("jpse");

// act
PowerShellExecutor executor = new WindowsPowerShellExecutor(tempPath);
new WindowsPowerShellExecutor(tempPath);

// assert
assertThat(Files.exists(tempPath)).isTrue();
Expand Down

0 comments on commit 8769e4d

Please sign in to comment.