Cleanup JUnits
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / util / test / JU_SubStandardConsole.java
diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_SubStandardConsole.java b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_SubStandardConsole.java
new file mode 100644 (file)
index 0000000..4d8e8f8
--- /dev/null
@@ -0,0 +1,124 @@
+/*******************************************************************************
+ * * org.onap.aaf
+ * * ===========================================================================
+ * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
+ * * ===========================================================================
+ * * Licensed under the Apache License, Version 2.0 (the "License");
+ * * you may not use this file except in compliance with the License.
+ * * You may obtain a copy of the License at
+ * * 
+ *  *      http://www.apache.org/licenses/LICENSE-2.0
+ * * 
+ *  * Unless required by applicable law or agreed to in writing, software
+ * * distributed under the License is distributed on an "AS IS" BASIS,
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * * See the License for the specific language governing permissions and
+ * * limitations under the License.
+ * * ============LICENSE_END====================================================
+ * *
+ * *
+ ******************************************************************************/
+package org.onap.aaf.cadi.util.test;
+
+import static org.junit.Assert.*;
+import static org.hamcrest.CoreMatchers.*;
+import static org.mockito.Mockito.*;
+import org.junit.*;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.lang.reflect.Field;
+
+import org.onap.aaf.cadi.util.SubStandardConsole;
+
+public class JU_SubStandardConsole {
+
+       private String inputString = "An input string";
+       private ByteArrayOutputStream outStream;
+       private ByteArrayOutputStream errStream;
+
+       @Before
+       public void setup() {
+               outStream = new ByteArrayOutputStream();
+               errStream = new ByteArrayOutputStream();
+               System.setOut(new PrintStream(outStream));
+               System.setErr(new PrintStream(errStream));
+       }
+
+       @After
+       public void tearDown() {
+               System.setOut(System.out);
+               System.setErr(System.err);
+       }
+
+       @Test
+       public void readLineTest() {
+               byte[] input = inputString.getBytes();
+               System.setIn(new ByteArrayInputStream(input));
+               SubStandardConsole ssc = new SubStandardConsole();
+               String output = ssc.readLine("%s\n", ">>> ");
+               assertThat(output, is(inputString));
+               assertThat(outStream.toString(), is(">>> \n"));
+       }
+
+       @Test
+       public void readLineTest2() {
+               byte[] input = inputString.getBytes();
+               System.setIn(new ByteArrayInputStream(input));
+               SubStandardConsole ssc = new SubStandardConsole();
+               String output = ssc.readLine("%s %s\n", ">>> ", "Another argument for coverage");
+               assertThat(output, is(inputString));
+       }
+
+       @Test
+       public void readLineTest3() {
+               byte[] input = "\n".getBytes();
+               System.setIn(new ByteArrayInputStream(input));
+               SubStandardConsole ssc = new SubStandardConsole();
+               String output = ssc.readLine("%s\n", ">>> ");
+               assertThat(output, is(">>> "));
+               assertThat(outStream.toString(), is(">>> \n"));
+       }
+
+       @Test
+       public void readPasswordTest() {
+               byte[] input = inputString.getBytes();
+               System.setIn(new ByteArrayInputStream(input));
+               SubStandardConsole ssc = new SubStandardConsole();
+               char[] output = ssc.readPassword("%s\n", ">>> ");
+               System.out.println(output);
+               assertThat(output, is(inputString.toCharArray()));
+               assertThat(outStream.toString(), is(">>> \nAn input string\n"));
+       }
+
+       @Test
+       public void printfTest() {
+               byte[] input = inputString.getBytes();
+               System.setIn(new ByteArrayInputStream(input));
+               SubStandardConsole ssc = new SubStandardConsole();
+               ssc.printf("%s", "A format specifier");
+               assertThat(outStream.toString(), is("A format specifier"));
+       }
+
+       @Test
+       public void throwsTest() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
+               BufferedReader brMock = mock(BufferedReader.class);
+               when(brMock.readLine()).thenThrow(new IOException());
+
+               SubStandardConsole ssc = new SubStandardConsole();
+
+               Field brField = SubStandardConsole.class.getDeclaredField("br");
+               brField.setAccessible(true);
+               brField.set(ssc, brMock);
+
+               assertThat(ssc.readLine(""), is(""));
+               assertThat(errStream.toString(), is("uh oh...\n"));
+        errStream.reset();
+               assertThat(ssc.readPassword("").length, is(0));
+               assertThat(errStream.toString(), is("uh oh...\n"));
+       }
+
+}