Java Reference
In-Depth Information
such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\"] },
\"GlossSee\": \"markup\" } } } } }";
String response1LineNoWs =
StringUtils.deleteWhitespace(response1Line);
Assert.assertTrue(responseString,
responseNoWs.equals(response1LineNoWs));
}
In this test, we perform the same steps as our first XML example: we create an Http-
Client , an HTTP GET method that we execute, and then read the results from the
server into a String b . Unlike XML , JSON has no API s to support checks for well-
formed and valid documents. We strip whitespaces from our JSON document fixture,
the server response, and compare the two C . Although this check is brute force, we
use it to provide a simple check that's free of formatting issues. The Apache Commons
Lang API StringUtils.deleteWhitespace performs the whitespace removal.
The next-best thing we can do is implement a well-formed check by parsing the
document. Although www.json.org lis ts many libraries, including Java libraries to parse
JSON , we use the JSL int wrapper jslint4java 24 to go beyond a simple well-formed check.
As you saw earlier in the RhinoUnit section, JSL int provides lint-style reporting for
JavaScript. In listing 13.13, we call JSL int through jslint4java and check its results.
C
Listing 13.13
A JSON service test with JSLint
@Test
public void testGetJsonAndValidate() throws IOException,
ParserConfigurationException, SAXException {
HttpClient httpClient = new HttpClient();
GetMethod get = new GetMethod(URL_FIXTURE);
String responseString;
try {
httpClient.executeMethod(get);
InputStream input = get.getResponseBodyAsStream();
responseString = IOUtils.toString(input, "UTF-8");
} finally {
get.releaseConnection();
}
JSLint JSLint = new JSLint();
List<Issue> issues = JSLint.lint(URL_FIXTURE, responseString);
StringBuilder builder = new StringBuilder();
String eol = System.getProperty("line.separator");
for (Issue issue : issues) {
builder.append(issue.toString());
builder.append(eol);
}
Assert.assertEquals(builder.toString(), 0, issues.size());
}
Our test starts as usual, and we check for results using a JSLint object b . We don't
provide options in this example, but the JSLint class provides an addOption method
B
C
D
E
24
jslint4java site: http://code.google.com/p/jslint4java/
 
 
 
 
 
Search WWH ::




Custom Search