Merge pull request #2274 from jamessspanggg/2163-inconsistent-number-parsing

Standardise 'edit' cell dialogue with 'toNumber()' when parsing String to Number
This commit is contained in:
Antonin Delpeuch 2020-01-08 09:46:11 +01:00 committed by GitHub
commit d82e5425e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 2 deletions

View File

@ -103,7 +103,11 @@ public class EditOneCellCommand extends Command {
Serializable value = null;
if ("number".equals(type)) {
value = Double.parseDouble(valueString);
try {
value = Long.parseLong(valueString);
} catch (NumberFormatException e) {
value = Double.parseDouble(valueString);
}
} else if ("boolean".equals(type)) {
value = "true".equalsIgnoreCase(valueString);
} else if ("date".equals(type)) {

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