Search in sources :

Example 96 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project hadoop by apache.

the class TestFindClass method testPrintLog4J.

@SuppressWarnings("UseOfSystemOutOrSystemErr")
@Test
public void testPrintLog4J() throws Throwable {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(baos);
    FindClass.setOutputStreams(out, System.err);
    run(FindClass.SUCCESS, FindClass.A_PRINTRESOURCE, LOG4J_PROPERTIES);
    //here the content should be done
    out.flush();
    String body = baos.toString("UTF8");
    LOG.info(LOG4J_PROPERTIES + " =\n" + body);
    assertTrue(body.contains("Apache"));
}
Also used : PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 97 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project hadoop by apache.

the class TestProtoUtil method doVarIntTest.

private void doVarIntTest(int value) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CodedOutputStream cout = CodedOutputStream.newInstance(baos);
    cout.writeRawVarint32(value);
    cout.flush();
    DataInputStream dis = new DataInputStream(new ByteArrayInputStream(baos.toByteArray()));
    assertEquals(value, ProtoUtil.readRawVarint32(dis));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CodedOutputStream(com.google.protobuf.CodedOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream)

Example 98 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project hadoop by apache.

the class TestFsShellPermission method execCmd.

static String execCmd(FsShell shell, final String[] args) throws Exception {
    ByteArrayOutputStream baout = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(baout, true);
    PrintStream old = System.out;
    int ret;
    try {
        System.setOut(out);
        ret = shell.run(args);
        out.close();
    } finally {
        System.setOut(old);
    }
    return String.valueOf(ret);
}
Also used : PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 99 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project hadoop by apache.

the class TestPacketReceiver method doTestReceiveAndMirror.

private void doTestReceiveAndMirror(PacketReceiver pr, int dataLen, int checksumsLen) throws IOException {
    final byte[] DATA = AppendTestUtil.initBuffer(dataLen);
    final byte[] CHECKSUMS = AppendTestUtil.initBuffer(checksumsLen);
    byte[] packet = prepareFakePacket(DATA, CHECKSUMS);
    ByteArrayInputStream in = new ByteArrayInputStream(packet);
    pr.receiveNextPacket(in);
    ByteBuffer parsedData = pr.getDataSlice();
    assertArrayEquals(DATA, remainingAsArray(parsedData));
    ByteBuffer parsedChecksums = pr.getChecksumSlice();
    assertArrayEquals(CHECKSUMS, remainingAsArray(parsedChecksums));
    PacketHeader header = pr.getHeader();
    assertEquals(SEQNO, header.getSeqno());
    assertEquals(OFFSET_IN_BLOCK, header.getOffsetInBlock());
    assertEquals(dataLen + checksumsLen + Ints.BYTES, header.getPacketLen());
    // Mirror the packet to an output stream and make sure it matches
    // the packet we sent.
    ByteArrayOutputStream mirrored = new ByteArrayOutputStream();
    mirrored = Mockito.spy(mirrored);
    pr.mirrorPacketTo(new DataOutputStream(mirrored));
    // The write should be done in a single call. Otherwise we may hit
    // nasty interactions with nagling (eg HDFS-4049).
    Mockito.verify(mirrored, Mockito.times(1)).write(Mockito.<byte[]>any(), Mockito.anyInt(), Mockito.eq(packet.length));
    Mockito.verifyNoMoreInteractions(mirrored);
    assertArrayEquals(packet, mirrored.toByteArray());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer)

Example 100 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project hadoop by apache.

the class TestMetadataVersionOutput method testMetadataVersionOutput.

@Test(timeout = 30000)
public void testMetadataVersionOutput() throws IOException {
    initConfig();
    dfsCluster = new MiniDFSCluster.Builder(conf).manageNameDfsDirs(false).numDataNodes(1).checkExitOnShutdown(false).build();
    dfsCluster.waitClusterUp();
    dfsCluster.shutdown(false);
    initConfig();
    final PrintStream origOut = System.out;
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final PrintStream stdOut = new PrintStream(baos);
    try {
        System.setOut(stdOut);
        try {
            NameNode.createNameNode(new String[] { "-metadataVersion" }, conf);
        } catch (Exception e) {
            assertExceptionContains("ExitException", e);
        }
        /* Check if meta data version is printed correctly. */
        final String verNumStr = HdfsServerConstants.NAMENODE_LAYOUT_VERSION + "";
        assertTrue(baos.toString("UTF-8").contains("HDFS Image Version: " + verNumStr));
        assertTrue(baos.toString("UTF-8").contains("Software format version: " + verNumStr));
    } finally {
        System.setOut(origOut);
    }
}
Also used : PrintStream(java.io.PrintStream) MiniDFSCluster(org.apache.hadoop.hdfs.MiniDFSCluster) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)7299 ByteArrayInputStream (java.io.ByteArrayInputStream)1833 IOException (java.io.IOException)1814 Test (org.junit.Test)1754 PrintStream (java.io.PrintStream)682 DataOutputStream (java.io.DataOutputStream)657 ObjectOutputStream (java.io.ObjectOutputStream)657 InputStream (java.io.InputStream)655 ObjectInputStream (java.io.ObjectInputStream)317 File (java.io.File)301 OutputStream (java.io.OutputStream)276 FileInputStream (java.io.FileInputStream)194 OutputStreamWriter (java.io.OutputStreamWriter)194 ArrayList (java.util.ArrayList)193 HashMap (java.util.HashMap)188 Test (org.testng.annotations.Test)184 DataInputStream (java.io.DataInputStream)174 PrintWriter (java.io.PrintWriter)139 FileOutputStream (java.io.FileOutputStream)134 URL (java.net.URL)133