Package org.apache.http

Examples of org.apache.http.WritableByteChannelMock


        }
    }

    @Test
    public void testCodingFromFile() throws Exception {
        final WritableByteChannelMock channel = new WritableByteChannelMock(64);
        final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
        final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();

        final IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics);

        createTempFile();
        RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw");
        try {
            testfile.write("stuff;".getBytes(Consts.ASCII));
            testfile.write("more stuff".getBytes(Consts.ASCII));
        } finally {
            testfile.close();
        }

        testfile = new RandomAccessFile(this.tmpfile, "rw");
        try {
            final FileChannel fchannel = testfile.getChannel();
            encoder.transfer(fchannel, 0, 20);
        } finally {
            testfile.close();
        }

        final String s = channel.dump(Consts.ASCII);

        Assert.assertFalse(encoder.isCompleted());
        Assert.assertEquals("stuff;more stuff", s);
    }
View Full Code Here


        Assert.assertEquals("stuff;more stuff", s);
    }

    @Test
    public void testCodingEmptyFile() throws Exception {
        final WritableByteChannelMock channel = new WritableByteChannelMock(64);
        final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
        final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();

        final IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics);
        encoder.write(CodecTestUtils.wrap("stuff;"));

        //Create an empty file
        createTempFile();
        RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw");
        testfile.close();

        testfile = new RandomAccessFile(this.tmpfile, "rw");
        try {
            final FileChannel fchannel = testfile.getChannel();
            encoder.transfer(fchannel, 0, 20);
            encoder.write(CodecTestUtils.wrap("more stuff"));
        } finally {
            testfile.close();
        }

        final String s = channel.dump(Consts.ASCII);

        Assert.assertFalse(encoder.isCompleted());
        Assert.assertEquals("stuff;more stuff", s);
    }
View Full Code Here

        Assert.assertEquals("stuff;more stuff", s);
    }

    @Test
    public void testCodingFromFileSmaller() throws Exception {
        final WritableByteChannelMock channel = new WritableByteChannelMock(64);
        final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
        final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();

        final IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics);

        createTempFile();
        RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw");
        try {
            testfile.write("stuff;".getBytes(Consts.ASCII));
            testfile.write("more stuff".getBytes(Consts.ASCII));
        } finally {
            testfile.close();
        }

        testfile = new RandomAccessFile(this.tmpfile, "rw");
        try {
            final FileChannel fchannel = testfile.getChannel();
            encoder.transfer(fchannel, 0, 20);
        } finally {
            testfile.close();
        }
        final String s = channel.dump(Consts.ASCII);

        Assert.assertFalse(encoder.isCompleted());
        Assert.assertEquals("stuff;more stuff", s);
    }
View Full Code Here

        Assert.assertEquals("stuff;more stuff", s);
    }

    @Test
    public void testCodingFromFileFlushBuffer() throws Exception {
        final WritableByteChannelMock channel = new WritableByteChannelMock(64);
        final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
        final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();

        final IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics);

        outbuf.writeLine("header");

        createTempFile();
        RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw");
        try {
            testfile.write("stuff;".getBytes(Consts.ASCII));
            testfile.write("more stuff".getBytes(Consts.ASCII));
        } finally {
            testfile.close();
        }

        testfile = new RandomAccessFile(this.tmpfile, "rw");
        try {
            final FileChannel fchannel = testfile.getChannel();
            encoder.transfer(fchannel, 0, 20);
        } finally {
            testfile.close();
        }
        final String s = channel.dump(Consts.ASCII);

        Assert.assertFalse(encoder.isCompleted());
        Assert.assertEquals("header\r\nstuff;more stuff", s);
    }
View Full Code Here

        Assert.assertEquals("header\r\nstuff;more stuff", s);
    }

    @Test
    public void testCodingFromFileChannelSaturated() throws Exception {
        final WritableByteChannelMock channel = new WritableByteChannelMock(64, 4);
        final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
        final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();

        final IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics);

        outbuf.writeLine("header");

        createTempFile();
        RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw");
        try {
            testfile.write("stuff".getBytes(Consts.ASCII));
        } finally {
            testfile.close();
        }

        testfile = new RandomAccessFile(this.tmpfile, "rw");
        try {
            final FileChannel fchannel = testfile.getChannel();
            encoder.transfer(fchannel, 0, 20);
            encoder.transfer(fchannel, 0, 20);
        } finally {
            testfile.close();
        }
        final String s = channel.dump(Consts.ASCII);

        Assert.assertFalse(encoder.isCompleted());
        Assert.assertEquals("head", s);
    }
View Full Code Here

        Assert.assertEquals("head", s);
    }

    @Test
    public void testCodingNoFragmentBuffering() throws Exception {
        final WritableByteChannelMock channel = Mockito.spy(new WritableByteChannelMock(64));
        final SessionOutputBuffer outbuf = Mockito.spy(new SessionOutputBufferImpl(1024, 128));
        final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();

        outbuf.writeLine("header");
        final IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics, 0);
        Assert.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));

        Mockito.verify(channel, Mockito.times(2)).write(Mockito.<ByteBuffer>any());
        Mockito.verify(outbuf, Mockito.never()).write(Mockito.<ByteBuffer>any());
        Mockito.verify(outbuf, Mockito.times(1)).flush(channel);

        Assert.assertEquals(13, metrics.getBytesTransferred());

        outbuf.flush(channel);
        final String s = channel.dump(Consts.ASCII);

        Assert.assertEquals("header\r\nstuff", s);
    }
View Full Code Here

        Assert.assertEquals("header\r\nstuff", s);
    }

    @Test
    public void testCodingFragmentBuffering() throws Exception {
        final WritableByteChannelMock channel = Mockito.spy(new WritableByteChannelMock(64));
        final SessionOutputBuffer outbuf = Mockito.spy(new SessionOutputBufferImpl(1024, 128));
        final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();

        outbuf.writeLine("header");
        final IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics, 32);
        Assert.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));

        Mockito.verify(channel, Mockito.never()).write(Mockito.<ByteBuffer>any());
        Mockito.verify(outbuf, Mockito.times(1)).write(Mockito.<ByteBuffer>any());
        Mockito.verify(outbuf, Mockito.never()).flush(channel);

        Assert.assertEquals(0, metrics.getBytesTransferred());

        outbuf.flush(channel);
        final String s = channel.dump(Consts.ASCII);

        Assert.assertEquals("header\r\nstuff", s);
    }
View Full Code Here

        Assert.assertEquals("header\r\nstuff", s);
    }

    @Test
    public void testCodingFragmentBufferingMultipleFragments() throws Exception {
        final WritableByteChannelMock channel = Mockito.spy(new WritableByteChannelMock(64));
        final SessionOutputBuffer outbuf = Mockito.spy(new SessionOutputBufferImpl(1024, 128));
        final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();

        final IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics, 32);
        Assert.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));
        Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
        Assert.assertEquals(10, encoder.write(CodecTestUtils.wrap("more stuff")));

        Mockito.verify(channel, Mockito.never()).write(Mockito.<ByteBuffer>any());
        Mockito.verify(outbuf, Mockito.times(3)).write(Mockito.<ByteBuffer>any());
        Mockito.verify(outbuf, Mockito.never()).flush(channel);

        Assert.assertEquals(0, metrics.getBytesTransferred());

        outbuf.flush(channel);
        final String s = channel.dump(Consts.ASCII);

        Assert.assertEquals("stuff-more stuff", s);
    }
View Full Code Here

        Assert.assertEquals("stuff-more stuff", s);
    }

    @Test
    public void testCodingFragmentBufferingLargeFragment() throws Exception {
        final WritableByteChannelMock channel = Mockito.spy(new WritableByteChannelMock(64));
        final SessionOutputBuffer outbuf = Mockito.spy(new SessionOutputBufferImpl(1024, 128));
        final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();

        outbuf.writeLine("header");
        final IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics, 2);
        Assert.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));

        Mockito.verify(channel, Mockito.times(2)).write(Mockito.<ByteBuffer>any());
        Mockito.verify(outbuf, Mockito.never()).write(Mockito.<ByteBuffer>any());
        Mockito.verify(outbuf, Mockito.times(1)).flush(channel);

        Assert.assertEquals(13, metrics.getBytesTransferred());

        outbuf.flush(channel);
        final String s = channel.dump(Consts.ASCII);
        Assert.assertEquals("header\r\nstuff", s);
    }
View Full Code Here

        Assert.assertEquals("header\r\nstuff", s);
    }

    @Test
    public void testCodingFragmentBufferingTinyFragments() throws Exception {
        final WritableByteChannelMock channel = Mockito.spy(new WritableByteChannelMock(64));
        final SessionOutputBuffer outbuf = Mockito.spy(new SessionOutputBufferImpl(1024, 128));
        final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();

        final IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics, 1);
        Assert.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));
        Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
        Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
        Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
        Assert.assertEquals(10, encoder.write(CodecTestUtils.wrap("more stuff")));

        Mockito.verify(channel, Mockito.times(5)).write(Mockito.<ByteBuffer>any());
        Mockito.verify(outbuf, Mockito.times(3)).write(Mockito.<ByteBuffer>any());
        Mockito.verify(outbuf, Mockito.times(3)).flush(channel);

        Assert.assertEquals(18, metrics.getBytesTransferred());

        outbuf.flush(channel);
        final String s = channel.dump(Consts.ASCII);

        Assert.assertEquals("stuff---more stuff", s);
    }
View Full Code Here

TOP

Related Classes of org.apache.http.WritableByteChannelMock

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.