Package hudson.plugins.emailext.plugins

Source Code of hudson.plugins.emailext.plugins.ZipDataSourceTest

package hudson.plugins.emailext.plugins;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipInputStream;

import static org.junit.Assert.*;
import org.junit.Test;

public class ZipDataSourceTest {

    private final static int BUFFER_SIZE = 1024;

    @Test
    public void testGetName() throws IOException {
        String name = "myFile";

        ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]);
        ZipDataSource dataSource = new ZipDataSource(name, in);

        assertEquals(name + ".zip", dataSource.getName());
    }

    @Test
    public void testGetContentType() throws IOException {
        String name = "myFile";

        ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]);
        ZipDataSource dataSource = new ZipDataSource(name, in);

        assertEquals("application/zip", dataSource.getContentType());
    }

    @Test
    public void testGetInputStream() throws IOException {
        byte[] sample = "Hello World lllllllllllots of repeated charactersssssssssssss Hello World again".getBytes();
        ZipDataSource dataSource = new ZipDataSource("name", new ByteArrayInputStream(sample));
        InputStream in = dataSource.getInputStream();

        ZipInputStream zin = new ZipInputStream(in);
        // Need this to move the ZipInputStream to the start of the "file"
        zin.getNextEntry();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int size;
        byte[] buffer = new byte[BUFFER_SIZE];
        while ((size = zin.read(buffer, 0, buffer.length)) > 0) {
            baos.write(buffer, 0, size);
        }
        assertArrayEquals(sample, baos.toByteArray());
    }

    @Test
    public void testGetOutputStream() throws IOException {
        String name = "myFile";

        ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]);
        ZipDataSource dataSource = new ZipDataSource(name, in);

        try {
            dataSource.getOutputStream();
        } catch (IOException e) {
            return;
        }
        fail("It is not possible to get an OutputStream from the ZipDataSource, an exception should have been thrown");
    }
}
TOP

Related Classes of hudson.plugins.emailext.plugins.ZipDataSourceTest

TOP
Copyright © 2018 www.massapi.com. 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.