org.apache.camel.dataformat.jasperreports.JasperReportsTestBase.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.camel.dataformat.jasperreports.JasperReportsTestBase.java

Source

/**
 * http://code.google.com/a/apache-extras.org/p/camel-extra
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
 * Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 *
 * You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * http://www.gnu.org/licenses/lgpl-3.0-standalone.html
 */
package org.apache.camel.dataformat.jasperreports;

import com.lowagie.text.pdf.PdfReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import static org.apache.camel.dataformat.jasperreports.JasperReportsConstants.JR_EXPORTED_TYPE;
import org.apache.camel.util.IOHelper;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import static org.hamcrest.core.Is.is;

public class JasperReportsTestBase extends CamelTestSupport {

    @EndpointInject(uri = "mock:out")
    MockEndpoint out;

    protected void assertOutputIsXls(MockEndpoint mock) throws IOException {
        Exchange exchange = mock.getReceivedExchanges().get(0);
        InputStream is = null;
        try {
            is = exchange.getIn().getBody(InputStream.class);
            Workbook workbook = new HSSFWorkbook(is);
        } finally {
            IOHelper.close(is);
        }
    }

    protected void assertOutputIsXlsx(MockEndpoint mock) throws IOException {
        Exchange exchange = mock.getReceivedExchanges().get(0);
        InputStream is = null;
        try {
            is = exchange.getIn().getBody(InputStream.class);
            Workbook workbook = new XSSFWorkbook(is);
        } finally {
            IOHelper.close(is);
        }
    }

    protected void assertOutputIsPdf(MockEndpoint mock) throws IOException {
        getPdfInfo(mock);
    }

    protected Map<String, String> getPdfInfo(MockEndpoint mock) throws IOException {
        Exchange exchange = mock.getReceivedExchanges().get(0);
        byte[] bytes = exchange.getIn().getBody(byte[].class);
        // read and parse a pdf
        PdfReader reader = new PdfReader(bytes);
        return reader.getInfo();
    }

    protected void assertExportedType(MockEndpoint mock, ExportType type) {
        Exchange exchange = mock.getReceivedExchanges().get(0);
        assertThat(exchange.getIn().getHeader(JR_EXPORTED_TYPE, String.class), is(type.name()));
    }

}