Package com.google.visualization.datasource.render

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

// 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.BooleanValue;
import com.google.visualization.datasource.datatable.value.DateTimeValue;
import com.google.visualization.datasource.datatable.value.DateValue;
import com.google.visualization.datasource.datatable.value.NumberValue;
import com.google.visualization.datasource.datatable.value.TextValue;
import com.google.visualization.datasource.datatable.value.TimeOfDayValue;
import com.google.visualization.datasource.datatable.value.ValueType;
import junit.framework.TestCase;

import static org.easymock.EasyMock.*;
import static org.easymock.EasyMock.verify;

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

    public void testCSVResponse() throws DataSourceException {
        final String csvContentType = "text/csv; charset=UTF-8";
        final String csvheaderName = "Content-Disposition";
        final String csvheaderValue = "attachment; filename=testFile.csv";

        DataSourceParameters parameters =
                new DataSourceParameters("outFileName:testFile;out:csv");

        HttpServletResponse mockHttpServletResponse = createMock(HttpServletResponse.class);
        mockHttpServletResponse.setContentType(eq(csvContentType));
        mockHttpServletResponse.setHeader(eq(csvheaderName), eq(csvheaderValue));
        expectLastCall();

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

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

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

        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);

        CsvRenderer r = new CsvRenderer();
        DataSourceRequest request = new DataSourceRequest();
        assertEquals(
                "\"col0\",\"col1\",\"col2\"\n" +
                        "\"aaa\",222,false\n" +
                        "\"\",null,true\n" +
                        "\"bb@@b\",333,true\n" +
                        "\"ddd\",222,false\n",
                r.render(request, testData).toString());
        assertEquals(
                "\"col0\",\"col1\",\"col2\"\n" +
                        "\"aaa\",222,false\n" +
                        "\"\",null,true\n" +
                        "\"bb@@b\",333,true\n" +
                        "\"ddd\",222,false\n",
                r.render(request, testData).toString());
    }

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

        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");

        CsvRenderer r = new CsvRenderer();
        DataSourceRequest request = new DataSourceRequest();
        assertEquals(
                "\"col0\",\"col1\"\n\"aaa\",2a2b2\n\"\",null\n",
                r.render(request, testData).toString());
        assertEquals(
                "\"col0\",\"col1\"\n\"aaa\",2a2b2\n\"\",null\n",
                r.render(request, testData).toString());
        testData.setCustomProperty("brandy", "cognac");
        assertEquals(
                "\"col0\",\"col1\"\n\"aaa\",2a2b2\n\"\",null\n",
                r.render(request, testData).toString());
        assertEquals(
                "\"col0\",\"col1\"\n\"aaa\",2a2b2\n\"\",null\n",
                r.render(request, testData).toString());
    }

    public void testRenderError() throws DataSourceException {
        ResponseStatus responseStatus = new ResponseStatus(
                StatusType.ERROR, ReasonType.INVALID_REQUEST, "but why? why?");
        CsvRenderer r = new CsvRenderer();
        DataSourceRequest request = new DataSourceRequest();
        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));
    }

    public void testRenderDataTableWithCommas() throws DataSourceException {
        DataTable testData;
        List<TableRow> rows;

        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");
        ColumnDescription c3 = new ColumnDescription("D", ValueType.DATE, "col3");
        ColumnDescription c4 = new ColumnDescription("E", ValueType.DATETIME, "col4");
        ColumnDescription c5 = new ColumnDescription("F", ValueType.TIMEOFDAY, "col5");

        testData.addColumn(c0);
        testData.addColumn(c1);
        testData.addColumn(c2);
        testData.addColumn(c3);
        testData.addColumn(c4);
        testData.addColumn(c5);

        rows = Lists.newArrayList();

        TableRow row = new TableRow();
        row.addCell(new TableCell(new TextValue("aaa"), "aaa"));
        row.addCell(new TableCell(new NumberValue(222), "222"));
        row.addCell(new TableCell(BooleanValue.TRUE, "true"));
        row.addCell(new TableCell(new DateValue(2009, 1, 1), "2009-02-01"));
        row.addCell(new TableCell(new DateTimeValue(2009, 1, 1, 12, 14, 1, 0), "2009-02-01 12:14:01"));
        row.addCell(new TableCell(new TimeOfDayValue(12, 14, 1), "12:14:01"));
        rows.add(row);

        row = new TableRow();
        row.addCell(new TableCell(new TextValue("aaa"), "a,aa"));
        row.addCell(new TableCell(new NumberValue(222), "2,22"));
        row.addCell(new TableCell(BooleanValue.TRUE, "true,"));
        row.addCell(new TableCell(new DateValue(2009, 1, 1), "2009-02-01"));
        row.addCell(new TableCell(new DateTimeValue(2009, 1, 1, 12, 14, 1, 0), "2009-02-01 12,14,01"));
        row.addCell(new TableCell(new TimeOfDayValue(12, 14, 1), "12:14:01"));
        rows.add(row);

        testData.addRows(rows);

        String expected = "\"col0\",\"col1\",\"col2\",\"col3\",\"col4\",\"col5\"\n";
        expected += "\"aaa\",222,true,2009-02-01,2009-02-01 12:14:01,12:14:01\n";
        expected += "\"a,aa\",\"2,22\",\"true,\",2009-02-01,\"2009-02-01 12,14,01\",12:14:01\n";
        CsvRenderer r = new CsvRenderer();
        DataSourceRequest request = new DataSourceRequest();
        assertEquals(expected, r.render(request, testData));
    }
}
TOP

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

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.