EditOneCellCommandTests: Add number parsing tests

This commit is contained in:
jamessspanggg 2020-01-08 09:55:51 +08:00
parent 5afd93e2d1
commit 67b62c5c16

View File

@ -3,6 +3,7 @@ package com.google.refine.commands.cell;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import static org.testng.AssertJUnit.assertTrue;
import java.io.IOException;
import java.io.PrintWriter;
@ -22,6 +23,8 @@ import com.google.refine.util.TestUtils;
public class EditOneCellCommandTests extends RefineTest {
private static final String PARSABLE_DOUBLE_NUMBER = "12345.123";
private static final String PARSABLE_LONG_NUMBER = "12345";
protected Project project = null;
protected HttpServletRequest request = null;
protected HttpServletResponse response = null;
@ -62,6 +65,42 @@ public class EditOneCellCommandTests extends RefineTest {
assertEquals("d", project.rows.get(1).cells.get(1).value);
}
@Test
public void testNumberParsing_parsableLong() throws ServletException, IOException {
when(request.getParameter("project")).thenReturn(Long.toString(project.id));
when(request.getParameter("row")).thenReturn("1");
when(request.getParameter("cell")).thenReturn("0");
when(request.getParameter("type")).thenReturn("number");
when(request.getParameter("value")).thenReturn(PARSABLE_LONG_NUMBER);
when(request.getParameter("csrf_token")).thenReturn(Command.csrfFactory.getFreshToken());
command.doPost(request, response);
assertEquals("a", project.rows.get(0).cells.get(0).value);
assertEquals("b", project.rows.get(0).cells.get(1).value);
assertTrue(project.rows.get(1).cells.get(0).value instanceof Long);
assertEquals(new Long(12345), project.rows.get(1).cells.get(0).value);
assertEquals("d", project.rows.get(1).cells.get(1).value);
}
@Test
public void testNumberParsing_parsableDouble() throws ServletException, IOException {
when(request.getParameter("project")).thenReturn(Long.toString(project.id));
when(request.getParameter("row")).thenReturn("1");
when(request.getParameter("cell")).thenReturn("0");
when(request.getParameter("type")).thenReturn("number");
when(request.getParameter("value")).thenReturn(PARSABLE_DOUBLE_NUMBER);
when(request.getParameter("csrf_token")).thenReturn(Command.csrfFactory.getFreshToken());
command.doPost(request, response);
assertEquals("a", project.rows.get(0).cells.get(0).value);
assertEquals("b", project.rows.get(0).cells.get(1).value);
assertTrue(project.rows.get(1).cells.get(0).value instanceof Double);
assertEquals(12345.123, project.rows.get(1).cells.get(0).value);
assertEquals("d", project.rows.get(1).cells.get(1).value);
}
@Test
public void testMissingCSRFToken() throws ServletException, IOException {
when(request.getParameter("project")).thenReturn(Long.toString(project.id));