### Question:
ConfServlet extends HttpServlet { static void writeResponse(Configuration conf, Writer out, String format) throws IOException, BadFormatException { if (FORMAT_JSON.equals(format)) { Configuration.dumpConfiguration(conf, out); } else if (FORMAT_XML.equals(format)) { conf.writeXml(out); } else { throw new BadFormatException("Bad format: " + format); } }  @Override void doGet(HttpServletRequest request, HttpServletResponse response);  }### Answer:
@Test @SuppressWarnings("unchecked") public void testWriteJson() throws Exception { StringWriter sw = new StringWriter(); ConfServlet.writeResponse(getTestConf(), sw, "json"); String json = sw.toString(); boolean foundSetting = false; Object parsed = JSON.parse(json); Object[] properties = ((Map<String, Object[]>)parsed).get("properties"); for (Object o : properties) { Map<String, Object> propertyInfo = (Map<String, Object>)o; String key = (String)propertyInfo.get("key"); String val = (String)propertyInfo.get("value"); String resource = (String)propertyInfo.get("resource"); System.err.println("k: " + key + " v: " + val + " r: " + resource); if (TEST_KEY.equals(key) && TEST_VAL.equals(val) && Configuration.UNKNOWN_RESOURCE.equals(resource)) { foundSetting = true; } } assertTrue(foundSetting); }
@Test public void testWriteXml() throws Exception { StringWriter sw = new StringWriter(); ConfServlet.writeResponse(getTestConf(), sw, "xml"); String xml = sw.toString(); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(new StringReader(xml))); NodeList nameNodes = doc.getElementsByTagName("name"); boolean foundSetting = false; for (int i = 0; i < nameNodes.getLength(); i++) { Node nameNode = nameNodes.item(i); String key = nameNode.getTextContent(); System.err.println("xml key: " + key); if (TEST_KEY.equals(key)) { foundSetting = true; Element propertyElem = (Element)nameNode.getParentNode(); String val = propertyElem.getElementsByTagName("value").item(0).getTextContent(); assertEquals(TEST_VAL, val); } } assertTrue(foundSetting); }
@Test public void testBadFormat() throws Exception { StringWriter sw = new StringWriter(); try { ConfServlet.writeResponse(getTestConf(), sw, "not a format"); fail("writeResponse with bad format didn't throw!"); } catch (ConfServlet.BadFormatException bfe) { } assertEquals("", sw.toString()); }
@Test @SuppressWarnings("unchecked") public void testWriteJson() throws Exception { StringWriter sw = new StringWriter(); ConfServlet.writeResponse(getTestConf(), sw, "json"); String json = sw.toString(); boolean foundSetting = false; Object parsed = JSON.parse(json); Object[] properties = ((Map<String, Object[]>)parsed).get("properties"); for (Object o : properties) { Map<String, Object> propertyInfo = (Map<String, Object>)o; String key = (String)propertyInfo.get("key"); String val = (String)propertyInfo.get("value"); String resource = (String)propertyInfo.get("resource"); System.err.println("k: " + key + " v: " + val + " r: " + resource); if (TEST_KEY.equals(key) && TEST_VAL.equals(val) && "programmatically".equals(resource)) { foundSetting = true; } } assertTrue(foundSetting); }