summaryrefslogtreecommitdiffstats
path: root/core/modules/qemu/runvirt-plugin-qemu/src/test/java/org/openslx/runvirt/plugin/qemu/AppTest.java
blob: d0eef82a6d36cdd4667d208c423a10948593edf2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package org.openslx.runvirt.plugin.qemu;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs.CmdLnOption;

import com.ginsberg.junit.exit.ExpectSystemExit;
import com.ginsberg.junit.exit.ExpectSystemExitWithStatus;

public class AppTest
{
	@BeforeAll
	private static void setUp()
	{
		// disable logging with log4j
		LogManager.getRootLogger().setLevel( Level.OFF );
	}

	@Nested
	public class CmdLnTest
	{
		private final ByteArrayOutputStream out = new ByteArrayOutputStream();
		private final ByteArrayOutputStream err = new ByteArrayOutputStream();

		private void setUp()
		{
			// redirect output and error stream content
			System.setOut( new PrintStream( this.out ) );
			System.setErr( new PrintStream( this.err ) );
		}

		@Test
		@DisplayName( "Test ouput of correct 'help' command line option (short version)" )
		@ExpectSystemExit
		public void testCmdLnOptionHelpShortCorrect()
		{
			String[] argsShortHelpOptionCorrect = { "-" + CmdLnOption.HELP.getShortOption() };

			this.setUp();

			// test correct usage of the short help option
			try {
				App.main( argsShortHelpOptionCorrect );
			} catch ( Exception e ) {
				// do nothing and check output afterwards
			}

			final String shortHelpOptionCorrectOutput = new String( this.out.toString() );
			final String shortHelpOptionCorrectErrOutput = new String( this.err.toString() );
			assertTrue( shortHelpOptionCorrectOutput.contains( "usage" ) );
			assertTrue( shortHelpOptionCorrectOutput.contains( App.APP_NAME ) );
			assertTrue( shortHelpOptionCorrectOutput.contains( App.APP_INFO ) );
			assertTrue( shortHelpOptionCorrectOutput.contains( App.APP_DESC ) );

			// test that no error was logged and output is available
			assertEquals( 2503, shortHelpOptionCorrectOutput.length() );
			assertEquals( 0, shortHelpOptionCorrectErrOutput.length() );
		}

		@Test
		@DisplayName( "Test ouput of correct 'help' command line option (long version)" )
		@ExpectSystemExit
		public void testCmdLnOptionHelpLongCorrect()
		{
			String[] argsLongHelpOptionCorrect = { "--" + CmdLnOption.HELP.getLongOption() };

			this.setUp();

			// test correct usage of the long help option
			try {
				App.main( argsLongHelpOptionCorrect );
			} catch ( Exception e ) {
				// do nothing and check output afterwards
			}

			final String longHelpOptionCorrectOutput = this.out.toString();
			final String longHelpOptionCorrectErrOutput = this.err.toString();
			assertTrue( longHelpOptionCorrectOutput.contains( "usage" ) );
			assertTrue( longHelpOptionCorrectOutput.contains( App.APP_NAME ) );
			assertTrue( longHelpOptionCorrectOutput.contains( App.APP_INFO ) );
			assertTrue( longHelpOptionCorrectOutput.contains( App.APP_DESC ) );

			// test that no error was logged and output is available
			assertEquals( 2503, longHelpOptionCorrectOutput.length() );
			assertEquals( 0, longHelpOptionCorrectErrOutput.length() );
		}

		@Test
		@DisplayName( "Test ouput of incorrect 'help' command line option (short version)" )
		@ExpectSystemExit
		public void testCmdLnOptionHelpShortIncorrect()
		{
			String[] argsShortHelpOptionIncorrect = { "---" + CmdLnOption.HELP.getShortOption() };

			this.setUp();

			// test incorrect usage of the short help option
			try {
				App.main( argsShortHelpOptionIncorrect );
			} catch ( Exception e ) {
				// do nothing and check output afterwards
			}

			final String shortHelpOptionIncorrectOutput = this.out.toString();
			final String shortHelpOptionIncorrectErrOutput = this.err.toString();
			assertTrue( shortHelpOptionIncorrectOutput.contains( "usage" ) );
			assertTrue( shortHelpOptionIncorrectOutput.contains( App.APP_NAME ) );
			assertTrue( shortHelpOptionIncorrectOutput.contains( App.APP_INFO ) );
			assertTrue( shortHelpOptionIncorrectOutput.contains( App.APP_DESC ) );

			// test that error was logged and output is available
			assertEquals( 2503, shortHelpOptionIncorrectOutput.length() );
			assertEquals( 0, shortHelpOptionIncorrectErrOutput.length() );
		}

		@Test
		@DisplayName( "Test ouput of incorrect 'help' command line option (long version)" )
		@ExpectSystemExit
		public void testCmdLnOptionHelpLongIncorrect()
		{
			String[] argsLongHelpOptionIncorrect = { "---" + CmdLnOption.HELP.getLongOption() };

			this.setUp();

			// test incorrect usage of the long help option
			try {
				App.main( argsLongHelpOptionIncorrect );
			} catch ( Exception e ) {
				// do nothing and check output afterwards
			}

			final String longHelpOptionIncorrectOutput = this.out.toString();
			final String longHelpOptionIncorrectErrOutput = this.err.toString();
			assertTrue( longHelpOptionIncorrectOutput.contains( "usage" ) );
			assertTrue( longHelpOptionIncorrectOutput.contains( App.APP_NAME ) );
			assertTrue( longHelpOptionIncorrectOutput.contains( App.APP_INFO ) );
			assertTrue( longHelpOptionIncorrectOutput.contains( App.APP_DESC ) );

			// test that error was logged and output is available
			assertEquals( 2503, longHelpOptionIncorrectOutput.length() );
			assertEquals( 0, longHelpOptionIncorrectErrOutput.length() );
		}

		@Test
		@DisplayName( "Test exit status of application invoked with correct 'help' command line option (short version)" )
		@ExpectSystemExitWithStatus( 0 )
		public void testCmdLnOptionHelpShortCorrectExit()
		{
			String[] argsShortHelpOptionCorrect = { "-" + CmdLnOption.HELP.getShortOption() };

			this.setUp();

			App.main( argsShortHelpOptionCorrect );
		}

		@Test
		@DisplayName( "Test exit status of application invoked with correct 'help' command line option (long version)" )
		@ExpectSystemExitWithStatus( 0 )
		public void testCmdLnOptionHelpLongCorrectExit()
		{
			String[] argsLongHelpOptionCorrect = { "--" + CmdLnOption.HELP.getLongOption() };

			this.setUp();

			App.main( argsLongHelpOptionCorrect );
		}

		@Test
		@DisplayName( "Test exit status of application invoked with incorrect 'help' command line option (short version)" )
		@ExpectSystemExitWithStatus( 1 )
		public void testCmdLnOptionHelpShortIncorrectExit()
		{
			String[] argsShortHelpOptionCorrect = { "---" + CmdLnOption.HELP.getShortOption() };

			this.setUp();

			App.main( argsShortHelpOptionCorrect );
		}

		@Test
		@DisplayName( "Test exit status of application invoked with incorrect 'help' command line option (long version)" )
		@ExpectSystemExitWithStatus( 1 )
		public void testCmdLnOptionHelpLongIncorrectExit()
		{
			String[] argsLongHelpOptionCorrect = { "---" + CmdLnOption.HELP.getLongOption() };

			this.setUp();

			App.main( argsLongHelpOptionCorrect );
		}
	}
}