source: trunk/JavaHelperLibTest/src/de/ugoe/cs/util/console/defaultcommands/CMDexitTest.java @ 363

Last change on this file since 363 was 323, checked in by sherbold, 13 years ago
  • added JUnit tests for de.ugoe.cs.util.console.defaultcommands.CMDexec and de.ugoe.cs.util.console.defaultcommands.CMDexit
  • Property svn:mime-type set to text/plain
File size: 2.1 KB
Line 
1package de.ugoe.cs.util.console.defaultcommands;
2
3import java.security.Permission;
4import java.util.ArrayList;
5import java.util.List;
6import org.junit.*;
7
8import de.ugoe.cs.util.console.Console;
9import de.ugoe.cs.util.console.mock.MockOutputListener;
10import static org.junit.Assert.*;
11
12/**
13 * The class <code>CMDexitTest</code> contains tests for the class
14 * <code>{@link CMDexit}</code>.
15 *
16 * @author Steffen Herbold
17 * @version 1.0
18 */
19public class CMDexitTest {
20
21        final static String ENDLINE = System.getProperty("line.separator");
22
23        private static class ExitException extends SecurityException {
24                private static final long serialVersionUID = 1L;
25        }
26
27        private static class NoExitSecurityManager extends SecurityManager {
28                @Override
29                public void checkPermission(Permission perm) {
30                        // allow anything.
31                }
32
33                @Override
34                public void checkPermission(Permission perm, Object context) {
35                        // allow anything.
36                }
37
38                @Override
39                public void checkExit(int status) {
40                        super.checkExit(status);
41                        throw new ExitException();
42                }
43        }
44
45        @Test
46        public void testHelp_1() throws Exception {
47                CMDexit fixture = new CMDexit();
48                MockOutputListener mockListener = new MockOutputListener();
49                Console.getInstance().registerOutputListener(mockListener);
50                String expected = "Usage: exit" + ENDLINE;
51
52                fixture.help();
53
54                assertEquals(expected, mockListener.getLastOutput());
55        }
56
57        @Test(expected = ExitException.class)
58        public void testRun_1() throws Exception {
59                CMDexit fixture = new CMDexit();
60                List<Object> parameters = new ArrayList<Object>();
61
62                fixture.run(parameters);
63        }
64
65        @Test(expected = ExitException.class)
66        public void testRun_2() throws Exception {
67                CMDexit fixture = new CMDexit();
68                List<Object> parameters = new ArrayList<Object>();
69                parameters.add("test");
70
71                fixture.run(parameters);
72        }
73
74        @Before
75        public void setUp() throws Exception {
76                System.setSecurityManager(new NoExitSecurityManager());
77        }
78
79        @After
80        public void tearDown() throws Exception {
81                System.setSecurityManager(null);
82        }
83
84        public static void main(String[] args) {
85                new org.junit.runner.JUnitCore().run(CMDexitTest.class);
86        }
87}
Note: See TracBrowser for help on using the repository browser.