// 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.ReasonType;
import com.google.visualization.datasource.base.ResponseStatus;
import com.google.visualization.datasource.base.StatusType;
import com.google.visualization.datasource.base.Warning;
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.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 HtmlRenderer.
*
* @author Nimrod T.
*/
public class HtmlRendererTest extends TestCase {
// Use platform dependent new-line to validate the results.
String nl = System.getProperty("line.separator");
public void testHTMLResponseContentType() {
final String HtmlContentType = "text/html; charset=UTF-8";
HttpServletResponse mockHttpServletResponse = createMock(HttpServletResponse.class);
mockHttpServletResponse.setContentType(eq(HtmlContentType));
expectLastCall();
replay(mockHttpServletResponse);
HtmlRenderer r = new HtmlRenderer();
r.setHeaders(new DataSourceRequest(), mockHttpServletResponse);
verify(mockHttpServletResponse);
}
public void testEmptyDataTableToHtml() throws DataSourceException {
DataTable dataTable = new DataTable();
String expected =
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">" + nl
+ "<html>" + nl
+ "<head>" + nl
+ "<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" + nl
+ "<title>Google Visualization</title>" + nl
+ "</head>" + nl
+ "<body>" + nl
+ "<table border=\"1\" cellpadding=\"2\" cellspacing=\"0\">" + nl
+ "<tr style=\"font-weight: bold; background-color: #aaa;\"></tr>" + nl
+ "</table>" + nl
+ "</body>" + nl
+ "</html>" + nl;
HtmlRenderer r = new HtmlRenderer();
DataSourceRequest request = new DataSourceRequest();
String actual = r.render(request, dataTable).toString();
assertEquals(expected, actual);
}
public void testSimpleDataTableToHtml() 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(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);
String expected =
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">" + nl
+ "<html>" + nl
+ "<head>" + nl
+ "<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" + nl
+ "<title>Google Visualization</title>" + nl
+ "</head>" + nl
+ "<body>" + nl
+ "<table border=\"1\" cellpadding=\"2\" cellspacing=\"0\">" + nl
+ "<tr style=\"font-weight: bold; background-color: #aaa;\">" + nl
+ "<td>col0</td>"
+ "<td>col1</td>"
+ "<td>col2</td>" + nl
+ "</tr>" + nl
+ "<tr style=\"background-color: #f0f0f0\">" + nl
+ "<td>aaa</td>"
+ "<td align=\"right\">222</td>"
+ "<td align=\"center\">\u2717</td>" + nl
+ "</tr>" + nl
+ "<tr style=\"background-color: #ffffff\">" + nl
+ "<td> </td>"
+ "<td> </td>"
+ "<td align=\"center\">\u2714</td>" + nl
+ "</tr>" + nl
+ "<tr style=\"background-color: #f0f0f0\">" + nl
+ "<td>bb@@b</td>"
+ "<td align=\"right\">333</td>"
+ "<td align=\"center\">\u2714</td>" + nl
+ "</tr>" + nl
+ "<tr style=\"background-color: #ffffff\">" + nl
+ "<td>ddd</td>"
+ "<td align=\"right\">222</td>"
+ "<td align=\"center\">\u2717</td>" + nl
+ "</tr>" + nl
+ "</table>" + nl
+ "</body>" + nl
+ "</html>" + nl;
HtmlRenderer r = new HtmlRenderer();
DataSourceRequest request = new DataSourceRequest();
String actual = r.render(request, testData).toString();
assertEquals(expected, actual);
}
public void testWarnings() 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");
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), "$222"));
rows.add(row);
row = new TableRow();
row.addCell(new TableCell("bbb"));
row.addCell(new TableCell(333));
rows.add(row);
testData.addRows(rows);
testData.addWarning(new Warning(ReasonType.DATA_TRUNCATED, "Sorry, data truncated"));
testData.addWarning(new Warning(ReasonType.NOT_SUPPORTED, "foobar"));
String expected =
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">" + nl
+ "<html>" + nl
+ "<head>" + nl
+ "<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" + nl
+ "<title>Google Visualization</title>" + nl
+ "</head>" + nl
+ "<body>" + nl
+ "<table border=\"1\" cellpadding=\"2\" cellspacing=\"0\">" + nl
+ "<tr style=\"font-weight: bold; background-color: #aaa;\">" + nl
+ "<td>col0</td>"
+ "<td>col1</td>" + nl
+ "</tr>" + nl
+ "<tr style=\"background-color: #f0f0f0\">" + nl
+ "<td>aaa</td>"
+ "<td align=\"right\">$222</td>" + nl
+ "</tr>" + nl
+ "<tr style=\"background-color: #ffffff\">" + nl
+ "<td>bbb</td>"
+ "<td align=\"right\">333</td>" + nl
+ "</tr>" + nl
+ "</table>" + nl
+ "<br>" + nl
+ "<br>" + nl
+ "<div>Retrieved data was truncated. Sorry, data truncated</div>" + nl
+ "<br>" + nl
+ "<br>" + nl
+ "<div>Operation not supported. foobar</div>" + nl
+ "</body>" + nl
+ "</html>" + nl;
DataSourceRequest dataSourceRequest = new DataSourceRequest();
HtmlRenderer r = new HtmlRenderer();
String actual = r.render(dataSourceRequest, testData).toString();
assertEquals(expected, actual);
}
public void testEscaping() throws DataSourceException {
ResponseStatus responseStatus = new ResponseStatus(
StatusType.ERROR, ReasonType.INVALID_REQUEST, "but why? why? why?");
HtmlRenderer r = new HtmlRenderer();
DataSourceRequest request = new DataSourceRequest();
assertEquals(
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">" + nl
+ "<html>" + nl
+ "<head>" + nl
+ "<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" + nl
+ "<title>Google Visualization</title>" + nl
+ "</head>" + nl
+ "<body>" + nl
+ "<h3>Oops, an error occured.</h3>" + nl
+ "<div>Status: error</div>" + nl
+ "<div>Reason: Invalid request</div>" + nl
+ "<div>Description: but why? why? why?</div>" + nl
+ "</body>" + nl
+ "</html>" + nl,
r.error(request, responseStatus).toString());
responseStatus = new ResponseStatus(
StatusType.ERROR, ReasonType.NOT_SUPPORTED, "Cannot do dat!");
assertEquals(
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">" + nl
+ "<html>" + nl
+ "<head>" + nl
+ "<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" + nl
+ "<title>Google Visualization</title>" + nl
+ "</head>" + nl
+ "<body>" + nl
+ "<h3>Oops, an error occured.</h3>" + nl
+ "<div>Status: error</div>" + nl
+ "<div>Reason: Operation not supported</div>" + nl
+ "<div>Description: Cannot do dat!</div>" + nl
+ "</body>" + nl
+ "</html>" + nl,
r.error(request, responseStatus).toString());
}
public void testRenderError() throws DataSourceException {
DataSourceRequest request = new DataSourceRequest();
ResponseStatus responseStatus = new ResponseStatus(
StatusType.ERROR, ReasonType.INVALID_REQUEST, "but why? why?");
HtmlRenderer r = new HtmlRenderer();
assertEquals(
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">" + nl
+ "<html>" + nl
+ "<head>" + nl
+ "<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" + nl
+ "<title>Google Visualization</title>" + nl
+ "</head>" + nl
+ "<body>" + nl
+ "<h3>Oops, an error occured.</h3>" + nl
+ "<div>Status: error</div>" + nl
+ "<div>Reason: Invalid request</div>" + nl
+ "<div>Description: but why? why?</div>" + nl
+ "</body>" + nl
+ "</html>" + nl,
r.error(request, responseStatus).toString());
responseStatus = new ResponseStatus(
StatusType.ERROR, ReasonType.NOT_SUPPORTED, "Cannot do dat!");
assertEquals(
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">" + nl
+ "<html>" + nl
+ "<head>" + nl
+ "<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" + nl
+ "<title>Google Visualization</title>" + nl
+ "</head>" + nl
+ "<body>" + nl
+ "<h3>Oops, an error occured.</h3>" + nl
+ "<div>Status: error</div>" + nl
+ "<div>Reason: Operation not supported</div>" + nl
+ "<div>Description: Cannot do dat!</div>" + nl
+ "</body>" + nl
+ "</html>" + nl,
r.error(request, responseStatus).toString());
}
}