org.opentestsystem.delivery.testreg.upload.ExcelFileAppenderTest.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.delivery.testreg.upload.ExcelFileAppenderTest.java

Source

/*******************************************************************************
 * Educational Online Test Delivery System
 * Copyright (c) 2013 American Institutes for Research
 * 
 * Distributed under the AIR Open Source License, Version 1.0
 * See accompanying file AIR-License-1_0.txt or at
 * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf
 ******************************************************************************/

package org.opentestsystem.delivery.testreg.upload;

import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertThat;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.junit.Before;
import org.junit.Test;

public class ExcelFileAppenderTest {
    FileAppender appender;

    @Before
    public void setup() {
        appender = new ExcelFileAppender();
    }

    @Test
    public void testSuccess() throws IOException, InvalidFormatException {
        InputStream appendedStream = appender.insertAtTop("DISTRICT", createWorkbookWithNoFormatTypeString());

        Workbook workbook = WorkbookFactory.create(appendedStream);
        assertThat(workbook.getNumberOfSheets(), is(1));

        Sheet sheet = workbook.getSheet("Test");
        assertThat(sheet, is(notNullValue()));

        //FormatType Row
        assertThat(sheet.getRow(0).getLastCellNum(), is((short) 1));
        assertThat(sheet.getRow(0).getCell(0).getStringCellValue(), is("DISTRICT"));

        //Header Row
        assertThat(sheet.getRow(1).getCell(0).getStringCellValue(), is("Column1"));
        assertThat(sheet.getRow(1).getCell(1).getStringCellValue(), is("Column2"));
        assertThat(sheet.getRow(1).getCell(2).getStringCellValue(), is("Column3"));
        assertThat(sheet.getRow(1).getCell(3).getStringCellValue(), is("Column4"));
    }

    private InputStream createWorkbookWithNoFormatTypeString() throws IOException {
        final Workbook wb = new HSSFWorkbook();

        Sheet testSheet = wb.createSheet("Test");
        Row row0 = testSheet.createRow(0);

        Cell cell0 = row0.createCell(0);
        cell0.setCellValue("Column1");

        Cell cell1 = row0.createCell(1);
        cell1.setCellValue("Column2");

        Cell cell2 = row0.createCell(2);
        cell2.setCellValue("Column3");

        Cell cell3 = row0.createCell(3);
        cell3.setCellValue("Column4");

        return new ByteArrayInputStream(new ByteArrayOutputStream() {
            {
                wb.write(this);
            }

        }.toByteArray());
    }

}