From af464eaefb409a8d808e89aa80e94e4b19aa8de5 Mon Sep 17 00:00:00 2001 From: huangguojie2024 <503601315@qq.com> Date: Fri, 24 Jan 2025 19:44:37 +0800 Subject: [PATCH] Add ut cases for emus and shell classes in common module --- .../manager/common/enums/CommandTest.java | 208 ++++++++++++++++++ .../manager/common/enums/JobStateTest.java | 143 ++++++++++++ .../common/enums/MaintainStateTest.java | 151 +++++++++++++ .../common/shell/ProcessContainerTest.java | 73 ++++++ .../manager/common/shell/ShellResultTest.java | 48 ++++ 5 files changed, 623 insertions(+) create mode 100644 bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/enums/CommandTest.java create mode 100644 bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/enums/JobStateTest.java create mode 100644 bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/enums/MaintainStateTest.java create mode 100644 bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/shell/ProcessContainerTest.java create mode 100644 bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/shell/ShellResultTest.java diff --git a/bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/enums/CommandTest.java b/bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/enums/CommandTest.java new file mode 100644 index 00000000..65950271 --- /dev/null +++ b/bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/enums/CommandTest.java @@ -0,0 +1,208 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.bigtop.manager.common.enums; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class CommandTest { + + // Test normal path: verify that the code and name of enum values are correct + @Test + public void testCommandNormalPath() { + assertEquals("add", Command.ADD.getCode(), "The code of ADD should be 'add'"); + assertEquals("Add", Command.ADD.getName(), "The name of ADD should be 'Add'"); + + assertEquals("start", Command.START.getCode(), "The code of START should be 'start'"); + assertEquals("Start", Command.START.getName(), "The name of START should be 'Start'"); + + assertEquals("stop", Command.STOP.getCode(), "The code of STOP should be 'stop'"); + assertEquals("Stop", Command.STOP.getName(), "The name of STOP should be 'Stop'"); + + assertEquals("restart", Command.RESTART.getCode(), "The code of RESTART should be 'restart'"); + assertEquals("Restart", Command.RESTART.getName(), "The name of RESTART should be 'Restart'"); + + assertEquals("check", Command.CHECK.getCode(), "The code of CHECK should be 'check'"); + assertEquals("Check", Command.CHECK.getName(), "The name of CHECK should be 'Check'"); + + assertEquals("configure", Command.CONFIGURE.getCode(), "The code of CONFIGURE should be 'configure'"); + assertEquals("Configure", Command.CONFIGURE.getName(), "The name of CONFIGURE should be 'Configure'"); + + assertEquals("custom", Command.CUSTOM.getCode(), "The code of CUSTOM should be 'custom'"); + assertEquals("Custom", Command.CUSTOM.getName(), "The name of CUSTOM should be 'Custom'"); + + assertEquals("init", Command.INIT.getCode(), "The code of INIT should be 'init'"); + assertEquals("Init", Command.INIT.getName(), "The name of INIT should be 'Init'"); + + assertEquals("prepare", Command.PREPARE.getCode(), "The code of PREPARE should be 'prepare'"); + assertEquals("Prepare", Command.PREPARE.getName(), "The name of PREPARE should be 'Prepare'"); + + assertEquals("status", Command.STATUS.getCode(), "The code of STATUS should be 'status'"); + assertEquals("Status", Command.STATUS.getName(), "The name of STATUS should be 'Status'"); + } + + // Test boundary conditions: verify that the fromString method can correctly convert strings in upper case, lower + // case, and mixed case + @Test + public void testFromStringWithVariousCases() { + assertEquals(Command.ADD, Command.fromString("ADD"), "Upper case 'ADD' should be converted to ADD enum value"); + assertEquals(Command.ADD, Command.fromString("add"), "Lower case 'add' should be converted to ADD enum value"); + assertEquals(Command.ADD, Command.fromString("AdD"), "Mixed case 'AdD' should be converted to ADD enum value"); + + assertEquals( + Command.START, + Command.fromString("START"), + "Upper case 'START' should be converted to START enum value"); + assertEquals( + Command.START, + Command.fromString("start"), + "Lower case 'start' should be converted to START enum value"); + assertEquals( + Command.START, + Command.fromString("StArT"), + "Mixed case 'StArT' should be converted to START enum value"); + + assertEquals( + Command.STOP, Command.fromString("STOP"), "Upper case 'STOP' should be converted to STOP enum value"); + assertEquals( + Command.STOP, Command.fromString("stop"), "Lower case 'stop' should be converted to STOP enum value"); + assertEquals( + Command.STOP, Command.fromString("StOp"), "Mixed case 'StOp' should be converted to STOP enum value"); + + assertEquals( + Command.RESTART, + Command.fromString("RESTART"), + "Upper case 'RESTART' should be converted to RESTART enum value"); + assertEquals( + Command.RESTART, + Command.fromString("restart"), + "Lower case 'restart' should be converted to RESTART enum value"); + assertEquals( + Command.RESTART, + Command.fromString("ReStArT"), + "Mixed case 'ReStArT' should be converted to RESTART enum value"); + + assertEquals( + Command.CHECK, + Command.fromString("CHECK"), + "Upper case 'CHECK' should be converted to CHECK enum value"); + assertEquals( + Command.CHECK, + Command.fromString("check"), + "Lower case 'check' should be converted to CHECK enum value"); + assertEquals( + Command.CHECK, + Command.fromString("ChEcK"), + "Mixed case 'ChEcK' should be converted to CHECK enum value"); + + assertEquals( + Command.CONFIGURE, + Command.fromString("CONFIGURE"), + "Upper case 'CONFIGURE' should be converted to CONFIGURE enum value"); + assertEquals( + Command.CONFIGURE, + Command.fromString("configure"), + "Lower case 'configure' should be converted to CONFIGURE enum value"); + assertEquals( + Command.CONFIGURE, + Command.fromString("CoNfIgUrE"), + "Mixed case 'CoNfIgUrE' should be converted to CONFIGURE enum value"); + + assertEquals( + Command.CUSTOM, + Command.fromString("CUSTOM"), + "Upper case 'CUSTOM' should be converted to CUSTOM enum value"); + assertEquals( + Command.CUSTOM, + Command.fromString("custom"), + "Lower case 'custom' should be converted to CUSTOM enum value"); + assertEquals( + Command.CUSTOM, + Command.fromString("CuStOm"), + "Mixed case 'CuStOm' should be converted to CUSTOM enum value"); + + assertEquals( + Command.INIT, Command.fromString("INIT"), "Upper case 'INIT' should be converted to INIT enum value"); + assertEquals( + Command.INIT, Command.fromString("init"), "Lower case 'init' should be converted to INIT enum value"); + assertEquals( + Command.INIT, Command.fromString("InIt"), "Mixed case 'InIt' should be converted to INIT enum value"); + + assertEquals( + Command.PREPARE, + Command.fromString("PREPARE"), + "Upper case 'PREPARE' should be converted to PREPARE enum value"); + assertEquals( + Command.PREPARE, + Command.fromString("prepare"), + "Lower case 'prepare' should be converted to PREPARE enum value"); + assertEquals( + Command.PREPARE, + Command.fromString("PrEpArE"), + "Mixed case 'PrEpArE' should be converted to PREPARE enum value"); + + assertEquals( + Command.STATUS, + Command.fromString("STATUS"), + "Upper case 'STATUS' should be converted to STATUS enum value"); + assertEquals( + Command.STATUS, + Command.fromString("status"), + "Lower case 'status' should be converted to STATUS enum value"); + assertEquals( + Command.STATUS, + Command.fromString("StAtUs"), + "Mixed case 'StAtUs' should be converted to STATUS enum value"); + } + + // Test boundary conditions: verify that the fromString method throws an exception when passed an invalid string + @Test + public void testFromStringWithInvalidString() { + assertThrows( + IllegalArgumentException.class, + () -> Command.fromString("INVALID"), + "Passing invalid string 'INVALID' should throw an IllegalArgumentException"); + assertThrows( + IllegalArgumentException.class, + () -> Command.fromString(""), + "Passing empty string '' should throw an IllegalArgumentException"); + assertThrows( + NullPointerException.class, + () -> Command.fromString(null), + "Passing null should throw a NullPointerException"); + } + + // Test boundary conditions: verify that the toCamelCase method can correctly convert enum values to camel case + // strings + @Test + public void testToCamelCase() { + assertEquals("Add", Command.ADD.toCamelCase(), "ADD should be converted to 'Add'"); + assertEquals("Start", Command.START.toCamelCase(), "START should be converted to 'Start'"); + assertEquals("Stop", Command.STOP.toCamelCase(), "STOP should be converted to 'Stop'"); + assertEquals("Restart", Command.RESTART.toCamelCase(), "RESTART should be converted to 'Restart'"); + assertEquals("Check", Command.CHECK.toCamelCase(), "CHECK should be converted to 'Check'"); + assertEquals("Configure", Command.CONFIGURE.toCamelCase(), "CONFIGURE should be converted to 'Configure'"); + assertEquals("Custom", Command.CUSTOM.toCamelCase(), "CUSTOM should be converted to 'Custom'"); + assertEquals("Init", Command.INIT.toCamelCase(), "INIT should be converted to 'Init'"); + assertEquals("Prepare", Command.PREPARE.toCamelCase(), "PREPARE should be converted to 'Prepare'"); + assertEquals("Status", Command.STATUS.toCamelCase(), "STATUS should be converted to 'Status'"); + } +} diff --git a/bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/enums/JobStateTest.java b/bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/enums/JobStateTest.java new file mode 100644 index 00000000..3165d010 --- /dev/null +++ b/bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/enums/JobStateTest.java @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.bigtop.manager.common.enums; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class JobStateTest { + // Test normal path: verify that the code and name of enum values are correct + @Test + public void testJobStateNormalPath() { + assertEquals("pending", JobState.PENDING.getCode(), "The code of PENDING should be 'pending'"); + assertEquals("Pending", JobState.PENDING.getName(), "The name of PENDING should be 'Pending'"); + + assertEquals("processing", JobState.PROCESSING.getCode(), "The code of PROCESSING should be 'processing'"); + assertEquals("Processing", JobState.PROCESSING.getName(), "The name of PROCESSING should be 'Processing'"); + + assertEquals("successful", JobState.SUCCESSFUL.getCode(), "The code of SUCCESSFUL should be 'successful'"); + assertEquals("Successful", JobState.SUCCESSFUL.getName(), "The name of SUCCESSFUL should be 'Successful'"); + + assertEquals("failed", JobState.FAILED.getCode(), "The code of FAILED should be 'failed'"); + assertEquals("Failed", JobState.FAILED.getName(), "The name of FAILED should be 'Failed'"); + + assertEquals("canceled", JobState.CANCELED.getCode(), "The code of CANCELED should be 'canceled'"); + assertEquals("Canceled", JobState.CANCELED.getName(), "The name of CANCELED should be 'Canceled'"); + } + + // Test boundary conditions: verify that the fromString method can correctly convert strings in upper case, lower + // case, and mixed case + @Test + public void testFromStringWithVariousCases() { + assertEquals( + JobState.PENDING, + JobState.fromString("PENDING"), + "Upper case 'PENDING' should be converted to PENDING enum value"); + assertEquals( + JobState.PENDING, + JobState.fromString("pending"), + "Lower case 'pending' should be converted to PENDING enum value"); + assertEquals( + JobState.PENDING, + JobState.fromString("PeNdiNg"), + "Mixed case 'PeNdiNg' should be converted to PENDING enum value"); + + assertEquals( + JobState.PROCESSING, + JobState.fromString("PROCESSING"), + "Upper case 'PROCESSING' should be converted to PROCESSING enum value"); + assertEquals( + JobState.PROCESSING, + JobState.fromString("processing"), + "Lower case 'processing' should be converted to PROCESSING enum value"); + assertEquals( + JobState.PROCESSING, + JobState.fromString("PrOcEsSiNg"), + "Mixed case 'PrOcEsSiNg' should be converted to PROCESSING enum value"); + + assertEquals( + JobState.SUCCESSFUL, + JobState.fromString("SUCCESSFUL"), + "Upper case 'SUCCESSFUL' should be converted to SUCCESSFUL enum value"); + assertEquals( + JobState.SUCCESSFUL, + JobState.fromString("successful"), + "Lower case 'successful' should be converted to SUCCESSFUL enum value"); + assertEquals( + JobState.SUCCESSFUL, + JobState.fromString("SuCcEsSfUl"), + "Mixed case 'SuCcEsSfUl' should be converted to SUCCESSFUL enum value"); + + assertEquals( + JobState.FAILED, + JobState.fromString("FAILED"), + "Upper case 'FAILED' should be converted to FAILED enum value"); + assertEquals( + JobState.FAILED, + JobState.fromString("failed"), + "Lower case 'failed' should be converted to FAILED enum value"); + assertEquals( + JobState.FAILED, + JobState.fromString("FaIlEd"), + "Mixed case 'FaIlEd' should be converted to FAILED enum value"); + + assertEquals( + JobState.CANCELED, + JobState.fromString("CANCELED"), + "Upper case 'CANCELED' should be converted to CANCELED enum value"); + assertEquals( + JobState.CANCELED, + JobState.fromString("canceled"), + "Lower case 'canceled' should be converted to CANCELED enum value"); + assertEquals( + JobState.CANCELED, + JobState.fromString("CaNcElEd"), + "Mixed case 'CaNcElEd' should be converted to CANCELED enum value"); + } + + // Test boundary conditions: verify that the fromString method throws an exception when passed an invalid string + @Test + public void testFromStringWithInvalidString() { + assertThrows( + IllegalArgumentException.class, + () -> JobState.fromString("INVALID"), + "Passing invalid string 'INVALID' should throw an IllegalArgumentException"); + assertThrows( + IllegalArgumentException.class, + () -> JobState.fromString(""), + "Passing empty string '' should throw an IllegalArgumentException"); + assertThrows( + NullPointerException.class, + () -> JobState.fromString(null), + "Passing null should throw a NullPointerException"); + } + + // Test boundary conditions: verify that the toCamelCase method can correctly convert enum values to camel case + // strings + @Test + public void testToCamelCase() { + assertEquals("Pending", JobState.PENDING.toCamelCase(), "PENDING should be converted to 'Pending'"); + assertEquals("Processing", JobState.PROCESSING.toCamelCase(), "PROCESSING should be converted to 'Processing'"); + assertEquals("Successful", JobState.SUCCESSFUL.toCamelCase(), "SUCCESSFUL should be converted to 'Successful'"); + assertEquals("Failed", JobState.FAILED.toCamelCase(), "FAILED should be converted to 'Failed'"); + assertEquals("Canceled", JobState.CANCELED.toCamelCase(), "CANCELED should be converted to 'Canceled'"); + } +} diff --git a/bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/enums/MaintainStateTest.java b/bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/enums/MaintainStateTest.java new file mode 100644 index 00000000..636020e5 --- /dev/null +++ b/bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/enums/MaintainStateTest.java @@ -0,0 +1,151 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.bigtop.manager.common.enums; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class MaintainStateTest { + + // Test normal path: verify that the code and name of enum values are correct + @Test + public void testMaintainStateNormalPath() { + assertEquals( + "uninstalled", MaintainState.UNINSTALLED.getCode(), "The code of UNINSTALLED should be 'uninstalled'"); + assertEquals( + "Uninstalled", MaintainState.UNINSTALLED.getName(), "The name of UNINSTALLED should be 'Uninstalled'"); + + assertEquals("installed", MaintainState.INSTALLED.getCode(), "The code of INSTALLED should be 'installed'"); + assertEquals("Installed", MaintainState.INSTALLED.getName(), "The name of INSTALLED should be 'Installed'"); + + assertEquals("maintained", MaintainState.MAINTAINED.getCode(), "The code of MAINTAINED should be 'maintained'"); + assertEquals("Maintained", MaintainState.MAINTAINED.getName(), "The name of MAINTAINED should be 'Maintained'"); + + assertEquals("started", MaintainState.STARTED.getCode(), "The code of STARTED should be 'started'"); + assertEquals("Started", MaintainState.STARTED.getName(), "The name of STARTED should be 'Started'"); + + assertEquals("stopped", MaintainState.STOPPED.getCode(), "The code of STOPPED should be 'stopped'"); + assertEquals("Stopped", MaintainState.STOPPED.getName(), "The name of STOPPED should be 'Stopped'"); + } + + // Test boundary conditions: verify that the fromString method can correctly convert strings in upper case, lower + // case, and mixed case + @Test + public void testFromStringWithVariousCases() { + assertEquals( + MaintainState.UNINSTALLED, + MaintainState.fromString("UNINSTALLED"), + "Upper case 'UNINSTALLED' should be converted to UNINSTALLED enum value"); + assertEquals( + MaintainState.UNINSTALLED, + MaintainState.fromString("uninstalled"), + "Lower case 'uninstalled' should be converted to UNINSTALLED enum value"); + assertEquals( + MaintainState.UNINSTALLED, + MaintainState.fromString("UnInStAlLeD"), + "Mixed case 'UnInStAlLeD' should be converted to UNINSTALLED enum value"); + + assertEquals( + MaintainState.INSTALLED, + MaintainState.fromString("INSTALLED"), + "Upper case 'INSTALLED' should be converted to INSTALLED enum value"); + assertEquals( + MaintainState.INSTALLED, + MaintainState.fromString("installed"), + "Lower case 'installed' should be converted to INSTALLED enum value"); + assertEquals( + MaintainState.INSTALLED, + MaintainState.fromString("InStAlLeD"), + "Mixed case 'InStAlLeD' should be converted to INSTALLED enum value"); + + assertEquals( + MaintainState.MAINTAINED, + MaintainState.fromString("MAINTAINED"), + "Upper case 'MAINTAINED' should be converted to MAINTAINED enum value"); + assertEquals( + MaintainState.MAINTAINED, + MaintainState.fromString("maintained"), + "Lower case 'maintained' should be converted to MAINTAINED enum value"); + assertEquals( + MaintainState.MAINTAINED, + MaintainState.fromString("MaInTaInEd"), + "Mixed case 'MaInTaInEd' should be converted to MAINTAINED enum value"); + + assertEquals( + MaintainState.STARTED, + MaintainState.fromString("STARTED"), + "Upper case 'STARTED' should be converted to STARTED enum value"); + assertEquals( + MaintainState.STARTED, + MaintainState.fromString("started"), + "Lower case 'started' should be converted to STARTED enum value"); + assertEquals( + MaintainState.STARTED, + MaintainState.fromString("StArTeD"), + "Mixed case 'StArTeD' should be converted to STARTED enum value"); + + assertEquals( + MaintainState.STOPPED, + MaintainState.fromString("STOPPED"), + "Upper case 'STOPPED' should be converted to STOPPED enum value"); + assertEquals( + MaintainState.STOPPED, + MaintainState.fromString("stopped"), + "Lower case 'stopped' should be converted to STOPPED enum value"); + assertEquals( + MaintainState.STOPPED, + MaintainState.fromString("StOpPeD"), + "Mixed case 'StOpPeD' should be converted to STOPPED enum value"); + } + + // Test boundary conditions: verify that the fromString method throws an exception when passed an invalid string + @Test + public void testFromStringWithInvalidString() { + assertThrows( + IllegalArgumentException.class, + () -> MaintainState.fromString("INVALID"), + "Passing invalid string 'INVALID' should throw an IllegalArgumentException"); + assertThrows( + IllegalArgumentException.class, + () -> MaintainState.fromString(""), + "Passing empty string '' should throw an IllegalArgumentException"); + assertThrows( + NullPointerException.class, + () -> MaintainState.fromString(null), + "Passing null should throw a NullPointerException"); + } + + // Test boundary conditions: verify that the toCamelCase method can correctly convert enum values to camel case + // strings + @Test + public void testToCamelCase() { + assertEquals( + "Uninstalled", + MaintainState.UNINSTALLED.toCamelCase(), + "UNINSTALLED should be converted to 'Uninstalled'"); + assertEquals( + "Installed", MaintainState.INSTALLED.toCamelCase(), "INSTALLED should be converted to 'Installed'"); + assertEquals( + "Maintained", MaintainState.MAINTAINED.toCamelCase(), "MAINTAINED should be converted to 'Maintained'"); + assertEquals("Started", MaintainState.STARTED.toCamelCase(), "STARTED should be converted to 'Started'"); + assertEquals("Stopped", MaintainState.STOPPED.toCamelCase(), "STOPPED should be converted to 'Stopped'"); + } +} diff --git a/bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/shell/ProcessContainerTest.java b/bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/shell/ProcessContainerTest.java new file mode 100644 index 00000000..c702be53 --- /dev/null +++ b/bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/shell/ProcessContainerTest.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.bigtop.manager.common.shell; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.verify; + +class ProcessContainerTest { + + @AfterEach + void tearDown() { + // Clean up remaining processes in the container + for (Process process : ProcessContainer.getInstance().values()) { + ProcessContainer.removeProcess(process); + } + } + + @Test + void testPutAndRemoveProcess() { + // Mock a Process object + Process process = Mockito.mock(Process.class); + + // Add process to the container + ProcessContainer.putProcess(process); + + // Verify that the process count in the container increases + assertEquals(1, ProcessContainer.processSize()); + + // Remove the process + ProcessContainer.removeProcess(process); + + // Verify that the process count in the container decreases + assertEquals(0, ProcessContainer.processSize()); + } + + @Test + void testDestroyAllProcess() { + // Mock Process objects + Process process1 = Mockito.mock(Process.class); + Process process2 = Mockito.mock(Process.class); + + // Add two processes + ProcessContainer.putProcess(process1); + ProcessContainer.putProcess(process2); + + // Call the destroyAllProcess method + ProcessContainer.destroyAllProcess(); + + // Verify that the destroy method is called + verify(process1).destroy(); + verify(process2).destroy(); + } +} diff --git a/bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/shell/ShellResultTest.java b/bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/shell/ShellResultTest.java new file mode 100644 index 00000000..831a1ad8 --- /dev/null +++ b/bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/shell/ShellResultTest.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.bigtop.manager.common.shell; + +import org.apache.bigtop.manager.common.constants.MessageConstants; + +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class ShellResultTest { + + @Test + void testExecCommandSuccess() throws IOException { + // Simulate successful shell command execution + ShellResult result = ShellResult.success(); + assertEquals(MessageConstants.SUCCESS_CODE, result.getExitCode()); + assertEquals("Run shell success.", result.getOutput()); + assertEquals("", result.getErrMsg()); + } + + @Test + void testExecCommandFailure() throws IOException { + // Simulate failed shell command execution + ShellResult result = ShellResult.fail(); + assertEquals(MessageConstants.FAIL_CODE, result.getExitCode()); + assertEquals("Run shell fail.", result.getOutput()); + assertEquals("", result.getErrMsg()); + } +}