source: trunk/java-utils-test/src/test/java/de/ugoe/cs/util/console/defaultcommands/CMDexitTest.java @ 927

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
  • Property svn:mime-type set to text/plain
File size: 2.7 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.util.console.defaultcommands;
16
17import java.security.Permission;
18import java.util.ArrayList;
19import java.util.List;
20import org.junit.*;
21
22import de.ugoe.cs.util.console.Console;
23import de.ugoe.cs.util.console.mock.MockOutputListener;
24import static org.junit.Assert.*;
25
26/**
27 * The class <code>CMDexitTest</code> contains tests for the class
28 * <code>{@link CMDexit}</code>.
29 *
30 * @author Steffen Herbold
31 * @version 1.0
32 */
33public class CMDexitTest {
34
35        final static String ENDLINE = System.getProperty("line.separator");
36
37        private static class ExitException extends SecurityException {
38                private static final long serialVersionUID = 1L;
39        }
40
41        private static class NoExitSecurityManager extends SecurityManager {
42                @Override
43                public void checkPermission(Permission perm) {
44                        // allow anything.
45                }
46
47                @Override
48                public void checkPermission(Permission perm, Object context) {
49                        // allow anything.
50                }
51
52                @Override
53                public void checkExit(int status) {
54                        super.checkExit(status);
55                        throw new ExitException();
56                }
57        }
58
59        @Test
60        public void testHelp_1() throws Exception {
61                CMDexit fixture = new CMDexit();
62                MockOutputListener mockListener = new MockOutputListener();
63                Console.getInstance().registerOutputListener(mockListener);
64                String expected = "exit";
65
66                assertEquals(expected, fixture.help());
67        }
68
69        @Test(expected = ExitException.class)
70        public void testRun_1() throws Exception {
71                CMDexit fixture = new CMDexit();
72                List<Object> parameters = new ArrayList<Object>();
73
74                fixture.run(parameters);
75        }
76
77        @Test(expected = ExitException.class)
78        public void testRun_2() throws Exception {
79                CMDexit fixture = new CMDexit();
80                List<Object> parameters = new ArrayList<Object>();
81                parameters.add("test");
82
83                fixture.run(parameters);
84        }
85
86        @Before
87        public void setUp() throws Exception {
88                System.setSecurityManager(new NoExitSecurityManager());
89        }
90
91        @After
92        public void tearDown() throws Exception {
93                System.setSecurityManager(null);
94        }
95
96        public static void main(String[] args) {
97                new org.junit.runner.JUnitCore().run(CMDexitTest.class);
98        }
99}
Note: See TracBrowser for help on using the repository browser.