1 package org.xmlBlaster.test.cluster;
  2 
  3 import java.util.logging.Logger;
  4 import org.xmlBlaster.util.Global;
  5 
  6 // for client connections:
  7 import org.xmlBlaster.util.*;
  8 import org.xmlBlaster.client.I_Callback;
  9 import org.xmlBlaster.client.key.PublishKey;
 10 import org.xmlBlaster.client.key.EraseKey;
 11 import org.xmlBlaster.client.key.SubscribeKey;
 12 import org.xmlBlaster.client.key.UpdateKey;
 13 import org.xmlBlaster.client.qos.PublishQos;
 14 import org.xmlBlaster.client.qos.PublishReturnQos;
 15 import org.xmlBlaster.client.qos.UpdateQos;
 16 import org.xmlBlaster.client.qos.SubscribeQos;
 17 import org.xmlBlaster.client.qos.SubscribeReturnQos;
 18 import org.xmlBlaster.client.qos.EraseQos;
 19 import org.xmlBlaster.client.qos.EraseReturnQos;
 20 import org.xmlBlaster.client.I_XmlBlasterAccess;
 21 import org.xmlBlaster.util.MsgUnit;
 22 
 23 import junit.framework.*;
 24 
 25 /**
 26  * Test publishing a message from bilbo to heron. 
 27  * <p />
 28  * <pre>
 29  * java -Djava.compiler= junit.textui.TestRunner -noloading org.xmlBlaster.test.cluster.EraseTest
 30  * </pre>
 31  * NOTE: asserts() in update() methods are routed back to server and are not handled
 32  *       by the junit testsuite, so we check double (see code).
 33  * @see <a href="http://www.xmlblaster.org/xmlBlaster/doc/requirements/cluster.html" target="others">Cluster requirement</a>
 34  */
 35 public class EraseTest extends TestCase {
 36    private String ME = "EraseTest";
 37    private Global glob;
 38    private static Logger log = Logger.getLogger(EraseTest.class.getName());
 39    private ServerHelper serverHelper;
 40 
 41    private I_XmlBlasterAccess heronCon, avalonCon, golanCon, frodoCon, bilboCon;
 42 
 43    private int updateCounterHeron = 0;
 44    private int updateCounterFrodo = 0;
 45    private int updateCounterBilbo = 0;
 46    private String oid = "SubscribeToBilbo";
 47    private String domain = "RUGBY_NEWS"; // heron is master for RUGBY_NEWS
 48    private String contentStr = "We win";
 49 
 50    private String assertInUpdate = null;
 51 
 52    private boolean isErase = false;
 53 
 54    public EraseTest(String name) {
 55       super(name);
 56       this.glob = new Global(null, true, false);
 57    }
 58 
 59    /**
 60     * Initialize the test ...
 61     */
 62    protected void setUp() {
 63 
 64       log.info("Entering setUp(), test starts");
 65 
 66       updateCounterHeron = 0;
 67       updateCounterFrodo = 0;
 68       updateCounterBilbo = 0;
 69 
 70 
 71       serverHelper = new ServerHelper(glob, log, ME);
 72 
 73       // Starts a cluster node
 74       serverHelper.startHeron();
 75       serverHelper.startAvalon();
 76       //serverHelper.startGolan();
 77       serverHelper.startFrodo();
 78       serverHelper.startBilbo();
 79    }
 80 
 81    /**
 82     * cleaning up ...
 83     */
 84    protected void tearDown() {
 85       log.info("Entering tearDown(), test is finished");
 86       try { Thread.sleep(1000); } catch( InterruptedException i) {} // Wait some time
 87 
 88       if (bilboCon != null) { bilboCon.disconnect(null); bilboCon = null; }
 89       if (frodoCon != null) { frodoCon.disconnect(null); frodoCon = null; }
 90       if (golanCon != null) { golanCon.disconnect(null); golanCon = null; }
 91       if (avalonCon != null) { avalonCon.disconnect(null); avalonCon = null; }
 92       if (heronCon != null) { heronCon.disconnect(null); heronCon = null; }
 93 
 94       serverHelper.tearDown();
 95    }
 96 
 97    /**
 98     * We start all nodes as described in requirement
 99     * <a href="http://www.xmlblaster.org/xmlBlaster/doc/requirements/cluster.html" target="others">cluster</a>
100     * <p />
101     * - Subscribe to RUGBY messages from bilbo<br />
102     * - publish RUGBY messages to avalon (heron is the master)<br />
103     * - bilbo should get an update
104     * - erase RUGBY message at avalon
105     * - bilbo should get an erase event
106     */ 
107    public void testErase() {
108       System.err.println("***EraseTest.testErase: Subscribe a message from a cluster slave ...");
109       try {
110          System.err.println("->Connect to avalon ...");
111          avalonCon = serverHelper.connect(serverHelper.getAvalonGlob(), null);
112 
113          {
114             System.err.println("->Connect to bilbo ...");
115             bilboCon = serverHelper.connect(serverHelper.getBilboGlob(), new I_Callback() {  // Login to xmlBlaster, register for updates
116                   public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
117                      updateCounterBilbo++;
118                      log.info(
119                               "Receiving update '" + updateKey.getOid() + "' state=" + updateQos.getState() +
120                               " #" + updateCounterBilbo + " ...");
121                      if (isErase && !updateQos.isErased()) {
122                         assertInUpdate = "Expected message erase event, expected:" + isErase + " but was:" + updateQos.isErased();
123                         assertEquals("Expected message erase event", isErase, updateQos.isErased());
124                      }
125                      assertInUpdate = "Wrong message updated, expected:" + oid + " but was:" + updateKey.getOid();
126                      assertEquals("Wrong message updated", oid, updateKey.getOid());
127                      assertInUpdate = null;
128                      return "";
129                   }
130                });
131 
132             System.err.println("->Subscribe from bilbo ...");
133             SubscribeKey sk = new SubscribeKey(glob, oid);
134             sk.setDomain(domain);
135             SubscribeQos sq = new SubscribeQos(glob);
136             SubscribeReturnQos srq = bilboCon.subscribe(sk.toXml(), sq.toXml());
137          }
138 
139          System.err.println("->Publish to avalon ...");
140          PublishKey avalon_pk = new PublishKey(glob, oid, "text/plain", "1.0", domain);
141          PublishQos avalon_pq = new PublishQos(glob);
142          MsgUnit avalon_msgUnit = new MsgUnit(avalon_pk, contentStr.getBytes(), avalon_pq);
143          PublishReturnQos avalon_prq = avalonCon.publish(avalon_msgUnit);
144          assertEquals("oid changed", oid, avalon_prq.getKeyOid());
145 
146 
147          try { Thread.sleep(2000); } catch( InterruptedException i) {}
148          if (1 != updateCounterBilbo) log.severe("Did not expect " + updateCounterBilbo + " updates");
149          assertEquals("message from avalon", 1, updateCounterBilbo);
150          assertTrue(assertInUpdate, assertInUpdate == null);
151          assertInUpdate = null;
152          updateCounterBilbo = 0;
153 
154          isErase = true;
155          System.err.println("->Trying to erase the message at the avalon slave node ...");
156          EraseKey ek = new EraseKey(glob, oid);
157          ek.setDomain(domain);
158          EraseQos eq = new EraseQos(glob);
159          EraseReturnQos[] arr = avalonCon.erase(ek.toXml(), eq.toXml());
160          assertEquals("Erase", 1, arr.length);
161 
162          try { Thread.sleep(2000); } catch( InterruptedException i) {}
163          assertEquals("message erase event for bilbo", 1, updateCounterBilbo);
164          assertTrue(assertInUpdate, assertInUpdate == null);
165          assertInUpdate = null;
166 
167          System.err.println("->testErase done, SUCCESS.");
168       }
169       catch (XmlBlasterException e) {
170          e.printStackTrace();
171          fail("SubscribeToBilbo-Exception: " + e.toString());
172       }
173       finally {
174          if (bilboCon != null) {
175             bilboCon.disconnect(null);
176             bilboCon = null;
177          }   
178          if (avalonCon != null) {
179             avalonCon.disconnect(null);
180             avalonCon = null;
181          }
182       }
183 
184       System.err.println("***EraseTest.testSubscribeTwice: testSubscribeTwice [SUCCESS]");
185    }
186 }


syntax highlighted by Code2HTML, v. 0.9.1