|
| 1 | +package org.baeldung.shell.simple; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import org.junit.AfterClass; |
| 5 | +import org.junit.Assert; |
| 6 | +import org.junit.BeforeClass; |
| 7 | +import org.junit.Test; |
| 8 | +import org.springframework.shell.Bootstrap; |
| 9 | +import org.springframework.shell.core.CommandResult; |
| 10 | +import org.springframework.shell.core.JLineShellComponent; |
| 11 | + |
| 12 | +public class SimpleCLIUnitTest { |
| 13 | + |
| 14 | + static JLineShellComponent shell; |
| 15 | + |
| 16 | + @BeforeClass |
| 17 | + public static void startUp() throws InterruptedException { |
| 18 | + Bootstrap bootstrap = new Bootstrap(); |
| 19 | + shell = bootstrap.getJLineShellComponent(); |
| 20 | + } |
| 21 | + |
| 22 | + @AfterClass |
| 23 | + public static void shutdown() { |
| 24 | + shell.stop(); |
| 25 | + } |
| 26 | + |
| 27 | + public static JLineShellComponent getShell() { |
| 28 | + return shell; |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void givenCommandConfig_whenExecutingWebGetCommand_thenCorrectResult() { |
| 33 | + |
| 34 | + CommandResult resultWebSave = shell.executeCommand("web-get --url https://www.google.com"); |
| 35 | + |
| 36 | + Assert.assertTrue(resultWebSave.isSuccess()); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void givenCommandConfig_whenExecutingWebSaveCommand_thenCorrectResult() { |
| 41 | + |
| 42 | + shell.executeCommand("admin-enable"); |
| 43 | + CommandResult result = shell.executeCommand("web-save --url https://www.google.com --out contents.txt"); |
| 44 | + |
| 45 | + Assert.assertArrayEquals( |
| 46 | + new boolean[]{ |
| 47 | + result.isSuccess(), |
| 48 | + new File("contents.txt").exists()}, |
| 49 | + new boolean[]{true, true}); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void givenCommandConfig_whenAdminEnableCommandExecuted_thenCorrectAvailability() { |
| 54 | + |
| 55 | + CommandResult resultAdminDisable = shell.executeCommand("admin-disable"); |
| 56 | + CommandResult resultWebSaveUnavailable = shell.executeCommand("web-save --url https://www.google.com --out contents.txt"); |
| 57 | + CommandResult resultAdminEnable = shell.executeCommand("admin-enable"); |
| 58 | + CommandResult resultWebSaveAvailable = shell.executeCommand("web-save --url https://www.google.com --out contents.txt"); |
| 59 | + |
| 60 | + Assert.assertArrayEquals( |
| 61 | + new boolean[]{ |
| 62 | + resultAdminDisable.isSuccess(), |
| 63 | + resultWebSaveUnavailable.isSuccess(), |
| 64 | + resultAdminEnable.isSuccess(), |
| 65 | + resultWebSaveAvailable.isSuccess()}, |
| 66 | + new boolean[]{true, false, true, true}); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void givenCommandConfig_whenWebSaveCommandExecutedNoOutArgument_thenError() { |
| 71 | + |
| 72 | + shell.executeCommand("admin-enable"); |
| 73 | + CommandResult resultWebSave = shell.executeCommand("web-save --url https://www.google.com"); |
| 74 | + |
| 75 | + Assert.assertEquals(resultWebSave.isSuccess(), false); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void givenCommandConfig_whenExecutingWebGetCommandWithDefaultArgument_thenCorrectResult() { |
| 80 | + |
| 81 | + CommandResult result = shell.executeCommand("web-get https://www.google.com"); |
| 82 | + |
| 83 | + Assert.assertEquals(result.isSuccess(), true); |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments