1 /*------------------------------------------------------------------------------
  2 Name:      PtPDestination.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 ------------------------------------------------------------------------------*/
  6 
  7 package org.xmlBlaster.test.util;
  8 
  9 import java.util.logging.Logger;
 10 
 11 import junit.framework.TestCase;
 12 
 13 import org.xmlBlaster.client.I_XmlBlasterAccess;
 14 import org.xmlBlaster.client.qos.ConnectQos;
 15 import org.xmlBlaster.client.qos.DisconnectQos;
 16 import org.xmlBlaster.test.MsgInterceptor;
 17 import org.xmlBlaster.util.Global;
 18 import org.xmlBlaster.util.SessionName;
 19 import org.xmlBlaster.util.XmlBlasterException;
 20 import org.xmlBlaster.util.qos.address.Address;
 21 import org.xmlBlaster.util.qos.address.CallbackAddress;
 22 
 23 
 24 /**
 25  * PtPDestination is a helper class when testing ptp destinations
 26  * @author <a href="mailto:michele@laghi.eu">Michele Laghi</a>
 27  */
 28 public class PtPDestination {
 29    private Global global;
 30    private MsgInterceptor updateInterceptor;
 31    private SessionName sessionName;
 32    Logger log = Logger.getLogger(PtPDestination.class.getName());
 33    
 34    public PtPDestination(Global parentGlobal, String sessionName) {
 35       this.global = parentGlobal.getClone(null);
 36       this.sessionName = new SessionName(this.global, sessionName);
 37       this.updateInterceptor = new MsgInterceptor(this.global, log, null);
 38    }
 39       
 40    public void init(boolean wantsPtP, boolean shutdownCb, long cbMaxEntries, long cbMaxEntriesCache, long subjMaxEntries, long subjMaxEntriesCache) throws XmlBlasterException {
 41       this.updateInterceptor.clear();
 42       ConnectQos qos = new ConnectQos(this.global);
 43       qos.setSessionName(this.sessionName);
 44       qos.setPtpAllowed(wantsPtP);
 45       qos.getSessionCbQueueProperty().setMaxEntries(cbMaxEntries);
 46       qos.getSessionCbQueueProperty().setMaxEntriesCache(cbMaxEntriesCache);
 47          
 48       if (subjMaxEntries > 0L || subjMaxEntriesCache > 0L) {   
 49          if (subjMaxEntries > 0L) qos.getSubjectQueueProperty().setMaxEntries(subjMaxEntries);
 50          if (subjMaxEntriesCache > 0L) qos.getSubjectQueueProperty().setMaxEntriesCache(subjMaxEntriesCache);
 51       }
 52          
 53       CallbackAddress cbAddress = new CallbackAddress(this.global);
 54       cbAddress.setRetries(-1);
 55       cbAddress.setPingInterval(-1);
 56       cbAddress.setDelay(250L);
 57       qos.addCallbackAddress(cbAddress);
 58       
 59       Address clientAddress = qos.getAddress();
 60       clientAddress.setRetries(-1);
 61       clientAddress.setPingInterval(-1);
 62       clientAddress.setDelay(10000L);
 63       
 64       this.updateInterceptor.clear();
 65       this.global.getXmlBlasterAccess().connect(qos, updateInterceptor);
 66       if (shutdownCb) {
 67          try {
 68             Thread.sleep(250L);
 69          }
 70          catch (InterruptedException ex) {
 71             ex.printStackTrace();
 72             TestCase.assertTrue("An interrupted exception occured", false);
 73          }
 74          Client.shutdownCb(global.getXmlBlasterAccess(), Client.Shutdown.LEAVE_SERVER);
 75       }
 76    }
 77 
 78    public void shutdown(boolean doDisconnect) {
 79       DisconnectQos qos = new DisconnectQos(this.global);
 80       if (doDisconnect) 
 81          this.global.getXmlBlasterAccess().disconnect(qos);
 82       this.global.shutdown();
 83       this.global = null;
 84    }
 85       
 86    public SessionName getSessionName() {
 87       return this.sessionName;
 88    }
 89       
 90    public I_XmlBlasterAccess getConnection() {
 91       return this.global.getXmlBlasterAccess();
 92    }
 93    
 94    public MsgInterceptor getUpdateInterceptor() {
 95       return this.updateInterceptor;
 96    }
 97    
 98    public void check(long timeout, int expected) {
 99       TestCase.assertEquals(this.getSessionName().getRelativeName(), expected, this.updateInterceptor.waitOnUpdate(timeout, expected));
100       this.updateInterceptor.clear();         
101    }
102 }


syntax highlighted by Code2HTML, v. 0.9.1