1 /*------------------------------------------------------------------------------
  2 Name:      Msg.java
  3 Project:   org.xmlBlasterProject:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 ------------------------------------------------------------------------------*/
  6 package org.xmlBlaster.test;
  7 
  8 import org.xmlBlaster.client.key.UpdateKey;
  9 import org.xmlBlaster.client.qos.UpdateQos;
 10 import org.xmlBlaster.client.qos.PublishReturnQos;
 11 import org.xmlBlaster.util.MsgUnit;
 12 import org.xmlBlaster.util.XmlBlasterException;
 13 import org.xmlBlaster.util.qos.MsgQosData;
 14 
 15 import junit.framework.Assert;
 16 
 17 /**
 18  * Helper for testsuite to store a received message with update()
 19  */
 20 public class Msg extends Assert
 21 {
 22    private String cbSessionId;
 23    private UpdateKey updateKey;
 24    private byte[] content;
 25    private UpdateQos updateQos;
 26 
 27    public Msg(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
 28       this.cbSessionId = cbSessionId;
 29       this.updateKey = updateKey;
 30       this.content = content;
 31       this.updateQos = updateQos;
 32    }
 33 
 34    public String getCbSessionId() {
 35       return this.cbSessionId;
 36    }
 37 
 38    public byte[] getContent() {
 39       return this.content;
 40    }
 41 
 42    public String getContentStr() {
 43       return new String(this.content);
 44    }
 45 
 46    /**
 47     * @exception IllegalArgumentException
 48     */
 49    public int getContentInt() {
 50       try {
 51          return Integer.parseInt(getContentStr());
 52       } catch(NumberFormatException e) {
 53          throw new IllegalArgumentException("Invalid number " + getContentStr() + ": " + e.toString() + ": " + updateKey.toXml());
 54       }
 55    }
 56 
 57    public UpdateKey getUpdateKey() {
 58       return this.updateKey;
 59    }
 60 
 61    public String getOid() {
 62       return (this.updateKey == null) ? null : this.updateKey.getOid();
 63    }
 64 
 65    public UpdateQos getUpdateQos() {
 66       return this.updateQos;
 67    }
 68 
 69    public String getState() {
 70       return (this.updateQos == null) ? null : this.updateQos.getState();
 71    }
 72    
 73    /**
 74     * Check if the given message (usually published) is the one we hold (usually updated).
 75     * throws a junit assert on error
 76     * @param msgUnit the expected message
 77     */
 78    public void compareMsg(MsgUnit msgUnit) {
 79       MsgQosData qos = (MsgQosData)msgUnit.getQosData();
 80       assertEquals("The keyOid is wrong", msgUnit.getKeyOid(), updateKey.getOid());
 81       assertEquals("The persistence flag is lost", qos.isPersistent(), updateQos.isPersistent());
 82       assertEquals("The message content length is corrupted", msgUnit.getContent().length, content.length);
 83       try {
 84          assertTrue("The message content is corrupted, expected='"+
 85                     msgUnit.getContentStr()+"' but was '"+new String(content)+"'", msgUnit.sameContent(content));
 86       } catch (XmlBlasterException e) {
 87          e.printStackTrace();
 88          fail("Exception: " + e.getMessage());
 89       }
 90    }   
 91 
 92    /**
 93     * Check if the given PublishReturnQos (from a publisher) is the one we hold (usually updated).
 94     * throws a junit assert on error
 95     * @param retQos the expected data
 96     */
 97    public void compareMsg(PublishReturnQos retQos) {
 98       assertEquals("The receive timestamp is corrupted", retQos.getRcvTimestamp(), updateQos.getRcvTimestamp());
 99    }   
100 
101    public String toString() {
102       return "oid='" + this.updateKey.getOid() + "'";
103    }
104 }


syntax highlighted by Code2HTML, v. 0.9.1