From 158cc8e5d75ba0b94f2eed62b85f2b39038aadd1 Mon Sep 17 00:00:00 2001 From: jprocter Date: Wed, 12 Nov 2008 15:57:33 +0000 Subject: [PATCH] added validation method to perform minimal check that message class implementation is consistent for receiving its own generated messages. git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@491 be28352e-c001-0410-b1a7-c7978e42abec --- src/uk/ac/vamsas/client/picking/Message.java | 33 ++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/uk/ac/vamsas/client/picking/Message.java b/src/uk/ac/vamsas/client/picking/Message.java index 1a0165d..c696934 100644 --- a/src/uk/ac/vamsas/client/picking/Message.java +++ b/src/uk/ac/vamsas/client/picking/Message.java @@ -21,4 +21,37 @@ public abstract class Message */ public String getRawMessage() { return message; } + /** + * compare the on-wire message content of the given message to this. + * @param msg + * @return true if message content is equal + */ + public boolean equals(Message msg) + { + return message.equals(msg.getRawMessage()); + } + /** + * Test consistence of a Message class implementation. This method throws an error if + * the message object cannot be parsed into another instance of the same object by + * invoking the MessageImpl(String this.getRawMessage()) constructor or that + * newinstance.getRawMessage != this.getRawMessage + */ + public void validate() { + try { + java.lang.reflect.Constructor msgcons = this.getClass().getConstructor(new Class[] { String.class }); + if (msgcons==null) + { + throw new Exception("No "+this.getClass().getName()+"(String rawmessage) constructor."); + } + Message instance = (Message) msgcons.newInstance(new Object[] { this.getRawMessage() }); + if (!instance.getRawMessage().equals(getRawMessage())) + { + throw new Error("Raw Message Content does not match :\nInitial Message:"+getRawMessage()+"\nParsed and regnerated as :\n"+instance.getRawMessage()+"\n"); + } + } catch (Exception e) + { + throw new Error("Message implementation broken for "+this.getClass(),e); + } + } + } \ No newline at end of file -- 1.7.10.2