diff --git a/tests/java/src/com/metaweb/gridworks/tests/GridworksServletTests.java b/tests/java/src/com/metaweb/gridworks/tests/GridworksServletTests.java index 616fc6c7a..7d601d6e9 100644 --- a/tests/java/src/com/metaweb/gridworks/tests/GridworksServletTests.java +++ b/tests/java/src/com/metaweb/gridworks/tests/GridworksServletTests.java @@ -22,7 +22,7 @@ import com.metaweb.gridworks.commands.Command; public class GridworksServletTests { // logging - final static protected Logger logger = LoggerFactory.getLogger("CancelProcessesCommandTests"); + final static protected Logger logger = LoggerFactory.getLogger("GridworksServletTests"); //System under test GridworksServletStub SUT = null; @@ -45,6 +45,7 @@ public class GridworksServletTests { request = mock(HttpServletRequest.class); response = mock(HttpServletResponse.class); command = mock(Command.class); + GridworksServletStub.InsertCommand(TEST_COMMAND_NAME,command); //inject mock into command container } @@ -56,8 +57,14 @@ public class GridworksServletTests { command = null; GridworksServletStub.RemoveCommand(TEST_COMMAND_NAME); //remove mock to clean command container } + //-------------------AutoSaveTimerTask tests----------- + //TODO would need to mock Timer and inject it into GridworksServlet. Also need to deal with ProjectManager.singleton + //-------------------init tests------------------------ + //TODO need to stub super.init(), mock Timer and inject it into GridworksServlet + //-------------------destroy tests--------------------- + //TODO need to mock Timer and inject it into GridworksServlet. Also need to deal with ProjectManager.singleton - //--------------------doGet tests--------------------- + //--------------------doGet tests---------------------- @Test public void doGetRegressionTest(){ whenGetCommandNameThenReturn(TEST_COMMAND_PATH); @@ -94,7 +101,7 @@ public class GridworksServletTests { verifyGetCommandNameCalled(); verifyError404Called(); - + } //----------------doPost tests------------------------- @@ -119,11 +126,11 @@ public class GridworksServletTests { Assert.fail(); } } - + @Test public void doPostReturns404WhenCommandNotFound(){ whenGetCommandNameThenReturn(BAD_COMMAND_PATH); - + try { SUT.wrapDoPost(request, response); } catch (ServletException e) { @@ -131,12 +138,11 @@ public class GridworksServletTests { } catch (IOException e) { Assert.fail(); } - + verifyGetCommandNameCalled(); verifyError404Called(); } - //----------------getCommandName tests---------------- @Test @@ -149,7 +155,7 @@ public class GridworksServletTests { verify(request, times(1)).getPathInfo(); } - //helpers + //------------helpers protected void whenGetCommandNameThenReturn(String commandName){ when(request.getPathInfo()).thenReturn(commandName); } diff --git a/tests/java/src/com/metaweb/gridworks/tests/importers/parsers/CSVRowParserTests.java b/tests/java/src/com/metaweb/gridworks/tests/importers/parsers/CSVRowParserTests.java new file mode 100644 index 000000000..a6eaa38f1 --- /dev/null +++ b/tests/java/src/com/metaweb/gridworks/tests/importers/parsers/CSVRowParserTests.java @@ -0,0 +1,105 @@ +package com.metaweb.gridworks.tests.importers.parsers; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.io.LineNumberReader; +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.metaweb.gridworks.importers.parsers.CSVRowParser; + +public class CSVRowParserTests { + // logging + final static protected Logger logger = LoggerFactory.getLogger("CSVRowParserTests"); + + //constants + String SAMPLE_ROW = "NDB_No,Shrt_Desc,Water"; + String ROW_WITH_QUOTED_COMMA = "01001,\"BUTTER,WITH SALT\",15.87"; + String UNCLOSED_QUOTED_ROW = "\"Open quoted value, with commas"; + String LEADING_QUOTE_WITH_COMMA = "value1, \"Open quoted, with commas\" and close quote but no comma, value3"; + String QUOTED = "value1, value2 with \"quote\" in middle, value3"; + + String SAMPLE_CSV = SAMPLE_ROW + "\n" + ROW_WITH_QUOTED_COMMA; //Unix line endings? + + //System Under Test + CSVRowParser SUT = null; + + //mocked dependencies + LineNumberReader lineReader = null; + + @Before + public void SetUp(){ + lineReader = mock(LineNumberReader.class); + SUT = new CSVRowParser(); + } + + @After + public void TearDown(){ + lineReader = null; + SUT = null; + } + //------------split tests------------------------- + @Test + public void split(){ + List splitLine = SUT.split(SAMPLE_ROW, lineReader); + Assert.assertEquals(3, splitLine.size()); + Assert.assertEquals("NDB_No", splitLine.get(0)); + Assert.assertEquals("Shrt_Desc", splitLine.get(1)); + Assert.assertEquals("Water", splitLine.get(2)); + } + + @Test + public void splitWithQuotedComma(){ + List splitLine = SUT.split(ROW_WITH_QUOTED_COMMA, lineReader); + Assert.assertEquals(3, splitLine.size()); + Assert.assertEquals("01001", splitLine.get(0)); + Assert.assertEquals("BUTTER,WITH SALT", splitLine.get(1)); + Assert.assertEquals("15.87", splitLine.get(2)); + } + + @Test + public void splitWithUnclosedQuote(){ + try { + when(lineReader.readLine()).thenReturn(""); + } catch (IOException e) { + Assert.fail(); + } + List splitLine = SUT.split(UNCLOSED_QUOTED_ROW, lineReader); + Assert.assertEquals(1, splitLine.size()); + Assert.assertEquals(UNCLOSED_QUOTED_ROW, splitLine.get(0)); + + try { + verify(lineReader, times(1)).readLine(); + } catch (IOException e) { + Assert.fail(); + } + } + + @Test + public void splitWithLeadingQuoteWithComma(){ + List splitLine = SUT.split(LEADING_QUOTE_WITH_COMMA, lineReader); + Assert.assertEquals(3, splitLine.size()); + Assert.assertEquals("value1", splitLine.get(0)); + Assert.assertEquals("\"Open quoted, with commas\" and close quote but no comma", splitLine.get(0)); + Assert.assertEquals("value3", splitLine.get(2)); + } + + @Test + public void splitWithQuoteInsideValue(){ + List splitLine = SUT.split(QUOTED, lineReader); + Assert.assertEquals(3, splitLine.size()); + Assert.assertEquals("value1", splitLine.get(0)); + Assert.assertEquals("value2 with \"quote\" in middle", splitLine.get(1)); + Assert.assertEquals("value3", splitLine.get(2)); + } +}