System dependent separators in JU tests
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / util / test / JU_SubStandardConsole.java
1 /*******************************************************************************
2  * * org.onap.aaf
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
9  * * 
10  *  *      http://www.apache.org/licenses/LICENSE-2.0
11  * * 
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====================================================
18  * *
19  * *
20  ******************************************************************************/
21 package org.onap.aaf.cadi.util.test;
22
23 import static org.junit.Assert.*;
24 import static org.hamcrest.CoreMatchers.*;
25 import static org.mockito.Mockito.*;
26 import org.junit.*;
27
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;
34
35 import org.onap.aaf.cadi.util.SubStandardConsole;
36
37 public class JU_SubStandardConsole {
38
39         private String inputString = "An input string";
40         private ByteArrayOutputStream outStream;
41         private ByteArrayOutputStream errStream;
42         private String lineSeparator = System.lineSeparator();
43
44         @Before
45         public void setup() {
46                 outStream = new ByteArrayOutputStream();
47                 errStream = new ByteArrayOutputStream();
48                 System.setOut(new PrintStream(outStream));
49                 System.setErr(new PrintStream(errStream));
50         }
51
52         @After
53         public void tearDown() {
54                 System.setOut(System.out);
55                 System.setErr(System.err);
56         }
57
58         @Test
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));
66         }
67
68         @Test
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));
75         }
76
77         @Test
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));
85         }
86
87         @Test
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));
96         }
97
98         @Test
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"));
105         }
106
107         @Test
108         public void throwsTest() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
109                 BufferedReader brMock = mock(BufferedReader.class);
110                 when(brMock.readLine()).thenThrow(new IOException());
111
112                 SubStandardConsole ssc = new SubStandardConsole();
113
114                 Field brField = SubStandardConsole.class.getDeclaredField("br");
115                 brField.setAccessible(true);
116                 brField.set(ssc, brMock);
117
118                 assertThat(ssc.readLine(""), is(""));
119                 assertThat(errStream.toString(), is("uh oh..." + lineSeparator));
120         errStream.reset();
121                 assertThat(ssc.readPassword("").length, is(0));
122                 assertThat(errStream.toString(), is("uh oh..." + lineSeparator));
123         }
124
125 }