split method of CSVRowParserTests is now unit tested. 3 tests fail (related to issue #19) - behaviour of CSV importer needs some further work.

logger now correct on GridworksServletTests


git-svn-id: http://google-refine.googlecode.com/svn/trunk@777 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Iain Sproat 2010-05-14 22:34:50 +00:00
parent ae986ebe49
commit 9b2aa5290c
2 changed files with 119 additions and 8 deletions

View File

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

View File

@ -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<String> 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<String> 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<String> 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<String> 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<String> 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));
}
}