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.util.test;
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;
42 private String lineSeparator = System.lineSeparator();
46 outStream = new ByteArrayOutputStream();
47 errStream = new ByteArrayOutputStream();
48 System.setOut(new PrintStream(outStream));
49 System.setErr(new PrintStream(errStream));
53 public void tearDown() {
54 System.setOut(System.out);
55 System.setErr(System.err);
59 public void readLineTest() {
60 byte[] input = inputString.getBytes();
61 System.setIn(new ByteArrayInputStream(input));
62 SubStandardConsole ssc = new SubStandardConsole();
63 String output = ssc.readLine("%s" + lineSeparator, ">>> ");
64 assertThat(output, is(inputString));
65 assertThat(outStream.toString(), is(">>> " + lineSeparator));
69 public void readLineTest2() {
70 byte[] input = inputString.getBytes();
71 System.setIn(new ByteArrayInputStream(input));
72 SubStandardConsole ssc = new SubStandardConsole();
73 String output = ssc.readLine("%s %s" + lineSeparator, ">>> ", "Another argument for coverage");
74 assertThat(output, is(inputString));
78 public void readLineTest3() {
79 byte[] input = "\n".getBytes();
80 System.setIn(new ByteArrayInputStream(input));
81 SubStandardConsole ssc = new SubStandardConsole();
82 String output = ssc.readLine("%s" + lineSeparator, ">>> ");
83 assertThat(output, is(">>> "));
84 assertThat(outStream.toString(), is(">>> " + lineSeparator));
88 public void readPasswordTest() {
89 byte[] input = inputString.getBytes();
90 System.setIn(new ByteArrayInputStream(input));
91 SubStandardConsole ssc = new SubStandardConsole();
92 char[] output = ssc.readPassword("%s" + lineSeparator, ">>> ");
93 System.out.println(output);
94 assertThat(output, is(inputString.toCharArray()));
95 assertThat(outStream.toString(), is(">>> " + lineSeparator + "An input string" + lineSeparator));
99 public void printfTest() {
100 byte[] input = inputString.getBytes();
101 System.setIn(new ByteArrayInputStream(input));
102 SubStandardConsole ssc = new SubStandardConsole();
103 ssc.printf("%s", "A format specifier");
104 assertThat(outStream.toString(), is("A format specifier"));
108 public void throwsTest() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
109 BufferedReader brMock = mock(BufferedReader.class);
110 when(brMock.readLine()).thenThrow(new IOException());
112 SubStandardConsole ssc = new SubStandardConsole();
114 Field brField = SubStandardConsole.class.getDeclaredField("br");
115 brField.setAccessible(true);
116 brField.set(ssc, brMock);
118 assertThat(ssc.readLine(""), is(""));
119 assertThat(errStream.toString(), is("uh oh..." + lineSeparator));
121 assertThat(ssc.readPassword("").length, is(0));
122 assertThat(errStream.toString(), is("uh oh..." + lineSeparator));