1 /*******************************************************************************
3 * * ===========================================================================
4 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
5 * * ===========================================================================
6 * * Licensed under the Apache License, Version 2.0 (the "License");
7 * * you may not use this file except in compliance with the License.
8 * * You may obtain a copy of the License at
10 * * http://www.apache.org/licenses/LICENSE-2.0
12 * * Unless required by applicable law or agreed to in writing, software
13 * * distributed under the License is distributed on an "AS IS" BASIS,
14 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * * See the License for the specific language governing permissions and
16 * * limitations under the License.
17 * * ============LICENSE_END====================================================
20 ******************************************************************************/
21 package org.onap.aaf.cadi.test.util;
23 import static org.junit.Assert.*;
24 import static org.hamcrest.CoreMatchers.*;
25 import static org.mockito.Mockito.*;
28 import java.io.BufferedReader;
29 import java.io.ByteArrayInputStream;
30 import java.io.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.io.PrintStream;
33 import java.lang.reflect.Field;
35 import org.onap.aaf.cadi.util.SubStandardConsole;
37 public class JU_SubStandardConsole {
39 private String inputString = "An input string";
40 private ByteArrayOutputStream outStream;
41 private ByteArrayOutputStream errStream;
45 outStream = new ByteArrayOutputStream();
46 errStream = new ByteArrayOutputStream();
47 System.setOut(new PrintStream(outStream));
48 System.setErr(new PrintStream(errStream));
52 public void tearDown() {
53 System.setOut(System.out);
54 System.setErr(System.err);
58 public void readLineTest() {
59 byte[] input = inputString.getBytes();
60 System.setIn(new ByteArrayInputStream(input));
61 SubStandardConsole ssc = new SubStandardConsole();
62 String output = ssc.readLine("%s\n", ">>> ");
63 assertThat(output, is(inputString));
64 assertThat(outStream.toString(), is(">>> \n"));
68 public void readLineTest2() {
69 byte[] input = inputString.getBytes();
70 System.setIn(new ByteArrayInputStream(input));
71 SubStandardConsole ssc = new SubStandardConsole();
72 String output = ssc.readLine("%s %s\n", ">>> ", "Another argument for coverage");
73 assertThat(output, is(inputString));
77 public void readLineTest3() {
78 byte[] input = "\n".getBytes();
79 System.setIn(new ByteArrayInputStream(input));
80 SubStandardConsole ssc = new SubStandardConsole();
81 String output = ssc.readLine("%s\n", ">>> ");
82 assertThat(output, is(">>> "));
83 assertThat(outStream.toString(), is(">>> \n"));
87 public void readPasswordTest() {
88 byte[] input = inputString.getBytes();
89 System.setIn(new ByteArrayInputStream(input));
90 SubStandardConsole ssc = new SubStandardConsole();
91 char[] output = ssc.readPassword("%s\n", ">>> ");
92 System.out.println(output);
93 assertThat(output, is(inputString.toCharArray()));
94 assertThat(outStream.toString(), is(">>> \nAn input string\n"));
98 public void printfTest() {
99 byte[] input = inputString.getBytes();
100 System.setIn(new ByteArrayInputStream(input));
101 SubStandardConsole ssc = new SubStandardConsole();
102 ssc.printf("%s", "A format specifier");
103 assertThat(outStream.toString(), is("A format specifier"));
107 public void throwsTest() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
108 BufferedReader brMock = mock(BufferedReader.class);
109 when(brMock.readLine()).thenThrow(new IOException());
111 SubStandardConsole ssc = new SubStandardConsole();
113 Field brField = SubStandardConsole.class.getDeclaredField("br");
114 brField.setAccessible(true);
115 brField.set(ssc, brMock);
117 assertThat(ssc.readLine(""), is(""));
118 assertThat(errStream.toString(), is("uh oh...\n"));
120 assertThat(ssc.readPassword("").length, is(0));
121 assertThat(errStream.toString(), is("uh oh...\n"));