KVMessage implements Serializable { public String toXML() throws KVException { if (msgType == null || (msgType.equals("delreq") && key == null) || (msgType.equals("putreq") && (value == null || key == null)) || (msgType.equals("resp") && key != null && value == null)) { throw new KVException(new KVMessage("resp", "Message format incorrect")); } DocumentBuilder db = null; try { db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new KVException(new KVMessage("resp", "Message format incorrect")); } Document doc = db.newDocument(); Element kv = doc.createElement("KVMessage"); Element keey, valoo, massage, toy; kv.setAttribute("type", msgType); if (key != null) { keey = doc.createElement("Key"); keey.setTextContent(key); kv.appendChild(keey); } if (value != null) { valoo = doc.createElement("Value"); valoo.setTextContent(value); kv.appendChild(valoo); } if (message != null) { massage = doc.createElement("Message"); massage.setTextContent(message); kv.appendChild(massage); } if (tpcOpId != null) { toy = doc.createElement("TPCOpId"); toy.setTextContent(tpcOpId); kv.appendChild(toy); } doc.appendChild(kv); duck = doc; doc.setXmlStandalone(true); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = null; try { transformer = tf.newTransformer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter writer = new StringWriter(); try { transformer.transform(new DOMSource(doc), new StreamResult(writer)); } catch (TransformerException e) { e.printStackTrace(); } String output = writer.getBuffer().toString(); return output; } KVMessage(KVMessage kvm);  KVMessage(String msgType);  KVMessage(String msgType, String message);  KVMessage(Socket sock);  KVMessage(Socket sock, int timeout); String getTpcOpId(); void setTpcOpId(String tpcOpId); Document getDuck(); final String getKey(); final void setKey(String key); final String getValue(); final void setValue(String value); final String getMessage(); final void setMessage(String message); String getMsgType(); String toXML(); void sendMessage(Socket sock); String toString(); static final long serialVersionUID; public String tpcOpId; }
@Test public void test2() throws Exception { FakeSocket fake = new FakeSocket(k.toXML()); KVMessage q = new KVMessage(fake); assertEquals( q.toXML(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<KVMessage type=\"putreq\">\n<Key>I like pie</Key>\n<Value>I like cake</Value>\n</KVMessage>\n"); }
@Test public void test3() throws Exception { FakeSocket fake = new FakeSocket(k.toXML()); KVMessage q = new KVMessage(fake, 100); assertEquals( q.toXML(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<KVMessage type=\"putreq\">\n<Key>I like pie</Key>\n<Value>I like cake</Value>\n</KVMessage>\n"); }
@Test public void test4() throws Exception { FakeSocket fake = new FakeSocket(k.toXML()); try { KVMessage q = new KVMessage(fake, 5); } catch (KVException e) { return; } assertTrue(false); }
@Test public void test() throws Exception { assertEquals( k.toXML(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<KVMessage type=\"putreq\">\n<Key>I like pie</Key>\n<Value>I like cake</Value>\n</KVMessage>\n"); }