ParsingUtilities.evaluateJsonStringToObject is now unit tested

CommandTests and CancelProcessesCommandTests are slightly formatted to match code standards.
Command and CancelProcessesCommand are slightly tweaked to pass under all unit tests.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@753 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Iain Sproat 2010-05-13 09:23:37 +00:00
parent 1f2531f303
commit a096fae7a0
6 changed files with 113 additions and 52 deletions

View File

@ -51,14 +51,19 @@ public abstract class Command {
*
* @param request
* @return
* @throws Exception
* @throws JSONException
*/
static protected JSONObject getEngineConfig(HttpServletRequest request)
throws JSONException {
if (request == null) throw new IllegalArgumentException("parameter 'request' should not be null");
String json = request.getParameter("engine");
try{
return (json == null) ? null : ParsingUtilities.evaluateJsonStringToObject(json);
} catch (JSONException e){
logger.debug( json + " could not be parsed to JSON");
return null;
}
}
/**

View File

@ -14,6 +14,8 @@ public class CancelProcessesCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if( request == null ) throw new IllegalArgumentException("parameter 'request' should not be null");
if( response == null ) throw new IllegalArgumentException("parameter 'request' should not be null");
try {
Project project = getProject(request);

View File

@ -77,6 +77,7 @@ public class ParsingUtilities {
}
static public JSONObject evaluateJsonStringToObject(String s) throws JSONException {
if( s == null ) throw new IllegalArgumentException("parameter 's' should not be null");
JSONTokener t = new JSONTokener(s);
Object o = t.nextValue();
if (o instanceof JSONObject) {

View File

@ -99,14 +99,13 @@ public class CommandTests {
}
@Test
public void getEngineConfigThrowsWithEmptyOrBadParameterValue() {
public void getEngineConfigReturnsNullWithEmptyOrBadParameterValue() {
when(request.getParameter("engine")).thenReturn("sdfasdfas");
try {
Assert.assertNull(SUT.wrapGetEngineConfig(request));
Assert.assertNull( SUT.wrapGetEngineConfig(request) );
} catch (JSONException e) {
Assert.fail();
} catch (Exception e) {
// expected
}
verify(request, times(1)).getParameter("engine");

View File

@ -77,8 +77,10 @@ public class CancelProcessesCommandTests {
try {
SUT.doPost(null, null);
Assert.fail(); // should have thrown exception by this point
} catch (IllegalArgumentException e){
//expected
} catch (ServletException e) {
// expected
Assert.fail();
} catch (Exception e) {
Assert.fail();
}
@ -87,8 +89,10 @@ public class CancelProcessesCommandTests {
try {
SUT.doPost(null, response);
Assert.fail(); // should have thrown exception by this point
} catch (IllegalArgumentException e){
//expected
} catch (ServletException e) {
// expected
Assert.fail();
} catch (Exception e) {
Assert.fail();
}
@ -97,8 +101,10 @@ public class CancelProcessesCommandTests {
try {
SUT.doPost(request, null);
Assert.fail(); // should have thrown exception by this point
} catch (ServletException e) {
} catch (IllegalArgumentException e){
// expected
} catch (ServletException e) {
Assert.fail();
} catch (Exception e) {
Assert.fail();
}
@ -135,7 +141,8 @@ public class CancelProcessesCommandTests {
verify(processMan, times(1)).cancelAll();
verify(response, times(1)).setCharacterEncoding("UTF-8");
verify(response, times(1)).setHeader("Content-Type", "application/json");
verify(response, times(1))
.setHeader("Content-Type", "application/json");
verify(proj, times(1)).getProcessManager();
try {
verify(response, times(1)).getWriter();
@ -175,8 +182,7 @@ public class CancelProcessesCommandTests {
when(projMan.getProject(anyLong())).thenReturn(proj);
when(proj.getProcessManager()).thenReturn(processMan);
try {
when(response.getWriter())
.thenThrow(new IllegalStateException(ERROR_MESSAGE))
when(response.getWriter()).thenThrow(new IllegalStateException(ERROR_MESSAGE))
.thenReturn(pw);
} catch (IOException e) {
Assert.fail();

View File

@ -0,0 +1,48 @@
package com.metaweb.gridworks.tests.util;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.metaweb.gridworks.util.ParsingUtilities;
public class ParsingUtilitiesTests {
final static protected Logger logger = LoggerFactory.getLogger("ParsingUtilitiesTests");
//--------------evaluateJsonStringToObject tests-----------------------
@Test
public void evaluateJsonStringToObjectRegressionTest(){
try {
JSONObject o = ParsingUtilities.evaluateJsonStringToObject("{\"foo\":\"bar\"}");
Assert.assertNotNull(o);
Assert.assertEquals("bar", o.getString("foo"));
} catch (JSONException e) {
Assert.fail();
}
}
@Test
public void evaluateJsonStringToObjectWithNullParameters(){
try {
Assert.assertNull(ParsingUtilities.evaluateJsonStringToObject(null));
Assert.fail();
} catch (IllegalArgumentException e){
//expected
} catch (JSONException e) {
Assert.fail();
}
}
@Test
public void evaluateJsonStringToObjectWithMalformedParameters(){
try {
ParsingUtilities.evaluateJsonStringToObject("malformed");
Assert.fail();
} catch (JSONException e) {
//expected
}
}
}