diff --git a/Batch/org.egovframe.rte.bat.core/src/main/java/org/egovframe/rte/bat/core/step/TaskletShellStep.java b/Batch/org.egovframe.rte.bat.core/src/main/java/org/egovframe/rte/bat/core/step/TaskletShellStep.java index 843278dd..bda252e7 100755 --- a/Batch/org.egovframe.rte.bat.core/src/main/java/org/egovframe/rte/bat/core/step/TaskletShellStep.java +++ b/Batch/org.egovframe.rte.bat.core/src/main/java/org/egovframe/rte/bat/core/step/TaskletShellStep.java @@ -37,21 +37,17 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext chunkCon throw new UnexpectedJobExecutionException("Shell Script is Empty!"); } - String[] arrCmdLine = shellScript.split("[\\r?\\n]+"); + String[] arrCmdLine = shellScript.split("[\\r\\n]+"); int resultShellScript = 0; - if (arrCmdLine.length == 0) { // single line - resultShellScript = ShellScriptSupport.shellCmd(shellScript, encoding); + for (String s : arrCmdLine) { + if (s.trim().isEmpty()) { + continue; + } + resultShellScript = ShellScriptSupport.shellCmd(s, encoding); if (resultShellScript > 0) { throw new UnexpectedJobExecutionException("Error Executing shell script!"); } - } else { // multiline - for (String s : arrCmdLine) { - resultShellScript = ShellScriptSupport.shellCmd(s, encoding); - if (resultShellScript > 0) { - throw new UnexpectedJobExecutionException("Error Executing shell script!"); - } - } } return RepeatStatus.FINISHED; diff --git a/Batch/org.egovframe.rte.bat.core/src/test/java/org/egovframe/rte/bat/core/step/TaskletShellStepTest.java b/Batch/org.egovframe.rte.bat.core/src/test/java/org/egovframe/rte/bat/core/step/TaskletShellStepTest.java new file mode 100644 index 00000000..3de75963 --- /dev/null +++ b/Batch/org.egovframe.rte.bat.core/src/test/java/org/egovframe/rte/bat/core/step/TaskletShellStepTest.java @@ -0,0 +1,44 @@ +package org.egovframe.rte.bat.core.step; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.batch.repeat.RepeatStatus; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * TaskletShellStep의 셸 스크립트 줄 분리 테스트 + */ +public class TaskletShellStepTest { + + private TaskletShellStep step(String shellScript) { + TaskletShellStep step = new TaskletShellStep(); + step.setShellScript(shellScript); + step.setEncoding("UTF-8"); + return step; + } + + @Test + @DisplayName("물음표가 든 한 줄 명령은 한 번에 실행된다") + public void executeSingleLineContainingQuestionMark() throws Exception { + assertEquals(RepeatStatus.FINISHED, step("echo a?b").execute(null, null)); + } + + @Test + @DisplayName("줄바꿈으로 구분된 여러 줄은 줄마다 실행된다") + public void executeMultipleLines() throws Exception { + assertEquals(RepeatStatus.FINISHED, step("echo first\necho second").execute(null, null)); + } + + @Test + @DisplayName("스크립트가 개행으로 시작해도 빈 명령을 실행하지 않는다") + public void executeScriptStartingWithNewline() throws Exception { + assertEquals(RepeatStatus.FINISHED, step("\necho hello").execute(null, null)); + } + + @Test + @DisplayName("공백만 있는 줄은 건너뛴다") + public void executeScriptWithBlankLine() throws Exception { + assertEquals(RepeatStatus.FINISHED, step("echo first\n \necho second").execute(null, null)); + } +}