Package com.google.visualization.datasource.render

Source Code of com.google.visualization.datasource.render.TsvRendererTest

// Copyright 2009 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.visualization.datasource.render;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import com.google.common.collect.Lists;
import com.google.visualization.datasource.DataSourceRequest;
import com.google.visualization.datasource.base.DataSourceException;
import com.google.visualization.datasource.base.DataSourceParameters;
import com.google.visualization.datasource.base.ReasonType;
import com.google.visualization.datasource.base.ResponseStatus;
import com.google.visualization.datasource.base.StatusType;
import com.google.visualization.datasource.datatable.ColumnDescription;
import com.google.visualization.datasource.datatable.DataTable;
import com.google.visualization.datasource.datatable.TableCell;
import com.google.visualization.datasource.datatable.TableRow;
import com.google.visualization.datasource.datatable.value.NumberValue;
import com.google.visualization.datasource.datatable.value.TextValue;
import com.google.visualization.datasource.datatable.value.ValueType;
import junit.framework.TestCase;

import static org.easymock.EasyMock.*;

/**
* Tests for CsvRenderer.
*
* @author Nimrod T.
*/
public class TsvRendererTest extends TestCase {

    public void testTSVExcelResponse() throws DataSourceException {
        final String tsvExcelContentType = "text/csv; charset=UTF-16LE";
        final String headerName = "Content-Disposition";
        final String headerValue = "attachment; filename=testFile.xls";

        DataSourceParameters parameters =
                new DataSourceParameters("outFileName:testFile.xls;out:tsv_excel");

        HttpServletResponse mockHttpServletResponse = createMock(HttpServletResponse.class);
        mockHttpServletResponse.setContentType(eq(tsvExcelContentType));
        mockHttpServletResponse.setHeader(eq(headerName), eq(headerValue));
        expectLastCall();

        replay(mockHttpServletResponse);
        TsvRenderer r = new TsvRenderer();
        r.setHeaders(new DataSourceRequest(parameters), mockHttpServletResponse);
        verify(mockHttpServletResponse);
    }

    public void testEmptyDataTableToCsv() throws DataSourceException {
        DataTable dataTable = new DataTable();
        TsvRenderer r = new TsvRenderer();
        DataSourceRequest request = new DataSourceRequest();
        assertEquals(TsvRenderer.BYTE_ORDER_MARK + "", r.render(request, dataTable));
        assertEquals(TsvRenderer.BYTE_ORDER_MARK + "", r.render(request, dataTable));
    }

    public void testSimpleDataTableToCsv() throws DataSourceException {
        List<TableRow> rows;

        DataTable testData = new DataTable();
        ColumnDescription c0 = new ColumnDescription("A", ValueType.TEXT, "col0");
        ColumnDescription c1 = new ColumnDescription("B", ValueType.NUMBER, "col1");
        ColumnDescription c2 = new ColumnDescription("C", ValueType.BOOLEAN, "col2");

        testData.addColumn(c0);
        testData.addColumn(c1);
        testData.addColumn(c2);

        rows = Lists.newArrayList();

        TableRow row = new TableRow();
        row.addCell(new TableCell("aaa"));
        row.addCell(new TableCell(new NumberValue(222), "222"));
        row.addCell(new TableCell(false));
        rows.add(row);

        row = new TableRow();
        row.addCell(new TableCell(""));
        row.addCell(new TableCell(NumberValue.getNullValue()));
        row.addCell(new TableCell(true));
        rows.add(row);

        row = new TableRow();
        row.addCell(new TableCell(new TextValue("bbb"), "bb@@b"));
        row.addCell(new TableCell(333));
        row.addCell(new TableCell(true));
        rows.add(row);

        row = new TableRow();
        row.addCell(new TableCell("ddd"));
        row.addCell(new TableCell(222));
        row.addCell(new TableCell(false));
        rows.add(row);

        testData.addRows(rows);

        TsvRenderer r = new TsvRenderer();
        DataSourceRequest request = new DataSourceRequest();
        assertEquals(TsvRenderer.BYTE_ORDER_MARK +
                "\"col0\"\t\"col1\"\t\"col2\"\n" +
                "\"aaa\"\t222\tfalse\n" +
                "\"\"\tnull\ttrue\n" +
                "\"bb@@b\"\t333\ttrue\n" +
                "\"ddd\"\t222\tfalse\n",
                r.render(request, testData).toString());
    }

    public void testCustomPropertiesToCsv() throws DataSourceException {
        List<TableRow> rows;

        DataTable testData;
        testData = new DataTable();
        ColumnDescription c0 = new ColumnDescription("A", ValueType.TEXT, "col0");
        ColumnDescription c1 = new ColumnDescription("B", ValueType.NUMBER, "col1");
        c1.setCustomProperty("arak", "elit");

        testData.addColumn(c0);
        testData.addColumn(c1);

        rows = Lists.newArrayList();

        TableRow row = new TableRow();
        row.addCell(new TableCell("aaa"));
        row.addCell(new TableCell(new NumberValue(222), "2a2b2"));
        rows.add(row);

        row = new TableRow();
        row.addCell(new TableCell(""));
        row.addCell(new TableCell(NumberValue.getNullValue()));
        rows.add(row);
        row.setCustomProperty("sensi", "puff");

        testData.addRows(rows);

        testData.getRow(0).getCell(0).setCustomProperty("a", "b");

        TsvRenderer r = new TsvRenderer();
        DataSourceRequest request = new DataSourceRequest();
        assertEquals(TsvRenderer.BYTE_ORDER_MARK +
                "\"col0\"\t\"col1\"\n\"aaa\"\t2a2b2\n\"\"\tnull\n",
                r.render(request, testData).toString());

        testData.setCustomProperty("brandy", "cognac");
        assertEquals(TsvRenderer.BYTE_ORDER_MARK +
                "\"col0\"\t\"col1\"\n\"aaa\"\t2a2b2\n\"\"\tnull\n",
                r.render(request, testData).toString());
    }

    public void testRenderError() throws DataSourceException {
        DataSourceRequest request = new DataSourceRequest();
        ResponseStatus responseStatus = new ResponseStatus(
                StatusType.ERROR, ReasonType.INVALID_REQUEST, "but why? why?");
        TsvRenderer r = new TsvRenderer();
        assertEquals(
                "\"Error: Invalid request. but why? why?\"",
                r.error(request, responseStatus));

        responseStatus = new ResponseStatus(
                StatusType.ERROR, ReasonType.NOT_SUPPORTED, "Cannot do dat!");
        assertEquals(
                "\"Error: Operation not supported. Cannot do dat!\"",
                r.error(request, responseStatus));

        responseStatus = new ResponseStatus(
                StatusType.ERROR, ReasonType.NOT_SUPPORTED, "Cannot \"do\" that, too late!");
        assertEquals(
                "\"Error: Operation not supported. Cannot \"\"do\"\" that, too late!\"",
                r.error(request, responseStatus));
    }
}
TOP

Related Classes of com.google.visualization.datasource.render.TsvRendererTest

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.