From 5afd93e2d157e7a2e02b00d99dcd502a4ae11e70 Mon Sep 17 00:00:00 2001 From: jamessspanggg Date: Tue, 7 Jan 2020 10:09:28 +0800 Subject: [PATCH 1/2] Standardise 'edit' cell dialogue with 'toNumber()' behavior --- .../com/google/refine/commands/cell/EditOneCellCommand.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/main/src/com/google/refine/commands/cell/EditOneCellCommand.java b/main/src/com/google/refine/commands/cell/EditOneCellCommand.java index 55cf39252..62662d2dc 100644 --- a/main/src/com/google/refine/commands/cell/EditOneCellCommand.java +++ b/main/src/com/google/refine/commands/cell/EditOneCellCommand.java @@ -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)) { From 67b62c5c1679ecbdf82a93b15646b26f7fc03f8e Mon Sep 17 00:00:00 2001 From: jamessspanggg Date: Wed, 8 Jan 2020 09:55:51 +0800 Subject: [PATCH 2/2] EditOneCellCommandTests: Add number parsing tests --- .../cell/EditOneCellCommandTests.java | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/main/tests/server/src/com/google/refine/commands/cell/EditOneCellCommandTests.java b/main/tests/server/src/com/google/refine/commands/cell/EditOneCellCommandTests.java index d9fcc4c07..efd12f0da 100644 --- a/main/tests/server/src/com/google/refine/commands/cell/EditOneCellCommandTests.java +++ b/main/tests/server/src/com/google/refine/commands/cell/EditOneCellCommandTests.java @@ -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; @@ -21,7 +22,9 @@ import com.google.refine.model.Project; 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));