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 ******************************************************************************/
22 package org.onap.aaf.cadi.util.test;
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.mockito.Mockito.*;
29 import java.io.BufferedReader;
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
32 import java.io.IOException;
33 import java.io.PrintStream;
34 import java.lang.reflect.Field;
36 import org.onap.aaf.cadi.util.SubStandardConsole;
38 public class JU_SubStandardConsole {
40 private String inputString = "An input string";
41 private ByteArrayOutputStream outStream;
42 private ByteArrayOutputStream errStream;
43 private String lineSeparator = System.lineSeparator();
47 outStream = new ByteArrayOutputStream();
48 errStream = new ByteArrayOutputStream();
49 System.setOut(new PrintStream(outStream));
50 System.setErr(new PrintStream(errStream));
54 public void tearDown() {
55 System.setOut(System.out);
56 System.setErr(System.err);
60 public void readLineTest() {
61 byte[] input = inputString.getBytes();
62 System.setIn(new ByteArrayInputStream(input));
63 SubStandardConsole ssc = new SubStandardConsole();
64 String output = ssc.readLine("%s" + lineSeparator, ">>> ");
65 assertThat(output, is(inputString));
66 assertThat(outStream.toString(), is(">>> " + lineSeparator));
70 public void readLineTest2() {
71 byte[] input = inputString.getBytes();
72 System.setIn(new ByteArrayInputStream(input));
73 SubStandardConsole ssc = new SubStandardConsole();
74 String output = ssc.readLine("%s %s" + lineSeparator, ">>> ", "Another argument for coverage");
75 assertThat(output, is(inputString));
79 public void readLineTest3() {
80 byte[] input = "\n".getBytes();
81 System.setIn(new ByteArrayInputStream(input));
82 SubStandardConsole ssc = new SubStandardConsole();
83 String output = ssc.readLine("%s" + lineSeparator, ">>> ");
84 assertThat(output, is(">>> "));
85 assertThat(outStream.toString(), is(">>> " + lineSeparator));
89 public void readPasswordTest() {
90 byte[] input = inputString.getBytes();
91 System.setIn(new ByteArrayInputStream(input));
92 SubStandardConsole ssc = new SubStandardConsole();
93 char[] output = ssc.readPassword("%s" + lineSeparator, ">>> ");
94 System.out.println(output);
95 assertThat(output, is(inputString.toCharArray()));
96 assertThat(outStream.toString(), is(">>> " + lineSeparator + "An input string" + lineSeparator));
100 public void printfTest() {
101 byte[] input = inputString.getBytes();
102 System.setIn(new ByteArrayInputStream(input));
103 SubStandardConsole ssc = new SubStandardConsole();
104 ssc.printf("%s", "A format specifier");
105 assertThat(outStream.toString(), is("A format specifier"));
109 public void throwsTest() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
110 BufferedReader brMock = mock(BufferedReader.class);
111 when(brMock.readLine()).thenThrow(new IOException());
113 SubStandardConsole ssc = new SubStandardConsole();
115 Field brField = SubStandardConsole.class.getDeclaredField("br");
116 brField.setAccessible(true);
117 brField.set(ssc, brMock);
119 assertThat(ssc.readLine(""), is(""));
120 assertThat(errStream.toString(), is("uh oh..." + lineSeparator));
122 assertThat(ssc.readPassword("").length, is(0));
123 assertThat(errStream.toString(), is("uh oh..." + lineSeparator));